Skip to content

Commit

Permalink
test: model tests check generation into multiple files (#283)
Browse files Browse the repository at this point in the history
This changes the ModelGeneratorTest so that it checks all files that
would normally be generated.
Removes the old Models.kt file and replaces it with the single files
  • Loading branch information
y-marion authored Apr 25, 2024
1 parent cbb86bd commit 6e1d953
Show file tree
Hide file tree
Showing 192 changed files with 3,467 additions and 2,760 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType
import com.cjbooms.fabrikt.cli.ValidationLibrary
import com.cjbooms.fabrikt.configurations.Packages
import com.cjbooms.fabrikt.generators.model.JacksonModelGenerator
import com.cjbooms.fabrikt.model.KotlinSourceSet
import com.cjbooms.fabrikt.model.Models
import com.cjbooms.fabrikt.model.SourceApi
import com.cjbooms.fabrikt.util.Linter
import com.cjbooms.fabrikt.util.ModelNameRegistry
import com.cjbooms.fabrikt.util.ResourceHelper.readFolder
import com.cjbooms.fabrikt.util.ResourceHelper.readTextResource
import com.squareup.kotlinpoet.FileSpec
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -20,6 +22,9 @@ import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Stream

Expand Down Expand Up @@ -89,14 +94,27 @@ class ModelGeneratorTest {
val basePackage = "examples.${testCaseName.replace("/", ".")}"
val apiLocation = javaClass.getResource("/examples/$testCaseName/api.yaml")!!
val sourceApi = SourceApi(apiLocation.readText(), baseDir = Paths.get(apiLocation.toURI()))
val expectedModels = readTextResource("/examples/$testCaseName/models/Models.kt")
val expectedModels = readFolder(Path.of("src/test/resources/examples/$testCaseName/models/"))

val models = JacksonModelGenerator(
Packages(basePackage),
sourceApi,
).generate().toSingleFile()
).generate()

assertThat(models).isEqualTo(expectedModels)
val sourceSet = setOf(KotlinSourceSet(models.files, Paths.get("")))
val tempDirectory = Files.createTempDirectory("model_generator_test_${testCaseName.replace("/", ".")}")
sourceSet.forEach {
it.writeFileTo(tempDirectory.toFile())
}

val tempFolderContents =
readFolder(tempDirectory.resolve(basePackage.replace(".", File.separator)).resolve("models"))
tempFolderContents.forEach {
assertThat(expectedModels).containsKeys(it.key)
assertThat(expectedModels[it.key]).isEqualTo(it.value)
}

tempDirectory.toFile().deleteRecursively()
}

@Test
Expand Down
7 changes: 7 additions & 0 deletions src/test/kotlin/com/cjbooms/fabrikt/util/ResourceHelper.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.cjbooms.fabrikt.util

import java.io.FileNotFoundException
import java.nio.file.Path
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.name
import kotlin.io.path.readText

