From 7ba3f96a02e44c48cf184ab50d7685945caa5dcf Mon Sep 17 00:00:00 2001 From: Timon Back Date: Sat, 3 Aug 2024 16:53:28 +0200 Subject: [PATCH] test(core): write actual definitions in componentServiceIntegrationTest --- .gitignore | 1 + ...tJsonComponentsServiceIntegrationTest.java | 24 ++++++++-------- ...ltXmlComponentsServiceIntegrationTest.java | 27 +++++++++--------- ...tYamlComponentsServiceIntegrationTest.java | 24 ++++++++-------- .../core/fixtures/ClasspathUtil.java | 28 +++++++++++++++++++ .../{ => json}/annotation-definitions.json | 0 .../schemas/{ => json}/array-definitions.json | 0 .../{ => json}/complex-definitions.json | 0 .../schemas/{ => json}/definitions.json | 0 .../{ => json}/documented-definitions.json | 0 .../generics-wrapper-definitions.json | 0 .../{ => json}/json-type-definitions.json | 0 12 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 springwolf-core/src/test/java/io/github/springwolf/core/fixtures/ClasspathUtil.java rename springwolf-core/src/test/resources/schemas/{ => json}/annotation-definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/array-definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/complex-definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/documented-definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/generics-wrapper-definitions.json (100%) rename springwolf-core/src/test/resources/schemas/{ => json}/json-type-definitions.json (100%) diff --git a/.gitignore b/.gitignore index a2da0882e..690fcb3b0 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ node_modules/ asyncapi.actual.json asyncapi.actual.yaml +*.actual.json # Eclipse IDE .classpath diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceIntegrationTest.java index f756dddeb..4f5a031b7 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceIntegrationTest.java @@ -17,6 +17,7 @@ import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService; import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil; import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; +import io.github.springwolf.core.fixtures.ClasspathUtil; import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.annotation.Nullable; @@ -29,9 +30,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; -import java.io.InputStream; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.ArrayList; @@ -66,7 +65,7 @@ void getSchemas() throws IOException { componentsService.registerSchema(FooWithEnum.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/definitions.json"); + String expected = loadDefinition("/schemas/json/definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -77,7 +76,7 @@ void getDocumentedDefinitions() throws IOException { componentsService.registerSchema(DocumentedSimpleFoo.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/documented-definitions.json"); + String expected = loadDefinition("/schemas/json/documented-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -88,7 +87,7 @@ void getArrayDefinitions() throws IOException { componentsService.registerSchema(ArrayFoo.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/array-definitions.json"); + String expected = loadDefinition("/schemas/json/array-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -99,7 +98,7 @@ void getComplexDefinitions() throws IOException { componentsService.registerSchema(ComplexFoo.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/complex-definitions.json"); + String expected = loadDefinition("/schemas/json/complex-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -110,16 +109,15 @@ void getListWrapperDefinitions() throws IOException { componentsService.registerSchema(ListWrapper.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/generics-wrapper-definitions.json"); + String expected = loadDefinition("/schemas/json/generics-wrapper-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); } - private String jsonResource(String path) throws IOException { - try (InputStream s = this.getClass().getResourceAsStream(path)) { - return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim(); - } + private String loadDefinition(String path, String content) throws IOException { + ClasspathUtil.writeAsActual(path, content); + return ClasspathUtil.readAsString(path); } @Data @@ -231,7 +229,7 @@ void testSchemaWithOneOf() throws IOException { componentsService.registerSchema(SchemaAnnotationFoo.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/annotation-definitions.json"); + String expected = loadDefinition("/schemas/json/annotation-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -310,7 +308,7 @@ void getJsonTypeDefinitions() throws IOException { componentsService.registerSchema(JsonTypeInfoPayloadDto.class, CONTENT_TYPE_APPLICATION_JSON); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/json-type-definitions.json"); + String expected = loadDefinition("/schemas/json/json-type-definitions.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceIntegrationTest.java index 0b8c0d219..6f8e63c51 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceIntegrationTest.java @@ -14,6 +14,7 @@ import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService; import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil; import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; +import io.github.springwolf.core.fixtures.ClasspathUtil; import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.annotation.Nullable; @@ -26,9 +27,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; -import java.io.InputStream; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.ArrayList; @@ -60,7 +59,7 @@ void getSchemas() throws IOException { componentsService.registerSchema(FooWithEnum.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -71,7 +70,7 @@ void getDocumentedDefinitions() throws IOException { componentsService.registerSchema(DocumentedSimpleFoo.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/documented-definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/documented-definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -82,7 +81,7 @@ void getArrayDefinitions() throws IOException { componentsService.registerSchema(ArrayFoo.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/array-definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/array-definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -93,7 +92,7 @@ void getComplexDefinitions() throws IOException { componentsService.registerSchema(ComplexFoo.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/complex-definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/complex-definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -104,7 +103,8 @@ void getComplexDefinitionsWithAttributes() throws IOException { componentsService.registerSchema(ComplexAttributesFoo.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/complex-definitions-with-attributes-xml.json"); + String expected = + loadDefinition("/schemas/xml/complex-definitions-with-attributes-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -115,16 +115,15 @@ void getListWrapperDefinitions() throws IOException { componentsService.registerSchema(ListWrapper.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/generics-wrapper-definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/generics-wrapper-definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); } - private String jsonResource(String path) throws IOException { - try (InputStream s = this.getClass().getResourceAsStream(path)) { - return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim(); - } + private String loadDefinition(String path, String content) throws IOException { + ClasspathUtil.writeAsActual(path, content); + return ClasspathUtil.readAsString(path); } @Data @@ -246,7 +245,7 @@ void testSchemaWithOneOf() throws IOException { componentsService.registerSchema(SchemaAnnotationFoo.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/annotation-definitions-xml.json"); + String expected = loadDefinition("/schemas/xml/annotation-definitions-xml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -346,7 +345,7 @@ void testSchemasWithSharedProperty() throws IOException { componentsService.registerSchema(XmlSchemaName.ClassA.class, "text/xml"); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/xml/schema-with-shared-property.json"); + String expected = loadDefinition("/schemas/xml/schema-with-shared-property.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceIntegrationTest.java index 6f60afe77..39afa73a0 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceIntegrationTest.java @@ -19,6 +19,7 @@ import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService; import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil; import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties; +import io.github.springwolf.core.fixtures.ClasspathUtil; import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.annotation.Nullable; @@ -31,9 +32,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; -import java.io.InputStream; import java.math.BigInteger; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.ArrayList; @@ -74,7 +73,7 @@ void getSchemas() throws IOException { componentsService.registerSchema(FooWithEnum.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -85,7 +84,7 @@ void getDocumentedDefinitions() throws IOException { componentsService.registerSchema(DocumentedSimpleFoo.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/documented-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/documented-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -96,7 +95,7 @@ void getArrayDefinitions() throws IOException { componentsService.registerSchema(ArrayFoo.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/array-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/array-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -107,7 +106,7 @@ void getComplexDefinitions() throws IOException { componentsService.registerSchema(ComplexFoo.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/complex-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/complex-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -118,16 +117,15 @@ void getListWrapperDefinitions() throws IOException { componentsService.registerSchema(ListWrapper.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/generics-wrapper-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/generics-wrapper-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); } - private String jsonResource(String path) throws IOException { - try (InputStream s = this.getClass().getResourceAsStream(path)) { - return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim(); - } + private String loadDefinitions(String path, String content) throws IOException { + ClasspathUtil.writeAsActual(path, content); + return ClasspathUtil.readAsString(path); } @Data @@ -239,7 +237,7 @@ void testSchemaWithOneOf() throws IOException { componentsService.registerSchema(SchemaAnnotationFoo.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/annotation-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/annotation-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); @@ -318,7 +316,7 @@ void getJsonTypeDefinitions() throws IOException { componentsService.registerSchema(JsonTypeTest.JsonTypeInfoPayloadDto.class, CONTENT_TYPE_APPLICATION_YAML); String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas()); - String expected = jsonResource("/schemas/yaml/json-type-definitions-yaml.json"); + String expected = loadDefinitions("/schemas/yaml/json-type-definitions-yaml.json", actualDefinitions); System.out.println("Got: " + actualDefinitions); assertEquals(expected, actualDefinitions); diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/fixtures/ClasspathUtil.java b/springwolf-core/src/test/java/io/github/springwolf/core/fixtures/ClasspathUtil.java new file mode 100644 index 000000000..3a3c057cc --- /dev/null +++ b/springwolf-core/src/test/java/io/github/springwolf/core/fixtures/ClasspathUtil.java @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Apache-2.0 +package io.github.springwolf.core.fixtures; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +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).trim(); + } + } + + public static void writeAsActual(String resourceName, String content) throws IOException { + String extension = resourceName.substring(resourceName.lastIndexOf('.')); + String actualResource = resourceName.replace(extension, ".actual" + extension); + + try (OutputStream outputStream = Files.newOutputStream(Path.of("src", "test", "resources", actualResource))) { + outputStream.write(content.getBytes(StandardCharsets.UTF_8)); + } + } +} diff --git a/springwolf-core/src/test/resources/schemas/annotation-definitions.json b/springwolf-core/src/test/resources/schemas/json/annotation-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/annotation-definitions.json rename to springwolf-core/src/test/resources/schemas/json/annotation-definitions.json diff --git a/springwolf-core/src/test/resources/schemas/array-definitions.json b/springwolf-core/src/test/resources/schemas/json/array-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/array-definitions.json rename to springwolf-core/src/test/resources/schemas/json/array-definitions.json diff --git a/springwolf-core/src/test/resources/schemas/complex-definitions.json b/springwolf-core/src/test/resources/schemas/json/complex-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/complex-definitions.json rename to springwolf-core/src/test/resources/schemas/json/complex-definitions.json diff --git a/springwolf-core/src/test/resources/schemas/definitions.json b/springwolf-core/src/test/resources/schemas/json/definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/definitions.json rename to springwolf-core/src/test/resources/schemas/json/definitions.json diff --git a/springwolf-core/src/test/resources/schemas/documented-definitions.json b/springwolf-core/src/test/resources/schemas/json/documented-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/documented-definitions.json rename to springwolf-core/src/test/resources/schemas/json/documented-definitions.json diff --git a/springwolf-core/src/test/resources/schemas/generics-wrapper-definitions.json b/springwolf-core/src/test/resources/schemas/json/generics-wrapper-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/generics-wrapper-definitions.json rename to springwolf-core/src/test/resources/schemas/json/generics-wrapper-definitions.json diff --git a/springwolf-core/src/test/resources/schemas/json-type-definitions.json b/springwolf-core/src/test/resources/schemas/json/json-type-definitions.json similarity index 100% rename from springwolf-core/src/test/resources/schemas/json-type-definitions.json rename to springwolf-core/src/test/resources/schemas/json/json-type-definitions.json