forked from loafle/openapi-generator-original
[scala][client] add Scala code generation test (#3859)
* [scala][client] add Scala reserved words test * fix filesystem path in [ScalaAkkaClientCodegenTest] * add additional reserved words in scala_reserved_words.yaml * ScalaAkkaClientCodegenTest: set mainPackage * scala_reserved_words.yaml: declare 'required' fields * rename test method * tweak test description
This commit is contained in:
+55
-4
@@ -21,14 +21,17 @@ import com.google.common.collect.Sets;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.media.*;
|
||||
import io.swagger.v3.parser.util.SchemaTypeUtil;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.TestUtils;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import org.openapitools.codegen.languages.ScalaAkkaClientCodegen;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ScalaAkkaClientCodegenTest {
|
||||
|
||||
private ScalaAkkaClientCodegen scalaAkkaClientCodegen;
|
||||
@@ -276,4 +279,52 @@ public class ScalaAkkaClientCodegenTest {
|
||||
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Map", "Children")).size(), 1);
|
||||
}
|
||||
|
||||
@Test(description = "validate codegen output")
|
||||
public void codeGenerationTest() 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();
|
||||
MockDefaultGenerator generator = new MockDefaultGenerator();
|
||||
generator.opts(clientOptInput).generate();
|
||||
|
||||
Map<String, String> generatedFiles = generator.getFiles();
|
||||
Assert.assertEquals(generatedFiles.size(), 13);
|
||||
|
||||
final String someObjFilename = new File(output, "src/main/scala/hello/world/model/SomeObj.scala").getAbsolutePath().replace("\\", "/");
|
||||
final String someObjFileContents = generatedFiles.get(someObjFilename);
|
||||
Assert.assertTrue(someObjFileContents.contains("package hello.world.model"));
|
||||
Assert.assertTrue(someObjFileContents.contains("case class SomeObj"));
|
||||
Assert.assertTrue(someObjFileContents.contains("id: Long,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("name: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`val`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`var`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`class`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`trait`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`object`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`try`: String,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`catch`: String,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`finally`: String,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`def`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`for`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`implicit`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`match`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`case`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`import`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`lazy`: String,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`private`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("`type`: Option[String] = None,"));
|
||||
Assert.assertTrue(someObjFileContents.contains("foobar: Boolean"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
openapi: 3.0.1
|
||||
info:
|
||||
title: ping some object
|
||||
version: '1.0'
|
||||
servers:
|
||||
- url: 'http://localhost:8082/'
|
||||
paths:
|
||||
/ping:
|
||||
post:
|
||||
operationId: postPing
|
||||
tags:
|
||||
- ping
|
||||
requestBody:
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: "#/components/schemas/SomeObj"
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: "#/components/schemas/SomeObj"
|
||||
components:
|
||||
schemas:
|
||||
SomeObj:
|
||||
type: object
|
||||
properties:
|
||||
$_type:
|
||||
type: string
|
||||
# using 'enum' & 'default' for '$_type' is a work-around until constants are supported
|
||||
# See https://github.com/OAI/OpenAPI-Specification/issues/1313
|
||||
enum:
|
||||
- SomeObjIdentifier
|
||||
default: SomeObjIdentifier
|
||||
id:
|
||||
type: integer
|
||||
format: int64
|
||||
name:
|
||||
type: string
|
||||
val:
|
||||
type: string
|
||||
var:
|
||||
type: string
|
||||
class:
|
||||
type: string
|
||||
trait:
|
||||
type: string
|
||||
object:
|
||||
type: string
|
||||
try:
|
||||
type: string
|
||||
catch:
|
||||
type: string
|
||||
finally:
|
||||
type: string
|
||||
def:
|
||||
type: string
|
||||
for:
|
||||
type: string
|
||||
implicit:
|
||||
type: string
|
||||
match:
|
||||
type: string
|
||||
case:
|
||||
type: string
|
||||
import:
|
||||
type: string
|
||||
lazy:
|
||||
type: string
|
||||
private:
|
||||
type: string
|
||||
type:
|
||||
type: string
|
||||
foobar:
|
||||
type: boolean
|
||||
required:
|
||||
- id
|
||||
- try
|
||||
- catch
|
||||
- finally
|
||||
- lazy
|
||||
- foobar
|
||||
Reference in New Issue
Block a user