Skip to content

Commit

Permalink
Change OpenApi2WiremockMapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Feb 13, 2024
1 parent f308875 commit d997a81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand All @@ -24,18 +23,20 @@

public class OpenApi2WiremockMapper {

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

public List<Stubbing> map(OpenAPI openAPI) {
public OpenApi2WiremockMapper(OpenAPI openAPI) {
this.openAPI = openAPI;
}

public List<Stubbing> generateWiremockStubbings() {

List<Stubbing> stubbings = new ArrayList<>();

Paths paths = openAPI.getPaths();

this.components = openAPI.getComponents();

Set<Map.Entry<String, PathItem>> pathsEntries = paths.entrySet();

for (Map.Entry<String, PathItem> pathEntry : pathsEntries) {
Expand Down Expand Up @@ -78,7 +79,10 @@ private List<Stubbing> generateStubbings(Map.Entry<String, PathItem> entry) {
}

private String getResponseBodyFrom$Ref(String component) {
Schema<?> schema = this.components.getSchemas().get(getComponentName(component));
Schema<?> schema = this.openAPI.getComponents()
.getSchemas()
.get(getComponentName(component));

if (!schema.getType().equals(OBJECT_TYPE)) {
return SchemaReader.EMPTY_JSON_OBJECT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private Path createOutputMappingsDir(Path outDir) throws IOException {
private Consumer<Path> generateWiremockStubbingFile(Path mappingsDir) {
return file -> {
OpenAPI openAPI = OPENAPI_PARSER_INSTANCE.read(file.toString());
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();
List<Stubbing> stubbings = openApi2WiremockMapper.map(openAPI);
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);
List<Stubbing> stubbings = openApi2WiremockMapper.generateWiremockStubbings();

try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class OpenApi2WiremockMapperTest {
void should_map_openapi_path_with_get_ping_to_wiremock_stubbing_correctly() throws URISyntaxException {
// given
OpenAPI openAPI = readOpenAPI("openapi/should_map_openapi_path_with_get_ping_to_wiremock_stubbing_correctly.yaml");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);

// when
List<Stubbing> stubbings = openApi2WiremockMapper.map(openAPI);
List<Stubbing> stubbings = openApi2WiremockMapper.generateWiremockStubbings();

// then
Stubbing stubbing = stubbings.stream().findFirst().orElseThrow();
Expand All @@ -38,10 +38,10 @@ void should_map_openapi_path_with_get_ping_to_wiremock_stubbing_correctly() thro
public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxException {
// given
OpenAPI openAPI = readOpenAPI("openapi/should_map_two_paths_to_two_stubbing_correctly.yaml");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);

// when
List<Stubbing> stubbings = openApi2WiremockMapper.map(openAPI);
List<Stubbing> stubbings = openApi2WiremockMapper.generateWiremockStubbings();

// then
Assertions.assertEquals(2, stubbings.size());
Expand All @@ -61,10 +61,10 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
// given
OpenAPI openAPI = readOpenAPI(
"openapi/should_map_api_response_$ref_to_empty_object_when_the_$ref_is_not_an_object.json");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);

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

// assert
Expand All @@ -78,10 +78,10 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
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_an_object.json");
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper();
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);

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

// assert
Expand All @@ -94,10 +94,10 @@ public void should_map_two_paths_to_two_stubbing_correctly() throws URISyntaxExc
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();
OpenApi2WiremockMapper openApi2WiremockMapper = new OpenApi2WiremockMapper(openAPI);

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

// assert
Expand Down

0 comments on commit d997a81

Please sign in to comment.