Skip to content

Commit 9c55de8

Browse files
committed
add test cases and update readme.md
1 parent 461f4e0 commit 9c55de8

File tree

4 files changed

+200
-5
lines changed

4 files changed

+200
-5
lines changed

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,91 @@ Add the following to your `pom.xml`:
4444

4545
## Quickstart
4646

47+
To use the validator, you need to have both JsonSchema object and JsonNode object constructed and there are many ways to do that. Here is my
48+
base test class that shows you several way to construct these from String, Stream, Url and JsonNode. Pay attention on JsonSchemaFactory class
49+
as it is the way to construct JsonSchema object.
50+
51+
52+
```
53+
/*
54+
* Copyright (c) 2016 Network New Technologies Inc.
55+
*
56+
* Licensed under the Apache License, Version 2.0 (the "License");
57+
* You may not use this file except in compliance with the License.
58+
* You may obtain a copy of the License at
59+
*
60+
* http://www.apache.org/licenses/LICENSE-2.0
61+
*
62+
* Unless required by applicable law or agreed to in writing, software
63+
* distributed under the License is distributed on an "AS IS" BASIS,
64+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65+
* See the License for the specific language governing permissions and
66+
* limitations under the License.
67+
*/
68+
69+
package com.networknt.schema;
70+
71+
import com.fasterxml.jackson.databind.JsonNode;
72+
import com.fasterxml.jackson.databind.ObjectMapper;
73+
74+
import java.io.InputStream;
75+
import java.net.URL;
76+
77+
/**
78+
* Created by steve on 22/10/16.
79+
*/
80+
public class BaseJsonSchemaValidatorTest {
81+
protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
82+
InputStream is1 = Thread.currentThread().getContextClassLoader()
83+
.getResourceAsStream(name);
84+
85+
ObjectMapper mapper = new ObjectMapper();
86+
JsonNode node = mapper.readTree(is1);
87+
return node;
88+
}
89+
90+
protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
91+
ObjectMapper mapper = new ObjectMapper();
92+
JsonNode node = mapper.readTree(content);
93+
return node;
94+
}
95+
96+
protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
97+
ObjectMapper mapper = new ObjectMapper();
98+
JsonNode node = mapper.readTree(new URL(url));
99+
return node;
100+
}
101+
102+
protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception {
103+
JsonSchemaFactory factory = new JsonSchemaFactory();
104+
InputStream is = Thread.currentThread().getContextClassLoader()
105+
.getResourceAsStream(name);
106+
JsonSchema schema = factory.getSchema(is);
107+
return schema;
108+
}
109+
110+
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception {
111+
JsonSchemaFactory factory = new JsonSchemaFactory();
112+
JsonSchema schema = factory.getSchema(schemaContent);
113+
return schema;
114+
}
115+
116+
protected JsonSchema getJsonSchemaFromUrl(String url) throws Exception {
117+
JsonSchemaFactory factory = new JsonSchemaFactory();
118+
JsonSchema schema = factory.getSchema(new URL(url));
119+
return schema;
120+
}
121+
122+
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception {
123+
JsonSchemaFactory factory = new JsonSchemaFactory();
124+
JsonSchema schema = factory.getSchema(jsonNode);
125+
return schema;
126+
}
127+
}
128+
129+
```
130+
And the following is one of the test case in one of the test class extends from above base class. As you can see, it constructs JsonSchema
131+
and JsonNode from String.
47132

48133
```
49134
JsonSchema schema = getJsonSchemaFromStringContent("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}");
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2016 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+
22+
import java.io.InputStream;
23+
import java.net.URL;
24+
25+
/**
26+
* Created by steve on 22/10/16.
27+
*/
28+
public class BaseJsonSchemaValidatorTest {
29+
protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
30+
InputStream is1 = Thread.currentThread().getContextClassLoader()
31+
.getResourceAsStream(name);
32+
33+
ObjectMapper mapper = new ObjectMapper();
34+
JsonNode node = mapper.readTree(is1);
35+
return node;
36+
}
37+
38+
protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
39+
ObjectMapper mapper = new ObjectMapper();
40+
JsonNode node = mapper.readTree(content);
41+
return node;
42+
}
43+
44+
protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
45+
ObjectMapper mapper = new ObjectMapper();
46+
JsonNode node = mapper.readTree(new URL(url));
47+
return node;
48+
}
49+
50+
protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception {
51+
JsonSchemaFactory factory = new JsonSchemaFactory();
52+
InputStream is = Thread.currentThread().getContextClassLoader()
53+
.getResourceAsStream(name);
54+
JsonSchema schema = factory.getSchema(is);
55+
return schema;
56+
}
57+
58+
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception {
59+
JsonSchemaFactory factory = new JsonSchemaFactory();
60+
JsonSchema schema = factory.getSchema(schemaContent);
61+
return schema;
62+
}
63+
64+
protected JsonSchema getJsonSchemaFromUrl(String url) throws Exception {
65+
JsonSchemaFactory factory = new JsonSchemaFactory();
66+
JsonSchema schema = factory.getSchema(new URL(url));
67+
return schema;
68+
}
69+
70+
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception {
71+
JsonSchemaFactory factory = new JsonSchemaFactory();
72+
JsonSchema schema = factory.getSchema(jsonNode);
73+
return schema;
74+
}
75+
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,6 @@ public void testNotValidator() throws Exception {
181181
runTestFile("tests/not.json");
182182
}
183183

184-
@Test
185-
public void testNullAndFormatValidator() throws Exception {
186-
runTestFile("tests/nullAndFormat.json");
187-
}
188-
189184
@Test
190185
public void testOneOfValidator() throws Exception {
191186
runTestFile("tests/oneOf.json");
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016 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 org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import java.util.List;
24+
import java.util.Set;
25+
26+
/**
27+
* Created by steve on 22/10/16.
28+
*/
29+
public class PatternPropertiesValidatorTest extends BaseJsonSchemaValidatorTest {
30+
31+
@Test(expected=JsonSchemaException.class)
32+
public void testInvalidPatternPropertiesValidator() throws Exception {
33+
JsonSchemaFactory factory = new JsonSchemaFactory();
34+
JsonSchema schema = factory.getSchema("{\"patternProperties\":6}");
35+
36+
JsonNode node = getJsonNodeFromStringContent("");
37+
Set<ValidationMessage> errors = schema.validate(node);
38+
Assert.assertEquals(errors.size(), 0);
39+
}
40+
}

0 commit comments

Comments
 (0)