Java Code Examples for org.openapitools.codegen.config.CodegenConfigurator#toClientOptInput()

The following examples show how to use org.openapitools.codegen.config.CodegenConfigurator#toClientOptInput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YamlGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePingOtherOutputFile() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(OpenAPIYamlGenerator.OUTPUT_NAME, "ping.yaml");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("openapi-yaml")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 5);
    TestUtils.ensureContainsFile(files, output, "ping.yaml");
    TestUtils.ensureContainsFile(files, output, "README.md");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");

    output.deleteOnExit();
}
 
Example 2
Source File: YamlGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePing() throws Exception {
    Map<String, Object> properties = new HashMap<>();

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("openapi-yaml")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();
    Assert.assertEquals(files.size(), 5);
    TestUtils.ensureContainsFile(files, output, "openapi/openapi.yaml");
    TestUtils.ensureContainsFile(files, output, "README.md");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");

    output.deleteOnExit();
}
 
Example 3
Source File: AsciidocGeneratorTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateIndexAsciidocMarkupFileWithAsciidocGenerator() throws Exception {

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("asciidoc")
            .setInputSpec("src/test/resources/3_0/ping.yaml").setOutputDir(output.getAbsolutePath())
            .addAdditionalProperty(AsciidocDocumentationCodegen.SNIPPET_DIR, "MY-SNIPPET-DIR")
            .addAdditionalProperty(AsciidocDocumentationCodegen.SPEC_DIR, "MY-SPEC-DIR");

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    generator.setGenerateMetadata(false);
    List<File> generatedFiles = generator.opts(clientOptInput).generate();
    TestUtils.ensureContainsFile(generatedFiles, output, "index.adoc");
}
 
Example 4
Source File: JavaJAXRSSpecServerCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePingAlternativeLocation1() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(JavaJAXRSSpecServerCodegen.OPEN_API_SPEC_FILE_LOCATION, "src/main/resources/META-INF/openapi.yaml");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("jaxrs-spec")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    validateJavaSourceFiles(files);

    TestUtils.ensureContainsFile(files, output, "src/main/resources/META-INF/openapi.yaml");

    output.deleteOnExit();
}
 
Example 5
Source File: JavaJAXRSSpecServerCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePingAlternativeLocation2() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(JavaJAXRSSpecServerCodegen.OPEN_API_SPEC_FILE_LOCATION, "openapi.yml");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("jaxrs-spec")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    validateJavaSourceFiles(files);
    TestUtils.ensureContainsFile(files, output, "openapi.yml");

    output.deleteOnExit();
}
 
Example 6
Source File: JavaJAXRSSpecServerCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateApiWithPreceedingPathParameter_issue1347() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(JavaJAXRSSpecServerCodegen.OPEN_API_SPEC_FILE_LOCATION, "openapi.yml");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("jaxrs-spec")
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/issue_1347.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator(false);
    List<File> files = generator.opts(clientOptInput).generate();

    validateJavaSourceFiles(files);

    TestUtils.ensureContainsFile(files, output, "openapi.yml");
    TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/DefaultApi.java");

    output.deleteOnExit();
}
 
Example 7
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdkHttpClient() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setLibrary(JavaClientCodegen.NATIVE)
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 28);
    validateJavaSourceFiles(files);

    TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/DefaultApi.java"),
            "public class DefaultApi",
            "import java.net.http.HttpClient;",
            "import java.net.http.HttpRequest;",
            "import java.net.http.HttpResponse;");

    TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/ApiClient.java"),
            "public class ApiClient",
            "import java.net.http.HttpClient;",
            "import java.net.http.HttpRequest;");
}
 
