Skip to content

Commit

Permalink
[scala][client] add Scala code generation test (#3859)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
sullis authored and wing328 committed Sep 12, 2019
1 parent dc2907a commit e56bfe4
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
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;
Expand Down Expand Up @@ -276,4 +279,52 @@ public void mapModelTest() {
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"));
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e56bfe4

Please sign in to comment.