Skip to content

Commit

Permalink
Add test cases for escaping special characters in the service type name
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinAkash01 committed Dec 4, 2024
1 parent 4efe8f2 commit 17caab7
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public class ServiceContractTests {
Filter filter = new Filter(list1, list2);
SyntaxTree syntaxTree;

@Test(description = "Test default service object type name")
public void testDefaultServiceTypeNameInGeneratedService() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/default_service_type_name.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/default_service_type_name.bal", syntaxTree);
}

@Test(description = "Test custom service object type name")
public void testCustomServiceTypeNameInGeneratedService() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_service_type_name.yaml");
Expand All @@ -60,20 +77,93 @@ public void testCustomServiceTypeNameInGeneratedService() throws IOException, Ba
"service_type/custom_service_type_name.bal", syntaxTree);
}

@Test(description = "Test default service object type name")
public void testDefaultServiceTypeNameInGeneratedService() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/default_service_type_name.yaml");
@Test(description = "Test custom service object type name with special characters")
public void testCustomServiceTypeNameWithSpecialCharacters() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_special_characters.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.withServiceObjectTypeName("CustomServiceObject%$&TypeName1")
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/default_service_type_name.bal", syntaxTree);
"service_type/custom_name_with_special_characters.bal", syntaxTree);
}

@Test(description = "Test custom service object type name with an empty value")
public void testCustomServiceTypeNameWithEmptyValue() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_empty_value.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.withServiceObjectTypeName("")
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/custom_name_with_empty_value.bal", syntaxTree);
}

@Test(description = "Test custom service object type name with a whitespace")
public void testCustomServiceTypeNameWithWhitespace() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_with_whitespace.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.withServiceObjectTypeName("CustomServiceObject TypeName2")
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/custom_name_with_whitespace.bal", syntaxTree);
}

@Test(description = "Test custom service object type name with only special characters")
public void testCustomServiceTypeNameWithOnlySpecialCharacters() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_only_special_characters.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.withServiceObjectTypeName("%$@#")
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/custom_name_only_special_characters.bal", syntaxTree);
}

@Test(description = "Test custom service object type name with only whitespace")
public void testCustomServiceTypeNameWithOnlyWhitespace() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/service_type/custom_name_only_whitespace.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.withServiceObjectTypeName(" ")
.build();
TypeHandler.createInstance(openAPI, false);
ServiceDeclarationGenerator ballerinaServiceGenerator = new ServiceDeclarationGenerator(oasServiceMetadata);
ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator(oasServiceMetadata,
ballerinaServiceGenerator.getFunctionsList());
syntaxTree = serviceContractGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"service_type/custom_name_only_whitespace.bal", syntaxTree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/http;

@http:ServiceConfig { basePath: "/v1" }
type OASServiceType service object {
*http:ServiceContract;
resource function post pets() returns InlineResponse400BadRequest;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/http;

@http:ServiceConfig { basePath: "/v1" }
type OASServiceType service object {
*http:ServiceContract;
resource function post pets() returns InlineResponse400BadRequest;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/http;

@http:ServiceConfig { basePath: "/v1" }
type OASServiceType service object {
*http:ServiceContract;
resource function post pets() returns InlineResponse400BadRequest;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/http;

@http:ServiceConfig { basePath: "/v1" }
type CustomServiceObjectTypeName1 service object {
*http:ServiceContract;
resource function post pets() returns InlineResponse400BadRequest;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ballerina/http;

@http:ServiceConfig { basePath: "/v1" }
type CustomServiceObjectTypeName2 service object {
*http:ServiceContract;
resource function post pets() returns InlineResponse400BadRequest;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
openapi: 3.0.0
info:
title: refComponent
description: refComponent
version: 1.0.0
servers:
- url: http://petstore.{host}.io/v1
description: The production API server
variables:
host:
default: openapi
description: this value is assigned by the service provider
- url: https://{subdomain}.swagger.io:{port}/{basePath}
description: The production API server
variables:
subdomain:
default: petstore
description: this value is assigned by the service provider
port:
enum:
- '8443'
- '443'
default: '443'
basePath:
default: v2
paths:
/pets:
post:
summary: Creates a new pets.
responses:
'400':
description: A User object
content:
application/json:
schema:
type: object
properties:
id:
type: integer
description: The error ID.
errorType:
type: string
description: The error name.
components:
schemas:
User:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Pet:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
PetForm:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
openapi: 3.0.0
info:
title: refComponent
description: refComponent
version: 1.0.0
servers:
- url: http://petstore.{host}.io/v1
description: The production API server
variables:
host:
default: openapi
description: this value is assigned by the service provider
- url: https://{subdomain}.swagger.io:{port}/{basePath}
description: The production API server
variables:
subdomain:
default: petstore
description: this value is assigned by the service provider
port:
enum:
- '8443'
- '443'
default: '443'
basePath:
default: v2
paths:
/pets:
post:
summary: Creates a new pets.
responses:
'400':
description: A User object
content:
application/json:
schema:
type: object
properties:
id:
type: integer
description: The error ID.
errorType:
type: string
description: The error name.
components:
schemas:
User:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Pet:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
PetForm:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
openapi: 3.0.0
info:
title: refComponent
description: refComponent
version: 1.0.0
servers:
- url: http://petstore.{host}.io/v1
description: The production API server
variables:
host:
default: openapi
description: this value is assigned by the service provider
- url: https://{subdomain}.swagger.io:{port}/{basePath}
description: The production API server
variables:
subdomain:
default: petstore
description: this value is assigned by the service provider
port:
enum:
- '8443'
- '443'
default: '443'
basePath:
default: v2
paths:
/pets:
post:
summary: Creates a new pets.
responses:
'400':
description: A User object
content:
application/json:
schema:
type: object
properties:
id:
type: integer
description: The error ID.
errorType:
type: string
description: The error name.
components:
schemas:
User:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Pet:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
PetForm:
type: object
required:
- userName
properties:
userName:
type: string
firstName:
type: string
lastName:
type: string
Loading

0 comments on commit 17caab7

Please sign in to comment.