Example 8
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void testJdkHttpAsyncClient() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
    properties.put(JavaClientCodegen.ASYNC_NATIVE, true);

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setLibrary(JavaClientCodegen.NATIVE)
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 30);
    validateJavaSourceFiles(files);

    Path defaultApi = Paths.get(output + "/src/main/java/xyz/abcdef/api/PingApi.java");
    TestUtils.assertFileContains(defaultApi,
            "public class PingApi",
            "import java.net.http.HttpClient;",
            "import java.net.http.HttpRequest;",
            "import java.net.http.HttpResponse;",
            "import java.util.concurrent.CompletableFuture;");

    Path apiClient = Paths.get(output + "/src/main/java/xyz/abcdef/ApiClient.java");
    TestUtils.assertFileContains(apiClient,
            "public class ApiClient",
            "import java.net.http.HttpClient;",
            "import java.net.http.HttpRequest;");
}
 
Example 9
Source File: ScalaAkkaClientCodegenTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test(description = "override only mainPackage")
public void mainPackageTest() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("mainPackage", "hello.world");

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final DefaultCodegen codegen = new ScalaAkkaClientCodegen();
    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName(codegen.getName())
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/scala_reserved_words.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator(false);

    generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
    generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");

    Generator gen = generator.opts(clientOptInput);
    List<File> files = gen.generate();

    Assert.assertEquals(files.size(), 15);

    TestUtils.ensureContainsFile(files, output, "src/main/scala/hello/world/model/SomeObj.scala");
    TestUtils.ensureContainsFile(files, output, "src/main/scala/hello/world/core/ApiSettings.scala");
    TestUtils.ensureContainsFile(files, output, "src/main/scala/hello/world/api/PingApi.scala");
}
 
Example 10
Source File: DefaultGeneratorTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void dryRunWithModelsOnly() throws IOException {
    Path target = Files.createTempDirectory("test");
    File output = target.toFile();
    try {
        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("java")
                .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
                .setOutputDir(target.toAbsolutePath().toString());

        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator(true);

        generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "false");

        List<File> files = generator.opts(clientOptInput).generate();

        Assert.assertEquals(files.size(), 1);
        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/SomeObj.java");
    } finally {
        output.delete();
    }
}
 
Example 11
Source File: MyclientcodegenGeneratorTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void launchCodeGenerator() {
  // to understand how the 'openapi-generator-cli' module is using 'CodegenConfigurator', have a look at the 'Generate' class:
  // https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java 
  final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("myClientCodegen") // use this codegen library
            .setInputSpec("../../../modules/openapi-generator/src/test/resources/2_0/petstore.yaml") // sample OpenAPI file
            // .setInputSpec("https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml") // or from the server
            .setOutputDir("out/myClientCodegen"); // output directory

  final ClientOptInput clientOptInput = configurator.toClientOptInput();
  DefaultGenerator generator = new DefaultGenerator();
  generator.opts(clientOptInput).generate();
}
 
Example 12
Source File: DefaultGeneratorTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void testIgnoreFileProcessing() throws IOException {
    Path target = Files.createTempDirectory("test");
    File output = target.toFile();
    try {
        List<String> ignoreFile = Arrays.asList(
                ".travis.yml",
                "build.sbt",
                "src/main/AndroidManifest.xml",
                "pom.xml",
                "src/test/**",
                "src/main/java/org/openapitools/client/api/UserApi.java"
        );
        File ignorePath = new File(output, ".openapi-generator-ignore");
        Files.write(ignorePath.toPath(),
                String.join(System.lineSeparator(), ignoreFile).getBytes(StandardCharsets.UTF_8),
                StandardOpenOption.CREATE);

        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("java")
                .setInputSpec("src/test/resources/3_0/petstore.yaml")
                .setOutputDir(target.toAbsolutePath().toString());

        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator(false);

        generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "false");

        List<File> files = generator.opts(clientOptInput).generate();

        Assert.assertEquals(files.size(), 44);

        // Check expected generated files
        // api sanity check
        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/api/PetApi.java");
        Assert.assertTrue(new File(output, "src/main/java/org/openapitools/client/api/PetApi.java").exists());

        // model sanity check
        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/Category.java");
        Assert.assertTrue(new File(output, "src/main/java/org/openapitools/client/model/Category.java").exists());

        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/ModelApiResponse.java");
        Assert.assertTrue(new File(output, "src/main/java/org/openapitools/client/model/ModelApiResponse.java").exists());

        // supporting files sanity check
        TestUtils.ensureContainsFile(files, output, "build.gradle");
        Assert.assertTrue(new File(output, "build.gradle").exists());

        TestUtils.ensureContainsFile(files, output, "api/openapi.yaml");
        Assert.assertTrue(new File(output, "build.gradle").exists());

        // Check excluded files
        TestUtils.ensureDoesNotContainsFile(files, output, ".travis.yml");
        Assert.assertFalse(new File(output, ".travis.yml").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, "build.sbt");
        Assert.assertFalse(new File(output, "build.sbt").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, "src/main/AndroidManifest.xml");
        Assert.assertFalse(new File(output, "src/main/AndroidManifest.xml").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, "pom.xml");
        Assert.assertFalse(new File(output, "pom.xml").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, "src/test/java/org/openapitools/client/model/CategoryTest.java");
        Assert.assertFalse(new File(output, "src/test/java/org/openapitools/client/model/CategoryTest.java").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, "src/main/java/org/openapitools/client/api/UserApi.java");
        Assert.assertFalse(new File(output, "src/main/java/org/openapitools/client/api/UserApi.java").exists());
    } finally {
        output.delete();
    }
}
 