object ResourceHelper {
fun readTextResource(path: String): String =
(javaClass.getResource(path) ?: throw FileNotFoundException(path)).readText()

fun readFolder(path: Path): Map<String, String> =
path.listDirectoryEntries().associate { it.name to it.readText() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import java.math.BigDecimal
import javax.validation.constraints.NotNull
import kotlin.Any
import kotlin.Int
import kotlin.String

public data class ComplexParent(
@param:JsonProperty("oneOf")
@get:JsonProperty("oneOf")
public val oneOf: Any? = null,
@param:JsonProperty("first_nested_any_of_prop")
@get:JsonProperty("first_nested_any_of_prop")
public val firstNestedAnyOfProp: String? = null,
@param:JsonProperty("second_nested_any_of_prop")
@get:JsonProperty("second_nested_any_of_prop")
public val secondNestedAnyOfProp: String? = null,
@param:JsonProperty("required_string")
@get:JsonProperty("required_string")
@get:NotNull
public val requiredString: String,
@param:JsonProperty("required_int")
@get:JsonProperty("required_int")
@get:NotNull
public val requiredInt: Int,
@param:JsonProperty("top_level_prop")
@get:JsonProperty("top_level_prop")
@get:NotNull
public val topLevelProp: BigDecimal,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import javax.validation.constraints.NotNull
import kotlin.String

public data class ComplexSecondOneA(
@param:JsonProperty("more_nested_prop_one")
@get:JsonProperty("more_nested_prop_one")
@get:NotNull
public val moreNestedPropOne: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.String

public data class ContainsNestedAnyOf(
@param:JsonProperty("first_nested_any_of_prop")
@get:JsonProperty("first_nested_any_of_prop")
public val firstNestedAnyOfProp: String? = null,
@param:JsonProperty("second_nested_any_of_prop")
@get:JsonProperty("second_nested_any_of_prop")
public val secondNestedAnyOfProp: String? = null,
)
10 changes: 10 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/FirstAnyA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.String

public data class FirstAnyA(
@param:JsonProperty("first_nested_any_of_prop")
@get:JsonProperty("first_nested_any_of_prop")
public val firstNestedAnyOfProp: String? = null,
)
10 changes: 10 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/FirstOneA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.String

public data class FirstOneA(
@param:JsonProperty("first_nested_one_of_prop")
@get:JsonProperty("first_nested_one_of_prop")
public val firstNestedOneOfProp: String? = null,
)
10 changes: 10 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/FirstOneB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.String

public data class FirstOneB(
@param:JsonProperty("first_property")
@get:JsonProperty("first_property")
public val firstProperty: String? = null,
)
132 changes: 0 additions & 132 deletions src/test/resources/examples/anyOfOneOfAllOf/models/Models.kt

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/MoreNesting.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import javax.validation.constraints.NotNull
import kotlin.String

public data class MoreNesting(
@param:JsonProperty("more_nested_prop_one")
@get:JsonProperty("more_nested_prop_one")
@get:NotNull
public val moreNestedPropOne: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonAnyGetter
import com.fasterxml.jackson.`annotation`.JsonAnySetter
import com.fasterxml.jackson.`annotation`.JsonIgnore
import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.Any
import kotlin.String
import kotlin.collections.Map
import kotlin.collections.MutableMap

public data class OneOfAdditionalProps(
@param:JsonProperty("second_nested_any_of_prop")
@get:JsonProperty("second_nested_any_of_prop")
public val secondNestedAnyOfProp: String? = null,
@get:JsonIgnore
public val properties: MutableMap<String, Any?> = mutableMapOf(),
) {
@JsonAnyGetter
public fun `get`(): Map<String, Any?> = properties

@JsonAnySetter
public fun `set`(name: String, `value`: Any?) {
properties[name] = value
}
}
10 changes: 10 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/SecondAnyA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.String

public data class SecondAnyA(
@param:JsonProperty("second_nested_any_of_prop")
@get:JsonProperty("second_nested_any_of_prop")
public val secondNestedAnyOfProp: String? = null,
)
17 changes: 17 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/SecondOneB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.Any
import kotlin.String

public data class SecondOneB(
@param:JsonProperty("second_property")
@get:JsonProperty("second_property")
public val secondProperty: String? = null,
@param:JsonProperty("third_property")
@get:JsonProperty("third_property")
public val thirdProperty: String? = null,
@param:JsonProperty("forth_property")
@get:JsonProperty("forth_property")
public val forthProperty: Any? = null,
)
13 changes: 13 additions & 0 deletions src/test/resources/examples/anyOfOneOfAllOf/models/SimpleOneOfs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import kotlin.Any

public data class SimpleOneOfs(
@param:JsonProperty("oneof_property")
@get:JsonProperty("oneof_property")
public val oneofProperty: Any? = null,
@param:JsonProperty("primitive_oneof_property")
@get:JsonProperty("primitive_oneof_property")
public val primitiveOneofProperty: Any? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package examples.anyOfOneOfAllOf.models

import com.fasterxml.jackson.`annotation`.JsonProperty
import javax.validation.constraints.NotNull
import kotlin.Int
import kotlin.String

public data class SimpleTypeWithRequiredProps(
@param:JsonProperty("required_string")
@get:JsonProperty("required_string")
@get:NotNull
public val requiredString: String,
@param:JsonProperty("required_int")
@get:JsonProperty("required_int")
@get:NotNull
public val requiredInt: Int,
)
Loading

0 comments on commit 6e1d953

Please sign in to comment.