Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): write actual definitions in componentServiceIntegrationTest #897

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ node_modules/

asyncapi.actual.json
asyncapi.actual.yaml
*.actual.json

# Eclipse IDE
.classpath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}