From a31b38b2234b8e9606cdb8a693c841732702f4b6 Mon Sep 17 00:00:00 2001 From: Mann Patel <44409895+MANN3005@users.noreply.github.com> Date: Tue, 7 Nov 2023 01:11:35 -0400 Subject: [PATCH] Added some Test Cases (#16994) * New Test Case Added to AndroidClientCodegenTest * New Test Case Added to GenApiControllerTest * New Test Case Added to BirdAndCategoryTest * New Test Case Added to WorkflowSettingsTest * Modified GenApiControllerTest * Added Test case in JavascriptClientCodegenTest * Modified BirdAndCategoryTest * Modified BirdAndCategoryTest --------- Co-authored-by: Mann --- .../codegen/config/WorkflowSettingsTest.java | 19 +++++++++++++++++++ .../online/api/GenApiControllerTest.java | 9 ++++++++- .../android/AndroidClientCodegenTest.java | 19 +++++++++++++++++++ .../AbstractJavaCodegenExampleValuesTest.java | 12 ++++++++++++ .../JavascriptClientCodegenTest.java | 9 +++++++++ .../client/model/BirdAndCategoryTest.java | 4 ++-- 6 files changed, 69 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator-core/src/test/java/org/openapitools/codegen/config/WorkflowSettingsTest.java b/modules/openapi-generator-core/src/test/java/org/openapitools/codegen/config/WorkflowSettingsTest.java index 33638f767356..626e02aca94b 100644 --- a/modules/openapi-generator-core/src/test/java/org/openapitools/codegen/config/WorkflowSettingsTest.java +++ b/modules/openapi-generator-core/src/test/java/org/openapitools/codegen/config/WorkflowSettingsTest.java @@ -120,4 +120,23 @@ public void defaultValuesCanBeChangedBuilder(){ WorkflowSettings defaults = WorkflowSettings.newBuilder().build(); assertOnChangesToDefaults(defaults); } + @Test + public void customOutputDirIsSet() { + WorkflowSettings settings = WorkflowSettings.newBuilder() + .withOutputDir("custom/output/directory") + .build(); + + assertEquals(settings.getOutputDir(), Paths.get("custom/output/directory").toAbsolutePath().toString()); + } + + @Test + public void customGlobalPropertiesAreSet() { + WorkflowSettings settings = WorkflowSettings.newBuilder() + .withGlobalProperty("customKey", "customValue") + .build(); + + Map properties = settings.getGlobalProperties(); + assertEquals(properties.size(), 1, "Global Properties map should allow custom entries"); + assertEquals(properties.getOrDefault("customKey", ""), "customValue"); + } } \ No newline at end of file diff --git a/modules/openapi-generator-online/src/test/java/org/openapitools/codegen/online/api/GenApiControllerTest.java b/modules/openapi-generator-online/src/test/java/org/openapitools/codegen/online/api/GenApiControllerTest.java index 262e0eba6603..8df33829c926 100644 --- a/modules/openapi-generator-online/src/test/java/org/openapitools/codegen/online/api/GenApiControllerTest.java +++ b/modules/openapi-generator-online/src/test/java/org/openapitools/codegen/online/api/GenApiControllerTest.java @@ -124,5 +124,12 @@ public void generateWIthForwardedHeaders() throws Exception { .andExpect(status().isOk()) .andExpect(header().string(HttpHeaders.CONTENT_LENGTH, not(0))); } - + @Test + public void generateClientWithInvalidOpenAPIUrl() throws Exception { + String invalidOpenAPIUrl = "https://test.com:1234/invalid_openapi.json"; + mockMvc.perform(post("http://test.com:1234/api/gen/clients/java") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"openAPIUrl\": \"" + invalidOpenAPIUrl + "\"}")) + .andExpect(status().isBadRequest()); + } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/android/AndroidClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/android/AndroidClientCodegenTest.java index f5ede0eb5a92..f7fab835e1fa 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/android/AndroidClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/android/AndroidClientCodegenTest.java @@ -70,4 +70,23 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception { Assert.assertEquals(codegen.getInvokerPackage(), "xyz.yyyyy.iiii.invoker"); Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xyz.yyyyy.iiii.invoker"); } + @Test + public void testHideGenerationTimestampDisabled() throws Exception { + final AndroidClientCodegen codegen = new AndroidClientCodegen(); + codegen.additionalProperties().put("hideGenerationTimestamp", false); + codegen.processOpts(); + + Assert.assertEquals(codegen.additionalProperties().get("hideGenerationTimestamp"), Boolean.FALSE); + Assert.assertFalse(codegen.isHideGenerationTimestamp()); + } + + @Test + public void testHideGenerationTimestampEnabled() throws Exception { + final AndroidClientCodegen codegen = new AndroidClientCodegen(); + codegen.additionalProperties().put("hideGenerationTimestamp", true); + codegen.processOpts(); + + Assert.assertEquals(codegen.additionalProperties().get("hideGenerationTimestamp"), Boolean.TRUE); + Assert.assertTrue(codegen.isHideGenerationTimestamp()); + } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenExampleValuesTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenExampleValuesTest.java index d9f451a462e0..2cbe4c623646 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenExampleValuesTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenExampleValuesTest.java @@ -153,5 +153,17 @@ public String getHelp() { public String getArtifactVersion() { return this.artifactVersion; } + @Test + void customExampleForEnumValue() { + final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen(); + final CodegenParameter p = new CodegenParameter(); + p.allowableValues = Collections.singletonMap("values", Arrays.asList("first", "second")); + p.dataType = "WrappedEnum"; + p.example = "CustomEnumValue"; + + fakeJavaCodegen.setParameterExampleValue(p); + // Custom example value should not be modified + Assert.assertEquals(p.example, "CustomEnumValue"); + } } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/javascript/JavascriptClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/javascript/JavascriptClientCodegenTest.java index 8514e36b803c..7b5e0ca5de77 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/javascript/JavascriptClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/javascript/JavascriptClientCodegenTest.java @@ -169,4 +169,13 @@ public void testIsJsonIsXmlInProduces() throws Exception { } } + @Test(description = "test additional properties for code generation") + public void testAdditionalProperties() throws Exception { + final JavascriptClientCodegen codegen = new JavascriptClientCodegen(); + codegen.additionalProperties().put("customProperty", "customValue"); + codegen.processOpts(); + + Assert.assertEquals(codegen.additionalProperties().get("customProperty"), "customValue"); + } + } diff --git a/samples/client/echo_api/java/okhttp-gson/src/test/java/org/openapitools/client/model/BirdAndCategoryTest.java b/samples/client/echo_api/java/okhttp-gson/src/test/java/org/openapitools/client/model/BirdAndCategoryTest.java index 205f5cff0cda..9237871cfb7f 100644 --- a/samples/client/echo_api/java/okhttp-gson/src/test/java/org/openapitools/client/model/BirdAndCategoryTest.java +++ b/samples/client/echo_api/java/okhttp-gson/src/test/java/org/openapitools/client/model/BirdAndCategoryTest.java @@ -34,7 +34,7 @@ public class BirdAndCategoryTest { */ @Test public void testBirdAndCategory() { - // TODO: test BirdAndCategory + } /** @@ -42,7 +42,7 @@ public void testBirdAndCategory() { */ @Test public void sizeTest() { - // TODO: test size + } /**