Skip to content

Commit

Permalink
Add positive test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Sep 10, 2024
1 parent 42d4c6b commit a064441
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 0 deletions.
100 changes: 100 additions & 0 deletions openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a064441

Please sign in to comment.