Skip to content

Commit

Permalink
Add negative tests scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
lnash94 committed Oct 15, 2023
1 parent ca37508 commit 0847854
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,8 @@ public String toString() {
public static final String YML_EXTENSION = ".yml";
public static final String PLUS = "+";
public static final String UNDERSCORE = "_";

public static final String HTTP_202 = "202";
public static final String HTTP_400 = "400";
public static final String ACCEPTED = "Accepted";
public static final String BAD_REQUEST = "BadRequest";
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
import static io.ballerina.compiler.syntax.tree.SyntaxKind.RECORD_FIELD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.SIMPLE_NAME_REFERENCE;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.TYPE_REFERENCE;
import static io.ballerina.openapi.converter.Constants.ACCEPTED;
import static io.ballerina.openapi.converter.Constants.APPLICATION_PREFIX;
import static io.ballerina.openapi.converter.Constants.BAD_REQUEST;
import static io.ballerina.openapi.converter.Constants.BODY;
import static io.ballerina.openapi.converter.Constants.BYTE;
import static io.ballerina.openapi.converter.Constants.BYTE_ARRAY;
Expand All @@ -106,7 +108,9 @@
import static io.ballerina.openapi.converter.Constants.HTTP_200_DESCRIPTION;
import static io.ballerina.openapi.converter.Constants.HTTP_201;
import static io.ballerina.openapi.converter.Constants.HTTP_201_DESCRIPTION;
import static io.ballerina.openapi.converter.Constants.HTTP_202;
import static io.ballerina.openapi.converter.Constants.HTTP_204;
import static io.ballerina.openapi.converter.Constants.HTTP_400;
import static io.ballerina.openapi.converter.Constants.HTTP_500;
import static io.ballerina.openapi.converter.Constants.HTTP_500_DESCRIPTION;
import static io.ballerina.openapi.converter.Constants.HTTP_CODES;
Expand Down Expand Up @@ -198,12 +202,12 @@ public void getResourceOutput(FunctionDefinitionNode resource, OperationAdaptor
} else {
// When the return type is not mention in the resource function.
ApiResponse apiResponse = new ApiResponse();
apiResponse.description("Accepted");
apiResponses.put("202", apiResponse);
apiResponse.description(ACCEPTED);
apiResponses.put(HTTP_202, apiResponse);
if (operation.getRequestBody() != null || operation.getParameters() != null) {
ApiResponse badRequestResponse = new ApiResponse();
badRequestResponse.description("BadRequest");
apiResponses.put("400", badRequestResponse);
badRequestResponse.description(BAD_REQUEST);
apiResponses.put(HTTP_400, badRequestResponse);
}
}
operation.setResponses(apiResponses);
Expand Down Expand Up @@ -441,13 +445,13 @@ private Optional<ApiResponses> getAPIResponses(OperationAdaptor operationAdaptor
String description = httpMethod.equals(POST) ? HTTP_201_DESCRIPTION : HTTP_200_DESCRIPTION;
if (typeNode.parent().kind() == SyntaxKind.OPTIONAL_TYPE_DESC) {
ApiResponse acceptedRequestResponse = new ApiResponse();
acceptedRequestResponse.description("Accepted");
apiResponses.put("202", acceptedRequestResponse);
acceptedRequestResponse.description(ACCEPTED);
apiResponses.put(HTTP_202, acceptedRequestResponse);
Operation operation = operationAdaptor.getOperation();
if (operation.getRequestBody() != null || operation.getParameters() != null) {
ApiResponse badRequestResponse = new ApiResponse();
badRequestResponse.description("BadRequest");
apiResponses.put("400", badRequestResponse);
badRequestResponse.description(BAD_REQUEST);
apiResponses.put(HTTP_400, badRequestResponse);
}
}
String mediaTypeString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.openapi.generators.openapi;

import io.ballerina.openapi.cmd.OASContractGenerator;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* This test class is covering the negative unit tests for return type scenarios.
*/
public class NegativeResponseTests {
private static final Path RES_DIR = Paths.get("src/test/resources/ballerina-to-openapi").toAbsolutePath();
private Path tempDir;

@BeforeMethod
public void setup() throws IOException {
this.tempDir = Files.createTempDirectory("bal-to-openapi-test-out-" + System.nanoTime());
}

@Test(description = "When the resource has nil type return")
public void testNilReturnType() {
Path ballerinaFilePath = RES_DIR.resolve("response/negative/nil_return_type.bal");
OASContractGenerator openApiConverterUtils = new OASContractGenerator();
openApiConverterUtils.generateOAS3DefinitionsAllService(ballerinaFilePath, this.tempDir, null
, false);
//TODO: Uncomment after merging this PR https://github.com/ballerina-platform/openapi-tools/pull/1370.
// Assert.assertFalse(openApiConverterUtils.getErrors().isEmpty());
Assert.assertFalse(Files.exists(tempDir.resolve("payloadV_openapi.yaml")));
}


@AfterMethod
public void cleanUp() {
TestUtils.deleteDirectory(this.tempDir);
}

@AfterTest
public void clean() {
System.setErr(null);
System.setOut(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ servers:
paths:
/path_get_error:
get:
summary: for `get` method with nil return type and no error status code
summary: for `get` method with nil return type and error
operationId: getPathGetError
responses:
"202":
Expand All @@ -25,7 +25,7 @@ paths:
$ref: '#/components/schemas/ErrorPayload'
/path_get:
get:
summary: for `get` method with nil return type and no error status code
summary: for `get` method with nil return type and success status code
operationId: getPathGet
responses:
"202":
Expand All @@ -38,7 +38,8 @@ paths:
type: string
/path_get_query_param:
get:
summary: for `get` method with nil return type and no error status code
summary: for `get` method with nil return type and success status code with
query param
operationId: getPathGetQueryParam
parameters:
- name: id
Expand All @@ -59,7 +60,7 @@ paths:
type: string
/path_post:
post:
summary: for `post` method with 201 payload with optional error status code
summary: for `post` method with 201 payload with error status code and nil type
operationId: postPathPost
responses:
"201":
Expand All @@ -74,7 +75,7 @@ paths:
description: NotFound
/path:
get:
summary: for union return type with error status code
summary: for union return type with error status code and nil type
operationId: getPath
responses:
"200":
Expand All @@ -88,8 +89,8 @@ paths:
"404":
description: NotFound
post:
summary: "with same error status code (202): by default for ? and explicitly\
\ mentioned as return type"
summary: "resource has 3 status codes that explicitly returns (202, 404, 400)\
\ while ? type returns implicitly 400, 202"
operationId: postPath
parameters:
- name: id
Expand All @@ -110,6 +111,8 @@ paths:
type: string
/path_with_query:
get:
summary: "resource has 4 status codes that explicitly returns (200, 404) while\
\ ? type returns implicitly 400, 202"
operationId: getPathWithQuery
parameters:
- name: id
Expand All @@ -132,6 +135,8 @@ paths:
description: NotFound
/path_with_path/{id}:
post:
summary: "resource has 3 status codes that explicitly returns (404) while ?\
\ type returns implicitly 400, 202"
operationId: postPathWithPathId
parameters:
- name: id
Expand All @@ -148,6 +153,8 @@ paths:
description: NotFound
/path_with_headers:
post:
summary: "method has 3 status codes that explicitly returns (404) while ? type\
\ returns implicitly 400, 202"
operationId: postPathWithHeaders
parameters:
- name: header
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ballerina/http;

service /payloadV on new http:Listener(9090) {

resource function get path_with_request_body(@http:Payload string payload) {
};

resource function head path_with_request_body(@http:Payload string payload) {
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ service /payloadV on new http:Listener(9090) {
};
resource function get path_with_headers(@http:Header string header) {
};
// resource function get path_with_request_body(@http:Payload string payload) {
// };

resource function post path1() {
};
Expand Down Expand Up @@ -64,8 +62,6 @@ service /payloadV on new http:Listener(9090) {
};
resource function head path_with_headers(@http:Header string header) {
};
// resource function head path_with_request_body(@http:Payload string payload) {
// };

resource function option path1() {
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,39 @@ type Links record {|


service /payloadV on new http:Listener(9090) {
# for `get` method with nil return type and no error status code
# for `get` method with nil return type and error
resource function get path_get_error() returns error? {
};

# for `get` method with nil return type and no error status code
# for `get` method with nil return type and success status code
resource function get path_get() returns string? {
};

# for `get` method with nil return type and no error status code
# for `get` method with nil return type and success status code with query param
resource function get path_get_query_param(string id) returns string? {
};

# for `post` method with 201 payload with optional error status code
# for `post` method with 201 payload with error status code and nil type
resource function post path_post() returns Links|http:NotFound? {
};

# for union return type with error status code
# for union return type with error status code and nil type
resource function get path() returns string|http:NotFound? {
};

# with same error status code (202): by default for ? and explicitly mentioned as return type
# resource has 3 status codes that explicitly returns (202, 404, 400) while ? type returns implicitly 400, 202
resource function post path(string id) returns http:Accepted|http:NotFound|BadRequestRecord? {
};

# resource has 4 status codes that explicitly returns (200, 404) while ? type returns implicitly 400, 202
resource function get path_with_query(string id) returns string|http:NotFound? {
};

# resource has 3 status codes that explicitly returns (404) while ? type returns implicitly 400, 202
resource function post path_with_path/[string id]() returns http:NotFound? {
};

# method has 3 status codes that explicitly returns (404) while ? type returns implicitly 400, 202
resource function post path_with_headers(@http:Header string header) returns http:NotFound? {
};

Expand Down
1 change: 1 addition & 0 deletions openapi-cli/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ under the License.
<class name="io.ballerina.openapi.generators.testcases.BallerinaTestGeneratorTests"/>
<class name="io.ballerina.openapi.generators.client.OneOfResponsesTests"/>
<class name="io.ballerina.openapi.generators.openapi.DataTypeTests"/>
<class name="io.ballerina.openapi.generators.openapi.NegativeResponseTests"/>
<!-- <class name="io.ballerina.openapi.generators.schema.SwaggerParserTests"/>-->
</classes>
</test>
Expand Down

0 comments on commit 0847854

Please sign in to comment.