Skip to content

Commit

Permalink
chore(citrus-test-api-generator-maven-plugin): review and code cleanup
Browse files Browse the repository at this point in the history
pr: #1224

`citrus-test-api-generator-maven-plugin` module.
  • Loading branch information
bbortt committed Oct 28, 2024
1 parent 8e3436b commit 17025a6
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -72,21 +74,22 @@ public CodeGenMojoWrapper configOptions(Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,7 +80,7 @@ private static List<String> 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);
}
}
Expand Down Expand Up @@ -107,24 +110,20 @@ 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);
});
}

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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 17025a6

Please sign in to comment.