• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 the original author or authors.
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 package com.networknt.schema;
17 
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URISyntaxException;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 
29 import org.junit.jupiter.api.Test;
30 
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.networknt.schema.walk.JsonSchemaWalkListener;
34 import com.networknt.schema.walk.WalkEvent;
35 import com.networknt.schema.walk.WalkFlow;
36 
37 public class Issue467Test {
38     private static JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
39     private static String schemaPath = "/schema/issue467.json";
40 
41     protected ObjectMapper mapper = new ObjectMapper();
42 
43     @Test
shouldWalkKeywordWithValidation()44     public void shouldWalkKeywordWithValidation() throws URISyntaxException, IOException {
45         InputStream schemaInputStream = Issue467Test.class.getResourceAsStream(schemaPath);
46         final Set<JsonNodePath> properties = new LinkedHashSet<>();
47         final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
48         config.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(), new JsonSchemaWalkListener() {
49             @Override
50             public WalkFlow onWalkStart(WalkEvent walkEvent) {
51                 properties.add(walkEvent.getSchema().getEvaluationPath().append(walkEvent.getKeyword()));
52                 return WalkFlow.CONTINUE;
53             }
54 
55             @Override
56             public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> set) {
57             }
58         });
59         JsonSchema schema = factory.getSchema(schemaInputStream, config);
60         JsonNode data = mapper.readTree(Issue467Test.class.getResource("/data/issue467.json"));
61         ValidationResult result = schema.walk(data, true);
62         assertEquals(new HashSet<>(Arrays.asList("$.properties", "$.properties.tags.items[0].properties")),
63                 properties.stream().map(Object::toString).collect(Collectors.toSet()));
64         assertEquals(1, result.getValidationMessages().size());
65     }
66 
67     @Test
shouldWalkPropertiesWithValidation()68     public void shouldWalkPropertiesWithValidation() throws URISyntaxException, IOException {
69         InputStream schemaInputStream = Issue467Test.class.getResourceAsStream(schemaPath);
70         final Set<JsonNodePath> properties = new LinkedHashSet<>();
71         final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
72         config.addPropertyWalkListener(new JsonSchemaWalkListener() {
73             @Override
74             public WalkFlow onWalkStart(WalkEvent walkEvent) {
75                 properties.add(walkEvent.getSchema().getEvaluationPath());
76                 return WalkFlow.CONTINUE;
77             }
78 
79             @Override
80             public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> set) {
81             }
82         });
83         JsonSchema schema = factory.getSchema(schemaInputStream, config);
84         JsonNode data = mapper.readTree(Issue467Test.class.getResource("/data/issue467.json"));
85         ValidationResult result = schema.walk(data, true);
86         assertEquals(
87                 new HashSet<>(Arrays.asList("$.properties.tags", "$.properties.tags.items[0].properties.category", "$.properties.tags.items[0].properties.value")),
88                 properties.stream().map(Object::toString).collect(Collectors.toSet()));
89         assertEquals(1, result.getValidationMessages().size());
90     }
91 
92 }
93