diff --git a/src/main/java/com/tweesky/cloudtools/codegen/PostmanV2Generator.java b/src/main/java/com/tweesky/cloudtools/codegen/PostmanV2Generator.java index b4c2049..33032aa 100644 --- a/src/main/java/com/tweesky/cloudtools/codegen/PostmanV2Generator.java +++ b/src/main/java/com/tweesky/cloudtools/codegen/PostmanV2Generator.java @@ -293,10 +293,16 @@ void addToMap(CodegenOperation codegenOperation){ codegenOperationsByTag.put(key, list); + // sort requests by path + Collections.sort(list, Comparator.comparing(obj -> obj.path)); + } void addToList(CodegenOperation codegenOperation) { codegenOperationsList.add(codegenOperation); + + // sort requests by path + Collections.sort(codegenOperationsList, Comparator.comparing(obj -> obj.path)); } String getResponseBody(CodegenResponse codegenResponse) { @@ -365,6 +371,8 @@ List getPostmanRequests(CodegenOperation codegenOperation) { return items; } + + // split, trim void extractPostmanVariableNames(String postmanVariablesCsv) { postmanVariableNames = postmanVariablesCsv.split("-"); diff --git a/src/test/java/com/tweesky/cloudtools/codegen/PostmanV2GeneratorTest.java b/src/test/java/com/tweesky/cloudtools/codegen/PostmanV2GeneratorTest.java index 870f3f9..2f6e349 100644 --- a/src/test/java/com/tweesky/cloudtools/codegen/PostmanV2GeneratorTest.java +++ b/src/test/java/com/tweesky/cloudtools/codegen/PostmanV2GeneratorTest.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.tweesky.cloudtools.codegen.model.PostmanRequestItem; +import io.swagger.v3.oas.models.tags.Tag; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -9,6 +10,7 @@ import org.junit.Assert; import org.junit.Test; import org.openapitools.codegen.ClientOptInput; +import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.DefaultGenerator; import org.openapitools.codegen.config.CodegenConfigurator; @@ -19,6 +21,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertEquals; @@ -207,7 +210,6 @@ public void testVariablesInRequestExample() throws IOException, ParseException { // verify request endpoint TestUtils.assertFileContains(path, "\"name\": \"/users/{{userId}}\""); - } @Test public void testVariableThatDoesNotExist() throws IOException, ParseException { @@ -530,4 +532,72 @@ public void testFormatDescription() throws Exception { assertEquals(EXPECTED, new PostmanV2Generator().formatDescription(DESCRIPTION)); } + @Test + public void testAddToList() { + + PostmanV2Generator postmanV2Generator = new PostmanV2Generator(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + postmanV2Generator.addToList(operationUsers); + + CodegenOperation operationGroups = new CodegenOperation(); + operationGroups.path = "/groups"; + postmanV2Generator.addToList(operationGroups); + + CodegenOperation operationUserId = new CodegenOperation(); + operationUserId.path = "/users/{id}"; + postmanV2Generator.addToList(operationUserId); + + assertEquals(3, postmanV2Generator.codegenOperationsList.size()); + // verify order + assertEquals("/groups", postmanV2Generator.codegenOperationsList.get(0).path); + assertEquals("/users", postmanV2Generator.codegenOperationsList.get(1).path); + assertEquals("/users/{id}", postmanV2Generator.codegenOperationsList.get(2).path); + } + + @Test + public void testAddToMap() { + + PostmanV2Generator postmanV2Generator = new PostmanV2Generator(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + operationUsers.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationUsers); + + CodegenOperation operationGroups = new CodegenOperation(); + operationGroups.path = "/groups"; + operationGroups.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationGroups); + + CodegenOperation operationUserId = new CodegenOperation(); + operationUserId.path = "/users/{id}"; + operationUserId.tags = new ArrayList<>(Arrays.asList(new Tag().name("basic"))); + postmanV2Generator.addToMap(operationUserId); + + // verify tag 'basic' + assertEquals(1, postmanV2Generator.codegenOperationsByTag.size()); + assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("basic")); + + List operations = postmanV2Generator.codegenOperationsByTag.get("basic"); + // verify order + assertEquals("/groups", operations.get(0).path); + assertEquals("/users", operations.get(1).path); + assertEquals("/users/{id}", operations.get(2).path); + } + + @Test + public void testAddToMapUsingDefaultTag() { + + PostmanV2Generator postmanV2Generator = new PostmanV2Generator(); + + CodegenOperation operationUsers = new CodegenOperation(); + operationUsers.path = "/users"; + postmanV2Generator.addToMap(operationUsers); + + // verify tag 'default' is used + assertEquals(1, postmanV2Generator.codegenOperationsByTag.size()); + assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("default")); + } } \ No newline at end of file