Skip to content
Snippets Groups Projects
Commit e3d7a47b authored by Jueun Park's avatar Jueun Park
Browse files

edited test

parent 5d13266b
No related branches found
No related tags found
1 merge request!1Tests/openapi generator
...@@ -25,7 +25,7 @@ ComponentsObject ::= S:SchemaTuple* R:ResponseTuple* P:ParameterTuple* E:Example ...@@ -25,7 +25,7 @@ ComponentsObject ::= S:SchemaTuple* R:ResponseTuple* P:ParameterTuple* E:Example
SchemaTuple ::= <Key> O:SchemaOb; SchemaTuple ::= <Key> O:SchemaOb;
ResponseTuple ::= <Key> O:ResponseOb; ResponseTuple ::= <Key> O:ResponseOb;
ParameterTuple ::= <Key> O:ParameterOb; ParameterTuple ::= <Key> O:ParameterOb;
ExampleTuple ::= <Key> O:ExampleObject; ExampleTuple ::= <Key> O:ExampleOb;
RequestBodyTuple ::= <Key> O:RequestBodyOb; RequestBodyTuple ::= <Key> O:RequestBodyOb;
HeaderTuple ::= <Key> O:HeaderOb; HeaderTuple ::= <Key> O:HeaderOb;
SecuritySchemeTuple ::= <Key> O:SecuritySchemeOb; SecuritySchemeTuple ::= <Key> O:SecuritySchemeOb;
...@@ -97,7 +97,10 @@ Extension ::= <Key> <Value:Object>; ...@@ -97,7 +97,10 @@ Extension ::= <Key> <Value:Object>;
Expression ::= <Name> P:PathItemOb; Expression ::= <Name> P:PathItemOb;
//Example Object //Example Object
ExampleObject ::= <Summary> <Description> <Value:Object> <ExternalValue> Ex:Extension*; abstract ExampleOb;
ExampleReference : ExampleOb ::= <Ref> <Summary> <Description>;
rel ExampleReference.r -> ExampleObject;
ExampleObject : ExampleOb ::= <Summary> <Description> <Value:Object> <ExternalValue> Ex:Extension*;
//Link Object //Link Object
abstract LinkOb; abstract LinkOb;
......
...@@ -542,10 +542,21 @@ public static CallbackOb CallbackOb.parseCallback(Callback callback) { ...@@ -542,10 +542,21 @@ public static CallbackOb CallbackOb.parseCallback(Callback callback) {
return callbackObject; return callbackObject;
} }
public static ExampleOb ExampleOb.parseExample(Example example){
public static ExampleObject ExampleObject.parseExample(Example example){
ExampleObject exampleObject = new ExampleObject(); ExampleObject exampleObject = new ExampleObject();
if (example.get$ref() != null) {
ExampleReference e = new ExampleReference();
e.setRef(example.get$ref());
/* 3.1.0 features
if (example.getSummary() != null) ...
if (example.getDescription() != null) ...
*/
return e;
} else {
if (example.getSummary() != null) if (example.getSummary() != null)
exampleObject.setSummary(example.getSummary()); exampleObject.setSummary(example.getSummary());
if (example.getDescription() != null) if (example.getDescription() != null)
...@@ -558,6 +569,7 @@ public static ExampleObject ExampleObject.parseExample(Example example){ ...@@ -558,6 +569,7 @@ public static ExampleObject ExampleObject.parseExample(Example example){
for (String key : example.getExtensions().keySet()) for (String key : example.getExtensions().keySet())
exampleObject.addEx(new Extension(key, example.getExtensions().get(key))); exampleObject.addEx(new Extension(key, example.getExtensions().get(key)));
} }
}
return exampleObject; return exampleObject;
} }
...@@ -793,8 +805,8 @@ public static DiscriminatorObject DiscriminatorObject.parseDiscriminator (Discri ...@@ -793,8 +805,8 @@ public static DiscriminatorObject DiscriminatorObject.parseDiscriminator (Discri
if (discriminator.getPropertyName() != null) if (discriminator.getPropertyName() != null)
discriminatorObject.setPropertyName(discriminator.getPropertyName()); discriminatorObject.setPropertyName(discriminator.getPropertyName());
if (discriminator.getMapping() != null) { if (discriminator.getMapping() != null) {
MappingTuple mapping = new MappingTuple();
for (String key : discriminator.getMapping().keySet()) { for (String key : discriminator.getMapping().keySet()) {
MappingTuple mapping = new MappingTuple();
mapping.setKey(key); mapping.setKey(key);
mapping.setValue(discriminator.getMapping().get(key)); mapping.setValue(discriminator.getMapping().get(key));
discriminatorObject.addM(mapping); discriminatorObject.addM(mapping);
......
...@@ -191,7 +191,7 @@ aspect ReverseParser{ ...@@ -191,7 +191,7 @@ aspect ReverseParser{
if (componentsObject.getNumE() != 0) { if (componentsObject.getNumE() != 0) {
Map<String, Example> exampleMap = new HashMap<>(); Map<String, Example> exampleMap = new HashMap<>();
for (ExampleTuple t : componentsObject.getEList()) for (ExampleTuple t : componentsObject.getEList())
exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO()));
components.setExamples(exampleMap); components.setExamples(exampleMap);
} }
if (componentsObject.getNumRb() != 0) { if (componentsObject.getNumRb() != 0) {
...@@ -435,7 +435,7 @@ aspect ReverseParser{ ...@@ -435,7 +435,7 @@ aspect ReverseParser{
if (p.getNumE() != 0) { if (p.getNumE() != 0) {
Map<String, Example> exampleMap = new HashMap<>(); Map<String, Example> exampleMap = new HashMap<>();
for (ExampleTuple t : p.getEList()) for (ExampleTuple t : p.getEList())
exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO()));
parameter.setExamples(exampleMap); parameter.setExamples(exampleMap);
} }
if (p.getNumC() != 0) { if (p.getNumC() != 0) {
...@@ -498,7 +498,7 @@ aspect ReverseParser{ ...@@ -498,7 +498,7 @@ aspect ReverseParser{
if (mediaTypeObject.getNumE() != 0) { if (mediaTypeObject.getNumE() != 0) {
Map<String, Example> exampleMap = new HashMap<>(); Map<String, Example> exampleMap = new HashMap<>();
for (ExampleTuple t : mediaTypeObject.getEList()) for (ExampleTuple t : mediaTypeObject.getEList())
exampleMap.put(t.getKey(), ExampleObject.reverseExample(t.getO())); exampleMap.put(t.getKey(), t.getO().reverseExample(t.getO()));
mediaType.setExamples(exampleMap); mediaType.setExamples(exampleMap);
} }
if (mediaTypeObject.getNumEn() != 0) { if (mediaTypeObject.getNumEn() != 0) {
...@@ -554,7 +554,7 @@ aspect ReverseParser{ ...@@ -554,7 +554,7 @@ aspect ReverseParser{
ApiResponse response = new ApiResponse(); ApiResponse response = new ApiResponse();
ResponseObject r = (ResponseObject) responseOb; ResponseObject r = (ResponseObject) responseOb;
if (!r.getDescription().isEmpty()) if (r.getDescription() != null)
response.setDescription(r.getDescription()); response.setDescription(r.getDescription());
if (r.getNumH() != 0) { if (r.getNumH() != 0) {
Map<String, Header> headers = new HashMap<>(); Map<String, Header> headers = new HashMap<>();
...@@ -603,8 +603,19 @@ aspect ReverseParser{ ...@@ -603,8 +603,19 @@ aspect ReverseParser{
return callback; return callback;
} }
public static Example ExampleObject.reverseExample(ExampleObject exampleObject){ syn Example ExampleOb.reverseExample(ExampleOb exampleOb);
eq ExampleReference.reverseExample(ExampleOb exampleOb){
Example example = new Example();
ExampleReference e = (ExampleReference) exampleOb;
if(!e.getRef().isEmpty())
example.set$ref(e.getRef());
return example;
}
eq ExampleObject.reverseExample(ExampleOb exampleOb){
Example example = new Example(); Example example = new Example();
ExampleObject exampleObject = (ExampleObject) exampleOb;
if (!exampleObject.getSummary().isEmpty()) if (!exampleObject.getSummary().isEmpty())
example.setSummary(exampleObject.getSummary()); example.setSummary(exampleObject.getSummary());
...@@ -693,7 +704,7 @@ aspect ReverseParser{ ...@@ -693,7 +704,7 @@ aspect ReverseParser{
if (h.getNumE() != 0) { if (h.getNumE() != 0) {
Map<String, Example> examples = new HashMap<>(); Map<String, Example> examples = new HashMap<>();
for (ExampleTuple t : h.getEList()) for (ExampleTuple t : h.getEList())
examples.put(t.getKey(), ExampleObject.reverseExample(t.getO())); examples.put(t.getKey(), t.getO().reverseExample(t.getO()));
header.setExample(examples); header.setExample(examples);
} }
if (h.getNumC() != 0) { if (h.getNumC() != 0) {
......
openapi: "3.1.0"
info:
version: 1.0.0
title: Swagger Petstore
paths:
/pets:
$ref: "#/components/pathItems/Pets"
summary: List all pets
description: description
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
security:
- apiKey: [ ]
tags:
- pets
parameters:
- $ref: "#/components/parameters/Pets"
description: The id of the pet to retrieve
responses:
"200":
$ref: "#/components/responses/Pets"
description: Expected response to a valid request
/pets/requestBody:
post:
description: It gets pets
responses:
'200':
description: successful operation
headers:
X-Rate-Limit:
$ref: "#/components/headers/Pets"
description: header sibling description
links:
userRepository:
$ref: '#/components/links/userRepository'
description: link sibling description
requestBody:
$ref: "#/components/requestBodies/Pets"
description: request body description
components:
securitySchemes:
api_key:
$ref: "#/components/securitySchemes/security"
description: This is another description
...@@ -38,74 +38,12 @@ public class OpenAPIMain_test { ...@@ -38,74 +38,12 @@ public class OpenAPIMain_test {
File genDirectory = new File(genDir); File genDirectory = new File(genDir);
File[] contents; File[] contents;
File resource = new File("./src/main/resources/3.0"); File resource = new File("./src/main/resources");
for (File file : resource.listFiles()) recursiveTest(resource);
filenames.add(file.getName());
for (String file : filenames) {
String writerName = genDir + file;
writerName = writerName.substring(0, writerName.length() - 5);
FileWriter expectedWriter = new FileWriter(writerName + "-expected.json");
FileWriter actualWriter = new FileWriter(writerName + "-actual.json");
// parsed openAPI object with swagger-parser
SwaggerParseResult result = new OpenAPIParser().readLocation(resource.getPath() + "/" + file, null, null);
POJOOpenAPI = result.getOpenAPI();
System.out.println("Loading expression DSL file '" + file + "'.");
// validation of OpenAPI in POJO
JsonNode expectedNode = mapper.readTree(Json.mapper().writeValueAsString(POJOOpenAPI));
validation = new OpenAPIV3Parser().readContents(expectedNode.toString()).getMessages();
if ( validation.size() != 0 ) {
System.out.println("validation failed!");
for ( String s : validation )
System.out.println(s);
} }
else
System.out.println("validated!");
// save expected object protected static void compareJson(JsonNode expectedNode, JsonNode actualNode, Path path) throws IOException {
expectedWriter.write(expectedNode.toPrettyString());
expectedWriter.close();
// OpenAPI in POJO to OpenAPI in JastAdd
jastAddObject = OpenAPIObject.parseOpenAPI(POJOOpenAPI);
// OpenAPI in JastAdd to OpenAPI in POJO
OpenAPI transformedAPI = OpenAPIObject.reverseOpenAPI(jastAddObject);
// validation of transferred OpenAPI
JsonNode actualNode = mapper.readTree(Json.mapper().writeValueAsString(transformedAPI));
validation = new OpenAPIV3Parser().readContents(actualNode.toString()).getMessages();
if ( validation.size() != 0 ) {
System.out.println("validation failed!");
for ( String s : validation )
System.out.println(s);
}
else
System.out.println("validated");
// save generated object
actualWriter.write(actualNode.toPrettyString());
actualWriter.close();
// compare if api (source object) is equivalent to api3 (generated object)
compareJson(expectedNode, actualNode, Paths.get(file));
}
// clean all generated jsons
/*
contents = genDirectory.listFiles();
if (contents != null) {
for (File file : contents)
file.delete();
}
*/
}
protected void compareJson(JsonNode expectedNode, JsonNode actualNode, Path path) throws IOException {
JsonNode diff = JsonDiff.asJson(expectedNode, actualNode); JsonNode diff = JsonDiff.asJson(expectedNode, actualNode);
String pathNode; String pathNode;
String result = ""; String result = "";
...@@ -116,6 +54,8 @@ public class OpenAPIMain_test { ...@@ -116,6 +54,8 @@ public class OpenAPIMain_test {
for (String s : pathNode.split("/")) { for (String s : pathNode.split("/")) {
if (s.contains(".")) if (s.contains("."))
pathNode = pathNode.replace(s, "['" + s + "']"); pathNode = pathNode.replace(s, "['" + s + "']");
else if (s.contains(" "))
pathNode = pathNode.replace(s, "['" + s + "']");
} }
pathNode = pathNode pathNode = pathNode
.replace("/", ".") .replace("/", ".")
...@@ -129,8 +69,11 @@ public class OpenAPIMain_test { ...@@ -129,8 +69,11 @@ public class OpenAPIMain_test {
} }
pathNode = result.substring(0, result.length()-1); pathNode = result.substring(0, result.length()-1);
//System.out.println(JsonPath.parse(expectedNode.toString()).read(pathNode, String.class));
//System.out.println(JsonPath.parse(actualNode.toString()).read(pathNode, String.class));
System.out.println(pathNode);
// check, if this node exists or has an empty value. // check, if this node exists or has an empty value.
if (JsonPath.parse(expectedNode.toString()).read(pathNode, String.class).isEmpty()) if (JsonPath.parse(expectedNode.toString()).read(pathNode, String.class) == null || JsonPath.parse(expectedNode.toString()).read(pathNode, String.class).isEmpty())
((ArrayNode) diff).remove(i); ((ArrayNode) diff).remove(i);
else if (JsonPath.parse(actualNode.toString()).read(pathNode, String.class) == null || JsonPath.parse(actualNode.toString()).read(pathNode, String.class).isEmpty()) else if (JsonPath.parse(actualNode.toString()).read(pathNode, String.class) == null || JsonPath.parse(actualNode.toString()).read(pathNode, String.class).isEmpty())
((ArrayNode) diff).remove(i); ((ArrayNode) diff).remove(i);
...@@ -155,4 +98,67 @@ public class OpenAPIMain_test { ...@@ -155,4 +98,67 @@ public class OpenAPIMain_test {
} }
return true; return true;
} }
protected static void recursiveTest(File file) throws Exception {
if ( file.isDirectory() ) {
for ( File f : file.listFiles() )
recursiveTest(f);
} else if ( file.isFile() && file.getPath().contains("yaml") ) {
OpenAPIObject jastAddObject;
OpenAPI POJOOpenAPI;
ObjectMapper mapper = new ObjectMapper();
List<String> validation;
/*
String writerName = genDir + file;
writerName = writerName.substring(0, writerName.length() - 5);
FileWriter expectedWriter = new FileWriter(writerName + "-expected.json");
FileWriter actualWriter = new FileWriter(writerName + "-actual.json");
*/
// parsed openAPI object with swagger-parser
SwaggerParseResult result = new OpenAPIParser().readLocation(file.getPath(), null, null);
POJOOpenAPI = result.getOpenAPI();
System.out.println("Loading expression DSL file '" + file + "'.");
// validation of OpenAPI in POJO
JsonNode expectedNode = mapper.readTree(Json.mapper().writeValueAsString(POJOOpenAPI));
validation = new OpenAPIV3Parser().readContents(expectedNode.toString()).getMessages();
if ( validation.size() != 0 ) {
System.out.println("validation failed!");
for ( String s : validation )
System.out.println(s);
}
else
System.out.println("validated!");
// save expected object
//expectedWriter.write(expectedNode.toPrettyString());
//expectedWriter.close();
// OpenAPI in POJO to OpenAPI in JastAdd
jastAddObject = OpenAPIObject.parseOpenAPI(POJOOpenAPI);
// OpenAPI in JastAdd to OpenAPI in POJO
OpenAPI transformedAPI = OpenAPIObject.reverseOpenAPI(jastAddObject);
// validation of transferred OpenAPI
JsonNode actualNode = mapper.readTree(Json.mapper().writeValueAsString(transformedAPI));
validation = new OpenAPIV3Parser().readContents(actualNode.toString()).getMessages();
if ( validation.size() != 0 ) {
System.out.println("validation failed!");
for ( String s : validation )
System.out.println(s);
}
else
System.out.println("validated");
// save generated object
//actualWriter.write(actualNode.toPrettyString());
//actualWriter.close();
// compare if api (source object) is equivalent to api3 (generated object)
compareJson(expectedNode, actualNode, Paths.get(file.getPath()));
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment