mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
Merge pull request #1447 from lugaru1234/optionsTest
Options tests have been added for Java and JaxRS
This commit is contained in:
commit
09c5aa3827
@ -31,6 +31,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
|
||||
public static final String FULL_JAVA_UTIL = "fullJavaUtil";
|
||||
|
||||
protected String invokerPackage = "io.swagger.client";
|
||||
protected String groupId = "io.swagger";
|
||||
@ -84,7 +85,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.LOCAL_VARIABLE_PREFIX, CodegenConstants.LOCAL_VARIABLE_PREFIX_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));
|
||||
cliOptions.add(new CliOption("fullJavaUtil", "whether to use fully qualified name for classes under java.util (default to false)"));
|
||||
cliOptions.add(new CliOption(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util (default to false)"));
|
||||
|
||||
supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
|
||||
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
|
||||
@ -160,13 +161,13 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
// need to put back serializableModel (boolean) into additionalProperties as value in additionalProperties is string
|
||||
additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel);
|
||||
|
||||
if (additionalProperties.containsKey("fullJavaUtil")) {
|
||||
fullJavaUtil = Boolean.valueOf(additionalProperties.get("fullJavaUtil").toString());
|
||||
if (additionalProperties.containsKey(FULL_JAVA_UTIL)) {
|
||||
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
|
||||
}
|
||||
if (fullJavaUtil) {
|
||||
javaUtilPrefix = "java.util.";
|
||||
}
|
||||
additionalProperties.put("fullJavaUtil", fullJavaUtil);
|
||||
additionalProperties.put(FULL_JAVA_UTIL, fullJavaUtil);
|
||||
additionalProperties.put("javaUtilPrefix", javaUtilPrefix);
|
||||
|
||||
if (fullJavaUtil) {
|
||||
@ -582,4 +583,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public void setFullJavaUtil(boolean fullJavaUtil) {
|
||||
this.fullJavaUtil = fullJavaUtil;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,57 @@
|
||||
package io.swagger.codegen;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
import mockit.FullVerifications;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class AbstractOptionsTest {
|
||||
|
||||
@Test
|
||||
public void checkOptionsProcessing() {
|
||||
getCodegenConfig().additionalProperties().putAll(getAvaliableOptions());
|
||||
setExpectations();
|
||||
|
||||
getCodegenConfig().processOpts();
|
||||
|
||||
new FullVerifications() {{
|
||||
}};
|
||||
}
|
||||
|
||||
@Test(description = "check if all options described in documentation are presented in test case")
|
||||
public void checkOptionsHelp() {
|
||||
final List<String> cliOptions = Lists.transform(getCodegenConfig().cliOptions(), getCliOptionTransformer());
|
||||
final Set<String> testOptions = getAvaliableOptions().keySet();
|
||||
final Set<String> skipped = new HashSet<String>(cliOptions);
|
||||
skipped.removeAll(testOptions);
|
||||
if (!skipped.isEmpty()) {
|
||||
Assert.fail(String.format("These options weren't checked: %s.", StringUtils.join(skipped, ", ")));
|
||||
}
|
||||
final Set<String> undocumented = new HashSet<String>(testOptions);
|
||||
undocumented.removeAll(cliOptions);
|
||||
if (!undocumented.isEmpty()) {
|
||||
Assert.fail(String.format("These options weren't documented: %s.", StringUtils.join(undocumented, ", ")));
|
||||
}
|
||||
}
|
||||
|
||||
private Function<CliOption, String> getCliOptionTransformer() {
|
||||
return new Function<CliOption, String>() {
|
||||
public String apply(CliOption option) {
|
||||
return option.getOpt();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected abstract CodegenConfig getCodegenConfig();
|
||||
|
||||
protected abstract void setExpectations();
|
||||
|
||||
protected abstract Map<String, String> getAvaliableOptions();
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package io.swagger.codegen.java;
|
||||
|
||||
import io.swagger.codegen.AbstractOptionsTest;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.languages.JavaClientCodegen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaClientOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
protected static final String ARTIFACT_ID_VALUE = "swagger-java-client-test";
|
||||
protected static final String MODEL_PACKAGE_VALUE = "package";
|
||||
protected static final String API_PACKAGE_VALUE = "apiPackage";
|
||||
protected static final String INVOKER_PACKAGE_VALUE = "io.swagger.client.test";
|
||||
protected static final String SORT_PARAMS_VALUE = "false";
|
||||
protected static final String GROUP_ID_VALUE = "io.swagger.test";
|
||||
protected static final String ARTIFACT_VERSION_VALUE = "1.0.0-SNAPSHOT";
|
||||
protected static final String SOURCE_FOLDER_VALUE = "src/main/java/test";
|
||||
protected static final String LOCAL_PREFIX_VALUE = "tst";
|
||||
protected static final String LIBRARY_VALUE = "jersey2";
|
||||
protected static final String SERIALIZABLE_MODEL_VALUE = "false";
|
||||
protected static final String FULL_JAVA_UTIL_VALUE = "true";
|
||||
|
||||
@Tested
|
||||
private JavaClientCodegen clientCodegen;
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setModelPackage(MODEL_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setApiPackage(API_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(SORT_PARAMS_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setInvokerPackage(INVOKER_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setGroupId(GROUP_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setArtifactId(ARTIFACT_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setArtifactVersion(ARTIFACT_VERSION_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSourceFolder(SOURCE_FOLDER_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setLocalVariablePrefix(LOCAL_PREFIX_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSerializableModel(Boolean.valueOf(SERIALIZABLE_MODEL_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setLibrary(LIBRARY_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setFullJavaUtil(Boolean.valueOf(FULL_JAVA_UTIL_VALUE));
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, String> getAvaliableOptions() {
|
||||
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
|
||||
return builder.put(CodegenConstants.MODEL_PACKAGE, MODEL_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.API_PACKAGE, API_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
|
||||
.put(CodegenConstants.INVOKER_PACKAGE, INVOKER_PACKAGE_VALUE)
|
||||
.put(CodegenConstants.GROUP_ID, GROUP_ID_VALUE)
|
||||
.put(CodegenConstants.ARTIFACT_ID, ARTIFACT_ID_VALUE)
|
||||
.put(CodegenConstants.ARTIFACT_VERSION, ARTIFACT_VERSION_VALUE)
|
||||
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
|
||||
.put(CodegenConstants.LOCAL_VARIABLE_PREFIX, LOCAL_PREFIX_VALUE)
|
||||
.put(CodegenConstants.SERIALIZABLE_MODEL, SERIALIZABLE_MODEL_VALUE)
|
||||
.put(JavaClientCodegen.FULL_JAVA_UTIL, FULL_JAVA_UTIL_VALUE)
|
||||
.put(CodegenConstants.LIBRARY, LIBRARY_VALUE)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package io.swagger.codegen.jaxrs;
|
||||
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.java.JavaClientOptionsTest;
|
||||
import io.swagger.codegen.languages.JaxRSServerCodegen;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class JaxRSServerOptionsTest extends JavaClientOptionsTest {
|
||||
|
||||
@Tested
|
||||
private JaxRSServerCodegen clientCodegen;
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
clientCodegen.setModelPackage(MODEL_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setApiPackage(API_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSortParamsByRequiredFlag(Boolean.valueOf(SORT_PARAMS_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setInvokerPackage(INVOKER_PACKAGE_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setGroupId(GROUP_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setArtifactId(ARTIFACT_ID_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setArtifactVersion(ARTIFACT_VERSION_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSourceFolder(SOURCE_FOLDER_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setLocalVariablePrefix(LOCAL_PREFIX_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setSerializableModel(Boolean.valueOf(SERIALIZABLE_MODEL_VALUE));
|
||||
times = 1;
|
||||
clientCodegen.setLibrary(LIBRARY_VALUE);
|
||||
times = 1;
|
||||
clientCodegen.setFullJavaUtil(Boolean.valueOf(FULL_JAVA_UTIL_VALUE));
|
||||
times = 1;
|
||||
}};
|
||||
}
|
||||
}
|
@ -86,9 +86,7 @@ public class Generator {
|
||||
}
|
||||
|
||||
if (opts.getOptions() != null) {
|
||||
for (Map.Entry<String, String> entry : opts.getOptions().entrySet()) {
|
||||
codegenConfig.additionalProperties().put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
codegenConfig.additionalProperties().putAll(opts.getOptions());
|
||||
}
|
||||
|
||||
codegenConfig.setOutputDir(outputFolder);
|
||||
|
Loading…
Reference in New Issue
Block a user