Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ina-openapi into pattern_1.6.0

# Conflicts:
#	openapi-cli/src/test/java/io/ballerina/openapi/generators/schema/ConstraintTests.java
#	openapi-cli/src/test/resources/testng.xml
  • Loading branch information
lnash94 committed May 9, 2023
2 parents 8fd2144 + 7872aeb commit 46b8fe5
Show file tree
Hide file tree
Showing 43 changed files with 2,434 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class BallerinaDiagnosticTests {
List<String> list2 = new ArrayList<>();
Filter filter = new Filter(list1, list2);

@Test(description = "Test openAPI definition to ballerina client source code generation with diagnostic issue",
@Test(description = "Test openAPI definition to ballerina client source code generation with remote functions",
dataProvider = "singleFileProviderForDiagnosticCheck")
public void checkDiagnosticIssues(String yamlFile) throws IOException, BallerinaOpenApiException,
public void checkDiagnosticIssuesWithRemoteFunctions(String yamlFile) throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RESDIR.resolve(yamlFile);
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
Expand All @@ -74,6 +74,25 @@ public void checkDiagnosticIssues(String yamlFile) throws IOException, Ballerina
Assert.assertFalse(hasErrors);
}

@Test(description = "Test openAPI definition to ballerina client source code generation with resource functions",
dataProvider = "singleFileProviderForDiagnosticCheck")
public void checkDiagnosticIssuesWithResourceFunctions(String yamlFile) throws IOException,
BallerinaOpenApiException, FormatterException {
Path definitionPath = RESDIR.resolve(yamlFile);
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(true)
.build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree, openAPI, ballerinaClientGenerator);
boolean hasErrors = diagnostics.stream()
.anyMatch(d -> DiagnosticSeverity.ERROR.equals(d.diagnosticInfo().severity()));
Assert.assertFalse(hasErrors);
}

@DataProvider(name = "singleFileProviderForDiagnosticCheck")
public Object[][] singleFileProviderForDiagnosticCheck() {
Expand All @@ -93,7 +112,7 @@ public Object[][] singleFileProviderForDiagnosticCheck() {
{"vendor_specific_mime_types.yaml"},
{"ballerinax_connector_tests/ably.yaml"},
{"ballerinax_connector_tests/azure.iot.yaml"},
// {"ballerinax_connector_tests/beezup.yaml"}, Disabled due to the issue openapi-tools/issues/1257
{"ballerinax_connector_tests/beezup.yaml"},
{"ballerinax_connector_tests/files.com.yaml"},
{"ballerinax_connector_tests/openweathermap.yaml"},
{"ballerinax_connector_tests/soundcloud.yaml"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com). All Rights Reserved.
*
* 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.client;

import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.openapi.core.GeneratorUtils;
import io.ballerina.openapi.core.exception.BallerinaOpenApiException;
import io.ballerina.openapi.core.generators.client.BallerinaClientGenerator;
import io.ballerina.openapi.core.generators.client.model.OASClientConfig;
import io.ballerina.openapi.core.model.Filter;
import io.ballerina.openapi.generators.common.TestUtils;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.swagger.v3.oas.models.OpenAPI;
import org.ballerinalang.formatter.core.FormatterException;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.openapi.generators.common.TestUtils.compareGeneratedSyntaxTreeWithExpectedSyntaxTree;
import static io.ballerina.openapi.generators.common.TestUtils.getDiagnostics;
import static io.ballerina.openapi.generators.common.TestUtils.getOpenAPI;

/**
* Test cases for generating ballerina parameters for openapi parameters with enum schemas.
*/
public class EnumGenerationTests {

private static final Path RES_DIR = Paths.get("src/test/resources/generators/client").toAbsolutePath();

List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
Filter filter = new Filter(list1, list2);
@Test(description = "Tests for all the enum scenarios in remote function parameter generation:" +
"Use case 01 : Enum in query parameter" +
"Use case 02 : Enums in path parameter" +
"Use case 03 : Enum in header parameter" +
"Use case 04 : Enum in reusable parameter" +
"Use case 05 : Enum in parameter with referenced schema")
public void generateRemoteParametersWithEnums() throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_enum.yaml");
Path expectedPath = RES_DIR.resolve("ballerina/parameters_with_enum.bal");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(false).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree, openAPI, ballerinaClientGenerator);
Assert.assertTrue(diagnostics.isEmpty());
compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}

@Test(description = "Tests for all the nullable enum scenarios in remote function parameter generation:" +
"Use case 01 : Nullable enum in query parameter" +
"Use case 02 : Nullable enum in path parameter" +
"Use case 03 : Nullable enum in header parameter" +
"Use case 04 : Nullable enum in reusable parameter" +
"Use case 05 : Nullable enum in parameter with referenced schema")
public void generateRemoteParametersWithNullableEnums() throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_nullable_enums.yaml");
Path expectedPath = RES_DIR.resolve("ballerina/parameters_with_nullable_enums.bal");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(false).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree, openAPI, ballerinaClientGenerator);
Assert.assertTrue(diagnostics.isEmpty());
compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}

