-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add end2end tests for jackson model generation
- Loading branch information
Jens Zettelmeyer
committed
Jul 13, 2024
1 parent
7f2ccb0
commit 7db3301
Showing
10 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
val fabrikt: Configuration by configurations.creating | ||
|
||
val generationDir = "$buildDir/generated" | ||
val apiFile = "$projectDir/openapi/api.yaml" | ||
|
||
sourceSets { | ||
main { java.srcDirs("$generationDir/src/main/kotlin") } | ||
test { java.srcDirs("$generationDir/src/test/kotlin") } | ||
} | ||
|
||
plugins { | ||
id("org.jetbrains.kotlin.jvm") version "1.8.20" // Apply the Kotlin JVM plugin to add support for Kotlin. | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
val jacksonVersion: String by rootProject.extra | ||
val junitVersion: String by rootProject.extra | ||
|
||
dependencies { | ||
implementation("jakarta.validation:jakarta.validation-api:3.0.2") | ||
implementation("javax.validation:validation-api:2.0.1.Final") | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") | ||
implementation("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion") | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") | ||
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion") | ||
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion") | ||
testImplementation("org.assertj:assertj-core:3.24.2") | ||
} | ||
|
||
tasks { | ||
|
||
val generateCode by creating(JavaExec::class) { | ||
inputs.files(apiFile) | ||
outputs.dir(generationDir) | ||
outputs.cacheIf { true } | ||
classpath = rootProject.files("./build/libs/fabrikt-${rootProject.version}.jar") | ||
mainClass.set("com.cjbooms.fabrikt.cli.CodeGen") | ||
args = listOf( | ||
"--output-directory", generationDir, | ||
"--base-package", "com.example", | ||
"--api-file", apiFile, | ||
"--targets", "http_models" | ||
) | ||
dependsOn(":jar") | ||
dependsOn(":shadowJar") | ||
} | ||
|
||
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { | ||
kotlinOptions.jvmTarget = "17" | ||
dependsOn(generateCode) | ||
} | ||
|
||
|
||
withType<Test> { | ||
useJUnitPlatform() | ||
jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED") | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
openapi: 3.0.0 | ||
paths: | ||
info: | ||
title: | ||
version: | ||
components: | ||
schemas: | ||
EnumHolder: | ||
type: object | ||
properties: | ||
array_of_enums: | ||
type: array | ||
items: | ||
type: string | ||
x-extensible-enum: | ||
- array_enum_one | ||
- array_enum_two | ||
inlined_enum: | ||
type: string | ||
enum: | ||
- inlined_one | ||
- inlined_two | ||
- inlined_three | ||
inlined_extensible_enum: | ||
type: string | ||
x-extensible-enum: | ||
- inlined_one | ||
- inlined_two | ||
- inlined_three | ||
enum_ref: | ||
$ref: '#/components/schemas/EnumObject' | ||
extensible_enum_ref: | ||
$ref: '#/components/schemas/ExtensibleEnumObject' | ||
list_enums: | ||
$ref: '#/components/schemas/ListEnums' | ||
|
||
EnumObject: | ||
type: string | ||
enum: | ||
- one | ||
- two | ||
- three | ||
- 4 | ||
- -5 | ||
- _6 | ||
|
||
ExtensibleEnumObject: | ||
type: string | ||
x-extensible-enum: | ||
- active | ||
- inactive | ||
|
||
ContentType: | ||
type: string | ||
x-extensible-enum: | ||
- application/json | ||
- application/x.some-type+json | ||
- application/x.some-other-type+json;version=2 | ||
|
||
ListEnums: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/ContentType' | ||
|
||
FooBars: | ||
type: object | ||
properties: | ||
prop_one: | ||
$ref: '#/components/schemas/Foo' | ||
prop_two: | ||
$ref: '#/components/schemas/Bar' | ||
|
||
Foo: | ||
type: array | ||
items: | ||
type: string | ||
enum: | ||
- X | ||
- Y | ||
Bar: | ||
type: array | ||
items: | ||
type: object | ||
properties: | ||
bar_prop: | ||
type: string | ||
|
||
|
||
|
121 changes: 121 additions & 0 deletions
121
end2end-tests/models-jackson/src/test/kotlin/com/cjbooms/fabrikt/models/jackson/EnumTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.cjbooms.fabrikt.models.jackson | ||
|
||
import com.example.models.EnumHolder | ||
import com.example.models.EnumHolderInlinedEnum | ||
import com.example.models.EnumObject | ||
import com.fasterxml.jackson.module.kotlin.jsonMapper | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class EnumTest { | ||
private val objectMapper = jsonMapper() | ||
private val writer = objectMapper.writerWithDefaultPrettyPrinter() | ||
|
||
@Test | ||
fun `must serialize array of enums`() { | ||
val wrapper = EnumHolder(arrayOfEnums = listOf("a", "b", "c")) | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/array_of_enums.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must deserialize array of enums`() { | ||
val result = readEnumHolder("array_of_enums") | ||
|
||
val expected = EnumHolder(arrayOfEnums = listOf("a", "b", "c")) | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize inlined enum`() { | ||
val wrapper = EnumHolder(inlinedEnum = EnumHolderInlinedEnum.INLINED_ONE) | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/inlined_enum.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must deserialize inlined enums`() { | ||
val result = readEnumHolder("inlined_enum") | ||
|
||
val expected = EnumHolder(inlinedEnum = EnumHolderInlinedEnum.INLINED_ONE) | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize extensible enum`() { | ||
val wrapper = EnumHolder(inlinedExtensibleEnum = "new enum value") | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/extensible_enum.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must deserialize extensible enum`() { | ||
val result = readEnumHolder("extensible_enum") | ||
|
||
val expected = EnumHolder(inlinedExtensibleEnum = "new enum value") | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize enum ref`() { | ||
val wrapper = EnumHolder(enumRef = EnumObject.ONE) | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/enum_ref.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must deserialize enum refs`() { | ||
val result = readEnumHolder("enum_ref") | ||
|
||
val expected = EnumHolder(enumRef = EnumObject.ONE) | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize extensible enum ref`() { | ||
val wrapper = EnumHolder(extensibleEnumRef = "extensible_enum_ref") | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/extensible_enum_ref.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize extensible enum refs`() { | ||
val result = readEnumHolder("extensible_enum_ref") | ||
|
||
val expected = EnumHolder(extensibleEnumRef = "extensible_enum_ref") | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must serialize a list of enum`() { | ||
val wrapper = EnumHolder(listEnums = listOf("list enum value 1")) | ||
val result = writer.writeValueAsString(wrapper) | ||
|
||
val expected = javaClass.getResource("/enums/list_of_enums.json")!!.readText() | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `must deserialize a list of enum`() { | ||
val result = readEnumHolder("list_of_enums") | ||
|
||
val expected = EnumHolder(listEnums = listOf("list enum value 1")) | ||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
private fun readEnumHolder(name: String): EnumHolder { | ||
val jsonString = javaClass.getResource("/enums/$name.json")!!.readText() | ||
|
||
return objectMapper.readValue(jsonString, EnumHolder::class.java) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/array_of_enums.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : [ "a", "b", "c" ], | ||
"inlined_enum" : null, | ||
"inlined_extensible_enum" : null, | ||
"enum_ref" : null, | ||
"extensible_enum_ref" : null, | ||
"list_enums" : null | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/enum_ref.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : null, | ||
"inlined_enum" : null, | ||
"inlined_extensible_enum" : null, | ||
"enum_ref" : "one", | ||
"extensible_enum_ref" : null, | ||
"list_enums" : null | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/extensible_enum.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : null, | ||
"inlined_enum" : null, | ||
"inlined_extensible_enum" : "new enum value", | ||
"enum_ref" : null, | ||
"extensible_enum_ref" : null, | ||
"list_enums" : null | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/extensible_enum_ref.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : null, | ||
"inlined_enum" : null, | ||
"inlined_extensible_enum" : null, | ||
"enum_ref" : null, | ||
"extensible_enum_ref" : "extensible_enum_ref", | ||
"list_enums" : null | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/inlined_enum.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : null, | ||
"inlined_enum" : "inlined_one", | ||
"inlined_extensible_enum" : null, | ||
"enum_ref" : null, | ||
"extensible_enum_ref" : null, | ||
"list_enums" : null | ||
} |
8 changes: 8 additions & 0 deletions
8
end2end-tests/models-jackson/src/test/resources/enums/list_of_enums.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"array_of_enums" : null, | ||
"inlined_enum" : null, | ||
"inlined_extensible_enum" : null, | ||
"enum_ref" : null, | ||
"extensible_enum_ref" : null, | ||
"list_enums" : [ "list enum value 1" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters