Skip to content

Commit dbdc79c

Browse files
fduttonfdutton
and
fdutton
authored
Updates tests from JSON Schema Test Suite commit 987a4c8fc4468f37c555db362f5de5f9052a13ff (#701)
Co-authored-by: fdutton <fdutton@noreply>
1 parent 4cf4783 commit dbdc79c

File tree

512 files changed

+70349
-23495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

512 files changed

+70349
-23495
lines changed

src/test/java/com/networknt/schema/BaseJsonSchemaValidatorTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,48 @@ public class BaseJsonSchemaValidatorTest {
3232

3333
private static final ObjectMapper mapper = new ObjectMapper();
3434

35-
protected JsonNode getJsonNodeFromClasspath(String name) throws IOException {
35+
public static JsonNode getJsonNodeFromClasspath(String name) throws IOException {
3636
InputStream is1 = Thread.currentThread().getContextClassLoader()
3737
.getResourceAsStream(name);
3838
return mapper.readTree(is1);
3939
}
4040

41-
protected JsonNode getJsonNodeFromStringContent(String content) throws IOException {
41+
public static JsonNode getJsonNodeFromStringContent(String content) throws IOException {
4242
return mapper.readTree(content);
4343
}
4444

45-
protected JsonNode getJsonNodeFromUrl(String url) throws IOException {
45+
public static JsonNode getJsonNodeFromUrl(String url) throws IOException {
4646
return mapper.readTree(new URL(url));
4747
}
4848

49-
protected static JsonSchema getJsonSchemaFromClasspath(String name) {
49+
public static JsonSchema getJsonSchemaFromClasspath(String name) {
5050
return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4);
5151
}
5252

53-
protected static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
53+
public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
5454
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
5555
InputStream is = Thread.currentThread().getContextClassLoader()
5656
.getResourceAsStream(name);
5757
return factory.getSchema(is);
5858
}
5959

60-
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
60+
public static JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
6161
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
6262
return factory.getSchema(schemaContent);
6363
}
6464

65-
protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
65+
public static JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
6666
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
6767
return factory.getSchema(new URI(uri));
6868
}
6969

70-
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
70+
public static JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
7171
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
7272
return factory.getSchema(jsonNode);
7373
}
7474

7575
// Automatically detect version for given JsonNode
76-
protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
76+
public static JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
7777
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
7878
return factory.getSchema(jsonNode);
7979
}

src/test/java/com/networknt/schema/Issue619Test.java

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
package com.networknt.schema;
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
19-
import io.undertow.Undertow;
20-
import io.undertow.server.handlers.resource.FileResourceManager;
19+
2120
import org.junit.jupiter.api.BeforeEach;
2221
import org.junit.jupiter.api.Test;
2322

24-
import java.io.File;
2523
import java.net.URI;
2624

27-
import static io.undertow.Handlers.resource;
28-
import static org.junit.jupiter.api.Assertions.*;
25+
import static com.networknt.schema.BaseJsonSchemaValidatorTest.getJsonNodeFromStringContent;
26+
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
2930

30-
public class Issue619Test extends BaseJsonSchemaValidatorTest {
31+
public class Issue619Test extends HTTPServiceSupport {
3132

3233
private JsonSchemaFactory factory;
3334
private JsonNode one;
@@ -116,51 +117,22 @@ public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Uri(
116117

117118
@Test
118119
public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Ref() throws Exception {
119-
runLocalServer(() -> {
120-
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
121-
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
122-
123-
JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:tests/draft4/refRemote.json#/3/schema\" }");
124-
assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty());
125-
assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty());
126-
});
120+
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
121+
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
122+
123+
JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:tests/draft4/refRemote.json#/3/schema\" }");
124+
assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty());
125+
assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty());
127126
}
128127

129128
@Test
130129
public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Uri() throws Exception {
131-
runLocalServer(() -> {
132-
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
133-
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
134-
135-
JsonSchema schemaWithIdFromUri = factory.getSchema(new URI("resource:tests/draft4/refRemote.json#/3/schema"));
136-
assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty());
137-
assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty());
138-
});
139-
}
130+
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
131+
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
140132

141-
private interface ThrowingRunnable {
142-
void run() throws Exception;
143-
}
144-
145-
private void runLocalServer(ThrowingRunnable actualTest) throws Exception {
146-
Undertow server = Undertow.builder()
147-
.addHttpListener(1234, "localhost")
148-
.setHandler(resource(new FileResourceManager(
149-
new File("./src/test/resources/remotes"), 100)))
150-
.build();
151-
try {
152-
server.start();
153-
154-
actualTest.run();
155-
156-
} finally {
157-
try {
158-
Thread.sleep(100);
159-
} catch (InterruptedException ignored) {
160-
Thread.currentThread().interrupt();
161-
}
162-
server.stop();
163-
}
133+
JsonSchema schemaWithIdFromUri = factory.getSchema(new URI("resource:tests/draft4/refRemote.json#/3/schema"));
134+
assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty());
135+
assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty());
164136
}
165137

166138
@Test
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.networknt.schema;
2+
3+
import com.networknt.schema.SpecVersion.VersionFlag;
4+
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.DynamicNode;
7+
import org.junit.jupiter.api.TestFactory;
8+
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.stream.Stream;
12+
13+
@DisplayName("JSON Schema Test Suite Extras")
14+
class JsonSchemaTestSuiteExtrasTest extends AbstractJsonSchemaTestSuite {
15+
16+
private static final Path excluded = Paths.get("src/test/resources/draft4/relativeRefRemote.json");
17+
18+
@TestFactory
19+
@DisplayName("Draft 2020-12")
20+
Stream<DynamicNode> draft2022012() {
21+
return createTests(VersionFlag.V202012, "src/test/resources/draft2020-12");
22+
}
23+
24+
@TestFactory
25+
@DisplayName("Draft 2019-09")
26+
Stream<DynamicNode> draft201909() {
27+
return createTests(VersionFlag.V201909, "src/test/resources/draft2019-09");
28+
}
29+
30+
@TestFactory
31+
@DisplayName("Draft 7")
32+
Stream<DynamicNode> draft7() {
33+
return createTests(VersionFlag.V7, "src/test/resources/draft7");
34+
}
35+
36+
@TestFactory
37+
@DisplayName("Draft 6")
38+
Stream<DynamicNode> draft6() {
39+
return createTests(VersionFlag.V6, "src/test/resources/draft6");
40+
}
41+
42+
@TestFactory
43+
@DisplayName("Draft 4")
44+
Stream<DynamicNode> draft4() {
45+
return createTests(VersionFlag.V4, "src/test/resources/draft4");
46+
}
47+
48+
@Override
49+
protected boolean enabled(Path path) {
50+
return !excluded.equals(path);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)