From 260141c06ad935647eda55d3d3495dee3befce28 Mon Sep 17 00:00:00 2001 From: Michal Foksa Date: Thu, 18 Jul 2019 08:10:31 +0200 Subject: [PATCH 1/3] Mustache lambda tests --- .../mustache/CamelCaseLambdaTest.java | 63 +++++++++++++++++++ .../mustache/IndentedLambdaTest.java | 35 +++++++++++ .../templating/mustache/LambdaTest.java | 44 +++++++++++++ .../mustache/LowercaseLambdaTest.java | 49 +++++++++++++++ .../mustache/OnChangeLambdaTest.java | 42 +++++++++++++ .../mustache/TitlecaseLambdaTest.java | 28 +++++++++ .../mustache/UppercaseLambdaTest.java | 18 ++++++ 7 files changed, 279 insertions(+) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/CamelCaseLambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/IndentedLambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LowercaseLambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/OnChangeLambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/TitlecaseLambdaTest.java create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UppercaseLambdaTest.java diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/CamelCaseLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/CamelCaseLambdaTest.java new file mode 100644 index 000000000000..f984adcb0f80 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/CamelCaseLambdaTest.java @@ -0,0 +1,63 @@ +package org.openapitools.codegen.templating.mustache; + +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openapitools.codegen.CodegenConfig; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class CamelCaseLambdaTest extends LambdaTest { + + @Mock + CodegenConfig generator; + + @BeforeMethod + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void camelCaseTest() { + // Given + Map ctx = context("camelcase", new CamelCaseLambda()); + + // When & Then + test("inputText", "{{#camelcase}}Input-text{{/camelcase}}", ctx); + } + + @Test + public void camelCaseReservedWordTest() { + // Given + Map ctx = context("camelcase", new CamelCaseLambda().generator(generator)); + + when(generator.sanitizeName(anyString())).then(returnsFirstArg()); + when(generator.reservedWords()).thenReturn(new HashSet(Arrays.asList("reservedWord"))); + when(generator.escapeReservedWord("reservedWord")).thenReturn("escapedReservedWord"); + + // When & Then + test("escapedReservedWord", "{{#camelcase}}reserved-word{{/camelcase}}", ctx); + } + + @Test + public void camelCaseEscapeParamTest() { + // Given + Map ctx = context("camelcase", new CamelCaseLambda() + .generator(generator).escapeAsParamName(true)); + + when(generator.sanitizeName(anyString())).then(returnsFirstArg()); + when(generator.reservedWords()).thenReturn(new HashSet()); + when(generator.toParamName("inputText")).thenReturn("inputTextAsParam"); + + // When & Then + test("inputTextAsParam", "{{#camelcase}}Input_text{{/camelcase}}", ctx); + } + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/IndentedLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/IndentedLambdaTest.java new file mode 100644 index 000000000000..51a4eeda0a31 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/IndentedLambdaTest.java @@ -0,0 +1,35 @@ +package org.openapitools.codegen.templating.mustache; + +import java.util.Map; + +import org.testng.annotations.Test; + +public class IndentedLambdaTest extends LambdaTest { + + String lineSeparator = System.lineSeparator(); + + @Test + public void defaultIndentTest() { + // Given + Map ctx = context("indented", new IndentedLambda()); + String lineSeparator = System.lineSeparator(); + + // When & Then + // IndentedLambda applies indentation from second line on of a template. + test("first line" + lineSeparator + " second line", + "{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx); + } + + @Test + public void indentedCountTest() { + // Given + Map ctx = context("indented", new IndentedLambda(8, " ")); + + // When & Then + // IndentedLambda applies indentation from second line on of a template. + test("first line" + lineSeparator + " second line", + "{{#indented}}first line" + lineSeparator +"second line{{/indented}}", ctx); + } + + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LambdaTest.java new file mode 100644 index 000000000000..fbaed952effb --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LambdaTest.java @@ -0,0 +1,44 @@ +package org.openapitools.codegen.templating.mustache; + +import static org.testng.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import com.samskivert.mustache.Mustache; + +/** + * Simple framework to test Mustache Lambdas. It avoids + *
compiler->compile->execute->assert
+ * boilerplate code. + * + * Inspired by Jmustache SharedTests.java + * + */ +public abstract class LambdaTest { + + protected String execute(String template, Object ctx) { + return execute(Mustache.compiler(), template, ctx); + } + + protected String execute(Mustache.Compiler compiler, String template, Object ctx) { + return compiler.compile(template).execute(ctx); + } + + protected void test(String expected, String template, Map ctx) { + test(Mustache.compiler(), expected, template, ctx); + } + + protected void test(Mustache.Compiler compiler, String expected,String template, Map ctx) { + assertEquals(execute(compiler, template, ctx), expected); + } + + protected static Map context(Object... data) { + Map ctx = new HashMap(); + for (int ii = 0; ii < data.length; ii += 2) { + ctx.put(data[ii].toString(), data[ii + 1]); + } + return ctx; + } + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LowercaseLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LowercaseLambdaTest.java new file mode 100644 index 000000000000..87ade0caa701 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/LowercaseLambdaTest.java @@ -0,0 +1,49 @@ +package org.openapitools.codegen.templating.mustache; + +import static org.mockito.AdditionalAnswers.returnsFirstArg; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openapitools.codegen.CodegenConfig; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class LowercaseLambdaTest extends LambdaTest { + + @Mock + CodegenConfig generator; + + @BeforeMethod + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void lowercaseTest() { + // Given + Map ctx = context("lowercase", new LowercaseLambda()); + + // When & Then + test("input text", "{{#lowercase}}InPut Text{{/lowercase}}", ctx); + } + + @Test + public void lowercaseReservedWordTest() { + // Given + Map ctx = context("lowercase", new LowercaseLambda().generator(generator)); + + when(generator.sanitizeName(anyString())).then(returnsFirstArg()); + when(generator.reservedWords()).thenReturn(new HashSet(Arrays.asList("reserved"))); + when(generator.escapeReservedWord("reserved")).thenReturn("escaped-reserved"); + + // When & Then + test("escaped-reserved", "{{#lowercase}}rEservEd{{/lowercase}}", ctx); + } + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/OnChangeLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/OnChangeLambdaTest.java new file mode 100644 index 000000000000..175524c0d286 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/OnChangeLambdaTest.java @@ -0,0 +1,42 @@ +package org.openapitools.codegen.templating.mustache; + +import java.util.Map; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class OnChangeLambdaTest extends LambdaTest { + + private Map ctx; + + // OnChangeLambda holds internal state it is important that context is + // reinitialize before each test. + @BeforeMethod + public void setup() { + ctx = context("onchange", new OnChangeLambda()); + } + + @Test + public void firstValueIsReturnedTest() { + // Given + + // When & Then + test("first", "{{#onchange}}first{{/onchange}}", ctx); + } + + @Test + public void repeatingValueReturnedOnFirstOccurrenceTest() { + // Given + + // When & Then + test("First", "{{#onchange}}First{{/onchange}}", ctx); + test("", "{{#onchange}}First{{/onchange}}", ctx); + + test("Another", "{{#onchange}}Another{{/onchange}}", ctx); + test("", "{{#onchange}}Another{{/onchange}}", ctx); + test("", "{{#onchange}}Another{{/onchange}}", ctx); + + test("First", "{{#onchange}}First{{/onchange}}", ctx); + } + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/TitlecaseLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/TitlecaseLambdaTest.java new file mode 100644 index 000000000000..2b3f632e9ddb --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/TitlecaseLambdaTest.java @@ -0,0 +1,28 @@ +package org.openapitools.codegen.templating.mustache; + +import java.util.Map; + +import org.testng.annotations.Test; + +public class TitlecaseLambdaTest extends LambdaTest { + + @Test + public void titlecaseTest() { + // Given + Map ctx = context("titlecase", new TitlecaseLambda()); + + // When & Then + test("Once Upon A Time", "{{#titlecase}}once upon a time{{/titlecase}}", ctx); + } + + @Test + public void titlecaseWithDelimiterTest() { + // Given + Map ctx = context("titlecase", new TitlecaseLambda("-")); + + // When & Then + test("Once-Upon-A-Time", "{{#titlecase}}once-upon-a-time{{/titlecase}}", ctx); + } + + +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UppercaseLambdaTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UppercaseLambdaTest.java new file mode 100644 index 000000000000..ebd12454ce6a --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/templating/mustache/UppercaseLambdaTest.java @@ -0,0 +1,18 @@ +package org.openapitools.codegen.templating.mustache; + +import java.util.Map; + +import org.testng.annotations.Test; + +public class UppercaseLambdaTest extends LambdaTest { + + @Test + public void uppercaseTest() { + // Given + Map ctx = context("uppercase", new UppercaseLambda()); + + // When & Then + test("INPUT TEXT", "{{#uppercase}}InPut Text{{/uppercase}}", ctx); + } + +} From e9e47ae0cb0276d71cd18677f942d7f76152b505 Mon Sep 17 00:00:00 2001 From: Michal Foksa Date: Wed, 24 Jul 2019 19:11:41 +0200 Subject: [PATCH 2/3] If lambda key is already taken in additionalProperties, throw an exception. --- .../java/org/openapitools/codegen/DefaultCodegen.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 90ae91674c1d..94b29ee1bacd 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -240,13 +240,10 @@ private void registerMustacheLambdas() { } if (additionalProperties.containsKey("lambda")) { - LOGGER.warn("A property named 'lambda' already exists. Mustache lambdas renamed from 'lambda' to '_lambda'. " + - "You'll likely need to use a custom template, " + - "see https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md. "); - additionalProperties.put("_lambda", lambdas); - } else { - additionalProperties.put("lambda", lambdas); + LOGGER.error("A property called 'lambda' already exists in additionalProperties"); + throw new RuntimeException("A property called 'lambda' already exists in additionalProperties"); } + additionalProperties.put("lambda", lambdas); } // override with any special post-processing for all models From fc37be33f3b8b68c5efe7109881cdf3e3e874800 Mon Sep 17 00:00:00 2001 From: Michal Foksa Date: Wed, 24 Jul 2019 19:14:01 +0200 Subject: [PATCH 3/3] Test whether common lambdas are registered in additionalProperties. --- .../codegen/DefaultCodegenTest.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 167177017f50..9dbb5bb5c134 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -18,6 +18,8 @@ package org.openapitools.codegen; import com.google.common.collect.Sets; +import com.samskivert.mustache.Mustache.Lambda; + import io.swagger.parser.OpenAPIParser; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; @@ -30,14 +32,19 @@ import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; import io.swagger.v3.parser.core.models.ParseOptions; -import org.openapitools.codegen.languages.features.CXFServerFeatures; + +import org.openapitools.codegen.templating.mustache.CamelCaseLambda; +import org.openapitools.codegen.templating.mustache.IndentedLambda; +import org.openapitools.codegen.templating.mustache.LowercaseLambda; +import org.openapitools.codegen.templating.mustache.TitlecaseLambda; +import org.openapitools.codegen.templating.mustache.UppercaseLambda; import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + import java.util.*; import java.util.stream.Collectors; @@ -888,6 +895,7 @@ public void modelWithPrefixDoNotContainInheritedVars() { Assert.assertEquals(codegenModel.vars.size(), 1); } + @Test public void modelWithSuffixDoNotContainInheritedVars() { DefaultCodegen codegen = new DefaultCodegen(); @@ -903,4 +911,25 @@ public void modelWithSuffixDoNotContainInheritedVars() { Assert.assertEquals(codegenModel.vars.size(), 1); } + @Test + @SuppressWarnings("unchecked") + public void commonLambdasRegistrationTest() { + + DefaultCodegen codegen = new DefaultCodegen(); + Object lambdasObj = codegen.additionalProperties.get("lambda"); + + assertNotNull(lambdasObj, "Expecting lambda in additionalProperties"); + + Map lambdas = (Map) lambdasObj; + + assertTrue(lambdas.get("lowercase") instanceof LowercaseLambda, "Expecting LowercaseLambda class"); + assertTrue(lambdas.get("uppercase") instanceof UppercaseLambda, "Expecting UppercaseLambda class"); + assertTrue(lambdas.get("titlecase") instanceof TitlecaseLambda, "Expecting TitlecaseLambda class"); + assertTrue(lambdas.get("camelcase") instanceof CamelCaseLambda, "Expecting CamelCaseLambda class"); + assertTrue(lambdas.get("indented") instanceof IndentedLambda, "Expecting IndentedLambda class"); + assertTrue(lambdas.get("indented_8") instanceof IndentedLambda, "Expecting IndentedLambda class"); + assertTrue(lambdas.get("indented_12") instanceof IndentedLambda, "Expecting IndentedLambda class"); + assertTrue(lambdas.get("indented_16") instanceof IndentedLambda, "Expecting IndentedLambda class"); + } + }