Skip to content

Commit fc8750a

Browse files
authored
test(core): write actual definitions in componentServiceIntegrationTest (#897)
1 parent 46abe71 commit fc8750a

12 files changed

+64
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ node_modules/
2525

2626
asyncapi.actual.json
2727
asyncapi.actual.yaml
28+
*.actual.json
2829

2930
# Eclipse IDE
3031
.classpath

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultJsonComponentsServiceIntegrationTest.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
1818
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
1919
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
20+
import io.github.springwolf.core.fixtures.ClasspathUtil;
2021
import io.swagger.v3.core.util.Json;
2122
import io.swagger.v3.oas.annotations.media.Schema;
2223
import jakarta.annotation.Nullable;
@@ -29,9 +30,7 @@
2930
import org.junit.jupiter.api.Test;
3031

3132
import java.io.IOException;
32-
import java.io.InputStream;
3333
import java.math.BigInteger;
34-
import java.nio.charset.StandardCharsets;
3534
import java.time.LocalDate;
3635
import java.time.OffsetDateTime;
3736
import java.util.ArrayList;
@@ -66,7 +65,7 @@ void getSchemas() throws IOException {
6665
componentsService.registerSchema(FooWithEnum.class, CONTENT_TYPE_APPLICATION_JSON);
6766

6867
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
69-
String expected = jsonResource("/schemas/definitions.json");
68+
String expected = loadDefinition("/schemas/json/definitions.json", actualDefinitions);
7069

7170
System.out.println("Got: " + actualDefinitions);
7271
assertEquals(expected, actualDefinitions);
@@ -77,7 +76,7 @@ void getDocumentedDefinitions() throws IOException {
7776
componentsService.registerSchema(DocumentedSimpleFoo.class, CONTENT_TYPE_APPLICATION_JSON);
7877

7978
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
80-
String expected = jsonResource("/schemas/documented-definitions.json");
79+
String expected = loadDefinition("/schemas/json/documented-definitions.json", actualDefinitions);
8180

8281
System.out.println("Got: " + actualDefinitions);
8382
assertEquals(expected, actualDefinitions);
@@ -88,7 +87,7 @@ void getArrayDefinitions() throws IOException {
8887
componentsService.registerSchema(ArrayFoo.class, CONTENT_TYPE_APPLICATION_JSON);
8988

9089
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
91-
String expected = jsonResource("/schemas/array-definitions.json");
90+
String expected = loadDefinition("/schemas/json/array-definitions.json", actualDefinitions);
9291

9392
System.out.println("Got: " + actualDefinitions);
9493
assertEquals(expected, actualDefinitions);
@@ -99,7 +98,7 @@ void getComplexDefinitions() throws IOException {
9998
componentsService.registerSchema(ComplexFoo.class, CONTENT_TYPE_APPLICATION_JSON);
10099

101100
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
102-
String expected = jsonResource("/schemas/complex-definitions.json");
101+
String expected = loadDefinition("/schemas/json/complex-definitions.json", actualDefinitions);
103102

104103
System.out.println("Got: " + actualDefinitions);
105104
assertEquals(expected, actualDefinitions);
@@ -110,16 +109,15 @@ void getListWrapperDefinitions() throws IOException {
110109
componentsService.registerSchema(ListWrapper.class, CONTENT_TYPE_APPLICATION_JSON);
111110

112111
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
113-
String expected = jsonResource("/schemas/generics-wrapper-definitions.json");
112+
String expected = loadDefinition("/schemas/json/generics-wrapper-definitions.json", actualDefinitions);
114113

115114
System.out.println("Got: " + actualDefinitions);
116115
assertEquals(expected, actualDefinitions);
117116
}
118117

119-
private String jsonResource(String path) throws IOException {
120-
try (InputStream s = this.getClass().getResourceAsStream(path)) {
121-
return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
122-
}
118+
private String loadDefinition(String path, String content) throws IOException {
119+
ClasspathUtil.writeAsActual(path, content);
120+
return ClasspathUtil.readAsString(path);
123121
}
124122

125123
@Data
@@ -231,7 +229,7 @@ void testSchemaWithOneOf() throws IOException {
231229
componentsService.registerSchema(SchemaAnnotationFoo.class, CONTENT_TYPE_APPLICATION_JSON);
232230

233231
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
234-
String expected = jsonResource("/schemas/annotation-definitions.json");
232+
String expected = loadDefinition("/schemas/json/annotation-definitions.json", actualDefinitions);
235233

236234
System.out.println("Got: " + actualDefinitions);
237235
assertEquals(expected, actualDefinitions);
@@ -310,7 +308,7 @@ void getJsonTypeDefinitions() throws IOException {
310308
componentsService.registerSchema(JsonTypeInfoPayloadDto.class, CONTENT_TYPE_APPLICATION_JSON);
311309

312310
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
313-
String expected = jsonResource("/schemas/json-type-definitions.json");
311+
String expected = loadDefinition("/schemas/json/json-type-definitions.json", actualDefinitions);
314312

315313
System.out.println("Got: " + actualDefinitions);
316314
assertEquals(expected, actualDefinitions);

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultXmlComponentsServiceIntegrationTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
1515
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
1616
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
17+
import io.github.springwolf.core.fixtures.ClasspathUtil;
1718
import io.swagger.v3.core.util.Json;
1819
import io.swagger.v3.oas.annotations.media.Schema;
1920
import jakarta.annotation.Nullable;
@@ -26,9 +27,7 @@
2627
import org.junit.jupiter.api.Test;
2728

2829
import java.io.IOException;
29-
import java.io.InputStream;
3030
import java.math.BigInteger;
31-
import java.nio.charset.StandardCharsets;
3231
import java.time.LocalDate;
3332
import java.time.OffsetDateTime;
3433
import java.util.ArrayList;
@@ -60,7 +59,7 @@ void getSchemas() throws IOException {
6059
componentsService.registerSchema(FooWithEnum.class, "text/xml");
6160

6261
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
63-
String expected = jsonResource("/schemas/xml/definitions-xml.json");
62+
String expected = loadDefinition("/schemas/xml/definitions-xml.json", actualDefinitions);
6463

6564
System.out.println("Got: " + actualDefinitions);
6665
assertEquals(expected, actualDefinitions);
@@ -71,7 +70,7 @@ void getDocumentedDefinitions() throws IOException {
7170
componentsService.registerSchema(DocumentedSimpleFoo.class, "text/xml");
7271

7372
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
74-
String expected = jsonResource("/schemas/xml/documented-definitions-xml.json");
73+
String expected = loadDefinition("/schemas/xml/documented-definitions-xml.json", actualDefinitions);
7574

7675
System.out.println("Got: " + actualDefinitions);
7776
assertEquals(expected, actualDefinitions);
@@ -82,7 +81,7 @@ void getArrayDefinitions() throws IOException {
8281
componentsService.registerSchema(ArrayFoo.class, "text/xml");
8382

8483
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
85-
String expected = jsonResource("/schemas/xml/array-definitions-xml.json");
84+
String expected = loadDefinition("/schemas/xml/array-definitions-xml.json", actualDefinitions);
8685

8786
System.out.println("Got: " + actualDefinitions);
8887
assertEquals(expected, actualDefinitions);
@@ -93,7 +92,7 @@ void getComplexDefinitions() throws IOException {
9392
componentsService.registerSchema(ComplexFoo.class, "text/xml");
9493

9594
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
96-
String expected = jsonResource("/schemas/xml/complex-definitions-xml.json");
95+
String expected = loadDefinition("/schemas/xml/complex-definitions-xml.json", actualDefinitions);
9796

9897
System.out.println("Got: " + actualDefinitions);
9998
assertEquals(expected, actualDefinitions);
@@ -104,7 +103,8 @@ void getComplexDefinitionsWithAttributes() throws IOException {
104103
componentsService.registerSchema(ComplexAttributesFoo.class, "text/xml");
105104

106105
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
107-
String expected = jsonResource("/schemas/xml/complex-definitions-with-attributes-xml.json");
106+
String expected =
107+
loadDefinition("/schemas/xml/complex-definitions-with-attributes-xml.json", actualDefinitions);
108108

109109
System.out.println("Got: " + actualDefinitions);
110110
assertEquals(expected, actualDefinitions);
@@ -115,16 +115,15 @@ void getListWrapperDefinitions() throws IOException {
115115
componentsService.registerSchema(ListWrapper.class, "text/xml");
116116

117117
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
118-
String expected = jsonResource("/schemas/xml/generics-wrapper-definitions-xml.json");
118+
String expected = loadDefinition("/schemas/xml/generics-wrapper-definitions-xml.json", actualDefinitions);
119119

120120
System.out.println("Got: " + actualDefinitions);
121121
assertEquals(expected, actualDefinitions);
122122
}
123123

124-
private String jsonResource(String path) throws IOException {
125-
try (InputStream s = this.getClass().getResourceAsStream(path)) {
126-
return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
127-
}
124+
private String loadDefinition(String path, String content) throws IOException {
125+
ClasspathUtil.writeAsActual(path, content);
126+
return ClasspathUtil.readAsString(path);
128127
}
129128

130129
@Data
@@ -246,7 +245,7 @@ void testSchemaWithOneOf() throws IOException {
246245
componentsService.registerSchema(SchemaAnnotationFoo.class, "text/xml");
247246

248247
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
249-
String expected = jsonResource("/schemas/xml/annotation-definitions-xml.json");
248+
String expected = loadDefinition("/schemas/xml/annotation-definitions-xml.json", actualDefinitions);
250249

251250
System.out.println("Got: " + actualDefinitions);
252251
assertEquals(expected, actualDefinitions);
@@ -346,7 +345,7 @@ void testSchemasWithSharedProperty() throws IOException {
346345
componentsService.registerSchema(XmlSchemaName.ClassA.class, "text/xml");
347346

348347
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
349-
String expected = jsonResource("/schemas/xml/schema-with-shared-property.json");
348+
String expected = loadDefinition("/schemas/xml/schema-with-shared-property.json", actualDefinitions);
350349

351350
System.out.println("Got: " + actualDefinitions);
352351
assertEquals(expected, actualDefinitions);

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultYamlComponentsServiceIntegrationTest.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaService;
2020
import io.github.springwolf.core.asyncapi.schemas.SwaggerSchemaUtil;
2121
import io.github.springwolf.core.configuration.properties.SpringwolfConfigProperties;
22+
import io.github.springwolf.core.fixtures.ClasspathUtil;
2223
import io.swagger.v3.core.util.Json;
2324
import io.swagger.v3.oas.annotations.media.Schema;
2425
import jakarta.annotation.Nullable;
@@ -31,9 +32,7 @@
3132
import org.junit.jupiter.api.Test;
3233

3334
import java.io.IOException;
34-
import java.io.InputStream;
3535
import java.math.BigInteger;
36-
import java.nio.charset.StandardCharsets;
3736
import java.time.LocalDate;
3837
import java.time.OffsetDateTime;
3938
import java.util.ArrayList;
@@ -74,7 +73,7 @@ void getSchemas() throws IOException {
7473
componentsService.registerSchema(FooWithEnum.class, CONTENT_TYPE_APPLICATION_YAML);
7574

7675
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
77-
String expected = jsonResource("/schemas/yaml/definitions-yaml.json");
76+
String expected = loadDefinitions("/schemas/yaml/definitions-yaml.json", actualDefinitions);
7877

7978
System.out.println("Got: " + actualDefinitions);
8079
assertEquals(expected, actualDefinitions);
@@ -85,7 +84,7 @@ void getDocumentedDefinitions() throws IOException {
8584
componentsService.registerSchema(DocumentedSimpleFoo.class, CONTENT_TYPE_APPLICATION_YAML);
8685

8786
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
88-
String expected = jsonResource("/schemas/yaml/documented-definitions-yaml.json");
87+
String expected = loadDefinitions("/schemas/yaml/documented-definitions-yaml.json", actualDefinitions);
8988

9089
System.out.println("Got: " + actualDefinitions);
9190
assertEquals(expected, actualDefinitions);
@@ -96,7 +95,7 @@ void getArrayDefinitions() throws IOException {
9695
componentsService.registerSchema(ArrayFoo.class, CONTENT_TYPE_APPLICATION_YAML);
9796

9897
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
99-
String expected = jsonResource("/schemas/yaml/array-definitions-yaml.json");
98+
String expected = loadDefinitions("/schemas/yaml/array-definitions-yaml.json", actualDefinitions);
10099

101100
System.out.println("Got: " + actualDefinitions);
102101
assertEquals(expected, actualDefinitions);
@@ -107,7 +106,7 @@ void getComplexDefinitions() throws IOException {
107106
componentsService.registerSchema(ComplexFoo.class, CONTENT_TYPE_APPLICATION_YAML);
108107

109108
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
110-
String expected = jsonResource("/schemas/yaml/complex-definitions-yaml.json");
109+
String expected = loadDefinitions("/schemas/yaml/complex-definitions-yaml.json", actualDefinitions);
111110

112111
System.out.println("Got: " + actualDefinitions);
113112
assertEquals(expected, actualDefinitions);
@@ -118,16 +117,15 @@ void getListWrapperDefinitions() throws IOException {
118117
componentsService.registerSchema(ListWrapper.class, CONTENT_TYPE_APPLICATION_YAML);
119118

120119
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
121-
String expected = jsonResource("/schemas/yaml/generics-wrapper-definitions-yaml.json");
120+
String expected = loadDefinitions("/schemas/yaml/generics-wrapper-definitions-yaml.json", actualDefinitions);
122121

123122
System.out.println("Got: " + actualDefinitions);
124123
assertEquals(expected, actualDefinitions);
125124
}
126125

127-
private String jsonResource(String path) throws IOException {
128-
try (InputStream s = this.getClass().getResourceAsStream(path)) {
129-
return new String(s.readAllBytes(), StandardCharsets.UTF_8).trim();
130-
}
126+
private String loadDefinitions(String path, String content) throws IOException {
127+
ClasspathUtil.writeAsActual(path, content);
128+
return ClasspathUtil.readAsString(path);
131129
}
132130

133131
@Data
@@ -239,7 +237,7 @@ void testSchemaWithOneOf() throws IOException {
239237
componentsService.registerSchema(SchemaAnnotationFoo.class, CONTENT_TYPE_APPLICATION_YAML);
240238

241239
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
242-
String expected = jsonResource("/schemas/yaml/annotation-definitions-yaml.json");
240+
String expected = loadDefinitions("/schemas/yaml/annotation-definitions-yaml.json", actualDefinitions);
243241

244242
System.out.println("Got: " + actualDefinitions);
245243
assertEquals(expected, actualDefinitions);
@@ -318,7 +316,7 @@ void getJsonTypeDefinitions() throws IOException {
318316
componentsService.registerSchema(JsonTypeTest.JsonTypeInfoPayloadDto.class, CONTENT_TYPE_APPLICATION_YAML);
319317

320318
String actualDefinitions = objectMapper.writer(printer).writeValueAsString(componentsService.getSchemas());
321-
String expected = jsonResource("/schemas/yaml/json-type-definitions-yaml.json");
319+
String expected = loadDefinitions("/schemas/yaml/json-type-definitions-yaml.json", actualDefinitions);
322320

323321
System.out.println("Got: " + actualDefinitions);
324322
assertEquals(expected, actualDefinitions);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package io.github.springwolf.core.fixtures;
3+
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
11+
public final class ClasspathUtil {
12+
private ClasspathUtil() {}
13+
14+
public static String readAsString(String resourceName) throws IOException {
15+
try (InputStream inputStream = ClasspathUtil.class.getResourceAsStream(resourceName)) {
16+
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8).trim();
17+
}
18+
}
19+
20+
public static void writeAsActual(String resourceName, String content) throws IOException {
21+
String extension = resourceName.substring(resourceName.lastIndexOf('.'));
22+
String actualResource = resourceName.replace(extension, ".actual" + extension);
23+
24+
try (OutputStream outputStream = Files.newOutputStream(Path.of("src", "test", "resources", actualResource))) {
25+
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
26+
}
27+
}
28+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)