Skip to content

Commit

Permalink
Add integration tests for default responses
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed May 28, 2024
1 parent 0f288ae commit ca5219d
Show file tree
Hide file tree
Showing 14 changed files with 1,490 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,71 @@ public void nonBallerinaPackageWithStatusCodeBinding() throws IOException, Inter
compareFiles(projectGenPath, "client.bal", projectExpectedPath, "client_normal.bal");
}

@Test(description = "`--status-code-binding` option with resource client methods and default responses")
public void resourceClientWithDefaultStatusCodeBinding() throws IOException, InterruptedException {
String openapiFilePath = "openapi.yaml";
List<String> buildArgs = new LinkedList<>();
buildArgs.add("-i");
buildArgs.add(openapiFilePath);
buildArgs.add("--mode");
buildArgs.add("client");
buildArgs.add("--status-code-binding");
Path projectGenPath = Paths.get(TEST_RESOURCE + "/project-06");
Path projectExpectedPath = Paths.get(EXPECTED_RESOURCE + "/project-expected");
boolean successful = TestUtil.executeOpenAPI(DISTRIBUTION_FILE_NAME, projectGenPath, buildArgs);
Assert.assertTrue(Files.exists(projectGenPath.resolve("Ballerina.toml")));
Assert.assertTrue(Files.exists(projectGenPath.resolve("client.bal")));
compareFiles(projectGenPath, "client.bal", projectExpectedPath, "client_resource_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("types.bal")));
compareFiles(projectGenPath, "types.bal", projectExpectedPath, "types_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("utils.bal")));
compareFiles(projectGenPath, "utils.bal", projectExpectedPath, "utils_with_default.bal");
}

@Test(description = "`--status-code-binding` option with remote client methods and default responses")
public void remoteClientWithDefaultStatusCodeBinding() throws IOException, InterruptedException {
String openapiFilePath = "openapi.yaml";
List<String> buildArgs = new LinkedList<>();
buildArgs.add("-i");
buildArgs.add(openapiFilePath);
buildArgs.add("--mode");
buildArgs.add("client");
buildArgs.add("--client-methods");
buildArgs.add("remote");
buildArgs.add("--status-code-binding");
Path projectGenPath = Paths.get(TEST_RESOURCE + "/project-07");
Path projectExpectedPath = Paths.get(EXPECTED_RESOURCE + "/project-expected");
boolean successful = TestUtil.executeOpenAPI(DISTRIBUTION_FILE_NAME, projectGenPath, buildArgs);
Assert.assertTrue(Files.exists(projectGenPath.resolve("Ballerina.toml")));
Assert.assertTrue(Files.exists(projectGenPath.resolve("client.bal")));
compareFiles(projectGenPath, "client.bal", projectExpectedPath, "client_remote_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("types.bal")));
compareFiles(projectGenPath, "types.bal", projectExpectedPath, "types_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("utils.bal")));
compareFiles(projectGenPath, "utils.bal", projectExpectedPath, "utils_with_default.bal");
}

@Test(description = "`--status-code-binding` option with service, client and default responses")
public void serviceAndClientWithDefaultStatusCodeBinding() throws IOException, InterruptedException {
String openapiFilePath = "openapi.yaml";
List<String> buildArgs = new LinkedList<>();
buildArgs.add("-i");
buildArgs.add(openapiFilePath);
buildArgs.add("--status-code-binding");
Path projectGenPath = Paths.get(TEST_RESOURCE + "/project-08");
Path projectExpectedPath = Paths.get(EXPECTED_RESOURCE + "/project-expected");
boolean successful = TestUtil.executeOpenAPI(DISTRIBUTION_FILE_NAME, projectGenPath, buildArgs);
Assert.assertTrue(Files.exists(projectGenPath.resolve("Ballerina.toml")));
Assert.assertTrue(Files.exists(projectGenPath.resolve("openapi_service.bal")));
compareFiles(projectGenPath, "openapi_service.bal", projectExpectedPath, "openapi_service_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("client.bal")));
compareFiles(projectGenPath, "client.bal", projectExpectedPath, "client_resource_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("types.bal")));
compareFiles(projectGenPath, "types.bal", projectExpectedPath, "types_all_with_default.bal");
Assert.assertTrue(Files.exists(projectGenPath.resolve("utils.bal")));
compareFiles(projectGenPath, "utils.bal", projectExpectedPath, "utils_all_with_default.bal");
}

/**
* Compare two files.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "openapi_client_test"
name = "project"
version = "0.1.0"

[build-options]
observabilityIncluded = true

Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
openapi: 3.0.1
info:
title: Api
version: 0.1.0
servers:
- url: "{server}:{port}/api"
variables:
server:
default: http://localhost
port:
default: "9090"
paths:
/albums/{id}:
get:
operationId: getAlbumsId
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
"200":
description: Ok
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Album'
"default":
description: Default Error
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorMessage'
/albums:
get:
operationId: getAlbums
parameters:
- name: genre
in: query
required: true
schema:
type: string
responses:
"200":
description: Ok
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Album'
"default":
description: NotFound
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorMessage'
post:
operationId: postAlbums
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Album'
required: true
responses:
"201":
description: Created
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Album'
"default":
description: Conflict
headers:
req-id:
required: true
schema:
type: integer
format: int64
user-id:
required: true
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorMessage'
components:
schemas:
Album:
required:
- artist
- genre
- id
- name
type: object
properties:
id:
type: string
name:
type: string
artist:
type: string
genre:
type: string
additionalProperties: false
ErrorMessage:
required:
- message
type: object
properties:
message:
type: string
additionalProperties:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "openapi_client_test"
name = "project"
version = "0.1.0"

[build-options]
observabilityIncluded = true

Loading

0 comments on commit ca5219d

Please sign in to comment.