From a064441f3bbd9789001bffe274c89f392bf9645b Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Tue, 10 Sep 2024 09:53:12 +0530 Subject: [PATCH] Add positive test cases --- .../ballerina/openapi/cmd/OpenAPICmdTest.java | 100 ++++++++++++ .../flatten/flattened_openapi_expected.json | 145 ++++++++++++++++++ .../flatten/flattened_openapi_expected.yaml | 99 ++++++++++++ .../flattened_openapi_expected_list_pets.json | 66 ++++++++ .../flattened_openapi_expected_pets.json | 102 ++++++++++++ .../test/resources/cmd/flatten/openapi.json | 145 ++++++++++++++++++ .../test/resources/cmd/flatten/openapi.yaml | 92 +++++++++++ 7 files changed, 749 insertions(+) create mode 100644 openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.json create mode 100644 openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.yaml create mode 100644 openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_list_pets.json create mode 100644 openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_pets.json create mode 100644 openapi-cli/src/test/resources/cmd/flatten/openapi.json create mode 100644 openapi-cli/src/test/resources/cmd/flatten/openapi.yaml diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java index 980ce071a..ba7319b95 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java @@ -915,6 +915,106 @@ public void testAddCmd() throws IOException { Assert.assertTrue(tomlContent.contains(generatedTool)); } + @Test(description = "Test openapi flatten sub command with default options with the json file") + public void testFlattenCmdDefaultJson() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected.json")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.json", "-o", tmpDir.toString()}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.json")); + } + + private void compareFiles(Path expectedFilePath, Path generatedFilePath) throws IOException { + if (Files.exists(generatedFilePath)) { + String expectedOpenAPIContent = ""; + try (Stream expectedOpenAPIContentLines = Files.lines(expectedFilePath)) { + expectedOpenAPIContent = expectedOpenAPIContentLines.collect(Collectors.joining(LINE_SEPARATOR)); + } catch (IOException e) { + Files.deleteIfExists(generatedFilePath); + Assert.fail(e.getMessage()); + } + + String generatedOpenAPIContent = ""; + try (Stream generatedOpenAPIContentLines = + Files.lines(generatedFilePath)) { + generatedOpenAPIContent = generatedOpenAPIContentLines.collect(Collectors.joining(LINE_SEPARATOR)); + } catch (IOException e) { + Files.deleteIfExists(generatedFilePath); + Assert.fail(e.getMessage()); + } + + generatedOpenAPIContent = (generatedOpenAPIContent.trim()).replaceAll("\\s+", ""); + expectedOpenAPIContent = (expectedOpenAPIContent.trim()).replaceAll("\\s+", ""); + Assert.assertEquals(expectedOpenAPIContent, generatedOpenAPIContent); + + Files.deleteIfExists(generatedFilePath); + } else { + Assert.fail("Generation failed: " + readOutput(true)); + } + } + + @Test(description = "Test openapi flatten sub command with default options with the yaml file") + public void testFlattenCmdDefaultYaml() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.yaml", "-o", tmpDir.toString()}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.yaml")); + } + + @Test(description = "Test openapi flatten sub command with the name option") + public void testFlattenCmdName() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected.json")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.json", "-o", tmpDir.toString(), "-n", "flattened"}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened.json")); + } + + @Test(description = "Test openapi flatten sub command with the json format option") + public void testFlattenCmdJsonFormat() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected.json")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.yaml", "-o", tmpDir.toString(), "-f", "json"}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.json")); + } + + @Test(description = "Test openapi flatten sub command with the yaml format option") + public void testFlattenCmdYamlFormat() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.json", "-o", tmpDir.toString(), "-f", "yaml"}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.yaml")); + } + + @Test(description = "Test openapi flatten sub command with the tags option") + public void testFlattenCmdTags() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected_pets.json")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.json", "-o", tmpDir.toString(), "-t", "pets"}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.json")); + } + + @Test(description = "Test openapi flatten sub command with the operations option") + public void testFlattenCmdOperations() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/flatten/flattened_openapi_expected_list_pets.json")); + String[] args = {"-i", resourceDir + "/cmd/flatten/openapi.json", "-o", tmpDir.toString(), "--operations", + "listPets"}; + Flatten flatten = new Flatten(); + new CommandLine(flatten).parseArgs(args); + flatten.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.json")); + } + @AfterTest public void clean() { System.setErr(null); diff --git a/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.json b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.json new file mode 100644 index 000000000..0fe12171f --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.json @@ -0,0 +1,145 @@ +{ + "openapi" : "3.0.0", + "info" : { + "title" : "Swagger Petstore", + "license" : { + "name" : "MIT" + }, + "version" : "1.0.0" + }, + "servers" : [ { + "url" : "/" + } ], + "paths" : { + "/pets" : { + "get" : { + "tags" : [ "pets" ], + "summary" : "List all pets", + "operationId" : "listPets", + "responses" : { + "200" : { + "description" : "An paged array of pets", + "headers" : { + "x-next" : { + "description" : "A link to the next page of responses", + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" + } + } + }, + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/InlineResponse200" + } + } + } + } + } + } + }, + "post" : { + "tags" : [ "pets" ], + "summary" : "Create a pet", + "operationId" : "createPets", + "requestBody" : { + "description" : "Created pet object", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/PetsBody" + } + } + }, + "required" : true + }, + "responses" : { + "201" : { + "description" : "Null response" + } + } + } + }, + "/locations" : { + "get" : { + "tags" : [ "locations" ], + "summary" : "List all locations", + "operationId" : "listLocations", + "parameters" : [ { + "name" : "area", + "in" : "query", + "description" : "area to filter by", + "required" : false, + "style" : "form", + "explode" : true, + "schema" : { + "type" : "string" + } + } ], + "responses" : { + "200" : { + "description" : "An paged array of locations", + "headers" : { + "x-next" : { + "description" : "A link to the next page of responses", + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" + } + } + }, + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/PetsBody" + } + } + } + } + } + } + } + } + }, + "components" : { + "schemas" : { + "InlineResponse200" : { + "type" : "object", + "properties" : { + "id" : { + "type" : "integer", + "format" : "int64" + }, + "name" : { + "type" : "string" + }, + "tag" : { + "type" : "string" + } + } + }, + "PetsBody" : { + "type" : "object", + "properties" : { + "id" : { + "type" : "integer", + "format" : "int64" + }, + "name" : { + "type" : "string" + }, + "tag" : { + "type" : "string" + } + } + } + } + } +} diff --git a/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.yaml b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.yaml new file mode 100644 index 000000000..8e25637c6 --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected.yaml @@ -0,0 +1,99 @@ +openapi: 3.0.0 +info: + title: Swagger Petstore + license: + name: MIT + version: 1.0.0 +servers: + - url: / +paths: + /pets: + get: + tags: + - pets + summary: List all pets + operationId: listPets + responses: + "200": + description: An paged array of pets + headers: + x-next: + description: A link to the next page of responses + style: simple + explode: false + schema: + type: string + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/InlineResponse200" + post: + tags: + - pets + summary: Create a pet + operationId: createPets + requestBody: + description: Created pet object + content: + application/json: + schema: + $ref: "#/components/schemas/PetsBody" + required: true + responses: + "201": + description: Null response + /locations: + get: + tags: + - locations + summary: List all locations + operationId: listLocations + parameters: + - name: area + in: query + description: area to filter by + required: false + style: form + explode: true + schema: + type: string + responses: + "200": + description: An paged array of locations + headers: + x-next: + description: A link to the next page of responses + style: simple + explode: false + schema: + type: string + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/PetsBody" +components: + schemas: + InlineResponse200: + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + PetsBody: + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string diff --git a/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_list_pets.json b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_list_pets.json new file mode 100644 index 000000000..6d6e51f6b --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_list_pets.json @@ -0,0 +1,66 @@ +{ + "openapi" : "3.0.0", + "info" : { + "title" : "Swagger Petstore", + "license" : { + "name" : "MIT" + }, + "version" : "1.0.0" + }, + "servers" : [ { + "url" : "/" + } ], + "paths" : { + "/pets" : { + "get" : { + "tags" : [ "pets" ], + "summary" : "List all pets", + "operationId" : "listPets", + "responses" : { + "200" : { + "description" : "An paged array of pets", + "headers" : { + "x-next" : { + "description" : "A link to the next page of responses", + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" + } + } + }, + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/InlineResponse200" + } + } + } + } + } + } + } + } + }, + "components" : { + "schemas" : { + "InlineResponse200" : { + "type" : "object", + "properties" : { + "id" : { + "type" : "integer", + "format" : "int64" + }, + "name" : { + "type" : "string" + }, + "tag" : { + "type" : "string" + } + } + } + } + } +} diff --git a/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_pets.json b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_pets.json new file mode 100644 index 000000000..7c6531eba --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/flattened_openapi_expected_pets.json @@ -0,0 +1,102 @@ +{ + "openapi" : "3.0.0", + "info" : { + "title" : "Swagger Petstore", + "license" : { + "name" : "MIT" + }, + "version" : "1.0.0" + }, + "servers" : [ { + "url" : "/" + } ], + "paths" : { + "/pets" : { + "get" : { + "tags" : [ "pets" ], + "summary" : "List all pets", + "operationId" : "listPets", + "responses" : { + "200" : { + "description" : "An paged array of pets", + "headers" : { + "x-next" : { + "description" : "A link to the next page of responses", + "style" : "simple", + "explode" : false, + "schema" : { + "type" : "string" + } + } + }, + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/InlineResponse200" + } + } + } + } + } + } + }, + "post" : { + "tags" : [ "pets" ], + "summary" : "Create a pet", + "operationId" : "createPets", + "requestBody" : { + "description" : "Created pet object", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/PetsBody" + } + } + }, + "required" : true + }, + "responses" : { + "201" : { + "description" : "Null response" + } + } + } + } + }, + "components" : { + "schemas" : { + "InlineResponse200" : { + "type" : "object", + "properties" : { + "id" : { + "type" : "integer", + "format" : "int64" + }, + "name" : { + "type" : "string" + }, + "tag" : { + "type" : "string" + } + } + }, + "PetsBody" : { + "type" : "object", + "properties" : { + "id" : { + "type" : "integer", + "format" : "int64" + }, + "name" : { + "type" : "string" + }, + "tag" : { + "type" : "string" + } + } + } + } + } +} diff --git a/openapi-cli/src/test/resources/cmd/flatten/openapi.json b/openapi-cli/src/test/resources/cmd/flatten/openapi.json new file mode 100644 index 000000000..2e00b94ed --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/openapi.json @@ -0,0 +1,145 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "license": { + "name": "MIT" + } + }, + "paths": { + "/pets": { + "get": { + "summary": "List all pets", + "operationId": "listPets", + "tags": [ + "pets" + ], + "responses": { + "200": { + "description": "An paged array of pets", + "headers": { + "x-next": { + "description": "A link to the next page of responses", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + } + } + } + } + } + } + }, + "post": { + "summary": "Create a pet", + "operationId": "createPets", + "tags": [ + "pets" + ], + "requestBody": { + "description": "Created pet object", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "Null response" + } + } + } + }, + "/locations": { + "get": { + "summary": "List all locations", + "operationId": "listLocations", + "tags": [ + "locations" + ], + "parameters": [ + { + "name": "area", + "in": "query", + "description": "area to filter by", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "An paged array of locations", + "headers": { + "x-next": { + "description": "A link to the next page of responses", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + } + } + } + } + } + } + } + } + } +} diff --git a/openapi-cli/src/test/resources/cmd/flatten/openapi.yaml b/openapi-cli/src/test/resources/cmd/flatten/openapi.yaml new file mode 100644 index 000000000..8ddab3021 --- /dev/null +++ b/openapi-cli/src/test/resources/cmd/flatten/openapi.yaml @@ -0,0 +1,92 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +paths: + /pets: + get: + summary: List all pets + operationId: listPets + tags: + - pets + responses: + '200': + description: An paged array of pets + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + type: array + items: + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + post: + summary: Create a pet + operationId: createPets + tags: + - pets + requestBody: + description: Created pet object + content: + application/json: + schema: + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + required: true + responses: + '201': + description: Null response + /locations: + get: + summary: List all locations + operationId: listLocations + tags: + - locations + parameters: + - name: area + in: query + description: area to filter by + schema: + type: string + responses: + '200': + description: An paged array of locations + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + type: array + items: + type: object + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string