Example 13
Source File: DefaultGeneratorTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void supportCustomTemplateEngine() throws IOException {
    Path target = Files.createTempDirectory("test");
    File templateDir = new File(target.toFile(), "template");
    Files.createDirectory(templateDir.toPath());
    File output = new File(target.toFile(), "out");
    Files.createDirectory(output.toPath());

    try {
        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("jmeter")
                .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
                .setSkipOverwrite(false)
                .setTemplateDir(templateDir.toPath().toAbsolutePath().toString())
                .setTemplatingEngineName("handlebars")
                .setOutputDir(output.toPath().toAbsolutePath().toString());

        // Create custom template directory
        Files.copy(
                Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("templating/templates/jmeter/api.hbs")),
                new File(templateDir, "api.hbs").toPath());
        Files.copy(
                Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("templating/templates/jmeter/testdata-localhost.hbs")),
                new File(templateDir, "testdata-localhost.hbs").toPath());

        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator(false);

        List<File> files = generator.opts(clientOptInput).generate();

        Assert.assertEquals(files.size(), 5);

        // Check API is written and Test is not
        TestUtils.ensureContainsFile(files, output, "PingApi.jmx");
        Assert.assertTrue(new File(output, "PingApi.jmx").exists());

        TestUtils.ensureContainsFile(files, output, "PingApi.csv");
        Assert.assertTrue(new File(output, "PingApi.csv").exists());

        TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore");
        Assert.assertTrue(new File(output, ".openapi-generator-ignore").exists());

        TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");
        Assert.assertTrue(new File(output, ".openapi-generator/VERSION").exists());

        TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES");
        Assert.assertTrue(new File(output, ".openapi-generator/FILES").exists());

        TestUtils.assertFileContains(java.nio.file.Paths.get(output + "/PingApi.jmx"), "PingApi Test Plan via Handlebars");
        TestUtils.assertFileContains(java.nio.file.Paths.get(output + "/PingApi.csv"),
                "testCase,httpStatusCode,someObj",
                "Success,200,0");
    } finally {
        output.delete();
    }
}
 
Example 14
Source File: ScalaAkkaClientCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test(description = "validate codegen java8 output")
public void codeGenerationJava8Test() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("mainPackage", "hello.world");
    properties.put("dateLibrary", "java8");

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final DefaultCodegen codegen = new ScalaAkkaClientCodegen();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName(codegen.getName())
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/scala_reserved_words.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();

    generator.setGenerateMetadata(false);

    generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 1);

    TestUtils.ensureContainsFile(files, output, "src/main/scala/hello/world/model/SomeObj.scala");

    File someObj = files.stream().filter(f -> f.getName().equals("SomeObj.scala"))
            .findFirst().get();

    byte[] fileContents = Files.readAllBytes(someObj.toPath());
    Assert.assertEquals(
            new String(fileContents, StandardCharsets.UTF_8),
            Resources.toString(Resources.getResource("codegen/scala/JavaTimeObj.scala.txt"), StandardCharsets.UTF_8));
}
 
