|
| 1 | +package org.openapitools.codegen.scala; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | + |
| 5 | +import org.openapitools.codegen.ClientOptInput; |
| 6 | +import org.openapitools.codegen.CodegenConstants; |
| 7 | +import org.openapitools.codegen.CodegenModel; |
| 8 | +import org.openapitools.codegen.CodegenType; |
| 9 | +import org.openapitools.codegen.DefaultGenerator; |
| 10 | +import org.openapitools.codegen.TestUtils; |
| 11 | +import org.openapitools.codegen.config.CodegenConfigurator; |
| 12 | +import org.openapitools.codegen.languages.ScalaGatlingCodegen; |
| 13 | +import static org.testng.Assert.assertEquals; |
| 14 | + |
| 15 | +import org.testng.annotations.Test; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import io.swagger.v3.oas.models.OpenAPI; |
| 24 | +import io.swagger.v3.oas.models.media.DateTimeSchema; |
| 25 | +import io.swagger.v3.oas.models.media.IntegerSchema; |
| 26 | +import io.swagger.v3.oas.models.media.Schema; |
| 27 | +import io.swagger.v3.oas.models.media.StringSchema; |
| 28 | +import io.swagger.v3.parser.util.SchemaTypeUtil; |
| 29 | + |
| 30 | +public class ScalaGatlingCodegenTest { |
| 31 | + |
| 32 | + private final ScalaGatlingCodegen codegen = new ScalaGatlingCodegen(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void happyPath() throws IOException { |
| 36 | + assertEquals(codegen.getName(), "scala-gatling"); |
| 37 | + assertEquals(codegen.getTag(), CodegenType.CLIENT); |
| 38 | + |
| 39 | + final List<String> filenames = codegen.supportingFiles().stream() |
| 40 | + .map(supportingFile -> supportingFile.getDestinationFilename()) |
| 41 | + .collect(Collectors.toList()); |
| 42 | + assertEquals(filenames, ImmutableList.of( |
| 43 | + "build.gradle", |
| 44 | + "logback.xml", |
| 45 | + "default.conf", |
| 46 | + "CI.conf", |
| 47 | + "CD.conf", |
| 48 | + "stress.conf", |
| 49 | + "baseline.conf", |
| 50 | + "longevity.conf")); |
| 51 | + |
| 52 | + final Schema model = new Schema() |
| 53 | + .description("a sample model") |
| 54 | + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) |
| 55 | + .addProperties("name", new StringSchema()) |
| 56 | + .addProperties("createdAt", new DateTimeSchema()) |
| 57 | + .addRequiredItem("id") |
| 58 | + .addRequiredItem("name"); |
| 59 | + final OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 60 | + |
| 61 | + codegen.setOpenAPI(openAPI); |
| 62 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 63 | + |
| 64 | + assertEquals(cm.name, "sample"); |
| 65 | + assertEquals(cm.classname, "Sample"); |
| 66 | + assertEquals(cm.description, "a sample model"); |
| 67 | + assertEquals(cm.vars.size(), 3); |
| 68 | + |
| 69 | + File output = Files.createTempDirectory("test").toFile(); |
| 70 | + output.deleteOnExit(); |
| 71 | + |
| 72 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 73 | + .setGeneratorName(codegen.getName()) |
| 74 | + .setInputSpec("src/test/resources/3_0/scala_reserved_words.yaml") |
| 75 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 76 | + |
| 77 | + final ClientOptInput clientOptInput = configurator.toClientOptInput(); |
| 78 | + DefaultGenerator generator = new DefaultGenerator(); |
| 79 | + |
| 80 | + generator.setGenerateMetadata(false); |
| 81 | + |
| 82 | + generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true"); |
| 83 | + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false"); |
| 84 | + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false"); |
| 85 | + generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false"); |
| 86 | + generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true"); |
| 87 | + |
| 88 | + List<File> files = generator.opts(clientOptInput).generate(); |
| 89 | + |
| 90 | + assertEquals(files.size(), 9); |
| 91 | + |
| 92 | + TestUtils.ensureContainsFile(files, output, "src/gatling/scala/org/openapitools/client/model/SomeObj.scala"); |
| 93 | + TestUtils.ensureContainsFile(files, output, "build.gradle"); |
| 94 | + TestUtils.ensureContainsFile(files, output, "src/gatling/resources/conf/logback.xml"); |
| 95 | + TestUtils.ensureContainsFile(files, output, "src/gatling/resources/conf/baseline.conf"); |
| 96 | + TestUtils.ensureContainsFile(files, output, "src/gatling/resources/conf/stress.conf"); |
| 97 | + TestUtils.ensureContainsFile(files, output, "src/gatling/resources/conf/longevity.conf"); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments