Skip to content

Commit

Permalink
Added test for the FQN
Browse files Browse the repository at this point in the history
  • Loading branch information
ctasada committed Mar 21, 2024
1 parent faac712 commit 29ea6f3
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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" ]
}
}
Original file line number Diff line number Diff line change
@@ -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" ]
}
}

0 comments on commit 29ea6f3

Please sign in to comment.