From 17025a680e4a7f16da96eb4ef93f4de0516e92ca Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Mon, 28 Oct 2024 14:33:44 +0100 Subject: [PATCH] chore(citrus-test-api-generator-maven-plugin): review and code cleanup pr: https://github.com/citrusframework/citrus/pull/1224 `citrus-test-api-generator-maven-plugin` module. --- .../maven/plugin/CodeGenMojoWrapper.java | 13 +- .../maven/plugin/SpringMetaFileGenerator.java | 21 ++-- .../maven/plugin/TestApiGeneratorMojo.java | 116 +++++++++++++++++- .../TestApiGeneratorMojoIntegrationTest.java | 26 ++-- .../plugin/TestApiGeneratorMojoUnitTest.java | 1 - 5 files changed, 140 insertions(+), 37 deletions(-) diff --git a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/CodeGenMojoWrapper.java b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/CodeGenMojoWrapper.java index 63d380f1ad..fc1f7f3be4 100644 --- a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/CodeGenMojoWrapper.java +++ b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/CodeGenMojoWrapper.java @@ -19,7 +19,6 @@ import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; -import org.citrusframework.openapi.generator.CitrusJavaCodegen; import org.openapitools.codegen.plugin.CodeGenMojo; import java.io.File; @@ -28,6 +27,9 @@ import static java.lang.String.format; import static org.citrusframework.openapi.generator.CitrusJavaCodegen.CODEGEN_NAME; +import static org.citrusframework.openapi.generator.CitrusJavaCodegen.GENERATED_SCHEMA_FOLDER; +import static org.citrusframework.openapi.generator.CitrusJavaCodegen.RESOURCE_FOLDER; +import static org.citrusframework.openapi.generator.CitrusJavaCodegen.SOURCE_FOLDER; /** * Wrapper class that uses reflection to expose several properties of the {@link CodeGenMojo} for explicit assignment. @@ -72,21 +74,22 @@ public CodeGenMojoWrapper configOptions(Map configOptionsPropert } public CodeGenMojoWrapper schemaFolder(String schemaFolder) { - configOptionsProperties.put(CitrusJavaCodegen.GENERATED_SCHEMA_FOLDER, schemaFolder); + configOptionsProperties.put(GENERATED_SCHEMA_FOLDER, schemaFolder); return this; } public CodeGenMojoWrapper resourceFolder(String resourceFolder) { - configOptionsProperties.put(CitrusJavaCodegen.RESOURCE_FOLDER, resourceFolder); + configOptionsProperties.put(RESOURCE_FOLDER, resourceFolder); return this; } public CodeGenMojoWrapper sourceFolder(String sourceFolder) { - configOptionsProperties.put(CitrusJavaCodegen.SOURCE_FOLDER, sourceFolder); + configOptionsProperties.put(SOURCE_FOLDER, sourceFolder); return this; } - @SuppressWarnings("java:S3011") // Accessibility bypass + // Accessibility bypass + @SuppressWarnings("java:S3011") private void setPrivateField(String fieldName, Object fieldValue) throws MojoExecutionException { try { var field = CodeGenMojo.class.getDeclaredField(fieldName); diff --git a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/SpringMetaFileGenerator.java b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/SpringMetaFileGenerator.java index 76680e8909..bb9acc7fbf 100644 --- a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/SpringMetaFileGenerator.java +++ b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/SpringMetaFileGenerator.java @@ -31,6 +31,9 @@ import static java.lang.String.format; import static java.util.Collections.emptyList; +import static org.citrusframework.maven.plugin.TestApiGeneratorMojo.CITRUS_TEST_SCHEMA; +import static org.citrusframework.maven.plugin.TestApiGeneratorMojo.replaceDynamicVars; +import static org.citrusframework.maven.plugin.TestApiGeneratorMojo.replaceDynamicVarsToLowerCase; /** * Utility class responsible for generating the Spring meta files 'spring.handlers' and 'spring.schemas', used @@ -77,7 +80,7 @@ private static List readAndFilterLines(File file) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { - if (!line.contains(TestApiGeneratorMojo.CITRUS_TEST_SCHEMA)) { + if (!line.contains(CITRUS_TEST_SCHEMA)) { filteredLines.add(line); } } @@ -107,11 +110,9 @@ public void generateSpringIntegrationMetaFiles() throws MojoExecutionException { private void writeSpringSchemaMetaFile(File springMetafileDirectory) throws MojoExecutionException { String filename = "spring.schemas"; writeSpringMetaFile(springMetafileDirectory, filename, (fileWriter, apiConfig) -> { - String targetXmlnsNamespace = TestApiGeneratorMojo.replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), - apiConfig.getVersion()); - String schemaFolderPath = TestApiGeneratorMojo.replaceDynamicVars(testApiGeneratorMojo.schemaFolder(apiConfig), apiConfig.getPrefix(), - apiConfig.getVersion()); - String schemaPath = String.format("%s/%s-api.xsd", schemaFolderPath, apiConfig.getPrefix().toLowerCase()); + String targetXmlnsNamespace = replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), apiConfig.getVersion()); + String schemaFolderPath = replaceDynamicVars(testApiGeneratorMojo.schemaFolder(apiConfig), apiConfig.getPrefix(), apiConfig.getVersion()); + String schemaPath = format("%s/%s-api.xsd", schemaFolderPath, apiConfig.getPrefix().toLowerCase()); appendLine(fileWriter, format("%s.xsd=%s%n", targetXmlnsNamespace.replace("http://", "http\\://"), schemaPath), filename); }); } @@ -119,12 +120,10 @@ private void writeSpringSchemaMetaFile(File springMetafileDirectory) throws Mojo private void writeSpringHandlerMetaFile(File springMetafileDirectory) throws MojoExecutionException { String filename = "spring.handlers"; writeSpringMetaFile(springMetafileDirectory, filename, (fileWriter, apiConfig) -> { - String targetXmlnsNamespace = TestApiGeneratorMojo.replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), - apiConfig.getVersion()); - String invokerPackage = TestApiGeneratorMojo.replaceDynamicVarsToLowerCase(apiConfig.getInvokerPackage(), apiConfig.getPrefix(), apiConfig.getVersion()); + String targetXmlnsNamespace = replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), apiConfig.getVersion()); + String invokerPackage = replaceDynamicVarsToLowerCase(apiConfig.getInvokerPackage(), apiConfig.getPrefix(), apiConfig.getVersion()); String namespaceHandlerClass = invokerPackage + ".citrus.extension." + apiConfig.getPrefix() + "NamespaceHandler"; - appendLine(fileWriter, format("%s=%s%n", targetXmlnsNamespace.replace("http://", "http\\://"), namespaceHandlerClass), - filename); + appendLine(fileWriter, format("%s=%s%n", targetXmlnsNamespace.replace("http://", "http\\://"), namespaceHandlerClass), filename); }); } diff --git a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/TestApiGeneratorMojo.java b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/TestApiGeneratorMojo.java index dc6931f01b..358e6f551d 100644 --- a/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/TestApiGeneratorMojo.java +++ b/test-api-generator/citrus-test-api-generator-maven-plugin/src/main/java/org/citrusframework/maven/plugin/TestApiGeneratorMojo.java @@ -17,8 +17,6 @@ package org.citrusframework.maven.plugin; import com.google.common.annotations.VisibleForTesting; -import lombok.Getter; -import lombok.Setter; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; @@ -92,44 +90,57 @@ public class TestApiGeneratorMojo extends AbstractMojo { * schemas/handlers. See also {@link TestApiGeneratorMojo}#DEFAULT_TARGET_NAMESPACE_TEMPLATE. */ public static final String DEFAULT_META_INF_FOLDER = "target/generated-test-resources/META-INF"; + /** * sourceFolder: specifies the location to which the sources are generated. Defaults to 'generated-test-sources'. */ public static final String SOURCE_FOLDER_PROPERTY = "citrus.test.api.generator.source.folder"; + /** * resourceFolder: specifies the location to which the resources are generated. Defaults to 'generated-test-resources'. */ public static final String RESOURCE_FOLDER_PROPERTY = "citrus.test.api.generator.resource.folder"; + /** * schemaFolder: specifies the location for the generated xsd schemas. Defaults to 'schema/xsd/%VERSION%' */ public static final String API_SCHEMA_FOLDER = "citrus.test.api.generator.schema.folder"; + /** * metaInfFolder: specifies the location to which the resources are generated. Defaults to 'generated-resources'. */ public static final String META_INF_FOLDER = "citrus.test.api.generator.meta.inf.folder"; + /** * resourceFolder: specifies the location to which the resources are generated. Defaults to 'generated-resources'. */ public static final String GENERATE_SPRING_INTEGRATION_FILES = "citrus.test.api.generator.generate.spring.integration.files"; + @Component private final BuildContext buildContext = new DefaultBuildContext(); + @Parameter(defaultValue = "${project}", readonly = true) private MavenProject mavenProject; + @Parameter(defaultValue = "${mojoExecution}", readonly = true) private MojoExecution mojoExecution; + @Parameter(property = SOURCE_FOLDER_PROPERTY, defaultValue = DEFAULT_SOURCE_FOLDER) @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) // Maven injected private String sourceFolder = DEFAULT_SOURCE_FOLDER; + @Parameter(property = RESOURCE_FOLDER_PROPERTY, defaultValue = DEFAULT_RESOURCE_FOLDER) @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) // Maven injected private String resourceFolder = DEFAULT_RESOURCE_FOLDER; + @Parameter(property = API_SCHEMA_FOLDER, defaultValue = DEFAULT_SCHEMA_FOLDER_TEMPLATE) @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) // Maven injected private String schemaFolder = DEFAULT_SCHEMA_FOLDER_TEMPLATE; + @Parameter(property = META_INF_FOLDER, defaultValue = DEFAULT_META_INF_FOLDER) @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) // Maven injected private String metaInfFolder = DEFAULT_META_INF_FOLDER; + @Parameter(property = GENERATE_SPRING_INTEGRATION_FILES, defaultValue = "true") @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"}) // Maven injected private boolean generateSpringIntegrationFiles = true; @@ -237,8 +248,6 @@ public enum ApiType { * Note that the default values are not properly set by maven processor. Therefore, the default values have been assigned additionally * on field level. */ - @Getter - @Setter public static class ApiConfig { public static final String DEFAULT_ENDPOINT = "PREFIX_ENDPOINT"; @@ -247,69 +256,168 @@ public static class ApiConfig { * prefix: specifies the prefixed used for the test api. Typically, an acronym for the application which is being tested. */ public static final String API_PREFIX_PROPERTY = "citrus.test.api.generator.prefix"; + /** * source: specifies the source of the test api. */ public static final String API_SOURCE_PROPERTY = "citrus.test.api.generator.source"; + /** * version: specifies the version of the api. May be null. */ public static final String API_VERSION_PROPERTY = "citrus.test.api.generator.version"; + /** * endpoint: specifies the endpoint of the test api. Defaults to 'prefixEndpoint'. */ public static final String API_ENDPOINT_PROPERTY = "citrus.test.api.generator.endpoint"; + /** * type: specifies the type of the test api. Defaults to 'REST' */ public static final String API_TYPE_PROPERTY = "citrus.test.api.generator.type"; + /** * useTags: specifies whether tags should be used by the generator. Defaults to 'true'. If useTags is set to true, the generator * will organize the generated code based on the tags defined in your API specification. */ public static final String API_USE_TAGS_PROPERTY = "citrus.test.api.generator.use.tags"; + /** * invokerPackage: specifies the package for the test api classes. Defaults to * 'org.citrusframework.automation.%PREFIX%.%VERSION%'. */ public static final String API_INVOKER_PACKAGE_PROPERTY = "citrus.test.api.generator.invoker.package"; + /** * apiPackage: specifies the package for the test api classes. Defaults to * 'org.citrusframework.automation.%PREFIX%.%VERSION%.api'. */ public static final String API_API_PACKAGE_PROPERTY = "citrus.test.api.generator.api.package"; + /** * modelPackage: specifies the package for the test api classes. Defaults to * 'org.citrusframework.automation.%PREFIX%.%VERSION%.model'. */ public static final String API_MODEL_PACKAGE_PROPERTY = "citrus.test.api.generator.model.package"; + /** * targetXmlNamespace: specifies the xml namespace to be used by the api. Defaults to * 'http://www.citrusframework.org/schema/%VERSION%/%PREFIX%-api' */ @SuppressWarnings("JavadocLinkAsPlainText") public static final String API_NAMESPACE_PROPERTY = "citrus.test.api.generator.namespace"; + @Parameter(required = true, property = API_PREFIX_PROPERTY) private String prefix; + @Parameter(required = true, property = API_SOURCE_PROPERTY) private String source; + @Parameter(property = API_VERSION_PROPERTY) private String version; + @Parameter(property = API_ENDPOINT_PROPERTY, defaultValue = DEFAULT_ENDPOINT) private String endpoint = DEFAULT_ENDPOINT; + @Parameter(property = API_TYPE_PROPERTY, defaultValue = "REST") private ApiType type = DEFAULT_API_TYPE; + @Parameter(property = API_USE_TAGS_PROPERTY, defaultValue = "true") private boolean useTags = true; + @Parameter(property = API_INVOKER_PACKAGE_PROPERTY, defaultValue = DEFAULT_INVOKER_PACKAGE) private String invokerPackage = DEFAULT_INVOKER_PACKAGE; + @Parameter(property = API_API_PACKAGE_PROPERTY, defaultValue = DEFAULT_API_PACKAGE) private String apiPackage = DEFAULT_API_PACKAGE; + @Parameter(property = API_MODEL_PACKAGE_PROPERTY, defaultValue = DEFAULT_MODEL_PACKAGE) private String modelPackage = DEFAULT_MODEL_PACKAGE; + @Parameter(property = API_NAMESPACE_PROPERTY, defaultValue = DEFAULT_TARGET_NAMESPACE_TEMPLATE) private String targetXmlnsNamespace = DEFAULT_TARGET_NAMESPACE_TEMPLATE; + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + public ApiType getType() { + return type; + } + + public void setType(ApiType type) { + this.type = type; + } + + public boolean isUseTags() { + return useTags; + } + + public void setUseTags(boolean useTags) { + this.useTags = useTags; + } + + public String getInvokerPackage() { + return invokerPackage; + } + + public void setInvokerPackage(String invokerPackage) { + this.invokerPackage = invokerPackage; + } + + public String getApiPackage() { + return apiPackage; + } + + public void setApiPackage(String apiPackage) { + this.apiPackage = apiPackage; + } + + public String getModelPackage() { + return modelPackage; + } + + public void setModelPackage(String modelPackage) { + this.modelPackage = modelPackage; + } + + public String getTargetXmlnsNamespace() { + return targetXmlnsNamespace; + } + + public void setTargetXmlnsNamespace(String targetXmlnsNamespace) { + this.targetXmlnsNamespace = targetXmlnsNamespace; + } + public String qualifiedEndpoint() { return DEFAULT_ENDPOINT.equals(endpoint) ? getPrefix().toLowerCase() + "Endpoint" : endpoint; } diff --git a/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoIntegrationTest.java b/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoIntegrationTest.java index 94bfbaa065..9500d15127 100644 --- a/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoIntegrationTest.java +++ b/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoIntegrationTest.java @@ -37,7 +37,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.springframework.test.util.ReflectionTestUtils.getField; -class TestApiGeneratorMojoIntegrationTest extends AbstractMojoTestCase { +public class TestApiGeneratorMojoIntegrationTest extends AbstractMojoTestCase { public static final String OTHER_META_FILE_CONTENT = "somenamespace=somevalue"; @@ -92,9 +92,7 @@ void beforeEachSetup() throws Exception { @ParameterizedTest @MethodSource - void executeMojoWithConfigurations(String configName, Exception expectedException) - throws Exception { - + void executeMojoWithConfigurations(String configName, Exception expectedException) throws Exception { try { fixture = fixtureFromPom(configName); } catch (MojoExecutionException | MojoFailureException e) { @@ -120,7 +118,8 @@ void executeMojoWithConfigurations(String configName, Exception expectedExceptio } } else { // When/Then - assertThatThrownBy(() -> fixture.execute()).isInstanceOf(expectedException.getClass()) + assertThatThrownBy(() -> fixture.execute()) + .isInstanceOf(expectedException.getClass()) .hasMessage(expectedException.getMessage()); } } @@ -131,7 +130,6 @@ void executeMojoWithConfigurations(String configName, Exception expectedExceptio private void writeSomeValuesToSpringMetaFiles(List apiConfigs) { for (ApiConfig apiConfig : apiConfigs) { for (String filePathTemplate : SPRING_META_FILE_TEMPLATES) { - String filePath = resolveFilePath(apiConfig, filePathTemplate); File file = new File(filePath); if (!file.getParentFile().exists() && !new File(filePath).getParentFile().mkdirs()) { @@ -149,7 +147,6 @@ private void writeSomeValuesToSpringMetaFiles(List apiConfigs) { } private void assertFilesGenerated(ApiConfig apiConfig) { - for (String filePathTemplate : STANDARD_FILE_PATH_TEMPLATES) { String filePath = resolveFilePath(apiConfig, filePathTemplate); assertThat(new File(filePath)).isFile().exists(); @@ -190,7 +187,6 @@ private void assertHandlersInSpringHandlers(ApiConfig apiConfig) throws IOExcept } private void assertSchemasInSpringSchemas(ApiConfig apiConfig) throws IOException { - String targetNamespace = replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), apiConfig.getVersion()); targetNamespace = targetNamespace.replace(":", "\\:"); String schemaPath = replaceDynamicVarsToLowerCase((String) getField(fixture, "schemaFolder"), apiConfig.getPrefix(), apiConfig.getVersion()); @@ -204,14 +200,14 @@ private void assertSchemasInSpringSchemas(ApiConfig apiConfig) throws IOExceptio } private void assertTargetNamespace(ApiConfig apiConfig) throws IOException { - assertThat(getContentOfFile(apiConfig, "-api.xsd")).contains( - String.format("targetNamespace=\"%s\"", + assertThat(getContentOfFile(apiConfig, "-api.xsd")) + .contains(String.format("targetNamespace=\"%s\"", replaceDynamicVarsToLowerCase(apiConfig.getTargetXmlnsNamespace(), apiConfig.getPrefix(), apiConfig.getVersion()))); } private void assertEndpointName(ApiConfig apiConfig) throws IOException { - assertThat(getContentOfFile(apiConfig, "BeanConfiguration")).contains( - String.format("@Qualifier(\"%s\")", apiConfig.qualifiedEndpoint())); + assertThat(getContentOfFile(apiConfig, "BeanConfiguration")) + .contains(String.format("@Qualifier(\"%s\")", apiConfig.qualifiedEndpoint())); } private String getContentOfFile(ApiConfig apiConfig, String fileIdentifier) throws IOException { @@ -228,13 +224,13 @@ private String getContentOfFile(ApiConfig apiConfig, String fileIdentifier) thro private String getTemplateContaining(String text) { return concat(stream(STANDARD_FILE_PATH_TEMPLATES), stream(SPRING_META_FILE_TEMPLATES)) - .filter(path -> path.contains(text)).findFirst() + .filter(path -> path.contains(text)) + .findFirst() .orElseThrow(() -> new AssertionError(String.format("Can't find file template with content: '%s'", text))); } @NotNull private String resolveFilePath(ApiConfig apiConfig, String filePathTemplate) { - String lowerCasePrefix = apiConfig.getPrefix().toLowerCase(); char[] prefixCharArray = apiConfig.getPrefix().toCharArray(); prefixCharArray[0] = Character.toUpperCase(prefixCharArray[0]); @@ -269,7 +265,6 @@ private String resolveFilePath(ApiConfig apiConfig, String filePathTemplate) { } private String toFolder(String text) { - if (text == null) { return ""; } @@ -291,5 +286,4 @@ private TestApiGeneratorMojo fixtureFromPom(String configName) throws Exception return testApiGeneratorMojo; } - } diff --git a/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoUnitTest.java b/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoUnitTest.java index c9c8bc042c..fc9e5f8bff 100644 --- a/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoUnitTest.java +++ b/test-api-generator/citrus-test-api-generator-maven-plugin/src/test/java/org/citrusframework/maven/plugin/TestApiGeneratorMojoUnitTest.java @@ -41,7 +41,6 @@ import static org.mockito.Mockito.doReturn; import static org.springframework.test.util.ReflectionTestUtils.getField; - @ExtendWith(MockitoExtension.class) @SuppressWarnings({"JUnitMalformedDeclaration", "JUnitMixedFramework"}) class TestApiGeneratorMojoUnitTest extends AbstractMojoTestCase {