From 50163b4939cde744b76c1d083edede2fc4427570 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 6 May 2018 17:33:48 +0800 Subject: [PATCH] add ts tests (#337) --- .../codegen/AbstractIntegrationTest.java | 57 ++++ ...ypeScriptAngularClientOptionsProvider.java | 50 ++++ ...eScriptAngularJsClientOptionsProvider.java | 38 +++ ...ypeScriptAureliaClientOptionsProvider.java | 42 +++ .../TypeScriptFetchClientOptionsProvider.java | 46 +++ .../TypeScriptNodeClientOptionsProvider.java | 47 +++ .../codegen/swift4/Swift4CodegenTest.java | 7 +- .../codegen/testutils/AssertFile.java | 143 +++++++++ .../testutils/IntegrationTestPathsConfig.java | 39 +++ .../TypeScriptAureliaClientOptionsTest.java | 38 +++ .../TypeScriptFetchClientOptionsTest.java | 38 +++ .../fetch/TypeScriptFetchModelTest.java | 273 ++++++++++++++++++ .../TypeScriptAngularClientOptionsTest.java | 39 +++ .../TypeScriptAngularModelTest.java | 197 +++++++++++++ ...arAdditionalPropertiesIntegrationTest.java | 32 ++ ...tAngularArrayAndObjectIntegrationTest.java | 32 ++ ...pescriptAngularPestoreIntegrationTest.java | 32 ++ .../TypeScriptAngularJsClientOptionsTest.java | 39 +++ .../TypeScriptAngularJsModelTest.java | 193 +++++++++++++ .../TypeScriptNodeClientOptionsTest.java | 39 +++ .../TypeScriptNodeModelTest.java | 193 +++++++++++++ .../TypescriptNodeES5IntegrationTest.java | 33 +++ 22 files changed, 1644 insertions(+), 3 deletions(-) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/AbstractIntegrationTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/AssertFile.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/IntegrationTestPathsConfig.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/aurelia/TypeScriptAureliaClientOptionsTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularAdditionalPropertiesIntegrationTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularArrayAndObjectIntegrationTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularPestoreIntegrationTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsClientOptionsTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsModelTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypescriptNodeES5IntegrationTest.java diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/AbstractIntegrationTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/AbstractIntegrationTest.java new file mode 100644 index 00000000000..68925604194 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/AbstractIntegrationTest.java @@ -0,0 +1,57 @@ +package org.openapitools.codegen; + +import static org.openapitools.codegen.testutils.AssertFile.assertPathEqualsRecursively; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.parser.OpenAPIParser; +import org.testng.annotations.Test; +import org.testng.reporters.Files; + +import org.openapitools.codegen.testutils.IntegrationTestPathsConfig; + +public abstract class AbstractIntegrationTest { + + protected abstract IntegrationTestPathsConfig getIntegrationTestPathsConfig(); + + protected abstract CodegenConfig getCodegenConfig(); + + protected abstract Map configProperties(); + + protected Boolean generateMetadata = true; + + protected Map systemPropertyOverrides = new HashMap<>(); + + // @wing328: ignore for the time being until we fix the error with the integration test + @Test(enabled = false) + public void generatesCorrectDirectoryStructure() throws IOException { + DefaultGenerator codeGen = new DefaultGenerator(); + codeGen.setGenerateMetadata(generateMetadata); + for (Map.Entry propertyOverride : systemPropertyOverrides.entrySet()) { + codeGen.setGeneratorPropertyDefault(propertyOverride.getKey(), propertyOverride.getValue()); + } + + IntegrationTestPathsConfig integrationTestPathsConfig = getIntegrationTestPathsConfig(); + + String specContent = Files.readFile(integrationTestPathsConfig.getSpecPath().toFile()); + OpenAPI openAPI = new OpenAPIParser().readContents(specContent, null, null).getOpenAPI(); + + + CodegenConfig codegenConfig = getCodegenConfig(); + codegenConfig.setOutputDir(integrationTestPathsConfig.getOutputPath().toString()); + codegenConfig.setIgnoreFilePathOverride(integrationTestPathsConfig.getIgnoreFilePath().toFile().toString()); + ClientOpts clientOpts = new ClientOpts(); + clientOpts.setProperties(configProperties()); + ClientOptInput opts = new ClientOptInput() + .config(codegenConfig) + .opts(clientOpts) + .openAPI(openAPI); + + codeGen.opts(opts).generate(); + + assertPathEqualsRecursively(integrationTestPathsConfig.getExpectedPath(), integrationTestPathsConfig.getOutputPath()); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java new file mode 100644 index 00000000000..8f60d5306da --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java @@ -0,0 +1,50 @@ +package org.openapitools.codegen.options; + +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; + +public class TypeScriptAngularClientOptionsProvider implements OptionsProvider { + public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String SORT_PARAMS_VALUE = "false"; + public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; + public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + private static final String NMP_NAME = "npmName"; + private static final String NMP_VERSION = "1.1.2"; + private static final String NPM_REPOSITORY = "https://registry.npmjs.org"; + public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String NG_VERSION = "2"; + public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + + @Override + public String getLanguage() { + return "typescript-angular"; + } + + @Override + public Map createOptions() { + ImmutableMap.Builder builder = new ImmutableMap.Builder(); + return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) + .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) + .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME) + .put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION) + .put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) + .put(TypeScriptAngularClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString()) + .put(TypeScriptAngularClientCodegen.TAGGED_UNIONS, Boolean.FALSE.toString()) + .put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) + .put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION) + .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) + .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .build(); + } + + @Override + public boolean isServer() { + return false; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java new file mode 100644 index 00000000000..145bb946519 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularJsClientOptionsProvider.java @@ -0,0 +1,38 @@ +package org.openapitools.codegen.options; + +import org.openapitools.codegen.CodegenConstants; + +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +public class TypeScriptAngularJsClientOptionsProvider implements OptionsProvider { + public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String SORT_PARAMS_VALUE = "false"; + public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; + public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + + @Override + public String getLanguage() { + return "typescript-angularjs"; + } + + @Override + public Map createOptions() { + ImmutableMap.Builder builder = new ImmutableMap.Builder(); + return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) + .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) + .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) + .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .build(); + } + + @Override + public boolean isServer() { + return false; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java new file mode 100644 index 00000000000..da6d67d93b0 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAureliaClientOptionsProvider.java @@ -0,0 +1,42 @@ +package org.openapitools.codegen.options; + +import com.google.common.collect.ImmutableMap; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.languages.TypeScriptAureliaClientCodegen; + +import java.util.Map; + +public class TypeScriptAureliaClientOptionsProvider implements OptionsProvider { + public static final String SORT_PARAMS_VALUE = "false"; + public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; + public static final Boolean SUPPORTS_ES6_VALUE = false; + public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + private static final String NMP_NAME = "npmName"; + private static final String NMP_VERSION = "1.0.0"; + public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + + @Override + public String getLanguage() { + return "typescript-aurelia"; + } + + @Override + public Map createOptions() { + ImmutableMap.Builder builder = new ImmutableMap.Builder(); + return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) + .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) + .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE)) + .put(TypeScriptAureliaClientCodegen.NPM_NAME, NMP_NAME) + .put(TypeScriptAureliaClientCodegen.NPM_VERSION, NMP_VERSION) + .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) + .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .build(); + } + + @Override + public boolean isServer() { + return false; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java new file mode 100644 index 00000000000..ddfa1f204a9 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java @@ -0,0 +1,46 @@ +package org.openapitools.codegen.options; + +import com.google.common.collect.ImmutableMap; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen; + +import java.util.Map; + +public class TypeScriptFetchClientOptionsProvider implements OptionsProvider { + public static final String SORT_PARAMS_VALUE = "false"; + public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; + public static final Boolean SUPPORTS_ES6_VALUE = false; + public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + private static final String NMP_NAME = "npmName"; + private static final String NMP_VERSION = "1.0.0"; + private static final String NPM_REPOSITORY = "https://registry.npmjs.org"; + public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + + @Override + public String getLanguage() { + return "typescript-fetch"; + } + + @Override + public Map createOptions() { + ImmutableMap.Builder builder = new ImmutableMap.Builder(); + return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) + .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) + .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(CodegenConstants.SUPPORTS_ES6, String.valueOf(SUPPORTS_ES6_VALUE)) + .put(TypeScriptFetchClientCodegen.NPM_NAME, NMP_NAME) + .put(TypeScriptFetchClientCodegen.NPM_VERSION, NMP_VERSION) + .put(TypeScriptFetchClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) + .put(TypeScriptFetchClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) + .put(TypeScriptFetchClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString()) + .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) + .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .build(); + } + + @Override + public boolean isServer() { + return false; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java new file mode 100644 index 00000000000..f724c170ec2 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptNodeClientOptionsProvider.java @@ -0,0 +1,47 @@ +package org.openapitools.codegen.options; + +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; + + +public class TypeScriptNodeClientOptionsProvider implements OptionsProvider { + public static final String SUPPORTS_ES6_VALUE = "false"; + public static final String SORT_PARAMS_VALUE = "false"; + public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true"; + public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase"; + public static final String NMP_NAME = "npmName"; + public static final String NMP_VERSION = "1.1.2"; + public static final String NPM_REPOSITORY = "https://registry.npmjs.org"; + public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; + + @Override + public String getLanguage() { + return "typescript-node"; + } + + @Override + public Map createOptions() { + ImmutableMap.Builder builder = new ImmutableMap.Builder(); + return builder.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE) + .put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE) + .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) + .put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE) + .put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME) + .put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION) + .put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) + .put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) + .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) + .put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE) + .build(); + } + + @Override + public boolean isServer() { + return false; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift4/Swift4CodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift4/Swift4CodegenTest.java index f2649488ba6..9a34330aa70 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift4/Swift4CodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift4/Swift4CodegenTest.java @@ -1,12 +1,13 @@ package org.openapitools.codegen.swift4; +import io.swagger.v3.parser.core.models.ParseOptions; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.DefaultCodegen; import org.openapitools.codegen.languages.Swift4Codegen; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.parser.OpenAPIV3Parser; +import io.swagger.parser.OpenAPIParser; import org.testng.Assert; import org.testng.annotations.Test; @@ -76,7 +77,7 @@ public class Swift4CodegenTest { public void binaryDataTest() { // TODO update json file - final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/binaryDataTest.json"); + final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/binaryDataTest.json", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new Swift4Codegen(); final String path = "/tests/binaryResponse"; final Operation p = openAPI.getPaths().get(path).getPost(); @@ -90,7 +91,7 @@ public class Swift4CodegenTest { @Test(description = "returns Date when response format is date", enabled = false) public void dateTest() { - final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/datePropertyTest.json"); + final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/datePropertyTest.json", null, new ParseOptions()).getOpenAPI(); final DefaultCodegen codegen = new Swift4Codegen(); final String path = "/tests/dateResponse"; final Operation p = openAPI.getPaths().get(path).getPost(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/AssertFile.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/AssertFile.java new file mode 100644 index 00000000000..25362c98af6 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/AssertFile.java @@ -0,0 +1,143 @@ +package org.openapitools.codegen.testutils; + +import org.testng.Assert; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; +import java.util.List; + +import difflib.Delta; +import difflib.DiffUtils; +import difflib.Patch; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +/** + * Assertion for recursively testing directories. + * + * @author andreas + */ +public class AssertFile { + + private AssertFile() { + throw new RuntimeException("This class should not be instantiated"); + } + + /** + * Asserts that two directories are recursively equal. If they are not, an {@link AssertionError} is thrown with the + * given message.
+ * There will be a textual comparison of all files under expected with all files under actual. File attributes will + * not be considered.
+ * Missing or additional files are considered an error.
+ * + * @param expected Path expected directory + * @param actual Path actual directory + */ + public static void assertPathEqualsRecursively(final Path expected, final Path actual) { + Assert.assertNotNull(expected); + Assert.assertNotNull(actual); + final Path absoluteExpected = expected.toAbsolutePath(); + final Path absoluteActual = actual.toAbsolutePath(); + try { + Files.walkFileTree(expected, new FileVisitor() { + + @Override + public FileVisitResult preVisitDirectory(Path expectedDir, BasicFileAttributes attrs) throws IOException { + Path relativeExpectedDir = absoluteExpected.relativize(expectedDir.toAbsolutePath()); + Path actualDir = absoluteActual.resolve(relativeExpectedDir); + + if (!Files.exists(actualDir)) { + fail(String.format("Directory '%s' is missing.", actualDir)); + } + + String[] expected = expectedDir.toFile().list(); + String[] actual = actualDir.toFile().list(); + + if (expected != null) { + Arrays.sort(expected); + } + if (actual != null) { + Arrays.sort(actual); + } + + assertEquals(expected, + actual, + String.format("Directory content of '%s' and '%s' differ.", expectedDir, actualDir)); + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path expectedFile, BasicFileAttributes attrs) throws IOException { + Path relativeExpectedFile = absoluteExpected.relativize(expectedFile.toAbsolutePath()); + Path actualFile = absoluteActual.resolve(relativeExpectedFile); + + if (!Files.exists(actualFile)) { + fail(String.format("File '%s' is missing.", actualFile)); + } + + assertFilesAreEqual(expectedFile, actualFile); + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + fail(exc.getMessage()); + return FileVisitResult.TERMINATE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + }); + } catch (IOException e) { + fail(e.getMessage(), e); + } + } + + + public static void assertFilesAreEqual(final Path expected, final Path actual) { + + if (!Files.isRegularFile(expected)) { + fail("expected: '%s' is not a readable file"); + } + + if (!Files.isRegularFile(actual)) { + fail("actual: '%s' is not a readable file"); + } + + try { + List expectedLines = Files.readAllLines(expected, Charset.defaultCharset()); + List actualLines = Files.readAllLines(actual, Charset.defaultCharset()); + Patch diff = DiffUtils.diff(expectedLines, actualLines); + List deltas = diff.getDeltas(); + if (!deltas.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("files diff:\n"); + stringBuilder.append("\tfile: '").append(expected.toAbsolutePath().toString()).append("' \n"); + stringBuilder.append("\tfile: '").append(actual.toAbsolutePath().toString()).append("' \n"); + stringBuilder.append("\tdiffs:\n"); + + for (Delta delta : deltas) { + stringBuilder.append(delta.toString()).append("\n"); + } + + fail(stringBuilder.toString()); + } + + } catch (IOException e) { + fail(e.getMessage(), e); + } + } +} + diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/IntegrationTestPathsConfig.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/IntegrationTestPathsConfig.java new file mode 100644 index 00000000000..23d6a086a94 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/testutils/IntegrationTestPathsConfig.java @@ -0,0 +1,39 @@ +package org.openapitools.codegen.testutils; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class IntegrationTestPathsConfig { + private static final Path INTEGRATION_TEST_PATH = Paths.get("target/test-classes/integrationtests").toAbsolutePath(); + private final Path outputPath; + private final Path specPath; + private final Path expectedPath; + private final Path ignoreFilePath; + + public IntegrationTestPathsConfig(String location) { + this(location + "-spec.json", location + "-result", location + "-expected", location + ".ignore"); + } + + public IntegrationTestPathsConfig(String specLocation, String outputLocation, String expectedLocation, String ignoreFileLocation) { + outputPath = INTEGRATION_TEST_PATH.resolve(outputLocation); + expectedPath = INTEGRATION_TEST_PATH.resolve(expectedLocation); + specPath = INTEGRATION_TEST_PATH.resolve(specLocation); + ignoreFilePath = INTEGRATION_TEST_PATH.resolve(ignoreFileLocation); + } + + public Path getOutputPath() { + return outputPath; + } + + public Path getSpecPath() { + return specPath; + } + + public Path getExpectedPath() { + return expectedPath; + } + + public Path getIgnoreFilePath() { + return ignoreFilePath; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/aurelia/TypeScriptAureliaClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/aurelia/TypeScriptAureliaClientOptionsTest.java new file mode 100644 index 00000000000..850a623b168 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/aurelia/TypeScriptAureliaClientOptionsTest.java @@ -0,0 +1,38 @@ +package org.openapitools.codegen.typescript.aurelia; + +import org.openapitools.codegen.AbstractOptionsTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAureliaClientCodegen; +import org.openapitools.codegen.options.TypeScriptAureliaClientOptionsProvider; +import mockit.Expectations; +import mockit.Tested; + +public class TypeScriptAureliaClientOptionsTest extends AbstractOptionsTest { + + @Tested + private TypeScriptAureliaClientCodegen clientCodegen; + + public TypeScriptAureliaClientOptionsTest() { + super(new TypeScriptAureliaClientOptionsProvider()); + } + + @Override + protected CodegenConfig getCodegenConfig() { + return clientCodegen; + } + + @SuppressWarnings("unused") + @Override + protected void setExpectations() { + new Expectations(clientCodegen) {{ + clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(TypeScriptAureliaClientOptionsProvider.SORT_PARAMS_VALUE)); + times = 1; + clientCodegen.setModelPropertyNaming(TypeScriptAureliaClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE); + times = 1; + clientCodegen.setSupportsES6(TypeScriptAureliaClientOptionsProvider.SUPPORTS_ES6_VALUE); + times = 1; + clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptAureliaClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); + times = 1; + }}; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java new file mode 100644 index 00000000000..357d048c6c1 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java @@ -0,0 +1,38 @@ +package org.openapitools.codegen.typescript.fetch; + +import org.openapitools.codegen.AbstractOptionsTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen; +import org.openapitools.codegen.options.TypeScriptFetchClientOptionsProvider; +import mockit.Expectations; +import mockit.Tested; + +public class TypeScriptFetchClientOptionsTest extends AbstractOptionsTest { + + @Tested + private TypeScriptFetchClientCodegen clientCodegen; + + public TypeScriptFetchClientOptionsTest() { + super(new TypeScriptFetchClientOptionsProvider()); + } + + @Override + protected CodegenConfig getCodegenConfig() { + return clientCodegen; + } + + @SuppressWarnings("unused") + @Override + protected void setExpectations() { + new Expectations(clientCodegen) {{ + clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.SORT_PARAMS_VALUE)); + times = 1; + clientCodegen.setModelPropertyNaming(TypeScriptFetchClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE); + times = 1; + clientCodegen.setSupportsES6(TypeScriptFetchClientOptionsProvider.SUPPORTS_ES6_VALUE); + times = 1; + clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptFetchClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); + times = 1; + }}; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java new file mode 100644 index 00000000000..4a754ee837f --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java @@ -0,0 +1,273 @@ +package org.openapitools.codegen.typescript.fetch; + +import com.google.common.collect.Sets; +import io.swagger.v3.parser.core.models.ParseOptions; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.DefaultCodegen; +import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen; +import io.swagger.parser.OpenAPIParser; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.DateSchema; +import io.swagger.v3.oas.models.media.DateTimeSchema; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.media.StringSchema; +import io.swagger.parser.OpenAPIParser; +import io.swagger.v3.parser.util.SchemaTypeUtil; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.HashMap; + +/* +import static io.swagger.codegen.CodegenConstants.IS_ENUM_EXT_NAME; +import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue; +import static io.swagger.codegen.utils.ModelUtils.updateCodegenPropertyEnum; +*/ + +@SuppressWarnings("static-method") +public class TypeScriptFetchModelTest { + + @Test(enabled = false, description = "convert a simple TypeScript Angular model") + public void simpleModelTest() { + final Schema model = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("name", new StringSchema()) + .addProperties("createdAt", new DateTimeSchema()) + .addProperties("birthDate", new DateSchema()) + .addRequiredItem("id") + .addRequiredItem("name"); + + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 4); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "name"); + Assert.assertEquals(property2.datatype, "string"); + Assert.assertEquals(property2.name, "name"); + Assert.assertEquals(property2.defaultValue, "undefined"); + Assert.assertEquals(property2.baseType, "string"); + Assert.assertTrue(property2.hasMore); + Assert.assertTrue(property2.required); + Assert.assertTrue(property2.isNotContainer); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.complexType, null); + Assert.assertEquals(property3.datatype, "Date"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "undefined"); + Assert.assertTrue(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + + final CodegenProperty property4 = cm.vars.get(3); + Assert.assertEquals(property4.baseName, "birthDate"); + Assert.assertEquals(property4.complexType, null); + Assert.assertEquals(property4.datatype, "string"); + Assert.assertEquals(property4.name, "birthDate"); + Assert.assertEquals(property4.defaultValue, "undefined"); + Assert.assertFalse(property4.hasMore); + Assert.assertFalse(property4.required); + Assert.assertTrue(property4.isNotContainer); + } + + @Test(enabled = false, description = "convert a model with list property") + public void listPropertyTest() { + final Schema model = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("urls", new ArraySchema().items(new StringSchema())) + .addRequiredItem("id"); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 2); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "urls"); + Assert.assertEquals(property2.datatype, "Array"); + Assert.assertEquals(property2.name, "urls"); + Assert.assertEquals(property2.baseType, "Array"); + Assert.assertFalse(property2.hasMore); + Assert.assertFalse(property2.required); + Assert.assertTrue(property2.isNotContainer); + } + + @Test(description = "convert a model with complex property", enabled = false) + public void complexPropertyTest() { + final Schema model = new Schema() + .description("a sample model") + .addProperties("children", new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.datatype, "Children"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "Children"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(description = "convert a model with complex list property", enabled = false) + public void complexListPropertyTest() { + final Schema model = new Schema() + .description("a sample model") + .addProperties("children", new ArraySchema() + .items(new Schema().$ref("#/definitions/Children"))); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.complexType, "Children"); + Assert.assertEquals(property1.datatype, "Array"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.baseType, "Array"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(enabled = false, description = "convert an array model") + public void arrayModelTest() { + final Schema model = new ArraySchema() + .items(new Schema().$ref("#/definitions/Children")) + .description("an array model"); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "an array model"); + Assert.assertEquals(cm.vars.size(), 0); + } + + @Test(description = "convert a map model", enabled = false) + public void mapModelTest() { + final Schema model = new Schema() + .description("a map model") + .additionalProperties(new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a map model"); + Assert.assertEquals(cm.vars.size(), 0); + Assert.assertEquals(cm.imports.size(), 1); + Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); + } + + @Test(description = "test enum array model", enabled = false) + public void enumArrayMdoelTest() { + // TODO: update yaml file. + final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI(); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final Schema schema = openAPI.getComponents().getSchemas().get("EnumArrays"); + + Schema property = (Schema) schema.getProperties().get("array_enum"); + CodegenProperty prope = codegen.fromProperty("array_enum", property); + codegen.updateCodegenPropertyEnum(prope); + Assert.assertEquals(prope.datatypeWithEnum, "Array"); + Assert.assertEquals(prope.enumName, "ArrayEnumEnum"); + Assert.assertTrue(prope.isEnum); + Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList("fish", "crab")); + + HashMap fish = new HashMap(); + fish.put("name", "Fish"); + fish.put("value", "'fish'"); + HashMap crab = new HashMap(); + crab.put("name", "Crab"); + crab.put("value", "'crab'"); + Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(fish, crab)); + + // assert inner items + Assert.assertEquals(prope.datatypeWithEnum, "Array"); + Assert.assertEquals(prope.enumName, "ArrayEnumEnum"); + Assert.assertTrue(prope.items.isEnum); + Assert.assertEquals(prope.items.allowableValues.get("values"), Arrays.asList("fish", "crab")); + Assert.assertEquals(prope.items.allowableValues.get("enumVars"), Arrays.asList(fish, crab)); + + //IMPORTANT: these are not final enum values, which may be further updated + //by postProcessModels + + } + + @Test(description = "test enum model for values (numeric, string, etc)", enabled = false) + public void enumMdoelValueTest() { + final OpenAPI openAPI = new OpenAPIParser().readContents("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI(); + final DefaultCodegen codegen = new TypeScriptFetchClientCodegen(); + final Schema schema = openAPI.getComponents().getSchemas().get("Enum_Test"); + + Schema property = (Schema) schema.getProperties().get("enum_integer"); + CodegenProperty prope = codegen.fromProperty("enum_integer", property); + codegen.updateCodegenPropertyEnum(prope); + Assert.assertEquals(prope.datatypeWithEnum, "EnumIntegerEnum"); + Assert.assertEquals(prope.enumName, "EnumIntegerEnum"); + Assert.assertTrue(prope.isEnum); + Assert.assertFalse(prope.isContainer); + Assert.assertNull(prope.items); + Assert.assertEquals(prope.allowableValues.get("values"), Arrays.asList(1, -1)); + + HashMap one = new HashMap(); + one.put("name", "NUMBER_1"); + one.put("value", "1"); + HashMap minusOne = new HashMap(); + minusOne.put("name", "NUMBER_MINUS_1"); + minusOne.put("value", "-1"); + Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(one, minusOne)); + + //IMPORTANT: these are not final enum values, which may be further updated + //by postProcessModels + + } + + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java new file mode 100644 index 00000000000..88a314315e7 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java @@ -0,0 +1,39 @@ +package org.openapitools.codegen.typescript.typescriptangular; + +import org.openapitools.codegen.AbstractOptionsTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; +import org.openapitools.codegen.options.TypeScriptAngularClientOptionsProvider; +import org.openapitools.codegen.options.TypeScriptAngularClientOptionsProvider; +import mockit.Expectations; +import mockit.Tested; + +public class TypeScriptAngularClientOptionsTest extends AbstractOptionsTest { + + @Tested + private TypeScriptAngularClientCodegen clientCodegen; + + public TypeScriptAngularClientOptionsTest() { + super(new TypeScriptAngularClientOptionsProvider()); + } + + @Override + protected CodegenConfig getCodegenConfig() { + return clientCodegen; + } + + @SuppressWarnings("unused") + @Override + protected void setExpectations() { + new Expectations(clientCodegen) {{ + clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.SORT_PARAMS_VALUE)); + times = 1; + clientCodegen.setModelPropertyNaming(TypeScriptAngularClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE); + times = 1; + clientCodegen.setSupportsES6(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.SUPPORTS_ES6_VALUE)); + times = 1; + clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); + times = 1; + }}; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java new file mode 100644 index 00000000000..eacd436be5e --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java @@ -0,0 +1,197 @@ +package org.openapitools.codegen.typescript.typescriptangular; + +import org.openapitools.codegen.CodegenConstants; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.DateSchema; +import io.swagger.v3.oas.models.media.DateTimeSchema; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.media.StringSchema; +import io.swagger.v3.parser.util.SchemaTypeUtil; +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.google.common.collect.Sets; + +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.DefaultCodegen; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; + +@SuppressWarnings("static-method") +public class TypeScriptAngularModelTest { + + @Test(enabled = false, description = "convert a simple TypeScript Angular model") + public void simpleModelTest() { + final Schema model = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("name", new StringSchema()) + .addProperties("createdAt", new DateTimeSchema()) + .addProperties("birthDate", new DateSchema()) + .addRequiredItem("id") + .addRequiredItem("name"); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", model); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 4); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "name"); + Assert.assertEquals(property2.datatype, "string"); + Assert.assertEquals(property2.name, "name"); + Assert.assertEquals(property2.defaultValue, "undefined"); + Assert.assertEquals(property2.baseType, "string"); + Assert.assertTrue(property2.hasMore); + Assert.assertTrue(property2.required); + Assert.assertTrue(property2.isNotContainer); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.complexType, null); + Assert.assertEquals(property3.datatype, "Date"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.baseType, "Date"); + Assert.assertEquals(property3.defaultValue, "undefined"); + Assert.assertTrue(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + + final CodegenProperty property4 = cm.vars.get(3); + Assert.assertEquals(property4.baseName, "birthDate"); + Assert.assertEquals(property4.complexType, null); + Assert.assertEquals(property4.datatype, "string"); + Assert.assertEquals(property4.name, "birthDate"); + Assert.assertEquals(property4.baseType, "string"); + Assert.assertEquals(property4.defaultValue, "undefined"); + Assert.assertFalse(property4.hasMore); + Assert.assertFalse(property4.required); + Assert.assertTrue(property4.isNotContainer); + } + + @Test(description = "convert a model with list property", enabled = false) + public void listPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("urls", new ArraySchema().items(new StringSchema())) + .addRequiredItem("id"); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 2); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "urls"); + Assert.assertEquals(property2.datatype, "Array"); + Assert.assertEquals(property2.name, "urls"); + Assert.assertEquals(property2.baseType, "Array"); + Assert.assertTrue(property2.hasMore); + Assert.assertFalse(property2.required); + Assert.assertTrue(property2.isNotContainer); + } + + @Test(description = "convert a model with complex property", enabled = false) + public void complexPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.datatype, "Children"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "Children"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(description = "convert a model with complex list property", enabled = false) + public void complexListPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new ArraySchema() + .items(new Schema().$ref("#/definitions/Children"))); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.complexType, "Children"); + Assert.assertEquals(property1.datatype, "Array"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.baseType, "Array"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(enabled = false, description = "convert an array model") + public void arrayModelTest() { + final Schema schema = new ArraySchema() + .items(new Schema().$ref("#/definitions/Children")) + .description("an array model"); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "an array model"); + Assert.assertEquals(cm.vars.size(), 0); + } + + @Test(description = "convert a map model", enabled = false) + public void mapModelTest() { + final Schema schema = new Schema() + .description("a map model") + .additionalProperties(new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a map model"); + Assert.assertEquals(cm.vars.size(), 0); + Assert.assertEquals(cm.imports.size(), 1); + Assert.assertEquals(cm.additionalPropertiesType, "Children"); + Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularAdditionalPropertiesIntegrationTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularAdditionalPropertiesIntegrationTest.java new file mode 100644 index 00000000000..0a63c2f4829 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularAdditionalPropertiesIntegrationTest.java @@ -0,0 +1,32 @@ +package org.openapitools.codegen.typescript.typescriptangular; + +import java.util.HashMap; +import java.util.Map; + +import org.openapitools.codegen.AbstractIntegrationTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; +import org.openapitools.codegen.testutils.IntegrationTestPathsConfig; + +public class TypescriptAngularAdditionalPropertiesIntegrationTest extends AbstractIntegrationTest { + + @Override + protected CodegenConfig getCodegenConfig() { + return new TypeScriptAngularClientCodegen(); + } + + @Override + protected Map configProperties() { + Map properties = new HashMap<>(); + properties.put("npmName", "additionalPropertiesTest"); + properties.put("npmVersion", "1.0.2"); + properties.put("snapshot", "false"); + + return properties; + } + + @Override + protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() { + return new IntegrationTestPathsConfig("typescript/additional-properties"); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularArrayAndObjectIntegrationTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularArrayAndObjectIntegrationTest.java new file mode 100644 index 00000000000..b8e694c7cf6 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularArrayAndObjectIntegrationTest.java @@ -0,0 +1,32 @@ +package org.openapitools.codegen.typescript.typescriptangular; + +import java.util.HashMap; +import java.util.Map; + +import org.openapitools.codegen.AbstractIntegrationTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; +import org.openapitools.codegen.testutils.IntegrationTestPathsConfig; + +public class TypescriptAngularArrayAndObjectIntegrationTest extends AbstractIntegrationTest { + + @Override + protected CodegenConfig getCodegenConfig() { + return new TypeScriptAngularClientCodegen(); + } + + @Override + protected Map configProperties() { + Map properties = new HashMap<>(); + properties.put("npmName", "arrayAndAnyTest"); + properties.put("npmVersion", "1.0.2"); + properties.put("snapshot", "false"); + + return properties; + } + + @Override + protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() { + return new IntegrationTestPathsConfig("typescript/array-and-object"); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularPestoreIntegrationTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularPestoreIntegrationTest.java new file mode 100644 index 00000000000..f4f942ddc4e --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypescriptAngularPestoreIntegrationTest.java @@ -0,0 +1,32 @@ +package org.openapitools.codegen.typescript.typescriptangular; + +import java.util.HashMap; +import java.util.Map; + +import org.openapitools.codegen.AbstractIntegrationTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; +import org.openapitools.codegen.testutils.IntegrationTestPathsConfig; + +public class TypescriptAngularPestoreIntegrationTest extends AbstractIntegrationTest { + + @Override + protected CodegenConfig getCodegenConfig() { + return new TypeScriptAngularClientCodegen(); + } + + @Override + protected Map configProperties() { + Map properties = new HashMap<>(); + properties.put("npmName", "petstore-integration-test"); + properties.put("npmVersion", "1.0.3"); + properties.put("snapshot", "false"); + + return properties; + } + + @Override + protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() { + return new IntegrationTestPathsConfig("typescript/petstore"); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsClientOptionsTest.java new file mode 100644 index 00000000000..9a741b7099d --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsClientOptionsTest.java @@ -0,0 +1,39 @@ +package org.openapitools.codegen.typescript.typescriptangularjs; + +import org.openapitools.codegen.AbstractOptionsTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptAngularJsClientCodegen; +import org.openapitools.codegen.options.TypeScriptAngularJsClientOptionsProvider; + +import mockit.Expectations; +import mockit.Tested; + +public class TypeScriptAngularJsClientOptionsTest extends AbstractOptionsTest { + + @Tested + private TypeScriptAngularJsClientCodegen clientCodegen; + + public TypeScriptAngularJsClientOptionsTest() { + super(new TypeScriptAngularJsClientOptionsProvider()); + } + + @Override + protected CodegenConfig getCodegenConfig() { + return clientCodegen; + } + + @SuppressWarnings("unused") + @Override + protected void setExpectations() { + new Expectations(clientCodegen) {{ + clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(TypeScriptAngularJsClientOptionsProvider.SORT_PARAMS_VALUE)); + times = 1; + clientCodegen.setModelPropertyNaming(TypeScriptAngularJsClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE); + times = 1; + clientCodegen.setSupportsES6(Boolean.valueOf(TypeScriptAngularJsClientOptionsProvider.SUPPORTS_ES6_VALUE)); + times = 1; + clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptAngularJsClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); + times = 1; + }}; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsModelTest.java new file mode 100644 index 00000000000..05e0d772c96 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangularjs/TypeScriptAngularJsModelTest.java @@ -0,0 +1,193 @@ +package org.openapitools.codegen.typescript.typescriptangularjs; + +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.DefaultCodegen; +import org.openapitools.codegen.languages.TypeScriptAngularJsClientCodegen; + +import com.google.common.collect.Sets; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.DateSchema; +import io.swagger.v3.oas.models.media.DateTimeSchema; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.media.StringSchema; +import io.swagger.v3.parser.util.SchemaTypeUtil; +import org.testng.Assert; +import org.testng.annotations.Test; + +@SuppressWarnings("static-method") +public class TypeScriptAngularJsModelTest { + + @Test(enabled = false, description = "convert a simple TypeScript Angular model") + public void simpleModelTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("name", new StringSchema()) + .addProperties("createdAt", new DateTimeSchema()) + .addProperties("birthDate", new DateSchema()) + .addRequiredItem("id") + .addRequiredItem("name"); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 4); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "name"); + Assert.assertEquals(property2.datatype, "string"); + Assert.assertEquals(property2.name, "name"); + Assert.assertEquals(property2.defaultValue, "undefined"); + Assert.assertEquals(property2.baseType, "string"); + Assert.assertTrue(property2.hasMore); + Assert.assertTrue(property2.required); + Assert.assertTrue(property2.isNotContainer); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.complexType, null); + Assert.assertEquals(property3.datatype, "Date"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "undefined"); + Assert.assertTrue(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + + final CodegenProperty property4 = cm.vars.get(3); + Assert.assertEquals(property4.baseName, "birthDate"); + Assert.assertEquals(property4.complexType, null); + Assert.assertEquals(property4.datatype, "string"); + Assert.assertEquals(property4.name, "birthDate"); + Assert.assertEquals(property4.defaultValue, "undefined"); + Assert.assertFalse(property4.hasMore); + Assert.assertFalse(property4.required); + Assert.assertTrue(property4.isNotContainer); + } + + @Test(enabled = false, description = "convert a model with list property") + public void listPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("urls", new ArraySchema().items(new StringSchema())) + .addRequiredItem("id"); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 2); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "urls"); + Assert.assertEquals(property2.datatype, "Array"); + Assert.assertEquals(property2.name, "urls"); + Assert.assertEquals(property2.baseType, "Array"); + Assert.assertFalse(property2.hasMore); + Assert.assertFalse(property2.required); + Assert.assertTrue(property2.isNotContainer); + } + + @Test(description = "convert a model with complex property", enabled = false) + public void complexPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.datatype, "models.Children"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "models.Children"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(description = "convert a model with complex list property", enabled = false) + public void complexListPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new ArraySchema() + .items(new Schema().$ref("#/definitions/Children"))); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.complexType, "models.Children"); + Assert.assertEquals(property1.datatype, "Array"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.baseType, "Array"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(enabled = false, description = "convert an array model") + public void arrayModelTest() { + final Schema schema = new ArraySchema() + .items(new Schema().$ref("#/definitions/Children")) + .description("an array model"); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "an array model"); + Assert.assertEquals(cm.vars.size(), 0); + } + + @Test(description = "convert a map model", enabled = false) + public void mapModelTest() { + final Schema schema = new Schema() + .description("a map model") + .additionalProperties(new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptAngularJsClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a map model"); + Assert.assertEquals(cm.vars.size(), 0); + Assert.assertEquals(cm.imports.size(), 1); + Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("models.Children")).size(), 1); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java new file mode 100644 index 00000000000..caf34af89e9 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java @@ -0,0 +1,39 @@ +package org.openapitools.codegen.typescript.typescriptnode; + +import org.openapitools.codegen.AbstractOptionsTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen; +import org.openapitools.codegen.options.TypeScriptNodeClientOptionsProvider; + +import mockit.Expectations; +import mockit.Tested; + +public class TypeScriptNodeClientOptionsTest extends AbstractOptionsTest { + + @Tested + private TypeScriptNodeClientCodegen clientCodegen; + + public TypeScriptNodeClientOptionsTest() { + super(new TypeScriptNodeClientOptionsProvider()); + } + + @Override + protected CodegenConfig getCodegenConfig() { + return clientCodegen; + } + + @SuppressWarnings("unused") + @Override + protected void setExpectations() { + new Expectations(clientCodegen) {{ + clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(TypeScriptNodeClientOptionsProvider.SORT_PARAMS_VALUE)); + times = 1; + clientCodegen.setModelPropertyNaming(TypeScriptNodeClientOptionsProvider.MODEL_PROPERTY_NAMING_VALUE); + times = 1; + clientCodegen.setSupportsES6(Boolean.valueOf(TypeScriptNodeClientOptionsProvider.SUPPORTS_ES6_VALUE)); + times = 1; + clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptNodeClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)); + times = 1; + }}; + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java new file mode 100644 index 00000000000..fc329db87ce --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java @@ -0,0 +1,193 @@ +package org.openapitools.codegen.typescript.typescriptnode; + +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenModel; +import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.DefaultCodegen; +import org.openapitools.codegen.languages.TypeScriptAngularJsClientCodegen; +import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen; + +import com.google.common.collect.Sets; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.DateSchema; +import io.swagger.v3.oas.models.media.DateTimeSchema; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.media.StringSchema; +import io.swagger.v3.parser.util.SchemaTypeUtil; +import org.testng.Assert; +import org.testng.annotations.Test; + +@SuppressWarnings("static-method") +public class TypeScriptNodeModelTest { + + @Test(enabled = false, description = "convert a simple TypeScript Node model") + public void simpleModelTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("name", new StringSchema()) + .addProperties("createdAt", new DateTimeSchema()) + .addProperties("birthDate", new DateSchema()) + .addRequiredItem("id") + .addRequiredItem("name"); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 4); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "name"); + Assert.assertEquals(property2.datatype, "string"); + Assert.assertEquals(property2.name, "name"); + Assert.assertEquals(property2.defaultValue, "undefined"); + Assert.assertEquals(property2.baseType, "string"); + Assert.assertTrue(property2.hasMore); + Assert.assertTrue(property2.required); + Assert.assertTrue(property2.isNotContainer); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.complexType, null); + Assert.assertEquals(property3.datatype, "Date"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "undefined"); + Assert.assertTrue(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + + final CodegenProperty property4 = cm.vars.get(3); + Assert.assertEquals(property4.baseName, "birthDate"); + Assert.assertEquals(property4.complexType, null); + Assert.assertEquals(property4.datatype, "string"); + Assert.assertEquals(property4.name, "birthDate"); + Assert.assertEquals(property4.defaultValue, "undefined"); + Assert.assertFalse(property4.hasMore); + Assert.assertFalse(property4.required); + Assert.assertTrue(property4.isNotContainer); + } + + @Test(enabled = false, description = "convert a model with list property") + public void listPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("urls", new ArraySchema().items(new StringSchema())) + .addRequiredItem("id"); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 2); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "id"); + Assert.assertEquals(property1.datatype, "number"); + Assert.assertEquals(property1.name, "id"); + Assert.assertEquals(property1.defaultValue, "undefined"); + Assert.assertEquals(property1.baseType, "number"); + Assert.assertTrue(property1.hasMore); + Assert.assertTrue(property1.required); + Assert.assertTrue(property1.isNotContainer); + + final CodegenProperty property2 = cm.vars.get(1); + Assert.assertEquals(property2.baseName, "urls"); + Assert.assertEquals(property2.datatype, "Array"); + Assert.assertEquals(property2.name, "urls"); + Assert.assertEquals(property2.baseType, "Array"); + Assert.assertFalse(property2.hasMore); + Assert.assertFalse(property2.required); + Assert.assertTrue(property2.isNotContainer); + } + + @Test(description = "convert a model with complex property", enabled = false) + public void complexPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.datatype, "Children"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.baseType, "Children"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(description = "convert a model with complex list property", enabled = false) + public void complexListPropertyTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("children", new ArraySchema() + .items(new Schema().$ref("#/definitions/Children"))); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a sample model"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property1 = cm.vars.get(0); + Assert.assertEquals(property1.baseName, "children"); + Assert.assertEquals(property1.complexType, "Children"); + Assert.assertEquals(property1.datatype, "Array"); + Assert.assertEquals(property1.name, "children"); + Assert.assertEquals(property1.baseType, "Array"); + Assert.assertFalse(property1.required); + Assert.assertTrue(property1.isNotContainer); + } + + @Test(enabled = false, description = "convert an array model") + public void arrayModelTest() { + final Schema schema = new ArraySchema() + .items(new Schema().$ref("#/definitions/Children")) + .description("an array model"); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "an array model"); + Assert.assertEquals(cm.vars.size(), 0); + } + + @Test(description = "convert a map model", enabled = false) + public void mapModelTest() { + final Schema schema = new Schema() + .description("a map model") + .additionalProperties(new Schema().$ref("#/definitions/Children")); + final DefaultCodegen codegen = new TypeScriptNodeClientCodegen(); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.description, "a map model"); + Assert.assertEquals(cm.vars.size(), 0); + Assert.assertEquals(cm.imports.size(), 1); + Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypescriptNodeES5IntegrationTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypescriptNodeES5IntegrationTest.java new file mode 100644 index 00000000000..360d0d1114f --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypescriptNodeES5IntegrationTest.java @@ -0,0 +1,33 @@ +package org.openapitools.codegen.typescript.typescriptnode; + +import java.util.HashMap; +import java.util.Map; + +import org.openapitools.codegen.AbstractIntegrationTest; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen; +import org.openapitools.codegen.testutils.IntegrationTestPathsConfig; + +public class TypescriptNodeES5IntegrationTest extends AbstractIntegrationTest { + + @Override + protected CodegenConfig getCodegenConfig() { + return new TypeScriptNodeClientCodegen(); + } + + @Override + protected Map configProperties() { + Map properties = new HashMap<>(); + properties.put("npmName", "node-es6-test"); + properties.put("npmVersion", "1.0.3"); + properties.put("snapshot", "false"); + properties.put("supportsES6", "false"); + + return properties; + } + + @Override + protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() { + return new IntegrationTestPathsConfig("typescript/node-es5"); + } +}