Example 15
Source File: ScalaAkkaClientCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test(description = "validate codegen joda output")
public void codeGenerationTest() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("mainPackage", "hello.world");
    properties.put("dateLibrary", "joda");

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final DefaultCodegen codegen = new ScalaAkkaClientCodegen();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName(codegen.getName())
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/scala_reserved_words.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();

    generator.setGenerateMetadata(false);

    generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 1);

    TestUtils.ensureContainsFile(files, output, "src/main/scala/hello/world/model/SomeObj.scala");

    File someObj = files.stream().filter(f -> f.getName().equals("SomeObj.scala"))
            .findFirst().get();

    byte[] fileContents = Files.readAllBytes(someObj.toPath());
    Assert.assertEquals(
            new String(fileContents, StandardCharsets.UTF_8),
            Resources.toString(Resources.getResource("codegen/scala/SomeObj.scala.txt"), StandardCharsets.UTF_8));
}
 
Example 16
Source File: DefaultGeneratorTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testFilesAreNeverOverwritten() throws IOException {
    Path target = Files.createTempDirectory("test");
    File output = target.toFile();
    try {
        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("java")
                .setInputSpec("src/test/resources/3_0/petstore.yaml")
                .setSkipOverwrite(false)
                .setOutputDir(target.toAbsolutePath().toString());

        // Create "existing" files
        String apiTestRelativePath = "src/test/java/org/openapitools/client/api/PetApiTest.java";
        String modelTestRelativePath = "src/test/java/org/openapitools/client/model/CategoryTest.java";

        File apiTestFile = new File(output, apiTestRelativePath);
        new File(apiTestFile.getParent()).mkdirs();
        Files.write(apiTestFile.toPath(),
                "empty".getBytes(StandardCharsets.UTF_8),
                StandardOpenOption.CREATE);

        File modelTestFile = new File(output, modelTestRelativePath);
        new File(modelTestFile.getParent()).mkdirs();
        Files.write(modelTestFile.toPath(),
                "empty".getBytes(StandardCharsets.UTF_8),
                StandardOpenOption.CREATE);

        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator(false);

        generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "true");

        List<File> files = generator.opts(clientOptInput).generate();

        Assert.assertEquals(files.size(), 20);

        // Check API is written and Test is not
        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/api/PetApi.java");
        Assert.assertTrue(new File(output, "src/main/java/org/openapitools/client/api/PetApi.java").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, apiTestRelativePath);
        Assert.assertTrue(apiTestFile.exists());
        String apiTestContents = Files.readAllLines(apiTestFile.toPath()).get(0);
        Assert.assertEquals(apiTestContents, "empty", "Expected test file to retain original contents.");

        // Check Model is written and Test is not
        TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/Category.java");
        Assert.assertTrue(new File(output, "src/test/java/org/openapitools/client/model/CategoryTest.java").exists());

        TestUtils.ensureDoesNotContainsFile(files, output, modelTestRelativePath);
        Assert.assertTrue(modelTestFile.exists());
        String modelTestContents = Files.readAllLines(modelTestFile.toPath()).get(0);
        Assert.assertEquals(modelTestContents, "empty", "Expected test file to retain original contents.");
    } finally {
        output.delete();
    }
}
 
Example 17
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * See https://github.com/OpenAPITools/openapi-generator/issues/3589
 */
