Add test cases for ExampleGenerator (#1802)

* Add test case : generateFromResponseSchemaWithArrayOfModel

* Add test case : generateFromResponseSchemaWithArrayOfPrimitiveTypes

* Add test case : generateFromResponseSchemaWithModel

* Add test case : generateFromResponseSchemaWithNoExample

* Use String.format for windows

* Specify locale to prevent issues due to locale
This commit is contained in:
Akihito Nakano
2019-01-06 13:27:27 +09:00
committed by GitHub
parent 1a2deb016b
commit bdf32775fb
2 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
package org.openapitools.codegen;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.examples.ExampleGenerator;
import org.testng.annotations.Test;
import java.util.*;
import static org.testng.AssertJUnit.*;
public class ExampleGeneratorTest {
@Test
public void generateFromResponseSchemaWithNoExample() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new
ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
"200",
openAPI
.getPaths()
.get("/generate_from_response_schema_with_no_example")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);
assertNull(examples);
}
@Test
public void generateFromResponseSchemaWithArrayOfModel() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new
ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
"200",
openAPI
.getPaths()
.get("/generate_from_response_schema_with_array_of_model")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);
assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals("\"string schema example value\"", examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}
@Test
public void generateFromResponseSchemaWithArrayOfPrimitiveTypes() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new
ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
"200",
openAPI
.getPaths()
.get("/generate_from_response_schema_with_array_of_primitive_types")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);
assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals("\"primitive types example value\"", examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}
@Test
public void generateFromResponseSchemaWithModel() {
OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/example_generator_test.yaml", null, new
ParseOptions()).getOpenAPI();
new InlineModelResolver().flatten(openAPI);
ExampleGenerator exampleGenerator = new ExampleGenerator(openAPI.getComponents().getSchemas(), openAPI);
Set<String> mediaTypeKeys = new TreeSet<>();
mediaTypeKeys.add("application/json");
List<Map<String, String>> examples = exampleGenerator.generateFromResponseSchema(
"200",
openAPI
.getPaths()
.get("/generate_from_response_schema_with_model")
.getGet()
.getResponses()
.get("200")
.getContent()
.get("application/json")
.getSchema(),
mediaTypeKeys
);
assertEquals(1, examples.size());
assertEquals("application/json", examples.get(0).get("contentType"));
assertEquals(String.format(Locale.ROOT, "{%n \"example_schema_property\" : \"example schema property value\"%n}"), examples.get(0).get("example"));
assertEquals("200", examples.get(0).get("statusCode"));
}
}

View File

@@ -0,0 +1,63 @@
openapi: 3.0.2
info:
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
paths:
/generate_from_response_schema_with_no_example:
get:
operationId: generateFromResponseSchemaWithNoExample
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: string
/generate_from_response_schema_with_array_of_model:
get:
operationId: generateFromResponseSchemaWithArrayOfModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StringSchema'
/generate_from_response_schema_with_array_of_primitive_types:
get:
operationId: generateFromResponseSchemaWithArrayOfPrimitiveTypes
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
type: string
example: primitive types example value
/generate_from_response_schema_with_model:
get:
operationId: generateFromResponseSchemaWithArrayOfPrimitiveTypes
responses:
'200':
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleSchema'
components:
schemas:
StringSchema:
type: string
example: string schema example value
ExampleSchema:
type: object
properties:
example_schema_property:
type: string
example: example schema property value