1
1
package com .networknt .schema ;
2
2
3
- import static org .junit .Assert .assertTrue ;
3
+ import static org .junit .Assert .assertEquals ;
4
4
5
5
import java .io .IOException ;
6
6
import java .io .InputStream ;
7
7
import java .util .LinkedHashSet ;
8
8
import java .util .Set ;
9
9
import java .util .TreeSet ;
10
10
11
- import com .networknt .schema .walk .WalkMethodInvocation ;
11
+ import com .networknt .schema .walk .WalkFlow ;
12
12
import org .junit .Before ;
13
13
import org .junit .Test ;
14
14
@@ -27,13 +27,12 @@ public class JsonWalkTest {
27
27
private static final String CUSTOM_KEYWORD = "custom-keyword" ;
28
28
29
29
@ Before
30
- public void setup () throws Exception {
30
+ public void setup () {
31
31
setupSchema ();
32
32
}
33
33
34
- private void setupSchema () throws Exception {
35
- final JsonMetaSchema metaSchema = getJsonMetaSchema (
36
- "https://github.com/networknt/json-schema-validator/tests/schemas/example01" );
34
+ private void setupSchema () {
35
+ final JsonMetaSchema metaSchema = getJsonMetaSchema ();
37
36
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig ();
38
37
schemaValidatorsConfig .addKeywordWalkListener (new AllKeywordListener ());
39
38
schemaValidatorsConfig .addKeywordWalkListener (ValidatorTypeCode .REF .getValue (), new RefKeywordListener ());
@@ -45,10 +44,10 @@ private void setupSchema() throws Exception {
45
44
this .jsonSchema = schemaFactory .getSchema (getSchema (), schemaValidatorsConfig );
46
45
}
47
46
48
- private JsonMetaSchema getJsonMetaSchema (String uri ) throws Exception {
49
- JsonMetaSchema jsonMetaSchema = JsonMetaSchema .builder (uri , JsonMetaSchema .getV201909 ())
47
+ private JsonMetaSchema getJsonMetaSchema () {
48
+ return JsonMetaSchema .builder (
49
+ "https://github.com/networknt/json-schema-validator/tests/schemas/example01" , JsonMetaSchema .getV201909 ())
50
50
.addKeyword (new CustomKeyword ()).build ();
51
- return jsonMetaSchema ;
52
51
}
53
52
54
53
@ Test
@@ -57,7 +56,7 @@ public void testWalk() throws IOException {
57
56
ValidationResult result = jsonSchema .walk (
58
57
objectMapper .readTree (getClass ().getClassLoader ().getResourceAsStream ("data/walk-data.json" )), false );
59
58
JsonNode collectedNode = (JsonNode ) result .getCollectorContext ().get (SAMPLE_COLLECTOR );
60
- assertTrue (collectedNode . equals (objectMapper .readTree ("{" +
59
+ assertEquals (collectedNode , (objectMapper .readTree ("{" +
61
60
" \" PROPERTY1\" : \" sample1\" ,"
62
61
+ " \" PROPERTY2\" : \" sample2\" ,"
63
62
+ " \" property3\" : {"
@@ -77,15 +76,15 @@ private InputStream getSchema() {
77
76
/**
78
77
* Our own custom keyword.
79
78
*/
80
- private class CustomKeyword implements Keyword {
79
+ private static class CustomKeyword implements Keyword {
81
80
@ Override
82
81
public String getValue () {
83
82
return "custom-keyword" ;
84
83
}
85
84
86
85
@ Override
87
86
public JsonValidator newValidator (String schemaPath , JsonNode schemaNode , JsonSchema parentSchema ,
88
- ValidationContext validationContext ) throws JsonSchemaException , Exception {
87
+ ValidationContext validationContext ) throws JsonSchemaException {
89
88
if (schemaNode != null && schemaNode .isArray ()) {
90
89
return new CustomValidator ();
91
90
}
@@ -98,7 +97,7 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
98
97
* This will be helpful in cases where we don't want to revisit the entire JSON
99
98
* document again just for gathering this kind of information.
100
99
*/
101
- private class CustomValidator implements JsonValidator {
100
+ private static class CustomValidator implements JsonValidator {
102
101
103
102
@ Override
104
103
public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
@@ -118,9 +117,9 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
118
117
}
119
118
}
120
119
121
- private class AllKeywordListener implements WalkListener {
120
+ private static class AllKeywordListener implements WalkListener {
122
121
@ Override
123
- public WalkMethodInvocation onWalkStart (WalkEvent keywordWalkEvent ) {
122
+ public WalkFlow onWalkStart (WalkEvent keywordWalkEvent ) {
124
123
ObjectMapper mapper = new ObjectMapper ();
125
124
String keyWordName = keywordWalkEvent .getKeyWordName ();
126
125
JsonNode schemaNode = keywordWalkEvent .getSchemaNode ();
@@ -133,7 +132,7 @@ public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
133
132
objectNode .put (keywordWalkEvent .getSchemaNode ().get ("title" ).textValue ().toUpperCase (),
134
133
keywordWalkEvent .getNode ().textValue ());
135
134
}
136
- return WalkMethodInvocation . CONTINUE_TO_WALK ;
135
+ return WalkFlow . CONTINUE ;
137
136
}
138
137
139
138
@ Override
@@ -142,10 +141,10 @@ public void onWalkEnd(WalkEvent keywordWalkEvent, Set<ValidationMessage> validat
142
141
}
143
142
}
144
143
145
- private class RefKeywordListener implements WalkListener {
144
+ private static class RefKeywordListener implements WalkListener {
146
145
147
146
@ Override
148
- public WalkMethodInvocation onWalkStart (WalkEvent keywordWalkEvent ) {
147
+ public WalkFlow onWalkStart (WalkEvent keywordWalkEvent ) {
149
148
ObjectMapper mapper = new ObjectMapper ();
150
149
CollectorContext collectorContext = CollectorContext .getInstance ();
151
150
if (collectorContext .get (SAMPLE_COLLECTOR ) == null ) {
@@ -154,7 +153,7 @@ public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
154
153
ObjectNode objectNode = (ObjectNode ) collectorContext .get (SAMPLE_COLLECTOR );
155
154
objectNode .set (keywordWalkEvent .getSchemaNode ().get ("title" ).textValue ().toLowerCase (),
156
155
keywordWalkEvent .getNode ());
157
- return WalkMethodInvocation . SKIP_WALK ;
156
+ return WalkFlow . SKIP ;
158
157
}
159
158
160
159
@ Override
@@ -163,15 +162,15 @@ public void onWalkEnd(WalkEvent keywordWalkEvent, Set<ValidationMessage> validat
163
162
}
164
163
}
165
164
166
- private class PropertiesKeywordListener implements WalkListener {
165
+ private static class PropertiesKeywordListener implements WalkListener {
167
166
168
167
@ Override
169
- public WalkMethodInvocation onWalkStart (WalkEvent keywordWalkEvent ) {
168
+ public WalkFlow onWalkStart (WalkEvent keywordWalkEvent ) {
170
169
JsonNode schemaNode = keywordWalkEvent .getSchemaNode ();
171
170
if (schemaNode .get ("title" ).textValue ().equals ("Property3" )) {
172
- return WalkMethodInvocation . SKIP_WALK ;
171
+ return WalkFlow . SKIP ;
173
172
}
174
- return WalkMethodInvocation . CONTINUE_TO_WALK ;
173
+ return WalkFlow . CONTINUE ;
175
174
}
176
175
177
176
@ Override
0 commit comments