Skip to content

Commit

Permalink
Get the lowest status code
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Feb 13, 2024
1 parent 81e6a26 commit 1b574cc
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package io.quarkiverse.openapi.wiremock.generator.deployment.wiremock;

import static io.swagger.v3.parser.util.SchemaTypeUtil.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Request;
import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Response;
import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Stubbing;
Expand All @@ -12,19 +24,12 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static io.swagger.v3.parser.util.SchemaTypeUtil.*;

public class OpenApi2WiremockMapper {

private Components components;
public static final String ONLY_SUPPORTED_MEDIA_TYPE = "application/json";
public static final Integer DEFAULT_STATUS_CODE = 200;


public List<Stubbing> map(OpenAPI openAPI) {

Expand Down Expand Up @@ -93,11 +98,11 @@ private String getResponseBody(final String statusCode, final Operation operatio

MediaType applicationJson = new ArrayList<>(apiResponse.getContent().entrySet())
.stream()
.filter(media -> media.getKey().equals(Response.ONLY_SUPPORTED_MEDIA_TYPE))
.filter(media -> media.getKey().equals(OpenApi2WiremockMapper.ONLY_SUPPORTED_MEDIA_TYPE))
.findFirst()
.orElseThrow(
() -> new IllegalArgumentException(
"This extension only supports " + Response.DEFAULT_STATUS_CODE + "media type"))
"This extension only supports " + OpenApi2WiremockMapper.ONLY_SUPPORTED_MEDIA_TYPE + "media type"))
.getValue();

if (!Objects.isNull(applicationJson.getSchema().get$ref())) {
Expand All @@ -116,11 +121,10 @@ private static String generateResponseBody(final Schema<?> schema) {
};
}


/**
* This method attempts to retrieve the first status code defined in the {@link Operation} object.
* If the OpenAPI response contains only the "default" status code, this method will return
* {@link Response#DEFAULT_STATUS_CODE}.
* {@link OpenApi2WiremockMapper#DEFAULT_STATUS_CODE}.
* If the OpenAPI response contains more than one status code, the method returns the first one.
* For example, if the status codes are "200" and after "400", this method will return the status "200".
*
Expand All @@ -131,12 +135,15 @@ private static int getStatusCode(final Operation operation) {

final String defaultStatus = "default";
ArrayList<Map.Entry<String, ApiResponse>> apiResponses = new ArrayList<>(operation.getResponses().entrySet());

apiResponses.sort(Map.Entry.comparingByKey());

String statusCode = apiResponses.stream().findFirst()
.orElseThrow(() -> new IllegalArgumentException("There is no status on operation"))
.getKey();

if (statusCode.contentEquals(defaultStatus)) {
return Response.DEFAULT_STATUS_CODE;
return OpenApi2WiremockMapper.DEFAULT_STATUS_CODE;
}

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.

return Integer.parseInt(statusCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.quarkiverse.openapi.wiremock.generator.deployment.wiremock;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.quarkiverse.openapi.wiremock.generator.deployment.wrapper.OpenApiWiremockGeneratorWrapper;
import io.swagger.v3.oas.models.media.Schema;
import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;
import com.fasterxml.jackson.core.JsonProcessingException;

import io.quarkiverse.openapi.wiremock.generator.deployment.wrapper.OpenApiWiremockGeneratorWrapper;
import io.swagger.v3.oas.models.media.Schema;

public class SchemaReader {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Response {

public static final Integer DEFAULT_STATUS_CODE = 200;
public static final String ONLY_SUPPORTED_MEDIA_TYPE = "application/json";
private final Integer status;
private final String body;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package io.quarkiverse.openapi.wiremock.generator.deployment.wiremock;

import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Stubbing;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Stubbing;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;

class OpenApi2WiremockMapperTest {

@Test
Expand Down Expand Up @@ -55,9 +56,11 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
}

@Test
void should_map_api_response_$ref_to_empty_object_when_the_$ref_is_not_a_object() throws URISyntaxException {
@DisplayName("Should map ApiResponse $ref to empty object when the $ref is not an object")
void should_map_api_response_$ref_to_empty_object_when_the_$ref_is_not_an_object() throws URISyntaxException {
// given
OpenAPI openAPI = readOpenAPI("openapi/should_map_api_response_$ref_to_empty_object_when_the_$ref_is_not_a_object.json");
OpenAPI openAPI = readOpenAPI(
"openapi/should_map_api_response_$ref_to_empty_object_when_the_$ref_is_not_an_object.json");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();

// arrange
Expand All @@ -71,9 +74,10 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
}

@Test
void should_map_api_response_$ref_to_user_object_when_the_$ref_is_a_object() throws URISyntaxException {
@DisplayName("Should map ApiResponse $ref to user object when the $ref is an object")
void should_map_api_response_$ref_to_user_object_when_the_$ref_is_an_object() throws URISyntaxException {
// given
OpenAPI openAPI = readOpenAPI("openapi/should_map_api_response_$ref_to_user_object_when_the_$ref_is_a_object.json");
OpenAPI openAPI = readOpenAPI("openapi/should_map_api_response_$ref_to_user_object_when_the_$ref_is_an_object.json");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();

// arrange
Expand All @@ -85,9 +89,25 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
Assertions.assertEquals("{\"user\":\"John Doe\"}", stubbing.response().getBody());
}

@Test
@DisplayName("Should get the lowest status code when there is two")
void should_get_the_lowest_status_code_when_there_is_two() throws URISyntaxException {
// given
OpenAPI openAPI = readOpenAPI("openapi/should_get_the_lowest_status_code_when_there_is_two.yaml");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();

// arrange
List<Stubbing> stubbings = openApi2WiremockMapper.map(openAPI);
Stubbing stubbing = stubbings.stream().findFirst().orElse(null);

// assert
Assertions.assertNotNull(stubbing);
Assertions.assertEquals(200, stubbing.response().getStatus());
}

OpenAPI readOpenAPI(final String file) throws URISyntaxException {
URI uri = Objects.requireNonNull(this.getClass().getClassLoader().getResource(file), "File %s not found ;/".formatted(file))
URI uri = Objects
.requireNonNull(this.getClass().getClassLoader().getResource(file), "File %s not found ;/".formatted(file))
.toURI();
OpenAPIV3Parser parser = new OpenAPIV3Parser();
return parser.read(uri.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.quarkiverse.openapi.wiremock.generator.deployment.wiremock;

import io.swagger.v3.oas.models.media.Schema;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.swagger.v3.oas.models.media.Schema;

class SchemaReaderTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: SchemaReaderTest is not referenced within this codebase. If not used as an external API it should be removed.
@Test
@DisplayName("Should read OpenAPI schema with one level correctly")
Expand Down Expand Up @@ -52,4 +53,4 @@ void should_read_openapi_schema_with_two_levels_correctly() {
// then
Assertions.assertEquals("{\"metadata\":{\"id\":10},\"name\":\"John Doe\"}", schema);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.2
info:
title: OpenAPI Generator Wiremock
version: 3.0.0-SNAPSHOT
paths:
/users/1:
delete:
responses:
"204":
description: No Content
get:
responses:
"404":
description: Not Found
"200":
description: Ok
content:
"application/json":
schema:
type: object
properties:
name:
type: string
example: John Doe
4 changes: 2 additions & 2 deletions wiremock/docs/modules/ROOT/pages/includes/features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ responses:
"500":
description: "Internal Server Error"
"200":
description: "OK"
description: "Ok"
----

The extension will generate the following Wiremock Response templating:
Expand All @@ -47,7 +47,7 @@ The extension will generate the following Wiremock Response templating:
----
{
"response": {
"status": 500
"status": 200
}
}
----
Expand Down

0 comments on commit 1b574cc

Please sign in to comment.