Skip to content

Commit 0181e34

Browse files
author
Thorsten Schlathoelter
committed
chore(#1459): load schema from all Resource types
1 parent 8e97512 commit 0181e34

File tree

4 files changed

+183
-11
lines changed

4 files changed

+183
-11
lines changed

validation/citrus-validation-json/src/main/java/org/citrusframework/json/schema/SimpleJsonSchema.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.citrusframework.json.schema;
1818

19-
import java.io.FileInputStream;
20-
import java.io.IOException;
19+
import java.io.InputStream;
2120
import java.util.Objects;
2221

2322
import com.networknt.schema.Schema;
@@ -28,6 +27,7 @@
2827
import org.citrusframework.common.InitializingPhase;
2928
import org.citrusframework.exceptions.CitrusRuntimeException;
3029
import org.citrusframework.spi.Resource;
30+
import org.citrusframework.spi.Resources.ByteArrayResource;
3131

3232
/**
3333
* Adapter between the resource reference from the bean configuration and the usable {@link SimpleJsonSchema} for
@@ -65,23 +65,35 @@ public SimpleJsonSchema() {
6565
*/
6666
@Override
6767
public void initialize() {
68-
try {
69-
schema = jsonSchemaFactory.getSchema(SchemaLocation.of(json.getURL().toString()));
70-
} catch (Exception e) {
71-
initializeFromFile();
72-
}
68+
69+
if (json instanceof ByteArrayResource byteArrayResource) {
70+
try (InputStream inputStream = byteArrayResource.getInputStream()) {
71+
schema = jsonSchemaFactory.getSchema(inputStream);
72+
} catch (Exception e) {
73+
throw new CitrusRuntimeException("Failed to load Json schema", e);
74+
}
75+
} else {
76+
try {
77+
// All other resources provide a URL, URL allows for loading or dependencies
78+
schema = jsonSchemaFactory.getSchema(SchemaLocation.of(json.getURL().toString()));
79+
} catch (Exception e) {
80+
// If all fails, go for the stream
81+
initializeFromStream();
82+
}
83+
}
7384
}
7485

7586
/**
7687
* Ensure backwards compatibility in any case, loading via SchemaLocation fails.
7788
*/
78-
private void initializeFromFile() {
79-
try (FileInputStream fileInputStream = new FileInputStream(json.getFile())) {
80-
schema = jsonSchemaFactory.getSchema(fileInputStream);
81-
} catch (IOException e) {
89+
private void initializeFromStream() {
90+
try (InputStream inputStream = json.getInputStream()) {
91+
schema = jsonSchemaFactory.getSchema(inputStream);
92+
} catch (Exception e) {
8293
throw new CitrusRuntimeException("Failed to load Json schema", e);
8394
}
8495
}
96+
8597
public Resource getJson() {
8698
return json;
8799
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package org.citrusframework.json.schema;
2+
3+
import static org.testng.Assert.assertEquals;
4+
import static org.testng.Assert.assertNotNull;
5+
import static org.testng.Assert.assertNull;
6+
import static org.testng.Assert.assertThrows;
7+
import java.io.ByteArrayInputStream;
8+
import java.io.InputStream;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.UUID;
12+
13+
import org.citrusframework.exceptions.CitrusRuntimeException;
14+
import org.citrusframework.spi.Resource;
15+
import org.citrusframework.spi.Resources.ByteArrayResource;
16+
import org.citrusframework.spi.Resources.ClasspathResource;
17+
import org.citrusframework.spi.Resources.FileSystemResource;
18+
import org.citrusframework.spi.Resources.UrlResource;
19+
import org.testng.annotations.Test;
20+
21+
public class SimpleJsonSchemaTest {
22+
23+
private static final String VALID_JSON_SCHEMA = """
24+
{
25+
"$schema": "http://json-schema.org/draft-07/schema#",
26+
"type": "object",
27+
"properties": {
28+
"name": { "type": "string" }
29+
}
30+
}
31+
""";
32+
33+
private static final String INVALID_JSON_SCHEMA = "{ not valid json schema";
34+
35+
@Test
36+
public void constructor_withResource_setsJsonField() {
37+
Resource resource = createByteArrayResource(VALID_JSON_SCHEMA);
38+
39+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);
40+
41+
assertEquals(simpleJsonSchema.getJson(), resource);
42+
assertNull(simpleJsonSchema.getSchema());
43+
}
44+
45+
@Test
46+
public void defaultConstructor_leavesFieldsUninitialized() {
47+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();
48+
49+
assertNull(simpleJsonSchema.getJson());
50+
assertNull(simpleJsonSchema.getSchema());
51+
}
52+
53+
@Test
54+
public void initialize_withByteArrayResource_parsesSchema() {
55+
ByteArrayResource resource = createByteArrayResource(VALID_JSON_SCHEMA);
56+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);
57+
58+
simpleJsonSchema.initialize();
59+
60+
assertNotNull(simpleJsonSchema.getSchema());
61+
}
62+
63+
@Test
64+
public void initialize_withUrlResource_parsesSchema() throws Exception {
65+
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
66+
URL url = resource.getFile().toURI().toURL();
67+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(new UrlResource(url));
68+
69+
simpleJsonSchema.initialize();
70+
71+
assertNotNull(simpleJsonSchema.getSchema());
72+
}
73+
74+
@Test
75+
public void initialize_withClasspathResource_parsesSchema() {
76+
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
77+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);
78+
79+
simpleJsonSchema.initialize();
80+
81+
assertNotNull(simpleJsonSchema.getSchema());
82+
}
83+
84+
@Test
85+
public void initialize_withFileSystemResource_parsesSchema() {
86+
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/jsonSchema.json");
87+
FileSystemResource fileSystemResource = new FileSystemResource(resource.getFile());
88+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(fileSystemResource);
89+
90+
simpleJsonSchema.initialize();
91+
92+
assertNotNull(simpleJsonSchema.getSchema());
93+
}
94+
95+
@Test
96+
public void initialize_withJarUrlResource_parsesSchema() throws Exception {
97+
ClasspathResource resource = new ClasspathResource("classpath:org/citrusframework/json/schema/test-schema.jar");
98+
99+
URL url = resource.getFile().toURI().toURL();
100+
URL jarUrl = new URL("jar:"+url+"!/jsonschema.json");
101+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(new UrlResource(jarUrl));
102+
103+
simpleJsonSchema.initialize();
104+
105+
assertNotNull(simpleJsonSchema.getSchema());
106+
}
107+
108+
@Test
109+
public void initialize_withUrlResourceFallback_usesInputStream() throws Exception {
110+
UrlResource urlResource = new UrlResource(new URL("http://"+ UUID.randomUUID().toString())) {
111+
@Override
112+
public InputStream getInputStream() {
113+
return new ByteArrayInputStream(VALID_JSON_SCHEMA.getBytes(StandardCharsets.UTF_8));
114+
}
115+
};
116+
117+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(urlResource);
118+
119+
simpleJsonSchema.initialize();
120+
121+
assertNotNull(simpleJsonSchema.getSchema());
122+
}
123+
124+
@Test
125+
public void initialize_withInvalidSchema_throwsCitrusRuntimeException() {
126+
ByteArrayResource resource = createByteArrayResource(INVALID_JSON_SCHEMA);
127+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource);
128+
129+
assertThrows(CitrusRuntimeException.class, simpleJsonSchema::initialize);
130+
}
131+
132+
@Test
133+
public void setJson_updatesJsonField() {
134+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();
135+
Resource resource = createByteArrayResource(VALID_JSON_SCHEMA);
136+
137+
simpleJsonSchema.setJson(resource);
138+
139+
assertEquals(simpleJsonSchema.getJson(), resource);
140+
}
141+
142+
@Test
143+
public void equals_sameInstance_returnsTrue() {
144+
SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema();
145+
146+
assertEquals(simpleJsonSchema, simpleJsonSchema);
147+
}
148+
149+
private ByteArrayResource createByteArrayResource(String content) {
150+
return new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
151+
}
152+
153+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"name": { "type": "string" }
6+
}
7+
}
Binary file not shown.

0 commit comments

Comments
 (0)