From 29ea6f38eaf14a2e26811a8239351b7f1c8a6b05 Mon Sep 17 00:00:00 2001 From: Carlos Tasada Date: Thu, 21 Mar 2024 14:47:10 +0100 Subject: [PATCH] Added test for the FQN --- .../build.gradle | 4 +- .../converter/ClasspathUtil.java | 16 +++ ...KotlinxSerializationTypeConverterTest.java | 29 ++++- .../src/test/resources/fqn.json | 111 ++++++++++++++++++ .../src/test/resources/simple.json | 111 ++++++++++++++++++ 5 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/ClasspathUtil.java create mode 100644 springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/fqn.json create mode 100644 springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/simple.json diff --git a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/build.gradle b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/build.gradle index 1f7535148..4de4f7d9c 100644 --- a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/build.gradle +++ b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/build.gradle @@ -33,9 +33,11 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:${kotlinxSerializationVersion}" implementation "org.jetbrains.kotlin:kotlin-reflect" + testRuntimeOnly "org.junit.jupiter:junit-jupiter:${junitJupiterVersion}" testImplementation "org.assertj:assertj-core:${assertjCoreVersion}" testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}" - testRuntimeOnly "org.junit.jupiter:junit-jupiter:${junitJupiterVersion}" + testImplementation "net.javacrumbs.json-unit:json-unit-assertj:${jsonUnitAssertJVersion}" + } jar { diff --git a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/ClasspathUtil.java b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/ClasspathUtil.java new file mode 100644 index 000000000..a052617b5 --- /dev/null +++ b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/ClasspathUtil.java @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 +package io.github.springwolf.addons.kotlinx_serialization_model_converter.converter; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +public final class ClasspathUtil { + private ClasspathUtil() {} + + public static String readAsString(String resourceName) throws IOException { + try (InputStream inputStream = ClasspathUtil.class.getResourceAsStream(resourceName)) { + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); + } + } +} diff --git a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/KotlinxSerializationTypeConverterTest.java b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/KotlinxSerializationTypeConverterTest.java index 2d933cd3e..37207eb29 100644 --- a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/KotlinxSerializationTypeConverterTest.java +++ b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/java/io/github/springwolf/addons/kotlinx_serialization_model_converter/converter/KotlinxSerializationTypeConverterTest.java @@ -1,7 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 package io.github.springwolf.addons.kotlinx_serialization_model_converter.converter; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.PrettyPrinter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; @@ -11,8 +10,10 @@ import io.swagger.v3.oas.models.media.Schema; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.util.List; +import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.assertj.core.api.Assertions.assertThat; class KotlinxSerializationTypeConverterTest { @@ -21,7 +22,7 @@ class KotlinxSerializationTypeConverterTest { private final PrettyPrinter printer = new DefaultPrettyPrinter(); @Test - void serializeKotlin() throws JsonProcessingException { + void serializeKotlin() { final KotlinxSerializationModelConverter modelConverter = new KotlinxSerializationModelConverter(); final ModelConverters converters = new ModelConverters(); converters.addConverter(modelConverter); @@ -46,8 +47,30 @@ void serializeKotlin() throws JsonProcessingException { model.getProperties().get("nested_class"), "#/components/schemas/SampleEvent$NestedClass", media.get("NestedClass")); + } + + @Test + void validateGeneratedJson() throws IOException { + final KotlinxSerializationModelConverter modelConverter = new KotlinxSerializationModelConverter(); + final ModelConverters converters = new ModelConverters(); + converters.addConverter(modelConverter); + + var media = converters.readAll(new AnnotatedType(SampleEvent.class)); + + String example = ClasspathUtil.readAsString("/simple.json"); + assertThatJson(jsonMapper.writer(printer).writeValueAsString(media)).isEqualTo(example); + } + + @Test + void validateGeneratedFqnJson() throws IOException { + final KotlinxSerializationModelConverter modelConverter = new KotlinxSerializationModelConverter(true); + final ModelConverters converters = new ModelConverters(); + converters.addConverter(modelConverter); + + var media = converters.readAll(new AnnotatedType(SampleEvent.class)); - System.out.print(jsonMapper.writer(printer).writeValueAsString(media)); + String example = ClasspathUtil.readAsString("/fqn.json"); + assertThatJson(jsonMapper.writer(printer).writeValueAsString(media)).isEqualTo(example); } private void assertLocalDateField(Schema dateField) { diff --git a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/fqn.json b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/fqn.json new file mode 100644 index 000000000..9977d17a9 --- /dev/null +++ b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/fqn.json @@ -0,0 +1,111 @@ +{ + "NestedClass" : { + "type" : "object", + "properties" : { + "color" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "id" : { + "type" : "integer", + "format" : "int32" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ "color", "id", "name" ] + }, + "SampleEvent" : { + "type" : "object", + "properties" : { + "boolean_field" : { + "type" : "boolean" + }, + "byte_field" : { + "type" : "integer", + "format" : "int32" + }, + "date_field" : { + "type" : "string", + "format" : "date" + }, + "double_field" : { + "type" : "number", + "format" : "double" + }, + "enum_field" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "float_field" : { + "type" : "number", + "format" : "float" + }, + "int_field" : { + "type" : "integer", + "format" : "int32" + }, + "list_field" : { + "type" : "array", + "items" : { + "type" : "string" + } + }, + "listed_references" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/io.github.springwolf.addons.kotlinx_serialization_model_converter.converter.SampleEvent$NestedClass" + } + }, + "long_field" : { + "type" : "integer", + "format" : "int64" + }, + "map_field" : { + "type" : "object", + "additionalProperties" : { + "type" : "string" + } + }, + "nested_class" : { + "$ref" : "#/components/schemas/io.github.springwolf.addons.kotlinx_serialization_model_converter.converter.SampleEvent$NestedClass" + }, + "optional_field" : { + "type" : "string" + }, + "set_field" : { + "type" : "array", + "items" : { + "type" : "string" + }, + "uniqueItems" : true + }, + "short_field" : { + "type" : "integer", + "format" : "int32" + }, + "string_field" : { + "type" : "string" + } + }, + "required" : [ "boolean_field", "byte_field", "date_field", "double_field", "enum_field", "float_field", "int_field", "list_field", "listed_references", "long_field", "map_field", "set_field", "short_field", "string_field" ] + }, + "io.github.springwolf.addons.kotlinx_serialization_model_converter.converter.SampleEvent$NestedClass" : { + "type" : "object", + "properties" : { + "color" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "id" : { + "type" : "integer", + "format" : "int32" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ "color", "id", "name" ] + } +} \ No newline at end of file diff --git a/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/simple.json b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/simple.json new file mode 100644 index 000000000..055e6c461 --- /dev/null +++ b/springwolf-add-ons/springwolf-kotlinx-serialization-model-converter/src/test/resources/simple.json @@ -0,0 +1,111 @@ +{ + "NestedClass" : { + "type" : "object", + "properties" : { + "color" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "id" : { + "type" : "integer", + "format" : "int32" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ "color", "id", "name" ] + }, + "SampleEvent" : { + "type" : "object", + "properties" : { + "boolean_field" : { + "type" : "boolean" + }, + "byte_field" : { + "type" : "integer", + "format" : "int32" + }, + "date_field" : { + "type" : "string", + "format" : "date" + }, + "double_field" : { + "type" : "number", + "format" : "double" + }, + "enum_field" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "float_field" : { + "type" : "number", + "format" : "float" + }, + "int_field" : { + "type" : "integer", + "format" : "int32" + }, + "list_field" : { + "type" : "array", + "items" : { + "type" : "string" + } + }, + "listed_references" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/SampleEvent$NestedClass" + } + }, + "long_field" : { + "type" : "integer", + "format" : "int64" + }, + "map_field" : { + "type" : "object", + "additionalProperties" : { + "type" : "string" + } + }, + "nested_class" : { + "$ref" : "#/components/schemas/SampleEvent$NestedClass" + }, + "optional_field" : { + "type" : "string" + }, + "set_field" : { + "type" : "array", + "items" : { + "type" : "string" + }, + "uniqueItems" : true + }, + "short_field" : { + "type" : "integer", + "format" : "int32" + }, + "string_field" : { + "type" : "string" + } + }, + "required" : [ "boolean_field", "byte_field", "date_field", "double_field", "enum_field", "float_field", "int_field", "list_field", "listed_references", "long_field", "map_field", "set_field", "short_field", "string_field" ] + }, + "SampleEvent$NestedClass" : { + "type" : "object", + "properties" : { + "color" : { + "type" : "string", + "enum" : [ "RED", "GREEN", "BLUE" ] + }, + "id" : { + "type" : "integer", + "format" : "int32" + }, + "name" : { + "type" : "string" + } + }, + "required" : [ "color", "id", "name" ] + } +} \ No newline at end of file