@Test
public void testImportMapping() throws IOException {

    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

    Map<String, String> importMappings = new HashMap<>();
    importMappings.put("TypeAlias", "foo.bar.TypeAlias");

    File output = Files.createTempDirectory("test").toFile();
    output.deleteOnExit();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setLibrary(JavaClientCodegen.RESTEASY)
            .setAdditionalProperties(properties)
            .setImportMappings(importMappings)
            .setInputSpec("src/test/resources/3_0/type-alias.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    Assert.assertEquals(clientOptInput.getConfig().importMapping().get("TypeAlias"), "foo.bar.TypeAlias");

    DefaultGenerator generator = new DefaultGenerator();
    generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
    generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
    generator.setGenerateMetadata(false);
    List<File> files = generator.opts(clientOptInput).generate();
    files.forEach(File::deleteOnExit);

    Assert.assertEquals(files.size(), 1);
    TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/ParentType.java");

    String parentTypeContents = "";
    try {
        File file = files.stream().filter(f -> f.getName().endsWith("ParentType.java")).findFirst().get();
        parentTypeContents = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    } catch (IOException ignored) {

    }

    final Pattern FIELD_PATTERN = Pattern.compile(".* private (.*?) typeAlias;.*", Pattern.DOTALL);
    Matcher fieldMatcher = FIELD_PATTERN.matcher(parentTypeContents);
    Assert.assertTrue(fieldMatcher.matches());

    // this is the type of the field 'typeAlias'. With a working importMapping it should
    // be 'foo.bar.TypeAlias' or just 'TypeAlias'
    Assert.assertEquals(fieldMatcher.group(1), "foo.bar.TypeAlias");
}
 
Example 18
Source File: DefaultGeneratorTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void dryRunWithSupportFilesSelections() throws IOException {
    Path target = Files.createTempDirectory("test");
    File output = target.toFile();
    try {
        final CodegenConfigurator configurator = new CodegenConfigurator()
                .setGeneratorName("java")
                .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
                .setOutputDir(target.toAbsolutePath().toString());

        final ClientOptInput clientOptInput = configurator.toClientOptInput();
        DefaultGenerator generator = new DefaultGenerator(true);

        generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
        generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "false");

        List<String> filesToGenerate = Arrays.asList(
                "pom.xml",
                ".travis.yml",
                ".gitignore",
                "git_push.sh"
        );
        GlobalSettings.setProperty(CodegenConstants.SUPPORTING_FILES, String.join(",", filesToGenerate));

        List<File> files = generator.opts(clientOptInput).generate();

        Assert.assertEquals(files.size(), 5);

        TestUtils.ensureContainsFile(files, output, "pom.xml");
        TestUtils.ensureContainsFile(files, output, ".travis.yml");
        TestUtils.ensureContainsFile(files, output, ".gitignore");
        TestUtils.ensureContainsFile(files, output, "git_push.sh");
        TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");
    } finally {
        GlobalSettings.reset();
        output.delete();
    }
}
 
Example 19
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeneratePingSomeObj() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(CodegenConstants.MODEL_PACKAGE, "zz.yyyy.model.xxxx");
    properties.put(CodegenConstants.API_PACKAGE, "zz.yyyy.api.xxxx");
    properties.put(CodegenConstants.INVOKER_PACKAGE, "zz.yyyy.invoker.xxxx");
    properties.put(AbstractJavaCodegen.BOOLEAN_GETTER_PREFIX, "is");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setLibrary(JavaClientCodegen.OKHTTP_GSON)
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 41);
    TestUtils.ensureContainsFile(files, output, ".gitignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");
    TestUtils.ensureContainsFile(files, output, ".travis.yml");
    TestUtils.ensureContainsFile(files, output, "build.gradle");
    TestUtils.ensureContainsFile(files, output, "build.sbt");
    TestUtils.ensureContainsFile(files, output, "docs/PingApi.md");
    TestUtils.ensureContainsFile(files, output, "docs/SomeObj.md");
    TestUtils.ensureContainsFile(files, output, "git_push.sh");
    TestUtils.ensureContainsFile(files, output, "gradle.properties");
    TestUtils.ensureContainsFile(files, output, "gradle/wrapper/gradle-wrapper.jar");
    TestUtils.ensureContainsFile(files, output, "gradle/wrapper/gradle-wrapper.properties");
    TestUtils.ensureContainsFile(files, output, "gradlew.bat");
    TestUtils.ensureContainsFile(files, output, "gradlew");
    TestUtils.ensureContainsFile(files, output, "pom.xml");
    TestUtils.ensureContainsFile(files, output, "README.md");
    TestUtils.ensureContainsFile(files, output, "settings.gradle");
    TestUtils.ensureContainsFile(files, output, "api/openapi.yaml");
    TestUtils.ensureContainsFile(files, output, "src/main/AndroidManifest.xml");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/api/xxxx/PingApi.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ApiCallback.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ApiClient.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ApiException.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ApiResponse.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ServerConfiguration.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ServerVariable.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/auth/ApiKeyAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/auth/Authentication.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/auth/HttpBasicAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/auth/HttpBearerAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/Configuration.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/GzipRequestInterceptor.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/JSON.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/Pair.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ProgressRequestBody.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/ProgressResponseBody.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/invoker/xxxx/StringUtil.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/zz/yyyy/model/xxxx/SomeObj.java");
    TestUtils.ensureContainsFile(files, output, "src/test/java/zz/yyyy/api/xxxx/PingApiTest.java");
    TestUtils.ensureContainsFile(files, output, "src/test/java/zz/yyyy/model/xxxx/SomeObjTest.java");

    validateJavaSourceFiles(files);

    TestUtils.assertFileContains(Paths.get(output + "/src/main/java/zz/yyyy/model/xxxx/SomeObj.java"),
            "public class SomeObj",
            "Boolean isActive()");

    output.deleteOnExit();
}
 
