Skip to content

Commit a03b92c

Browse files
authored
Refactor JSON Schema Test Suite tests (networknt#449)
1 parent 9e6b137 commit a03b92c

File tree

5 files changed

+132
-395
lines changed

5 files changed

+132
-395
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2020 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.node.ArrayNode;
22+
import io.undertow.Undertow;
23+
import io.undertow.server.handlers.resource.FileResourceManager;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.BeforeAll;
26+
27+
import java.io.File;
28+
import java.io.InputStream;
29+
import java.net.URI;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
import static io.undertow.Handlers.resource;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
36+
public abstract class BaseSuiteJsonSchemaTest {
37+
protected ObjectMapper mapper = new ObjectMapper();
38+
protected JsonSchemaFactory validatorFactory;
39+
protected static Undertow server = null;
40+
41+
protected BaseSuiteJsonSchemaTest(SpecVersion.VersionFlag version) {
42+
validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(version)).objectMapper(mapper).build();
43+
}
44+
45+
@BeforeAll
46+
public static void setUp() {
47+
if (server == null) {
48+
server = Undertow.builder()
49+
.addHttpListener(1234, "localhost")
50+
.setHandler(resource(new FileResourceManager(
51+
new File("./src/test/resources/remotes"), 100)))
52+
.build();
53+
server.start();
54+
}
55+
}
56+
57+
@AfterAll
58+
public static void tearDown() throws Exception {
59+
if (server != null) {
60+
try {
61+
Thread.sleep(100);
62+
} catch (InterruptedException ignored) {
63+
64+
}
65+
server.stop();
66+
server = null;
67+
}
68+
}
69+
70+
protected void runTestFile(String testCaseFile) throws Exception {
71+
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
72+
InputStream in = Thread.currentThread().getContextClassLoader()
73+
.getResourceAsStream(testCaseFile);
74+
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);
75+
76+
for (int j = 0; j < testCases.size(); j++) {
77+
try {
78+
JsonNode testCase = testCases.get(j);
79+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
80+
81+
ArrayNode testNodes = (ArrayNode) testCase.get("tests");
82+
for (int i = 0; i < testNodes.size(); i++) {
83+
JsonNode test = testNodes.get(i);
84+
JsonNode node = test.get("data");
85+
JsonNode typeLooseNode = test.get("isTypeLoose");
86+
// Configure the schemaValidator to set typeLoose's value based on the test file,
87+
// if test file do not contains typeLoose flag, use default value: true.
88+
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
89+
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
90+
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
91+
92+
errors.addAll(schema.validate(node));
93+
94+
if (test.get("valid").asBoolean()) {
95+
if (!errors.isEmpty()) {
96+
System.out.println("---- test case failed ----");
97+
System.out.println("schema: " + schema.toString());
98+
System.out.println("data: " + test.get("data"));
99+
}
100+
assertEquals(0, errors.size());
101+
} else {
102+
if (errors.isEmpty()) {
103+
System.out.println("---- test case failed ----");
104+
System.out.println("schema: " + schema);
105+
System.out.println("data: " + test.get("data"));
106+
} else {
107+
JsonNode errorCount = test.get("errorCount");
108+
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
109+
System.out.println("---- test case failed ----");
110+
System.out.println("schema: " + schema);
111+
System.out.println("data: " + test.get("data"));
112+
System.out.println("errors: " + errors);
113+
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
114+
}
115+
}
116+
assertEquals(false, errors.isEmpty());
117+
}
118+
}
119+
} catch (JsonSchemaException e) {
120+
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
121+
}
122+
}
123+
}
124+
}

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

Lines changed: 2 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -15,110 +15,12 @@
1515
*/
1616
package com.networknt.schema;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
19-
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import com.fasterxml.jackson.databind.node.ArrayNode;
21-
import io.undertow.Undertow;
22-
import io.undertow.server.handlers.resource.FileResourceManager;
23-
import org.junit.jupiter.api.AfterAll;
24-
import org.junit.jupiter.api.BeforeAll;
2518
import org.junit.jupiter.api.Disabled;
2619
import org.junit.jupiter.api.Test;
2720

