forked from loafle/openapi-generator-original
[test] Removes jmockit in favor of mockito (#5063)
* [test] Removes jmockit in favor of mockito We use mockito in many tests. This removes jmockit which is run as a javaagent in favor of Mockito which is not. This work is in preparation for applying some static analysis tools, while evaluating others such as Jacoco. I'm also look at ways to improve build times while also decreasing "ramp up time" for contributions from the community. Reducing the number of mock frameworks and dependencies is a step toward that goal. * Rename method in new.sh * [cli] Mock the generate task
This commit is contained in:
@@ -109,9 +109,9 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jmockit</groupId>
|
||||
<artifactId>jmockit</artifactId>
|
||||
<!-- <version>${jmockit-version}</version> -->
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -28,22 +28,17 @@ import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import org.openapitools.codegen.ClientOptInput;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.DefaultGenerator;
|
||||
import org.openapitools.codegen.GeneratorNotFoundException;
|
||||
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* User: lanwen Date: 24.03.15 Time: 20:22
|
||||
*/
|
||||
|
||||
@Command(name = "generate", description = "Generate code with the specified generator.")
|
||||
public class Generate implements Runnable {
|
||||
|
||||
// private static final Logger LOGGER = LoggerFactory.getLogger(Generate.class);
|
||||
CodegenConfigurator configurator;
|
||||
Generator generator;
|
||||
|
||||
@Option(name = {"-v", "--verbose"}, description = "verbose mode")
|
||||
private Boolean verbose;
|
||||
@@ -257,13 +252,18 @@ public class Generate implements Runnable {
|
||||
.ifPresent(FilterAttachable::clearAllFilters);
|
||||
}
|
||||
|
||||
// attempt to read from config file
|
||||
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configFile);
|
||||
|
||||
// if a config file wasn't specified or we were unable to read it
|
||||
// this initial check allows for field-level package private injection (for unit testing)
|
||||
if (configurator == null) {
|
||||
// createa a fresh configurator
|
||||
configurator = new CodegenConfigurator();
|
||||
if (configFile != null && configFile.length() > 0) {
|
||||
// attempt to load from configFile
|
||||
configurator = CodegenConfigurator.fromFile(configFile);
|
||||
}
|
||||
|
||||
// if a config file wasn't specified, or we were unable to read it
|
||||
if (configurator == null) {
|
||||
// create a fresh configurator
|
||||
configurator = new CodegenConfigurator();
|
||||
}
|
||||
}
|
||||
|
||||
// now override with any specified parameters
|
||||
@@ -413,7 +413,14 @@ public class Generate implements Runnable {
|
||||
|
||||
try {
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
new DefaultGenerator().opts(clientOptInput).generate();
|
||||
|
||||
// this null check allows us to inject for unit testing.
|
||||
if (generator == null) {
|
||||
generator = new DefaultGenerator();
|
||||
}
|
||||
|
||||
generator.opts(clientOptInput);
|
||||
generator.generate();
|
||||
} catch (GeneratorNotFoundException e) {
|
||||
System.err.println(e.getMessage());
|
||||
System.err.println("[error] Check the spelling of the generator's name and try again.");
|
||||
|
||||
@@ -17,536 +17,417 @@
|
||||
|
||||
package org.openapitools.codegen.cmd;
|
||||
|
||||
import org.openapitools.codegen.ClientOptInput;
|
||||
import org.openapitools.codegen.DefaultGenerator;
|
||||
import org.openapitools.codegen.OpenAPIGenerator;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import mockit.Expectations;
|
||||
import mockit.FullVerifications;
|
||||
import mockit.Injectable;
|
||||
import mockit.Mocked;
|
||||
import mockit.Verifications;
|
||||
import io.airlift.airline.Cli;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.mockito.MockSettings;
|
||||
import org.openapitools.codegen.DefaultGenerator;
|
||||
import org.openapitools.codegen.Generator;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.mockito.Answers.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.testng.Assert.fail;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class GenerateTest {
|
||||
|
||||
@Mocked
|
||||
CodegenConfigurator configurator;
|
||||
protected MockSettings mockSettings = withSettings().useConstructor().defaultAnswer(CALLS_REAL_METHODS);
|
||||
private Generator generator;
|
||||
private CodegenConfigurator configurator;
|
||||
private Path outputDirectory;
|
||||
|
||||
@Injectable
|
||||
ClientOptInput clientOptInput;
|
||||
@AfterMethod
|
||||
public void afterEachTest() {
|
||||
outputDirectory.toFile().deleteOnExit();
|
||||
}
|
||||
|
||||
@Mocked
|
||||
DefaultGenerator generator;
|
||||
@BeforeMethod
|
||||
public void beforeEachTest() throws IOException {
|
||||
outputDirectory = Files.createTempDirectory("GenerateTest");
|
||||
generator = mock(DefaultGenerator.class);
|
||||
when(generator.generate()).thenReturn(new ArrayList<>());
|
||||
|
||||
@Test
|
||||
public void testVerbose() throws Exception {
|
||||
setupAndRunGenericTest("-v");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setVerbose(true);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--verbose");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setVerbose(true);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
configurator = mock(CodegenConfigurator.class, mockSettings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredArgs_ShortArgs() throws Exception {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", false, null, "-p", "foo=bar");
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addAdditionalProperty("foo", "bar");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredArgs_LongArgs() throws Exception {
|
||||
setupAndRunTest("--input-spec", "src/test/resources/swagger.yaml", "--generator-name", "java", "--output",
|
||||
"src/main/java", false, null);
|
||||
new FullVerifications() {
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateDir() throws Exception {
|
||||
|
||||
final String templateDir = "src/main/resources/customTemplates";
|
||||
|
||||
setupAndRunGenericTest("--template-dir", templateDir);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setTemplateDir(templateDir);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("-t", templateDir);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setTemplateDir(templateDir);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuth() throws Exception {
|
||||
|
||||
final String auth = "hello:world";
|
||||
|
||||
setupAndRunGenericTest("--auth", auth);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setAuth(auth);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("-a", auth);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setAuth(auth);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest();
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setAuth(anyString);
|
||||
times = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigJson() throws Exception {
|
||||
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.json", "-c", "config.json");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.json", "--config", "config.json");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigYaml() throws Exception {
|
||||
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.yaml", "-c", "config.yaml");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.yaml", "--config", "config.yaml");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipOverwrite() throws Exception {
|
||||
|
||||
setupAndRunGenericTest("-s");
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setSkipOverwrite(true);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--skip-overwrite");
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setSkipOverwrite(true);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictSpec() throws Exception {
|
||||
|
||||
setupAndRunGenericTest("--strict-spec", "true");
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setStrictSpecBehavior(true);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--strict-spec", "false");
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setStrictSpecBehavior(false);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackageName() throws Exception {
|
||||
final String value = "io.foo.bar.baz";
|
||||
setupAndRunGenericTest("--package-name", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setPackageName(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApiPackage() throws Exception {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--api-package", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setApiPackage(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModelPackage() throws Exception {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--model-package", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setModelPackage(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstantiationTypes() throws Exception {
|
||||
|
||||
setupAndRunGenericTest("--instantiation-types", "hello=world,key=,foo=bar,key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addInstantiationType("hello", "world");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("key", "");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--instantiation-types", "hello=world", "--instantiation-types",
|
||||
"key=", "--instantiation-types", "foo=bar", "--instantiation-types", "key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addInstantiationType("hello", "world");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("key", "");
|
||||
times = 1;
|
||||
configurator.addInstantiationType("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeMappings() throws Exception {
|
||||
setupAndRunGenericTest("--type-mappings", "hello=world,key=,foo=bar,key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addTypeMapping("hello", "world");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("key", "");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--type-mappings", "hello=world", "--type-mappings", "key=",
|
||||
"--type-mappings", "foo=bar", "--type-mappings", "key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addTypeMapping("hello", "world");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("key", "");
|
||||
times = 1;
|
||||
configurator.addTypeMapping("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdditionalProperties() throws Exception {
|
||||
public void testAdditionalPropertiesLong() {
|
||||
setupAndRunGenericTest("--additional-properties", "hello=world,key=,foo=bar,key2");
|
||||
verify(configurator).addAdditionalProperty("hello", "world");
|
||||
verify(configurator).addAdditionalProperty("foo", "bar");
|
||||
verify(configurator).addAdditionalProperty("key", "");
|
||||
verify(configurator).addAdditionalProperty("key2", "");
|
||||
}
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addAdditionalProperty("hello", "world");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("key", "");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testAdditionalPropertiesLongMultiple() {
|
||||
setupAndRunGenericTest("--additional-properties", "hello=world", "--additional-properties",
|
||||
"key=", "--additional-properties", "foo=bar", "--additional-properties", "key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addAdditionalProperty("hello", "world");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("key", "");
|
||||
times = 1;
|
||||
configurator.addAdditionalProperty("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
verify(configurator).addAdditionalProperty("hello", "world");
|
||||
verify(configurator).addAdditionalProperty("foo", "bar");
|
||||
verify(configurator).addAdditionalProperty("key", "");
|
||||
verify(configurator).addAdditionalProperty("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLanguageSpecificPrimitives() throws Exception {
|
||||
setupAndRunGenericTest("--language-specific-primitives", "foo,,bar",
|
||||
"--language-specific-primitives", "hello,world");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addLanguageSpecificPrimitive("foo");
|
||||
times = 1;
|
||||
configurator.addLanguageSpecificPrimitive("bar");
|
||||
times = 1;
|
||||
configurator.addLanguageSpecificPrimitive("hello");
|
||||
times = 1;
|
||||
configurator.addLanguageSpecificPrimitive("world");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportMappings() throws Exception {
|
||||
setupAndRunGenericTest("--import-mappings", "hello=world,key=,foo=bar,key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addImportMapping("hello", "world");
|
||||
times = 1;
|
||||
configurator.addImportMapping("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addImportMapping("key", "");
|
||||
times = 1;
|
||||
configurator.addImportMapping("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
|
||||
setupAndRunGenericTest("--import-mappings", "hello=world", "--import-mappings", "key=",
|
||||
"--import-mappings", "foo=bar", "--import-mappings", "key2");
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.addImportMapping("hello", "world");
|
||||
times = 1;
|
||||
configurator.addImportMapping("foo", "bar");
|
||||
times = 1;
|
||||
configurator.addImportMapping("key", "");
|
||||
times = 1;
|
||||
configurator.addImportMapping("key2", "");
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokerPackage() throws Exception {
|
||||
public void testApiPackage() {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--invoker-package", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setInvokerPackage(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
setupAndRunGenericTest("--api-package", value);
|
||||
verify(configurator).setApiPackage(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupId() throws Exception {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--group-id", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setGroupId(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArtifactId() throws Exception {
|
||||
public void testArtifactId() {
|
||||
final String value = "awesome-api";
|
||||
setupAndRunGenericTest("--artifact-id", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setArtifactId(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArtifactVersion() throws Exception {
|
||||
final String value = "1.2.3";
|
||||
setupAndRunGenericTest("--artifact-version", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setArtifactVersion(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLibrary() throws Exception {
|
||||
final String value = "library1";
|
||||
setupAndRunGenericTest("--library", value);
|
||||
|
||||
new FullVerifications() {
|
||||
{
|
||||
configurator.setLibrary(value);
|
||||
times = 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void setupAndRunTest(String specFlag, final String spec, String langFlag,
|
||||
final String lang, String outputDirFlag, final String outputDir,
|
||||
boolean configuratorFromFile, final String configFile, String... additionalParameters) {
|
||||
final String[] commonArgs =
|
||||
{"generate", langFlag, lang, outputDirFlag, outputDir, specFlag, spec};
|
||||
|
||||
String[] argsToUse = ArrayUtils.addAll(commonArgs, additionalParameters);
|
||||
|
||||
if (configuratorFromFile) {
|
||||
|
||||
new Expectations() {
|
||||
{
|
||||
CodegenConfigurator.fromFile(configFile);
|
||||
times = 1;
|
||||
result = configurator;
|
||||
}
|
||||
};
|
||||
|
||||
} else {
|
||||
new Expectations() {
|
||||
{
|
||||
CodegenConfigurator.fromFile(anyString);
|
||||
result = null;
|
||||
|
||||
new CodegenConfigurator();
|
||||
times = 1;
|
||||
result = configurator;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
new Expectations() {
|
||||
{
|
||||
|
||||
configurator.toClientOptInput();
|
||||
times = 1;
|
||||
result = clientOptInput;
|
||||
|
||||
new DefaultGenerator();
|
||||
times = 1;
|
||||
result = generator;
|
||||
|
||||
generator.opts(clientOptInput);
|
||||
times = 1;
|
||||
result = generator;
|
||||
|
||||
generator.generate();
|
||||
times = 1;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
OpenAPIGenerator.main(argsToUse);
|
||||
|
||||
new Verifications() {
|
||||
{
|
||||
configurator.setGeneratorName(lang);
|
||||
times = 1;
|
||||
configurator.setInputSpec(spec);
|
||||
times = 1;
|
||||
configurator.setOutputDir(outputDir);
|
||||
}
|
||||
};
|
||||
verify(configurator).setArtifactId(value);
|
||||
}
|
||||
|
||||
private void setupAndRunGenericTest(String... additionalParameters) {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", false, null,
|
||||
additionalParameters);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
private void setupAndRunTest(String specFlag, final String spec, String langFlag,
|
||||
final String lang, String outputDirFlag, final String outputDir,
|
||||
boolean configuratorFromFile, final String configFile, String... additionalParameters) {
|
||||
final String[] commonArgs =
|
||||
{"generate", langFlag, lang, outputDirFlag, outputDir, specFlag, spec};
|
||||
|
||||
String[] argsToUse = ArrayUtils.addAll(commonArgs, additionalParameters);
|
||||
|
||||
Cli.CliBuilder<Runnable> builder =
|
||||
Cli.<Runnable>builder("openapi-generator-cli")
|
||||
.withCommands(Generate.class);
|
||||
|
||||
Generate generate = (Generate) builder.build().parse(argsToUse);
|
||||
|
||||
generate.configurator = configurator;
|
||||
generate.generator = generator;
|
||||
|
||||
try {
|
||||
generate.run();
|
||||
} finally {
|
||||
verify(configurator).setInputSpec(spec);
|
||||
verify(configurator).setGeneratorName(lang);
|
||||
verify(configurator).setOutputDir(outputDir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArtifactVersion() {
|
||||
final String value = "1.2.3";
|
||||
setupAndRunGenericTest("--artifact-version", value);
|
||||
|
||||
verify(configurator).setArtifactVersion(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthLong() {
|
||||
final String auth = "hello:world";
|
||||
setupAndRunGenericTest("--auth", auth);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verify(configurator).setAuth(auth);
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthShort() {
|
||||
final String auth = "hello:world";
|
||||
setupAndRunGenericTest("-a", auth);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verify(configurator).setAuth(auth);
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthUnspecified() {
|
||||
setupAndRunGenericTest();
|
||||
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verify(configurator, never()).setAuth(anyString());
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigJsonLong() {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.json", "--config", "config.json");
|
||||
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigJsonShort() {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.json", "-c", "config.json");
|
||||
|
||||
// on top of those in setupAndRunTest
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigYamlLong() {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.yaml", "--config", "config.yaml");
|
||||
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigYamlShort() {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", true,
|
||||
"config.yaml", "-c", "config.yaml");
|
||||
|
||||
// on top of those in setupAndRunTest
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupId() {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--group-id", value);
|
||||
verify(configurator).setGroupId(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportMappingsLong() {
|
||||
setupAndRunGenericTest("--import-mappings", "hello=world,key=,foo=bar,key2");
|
||||
|
||||
verify(configurator).addImportMapping("hello", "world");
|
||||
verify(configurator).addImportMapping("foo", "bar");
|
||||
verify(configurator).addImportMapping("key", "");
|
||||
verify(configurator).addImportMapping("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImportMappingsLongMultiple() {
|
||||
setupAndRunGenericTest("--import-mappings", "hello=world", "--import-mappings", "key=",
|
||||
"--import-mappings", "foo=bar", "--import-mappings", "key2");
|
||||
|
||||
verify(configurator).addImportMapping("hello", "world");
|
||||
verify(configurator).addImportMapping("foo", "bar");
|
||||
verify(configurator).addImportMapping("key", "");
|
||||
verify(configurator).addImportMapping("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstantiationTypesLong() {
|
||||
setupAndRunGenericTest("--instantiation-types", "hello=world,key=,foo=bar,key2");
|
||||
verify(configurator).addInstantiationType("hello", "world");
|
||||
verify(configurator).addInstantiationType("foo", "bar");
|
||||
verify(configurator).addInstantiationType("key", "");
|
||||
verify(configurator).addInstantiationType("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstantiationTypesLongMultiple() {
|
||||
setupAndRunGenericTest("--instantiation-types", "hello=world", "--instantiation-types",
|
||||
"key=", "--instantiation-types", "foo=bar", "--instantiation-types", "key2");
|
||||
verify(configurator).addInstantiationType("hello", "world");
|
||||
verify(configurator).addInstantiationType("foo", "bar");
|
||||
verify(configurator).addInstantiationType("key", "");
|
||||
verify(configurator).addInstantiationType("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokerPackage() {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--invoker-package", value);
|
||||
verify(configurator).setInvokerPackage(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLanguageSpecificPrimitives() {
|
||||
setupAndRunGenericTest("--language-specific-primitives", "foo,,bar",
|
||||
"--language-specific-primitives", "hello,world");
|
||||
|
||||
verify(configurator).addLanguageSpecificPrimitive("foo");
|
||||
verify(configurator).addLanguageSpecificPrimitive("bar");
|
||||
verify(configurator).addLanguageSpecificPrimitive("hello");
|
||||
verify(configurator).addLanguageSpecificPrimitive("world");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLibrary() {
|
||||
final String value = "feign";
|
||||
setupAndRunGenericTest("--library", value);
|
||||
verify(configurator).setLibrary(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModelPackage() {
|
||||
final String value = "io.foo.bar.api";
|
||||
setupAndRunGenericTest("--model-package", value);
|
||||
verify(configurator).setModelPackage(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackageName() {
|
||||
final String value = "io.foo.bar.baz";
|
||||
setupAndRunGenericTest("--package-name", value);
|
||||
verify(configurator).setPackageName(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredArgs_LongArgs() {
|
||||
setupAndRunTest("--input-spec", "src/test/resources/swagger.yaml", "--generator-name", "java", "--output",
|
||||
"src/main/java", false, null);
|
||||
|
||||
// on top of those in setupAndRunTest:
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredArgs_ShortArgs() {
|
||||
setupAndRunTest("-i", "src/test/resources/swagger.yaml", "-g", "java", "-o", "src/main/java", false, null, "-p", "foo=bar");
|
||||
|
||||
verify(configurator).addAdditionalProperty("foo", "bar");
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipOverwriteLong() {
|
||||
setupAndRunGenericTest("--skip-overwrite");
|
||||
verify(configurator).setSkipOverwrite(true);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipOverwriteShort() {
|
||||
setupAndRunGenericTest("-s");
|
||||
verify(configurator).setSkipOverwrite(true);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictSpecFalse() {
|
||||
setupAndRunGenericTest("--strict-spec", "false");
|
||||
verify(configurator).setStrictSpecBehavior(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictSpecTrue() {
|
||||
setupAndRunGenericTest("--strict-spec", "true");
|
||||
verify(configurator).setStrictSpecBehavior(true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
@Test
|
||||
public void testTemplateDirLong() {
|
||||
final String templateDir = "src/main/resources/customTemplates";
|
||||
File f = outputDirectory.resolve(templateDir).toFile();
|
||||
try {
|
||||
f.mkdirs();
|
||||
setupAndRunGenericTest("--template-dir", f.getAbsolutePath());
|
||||
verify(configurator).setTemplateDir(f.getAbsolutePath());
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
} finally {
|
||||
if(!f.delete()) {
|
||||
System.out.println("Directory didn't delete. You can ignore this.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Template directory src/main/resources/customTemplates does not exist.")
|
||||
public void testTemplateDirMustExist() {
|
||||
final String templateDir = "src/main/resources/customTemplates";
|
||||
setupAndRunGenericTest("-t", templateDir);
|
||||
fail("Expected exception was not thrown.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
@Test
|
||||
public void testTemplateDirShort() {
|
||||
final String templateDir = "src/main/resources/customTemplates";
|
||||
File f = outputDirectory.resolve(templateDir).toFile();
|
||||
try {
|
||||
f.mkdirs();
|
||||
setupAndRunGenericTest("-t", f.getAbsolutePath());
|
||||
} finally {
|
||||
verify(configurator).setTemplateDir(f.getAbsolutePath());
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
if(!f.delete()) {
|
||||
System.out.println("Directory didn't delete. You can ignore this.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeMappingsLong() {
|
||||
setupAndRunGenericTest("--type-mappings", "hello=world,key=,foo=bar,key2");
|
||||
verify(configurator).addTypeMapping("hello", "world");
|
||||
verify(configurator).addTypeMapping("foo", "bar");
|
||||
verify(configurator).addTypeMapping("key", "");
|
||||
verify(configurator).addTypeMapping("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeMappingsLongMultiple() {
|
||||
setupAndRunGenericTest("--type-mappings", "hello=world", "--type-mappings", "key=",
|
||||
"--type-mappings", "foo=bar", "--type-mappings", "key2");
|
||||
verify(configurator).addTypeMapping("hello", "world");
|
||||
verify(configurator).addTypeMapping("foo", "bar");
|
||||
verify(configurator).addTypeMapping("key", "");
|
||||
verify(configurator).addTypeMapping("key2", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerboseLong() {
|
||||
setupAndRunGenericTest("--verbose");
|
||||
verify(configurator).setVerbose(true);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerboseShort() {
|
||||
setupAndRunGenericTest("-v");
|
||||
verify(configurator).setVerbose(true);
|
||||
verify(configurator).toClientOptInput();
|
||||
verify(configurator).toContext();
|
||||
verifyNoMoreInteractions(configurator);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user