@Test(description = "Tests for all the enum scenarios in resource function parameter generation:" +
"Use case 01 : Enum in query parameter" +
"Use case 02 : Enums in path parameter" +
"Use case 03 : Enum in header parameter" +
"Use case 04 : Enum in reusable parameter" +
"Use case 05 : Enum in parameter with referenced schema")
public void generateResourceParametersWithEnums() throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_enum.yaml");
Path expectedPath = RES_DIR.resolve("ballerina/paramters_with_enum_resource.bal");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(true).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree, openAPI, ballerinaClientGenerator);
Assert.assertTrue(diagnostics.isEmpty());
compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}

@Test(description = "Tests for all the nullable enum scenarios in resource function parameter generation:" +
"Use case 01 : Nullable enum in query parameter" +
"Use case 02 : Nullable enum in path parameter" +
"Use case 03 : Nullable enum in header parameter" +
"Use case 04 : Nullable enum in reusable parameter" +
"Use case 05 : Nullable enum in parameter with referenced schema")
public void generateResourceParametersWithNullableEnums() throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RES_DIR.resolve("swagger/parameters_with_nullable_enums.yaml");
Path expectedPath = RES_DIR.resolve("ballerina/parameters_with_nullable_enums_resource.bal");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(true).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree, openAPI, ballerinaClientGenerator);
Assert.assertTrue(diagnostics.isEmpty());
compareGeneratedSyntaxTreeWithExpectedSyntaxTree(expectedPath, syntaxTree);
}

@Test(description = "Test unsupported nullable path parameter with enums",
expectedExceptions = BallerinaOpenApiException.class,
expectedExceptionsMessageRegExp = "Path parameter value cannot be null.")
public void testNullablePathParamWithEnum() throws IOException, BallerinaOpenApiException {
OpenAPI openAPI = getOpenAPI(RES_DIR.resolve("swagger/path_param_nullable_enum.yaml"));
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(filter)
.withOpenAPI(openAPI)
.withResourceMode(true).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
}

@AfterMethod
private void deleteGeneratedFiles() {
try {
TestUtils.deleteGeneratedFiles();
} catch (IOException ignored) {
}
}