28-
import java.io.File;
29-
import java.io.InputStream;
30-
import java.net.URI;
31-
import java.util.ArrayList;
32-
import java.util.List;
33-
34-
import static io.undertow.Handlers.resource;
35-
import static org.junit.jupiter.api.Assertions.assertEquals;
36-
37-
public class V201909JsonSchemaTest {
38-
protected ObjectMapper mapper = new ObjectMapper();
39-
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).objectMapper(mapper).build();
40-
protected static Undertow server = null;
41-
21+
public class V201909JsonSchemaTest extends BaseSuiteJsonSchemaTest {
4222
public V201909JsonSchemaTest() {
43-
}
44-
45-
@BeforeAll
46-
public static void setUp() {
47-
if (server == null) {
48-
server = Undertow.builder()
49-
.addHttpListener(1234, "localhost")
50-
.setHandler(resource(new FileResourceManager(
51-
new File("./src/test/resources/remotes"), 100)))
52-
.build();
53-
server.start();
54-
}
55-
}
56-
57-
@AfterAll
58-
public static void tearDown() throws Exception {
59-
if (server != null) {
60-
try {
61-
Thread.sleep(100);
62-
} catch (InterruptedException ignored) {
63-
64-
}
65-
server.stop();
66-
}
67-
}
68-
69-
private void runTestFile(String testCaseFile) throws Exception {
70-
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
71-
InputStream in = Thread.currentThread().getContextClassLoader()
72-
.getResourceAsStream(testCaseFile);
73-
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);
74-
75-
for (int j = 0; j < testCases.size(); j++) {
76-
try {
77-
JsonNode testCase = testCases.get(j);
78-
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
79-
80-
ArrayNode testNodes = (ArrayNode) testCase.get("tests");
81-
for (int i = 0; i < testNodes.size(); i++) {
82-
JsonNode test = testNodes.get(i);
83-
JsonNode node = test.get("data");
84-
JsonNode typeLooseNode = test.get("isTypeLoose");
85-
// Configure the schemaValidator to set typeLoose's value based on the test file,
86-
// if test file do not contains typeLoose flag, use default value: true.
87-
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
88-
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
89-
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
90-
91-
errors.addAll(schema.validate(node));
92-
93-
if (test.get("valid").asBoolean()) {
94-
if (!errors.isEmpty()) {
95-
System.out.println("---- test case failed ----");
96-
System.out.println("schema: " + schema.toString());
97-
System.out.println("data: " + test.get("data"));
98-
}
99-
assertEquals(0, errors.size());
100-
} else {
101-
if (errors.isEmpty()) {
102-
System.out.println("---- test case failed ----");
103-
System.out.println("schema: " + schema);
104-
System.out.println("data: " + test.get("data"));
105-
} else {
106-
JsonNode errorCount = test.get("errorCount");
107-
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
108-
System.out.println("---- test case failed ----");
109-
System.out.println("schema: " + schema);
110-
System.out.println("data: " + test.get("data"));
111-
System.out.println("errors: " + errors);
112-
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
113-
}
114-
}
115-
assertEquals(false, errors.isEmpty());
116-
}
117-
}
118-
} catch (JsonSchemaException e) {
119-
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
120-
}
121-
}
23+
super(SpecVersion.VersionFlag.V201909);
12224
}
12325

12426
@Test

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

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,110 +18,17 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.fasterxml.jackson.databind.node.ArrayNode;
22-
import io.undertow.Undertow;
23-
import io.undertow.server.handlers.resource.FileResourceManager;
24-
import org.junit.jupiter.api.AfterAll;
25-
import org.junit.jupiter.api.BeforeAll;
2621
import org.junit.jupiter.api.Test;
2722

28-
import java.io.File;
2923
import java.io.IOException;
30-
import java.io.InputStream;
31-
import java.net.URI;
3224
import java.net.URL;
33-
import java.util.ArrayList;
34-
import java.util.List;
3525
import java.util.Set;
3626

37-
import static io.undertow.Handlers.resource;
3827
import static org.junit.jupiter.api.Assertions.*;
3928

40-
public class V4JsonSchemaTest {
41-
protected ObjectMapper mapper = new ObjectMapper();
42-
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).objectMapper(mapper).build();
43-
protected static Undertow server = null;
44-
29+
public class V4JsonSchemaTest extends BaseSuiteJsonSchemaTest {
4530
public V4JsonSchemaTest() {
46-
}
47-
48-
@BeforeAll
49-
public static void setUp() {
50-
if (server == null) {
51-
server = Undertow.builder()
52-
.addHttpListener(1234, "localhost")
53-
.setHandler(resource(new FileResourceManager(
54-
new File("./src/test/resources/remotes"), 100)))
55-
.build();
56-
server.start();
57-
}
58-
}
59-
60-
@AfterAll
61-
public static void tearDown() throws Exception {
62-
if (server != null) {
63-
try {
64-
Thread.sleep(100);
65-
} catch (InterruptedException ignored) {
66-
67-
}
68-
server.stop();
69-
}
70-
}
71-
72-
private void runTestFile(String testCaseFile) throws Exception {
73-
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
74-
InputStream in = Thread.currentThread().getContextClassLoader()
75-
.getResourceAsStream(testCaseFile);
76-
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);
77-
78-
for (int j = 0; j < testCases.size(); j++) {
79-
try {
80-
JsonNode testCase = testCases.get(j);
81-
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
82-
83-
ArrayNode testNodes = (ArrayNode) testCase.get("tests");
84-
for (int i = 0; i < testNodes.size(); i++) {
85-
JsonNode test = testNodes.get(i);
86-
JsonNode node = test.get("data");
87-
JsonNode typeLooseNode = test.get("isTypeLoose");
88-
// Configure the schemaValidator to set typeLoose's value based on the test file,
89-
// if test file do not contains typeLoose flag, use default value: true.
90-
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
91-
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
92-
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
93-
94-
errors.addAll(schema.validate(node));
95-
96-
if (test.get("valid").asBoolean()) {
97-
if (!errors.isEmpty()) {
98-
System.out.println("---- test case failed ----");
99-
System.out.println("schema: " + schema.toString());
100-
System.out.println("data: " + test.get("data"));
101-
}
102-
assertEquals(0, errors.size());
103-
} else {
104-
if (errors.isEmpty()) {
105-
System.out.println("---- test case failed ----");
106-
System.out.println("schema: " + schema);
107-
System.out.println("data: " + test.get("data"));
108-
} else {
109-
JsonNode errorCount = test.get("errorCount");
110-
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
111-
System.out.println("---- test case failed ----");
112-
System.out.println("schema: " + schema);
113-
System.out.println("data: " + test.get("data"));
114-
System.out.println("errors: " + errors);
115-
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
116-
}
117-
}
118-
assertEquals(false, errors.isEmpty());
119-
}
120-
}
121-
} catch (JsonSchemaException e) {
122-
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
123-
}
124-
}
31+
super(SpecVersion.VersionFlag.V4);
12532
}
12633

12734
@Test(/*expected = java.lang.StackOverflowError.class*/)

0 commit comments

Comments
 (0)