Skip to content

Commit 3c675e6

Browse files
Walk Changes
1 parent a47f34f commit 3c675e6

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected boolean runPreWalkListeners(List<WalkListener> walkListeners, WalkEven
2626
boolean continueRunningListenersAndWalk = true;
2727
if (walkListeners != null) {
2828
for (WalkListener walkListener : walkListeners) {
29-
if (WalkMethodInvocation.SKIP_WALK.equals(walkListener.onWalkStart(walkEvent))) {
29+
if (WalkFlow.SKIP.equals(walkListener.onWalkStart(walkEvent))) {
3030
continueRunningListenersAndWalk = false;
3131
break;
3232
}

src/main/java/com/networknt/schema/walk/WalkMethodInvocation.java renamed to src/main/java/com/networknt/schema/walk/WalkFlow.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.networknt.schema.walk;
22

3-
public enum WalkMethodInvocation {
3+
public enum WalkFlow {
44

5-
SKIP_WALK("SkipWalk", "Skip the walk methods"),
5+
SKIP("SkipWalk", "Skip the walk methods"),
66

7-
CONTINUE_TO_WALK("ContinueToWalk", "continue to invoke the walk method");
7+
CONTINUE("ContinueToWalk", "continue to invoke the walk method");
88

99
private String name;
1010

1111
private String description;
1212

13-
WalkMethodInvocation(String name, String description) {
13+
WalkFlow(String name, String description) {
1414
this.name = name;
1515
this.description = description;
1616
}

src/main/java/com/networknt/schema/walk/WalkListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public interface WalkListener {
1313

14-
public WalkMethodInvocation onWalkStart(WalkEvent walkEvent);
14+
public WalkFlow onWalkStart(WalkEvent walkEvent);
1515

1616
public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMessages);
1717
}

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.networknt.schema;
22

3-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.Assert.assertEquals;
44

55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.util.LinkedHashSet;
88
import java.util.Set;
99
import java.util.TreeSet;
1010

11-
import com.networknt.schema.walk.WalkMethodInvocation;
11+
import com.networknt.schema.walk.WalkFlow;
1212
import org.junit.Before;
1313
import org.junit.Test;
1414

@@ -27,13 +27,12 @@ public class JsonWalkTest {
2727
private static final String CUSTOM_KEYWORD = "custom-keyword";
2828

2929
@Before
30-
public void setup() throws Exception {
30+
public void setup() {
3131
setupSchema();
3232
}
3333

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();
3736
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
3837
schemaValidatorsConfig.addKeywordWalkListener(new AllKeywordListener());
3938
schemaValidatorsConfig.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener());
@@ -45,10 +44,10 @@ private void setupSchema() throws Exception {
4544
this.jsonSchema = schemaFactory.getSchema(getSchema(), schemaValidatorsConfig);
4645
}
4746

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())
5050
.addKeyword(new CustomKeyword()).build();
51-
return jsonMetaSchema;
5251
}
5352

5453
@Test
@@ -57,7 +56,7 @@ public void testWalk() throws IOException {
5756
ValidationResult result = jsonSchema.walk(
5857
objectMapper.readTree(getClass().getClassLoader().getResourceAsStream("data/walk-data.json")), false);
5958
JsonNode collectedNode = (JsonNode) result.getCollectorContext().get(SAMPLE_COLLECTOR);
60-
assertTrue(collectedNode.equals(objectMapper.readTree("{" +
59+
assertEquals(collectedNode, (objectMapper.readTree("{" +
6160
" \"PROPERTY1\": \"sample1\","
6261
+ " \"PROPERTY2\": \"sample2\","
6362
+ " \"property3\": {"
@@ -77,15 +76,15 @@ private InputStream getSchema() {
7776
/**
7877
* Our own custom keyword.
7978
*/
80-
private class CustomKeyword implements Keyword {
79+
private static class CustomKeyword implements Keyword {
8180
@Override
8281
public String getValue() {
8382
return "custom-keyword";
8483
}
8584

8685
@Override
8786
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
88-
ValidationContext validationContext) throws JsonSchemaException, Exception {
87+
ValidationContext validationContext) throws JsonSchemaException {
8988
if (schemaNode != null && schemaNode.isArray()) {
9089
return new CustomValidator();
9190
}
@@ -98,7 +97,7 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
9897
* This will be helpful in cases where we don't want to revisit the entire JSON
9998
* document again just for gathering this kind of information.
10099
*/
101-
private class CustomValidator implements JsonValidator {
100+
private static class CustomValidator implements JsonValidator {
102101

103102
@Override
104103
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
@@ -118,9 +117,9 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
118117
}
119118
}
120119

121-
private class AllKeywordListener implements WalkListener {
120+
private static class AllKeywordListener implements WalkListener {
122121
@Override
123-
public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
122+
public WalkFlow onWalkStart(WalkEvent keywordWalkEvent) {
124123
ObjectMapper mapper = new ObjectMapper();
125124
String keyWordName = keywordWalkEvent.getKeyWordName();
126125
JsonNode schemaNode = keywordWalkEvent.getSchemaNode();
@@ -133,7 +132,7 @@ public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
133132
objectNode.put(keywordWalkEvent.getSchemaNode().get("title").textValue().toUpperCase(),
134133
keywordWalkEvent.getNode().textValue());
135134
}
136-
return WalkMethodInvocation.CONTINUE_TO_WALK;
135+
return WalkFlow.CONTINUE;
137136
}
138137

139138
@Override
@@ -142,10 +141,10 @@ public void onWalkEnd(WalkEvent keywordWalkEvent, Set<ValidationMessage> validat
142141
}
143142
}
144143

145-
private class RefKeywordListener implements WalkListener {
144+
private static class RefKeywordListener implements WalkListener {
146145

147146
@Override
148-
public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
147+
public WalkFlow onWalkStart(WalkEvent keywordWalkEvent) {
149148
ObjectMapper mapper = new ObjectMapper();
150149
CollectorContext collectorContext = CollectorContext.getInstance();
151150
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
@@ -154,7 +153,7 @@ public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
154153
ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_COLLECTOR);
155154
objectNode.set(keywordWalkEvent.getSchemaNode().get("title").textValue().toLowerCase(),
156155
keywordWalkEvent.getNode());
157-
return WalkMethodInvocation.SKIP_WALK;
156+
return WalkFlow.SKIP;
158157
}
159158

160159
@Override
@@ -163,15 +162,15 @@ public void onWalkEnd(WalkEvent keywordWalkEvent, Set<ValidationMessage> validat
163162
}
164163
}
165164

166-
private class PropertiesKeywordListener implements WalkListener {
165+
private static class PropertiesKeywordListener implements WalkListener {
167166

168167
@Override
169-
public WalkMethodInvocation onWalkStart(WalkEvent keywordWalkEvent) {
168+
public WalkFlow onWalkStart(WalkEvent keywordWalkEvent) {
170169
JsonNode schemaNode = keywordWalkEvent.getSchemaNode();
171170
if (schemaNode.get("title").textValue().equals("Property3")) {
172-
return WalkMethodInvocation.SKIP_WALK;
171+
return WalkFlow.SKIP;
173172
}
174-
return WalkMethodInvocation.CONTINUE_TO_WALK;
173+
return WalkFlow.CONTINUE;
175174
}
176175

177176
@Override

0 commit comments

Comments
 (0)