@AfterClass
public void cleanUp() throws IOException {
TestUtils.deleteGeneratedFiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import io.swagger.v3.oas.models.OpenAPI;
import org.ballerinalang.formatter.core.FormatterException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
Expand All @@ -43,6 +46,11 @@
public class ConstraintTests {
private static final Path RES_DIR = Paths.get("src/test/resources/generators/schema").toAbsolutePath();

@BeforeMethod
public void setUp() throws IOException {
TestUtils.deleteGeneratedFiles();
}

@Test(description = "Tests with record field has constraint and record field type can be user defined datatype " +
"with constraint.")
public void testRecordFiledConstraint() throws IOException, BallerinaOpenApiException, FormatterException {
Expand Down Expand Up @@ -118,7 +126,7 @@ public void testNestedArrayWithConstraint() throws IOException, BallerinaOpenApi
assertFalse(hasErrors);
}

@Test
@Test(description = "Tests with additional properties field has constraint.")
public void testAdditionalPropertiesWithConstraint() throws IOException, BallerinaOpenApiException,
FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve("swagger/constraint" +
Expand Down Expand Up @@ -206,4 +214,17 @@ public void testForRecordStringHasPattern() throws IOException, BallerinaOpenApi
.anyMatch(d -> DiagnosticSeverity.ERROR.equals(d.diagnosticInfo().severity()));
assertFalse(hasErrors);
}

@AfterMethod
private void deleteGeneratedFiles() {
try {
TestUtils.deleteGeneratedFiles();
} catch (IOException ignored) {
}
}

@AfterClass
public void cleanUp() throws IOException {
TestUtils.deleteGeneratedFiles();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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.schema;

import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.openapi.core.GeneratorUtils;
import io.ballerina.openapi.core.exception.BallerinaOpenApiException;
import io.ballerina.openapi.core.generators.schema.BallerinaTypesGenerator;
import io.ballerina.openapi.generators.common.TestUtils;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.swagger.v3.oas.models.OpenAPI;
import org.ballerinalang.formatter.core.FormatterException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static io.ballerina.openapi.generators.common.TestUtils.getDiagnostics;
import static org.testng.Assert.assertTrue;

/**
* Test cases for generating ballerina types for enum schemas.
*/
public class EnumGenerationTests {

private static final Path RES_DIR = Paths.get("src/test/resources/generators/schema").toAbsolutePath();

@BeforeTest
public void setUp() throws IOException {
TestUtils.deleteGeneratedFiles();
}

@Test(description = "Tests for all the enum scenarios in schema generation:" +
"Use case 01 : Reusable enum" +
"Use case 02 : Enums in schema properties" +
"Use case 03 : Enum with null value" +
"Use case 04 : Enum as array items")
public void testForEnums() throws IOException, BallerinaOpenApiException, FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve("swagger/schema_with_enums.yaml"), true);
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(openAPI);
SyntaxTree syntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree("schema/ballerina/schema_with_enums.bal",
syntaxTree);
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree);
assertTrue(diagnostics.isEmpty());
}

@Test(description = "Tests for all the enum scenarios in schema generation:" +
"Use case 01 : Nullable reusable enum" +
"Use case 02 : Nullable integer enum" +
"Use case 03 : Nullable boolean enum" +
"Use case 04 : Nullable float enum" +
"Use case 05 : Nullable enum in arrays")
public void testForNullableEnums() throws IOException, BallerinaOpenApiException, FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve(
"swagger/schema_with_nullable_enums.yaml"), true);
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(openAPI);
SyntaxTree syntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree("schema/ballerina/schema_with_nullable_enums.bal",
syntaxTree);
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree);
assertTrue(diagnostics.isEmpty());
}

@Test(description = "Test enum with no values")
public void testForEmptyEnums() throws IOException, BallerinaOpenApiException, FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve(
"swagger/empty_enum.yaml"), true);
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(openAPI);
SyntaxTree syntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree("schema/ballerina/empty_enum.bal",
syntaxTree);
List<Diagnostic> diagnostics = getDiagnostics(syntaxTree);
assertTrue(diagnostics.isEmpty());
}
@AfterTest
private void deleteGeneratedFiles() {
try {
TestUtils.deleteGeneratedFiles();
} catch (IOException ignored) {
}
}

@AfterClass
public void cleanUp() throws IOException {
TestUtils.deleteGeneratedFiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
public class NegativeConstraintTests {
private static final Path RES_DIR = Paths.get("src/test/resources/generators/schema").toAbsolutePath();

@Test(description = "Tests for string type record field has invalid pattern constraint.")
//TODO: Made this disable till the Ballerina lang API received
@Test(description = "Tests for string type record field has invalid pattern constraint.", enabled = false)
public void testForRecordStringHasInvalidPattern() throws IOException, BallerinaOpenApiException,
FormatterException {
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(RES_DIR.resolve("swagger/constraint" +
Expand Down
Loading

0 comments on commit 46b8fe5

Please sign in to comment.