Example 20
Source File: JavaClientCodegenTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeneratePing() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put(JavaClientCodegen.JAVA8_MODE, true);
    properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

    File output = Files.createTempDirectory("test").toFile();

    final CodegenConfigurator configurator = new CodegenConfigurator()
            .setGeneratorName("java")
            .setLibrary(JavaClientCodegen.OKHTTP_GSON)
            .setAdditionalProperties(properties)
            .setInputSpec("src/test/resources/3_0/ping.yaml")
            .setOutputDir(output.getAbsolutePath().replace("\\", "/"));

    final ClientOptInput clientOptInput = configurator.toClientOptInput();
    DefaultGenerator generator = new DefaultGenerator();
    List<File> files = generator.opts(clientOptInput).generate();

    Assert.assertEquals(files.size(), 38);
    TestUtils.ensureContainsFile(files, output, ".gitignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator-ignore");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/FILES");
    TestUtils.ensureContainsFile(files, output, ".openapi-generator/VERSION");
    TestUtils.ensureContainsFile(files, output, ".travis.yml");
    TestUtils.ensureContainsFile(files, output, "build.gradle");
    TestUtils.ensureContainsFile(files, output, "build.sbt");
    TestUtils.ensureContainsFile(files, output, "docs/DefaultApi.md");
    TestUtils.ensureContainsFile(files, output, "git_push.sh");
    TestUtils.ensureContainsFile(files, output, "gradle.properties");
    TestUtils.ensureContainsFile(files, output, "gradle/wrapper/gradle-wrapper.jar");
    TestUtils.ensureContainsFile(files, output, "gradle/wrapper/gradle-wrapper.properties");
    TestUtils.ensureContainsFile(files, output, "gradlew.bat");
    TestUtils.ensureContainsFile(files, output, "gradlew");
    TestUtils.ensureContainsFile(files, output, "pom.xml");
    TestUtils.ensureContainsFile(files, output, "README.md");
    TestUtils.ensureContainsFile(files, output, "settings.gradle");
    TestUtils.ensureContainsFile(files, output, "api/openapi.yaml");
    TestUtils.ensureContainsFile(files, output, "src/main/AndroidManifest.xml");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/api/DefaultApi.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ApiCallback.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ApiClient.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ApiException.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ApiResponse.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ServerConfiguration.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ServerVariable.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/auth/Authentication.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/auth/HttpBearerAuth.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/Configuration.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/GzipRequestInterceptor.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/JSON.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/Pair.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ProgressRequestBody.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/ProgressResponseBody.java");
    TestUtils.ensureContainsFile(files, output, "src/main/java/xyz/abcdef/StringUtil.java");
    TestUtils.ensureContainsFile(files, output, "src/test/java/xyz/abcdef/api/DefaultApiTest.java");

    validateJavaSourceFiles(files);

    TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/DefaultApi.java"), "public class DefaultApi");

    output.deleteOnExit();
}