5
5
import com .networknt .schema .walk .JsonSchemaWalkListener ;
6
6
import com .networknt .schema .walk .WalkEvent ;
7
7
import com .networknt .schema .walk .WalkFlow ;
8
+ import org .junit .jupiter .api .AfterEach ;
8
9
import org .junit .jupiter .api .Assertions ;
10
+ import org .junit .jupiter .api .Disabled ;
9
11
import org .junit .jupiter .api .Test ;
10
12
11
13
import java .io .InputStream ;
17
19
* Validating anyOf walker
18
20
*/
19
21
public class Issue451Test {
22
+
23
+ private static final String COLLECTOR_ID = "collector-451" ;
24
+
20
25
protected JsonSchema getJsonSchemaFromStreamContentV7 (InputStream schemaContent ) {
21
26
JsonSchemaFactory factory = JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V7 );
22
27
SchemaValidatorsConfig svc = new SchemaValidatorsConfig ();
@@ -29,20 +34,46 @@ protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exce
29
34
return mapper .readTree (content );
30
35
}
31
36
32
- @ SuppressWarnings ("unchecked" )
37
+ @ AfterEach
38
+ public void cleanup () {
39
+ if (CollectorContext .getInstance () != null ) {
40
+ CollectorContext .getInstance ().reset ();
41
+ }
42
+ }
43
+
44
+ @ Test
45
+ public void shouldWalkAnyOfProperties () {
46
+ walk (null , false );
47
+ }
48
+
49
+ @ Test
50
+ public void shouldWalkAnyOfPropertiesWithWithPayloadAndValidation () throws Exception {
51
+ JsonNode data = getJsonNodeFromStreamContent (Issue451Test .class .getResourceAsStream (
52
+ "/data/issue451.json" ));
53
+ walk (data ,true );
54
+ }
55
+
33
56
@ Test
34
- public void shouldWalkAnyOfProperties () throws Exception {
57
+ public void shouldWalkAnyOfPropertiesWithWithPayload () throws Exception {
58
+ JsonNode data = getJsonNodeFromStreamContent (Issue451Test .class .getResourceAsStream (
59
+ "/data/issue451.json" ));
60
+ walk (data , false );
61
+ }
62
+
63
+ @ SuppressWarnings ("unchecked" )
64
+ private void walk (JsonNode data , boolean shouldValidate ) {
35
65
String schemaPath = "/schema/issue451-v7.json" ;
36
66
InputStream schemaInputStream = getClass ().getResourceAsStream (schemaPath );
37
67
JsonSchema schema = getJsonSchemaFromStreamContentV7 (schemaInputStream );
38
68
39
- schema .walk (null , false );
69
+ schema .walk (data , shouldValidate );
40
70
41
- Map <String , Integer > collector = (Map <String , Integer >) CollectorContext .getInstance ().get ("collector-451" );
71
+ Map <String , Integer > collector = (Map <String , Integer >) CollectorContext .getInstance ().get (COLLECTOR_ID );
42
72
Assertions .assertEquals (2 , collector .get ("#/definitions/definition1/properties/a" ));
43
73
Assertions .assertEquals (2 , collector .get ("#/definitions/definition2/properties/x" ));
44
74
}
45
75
76
+
46
77
private static class CountingWalker implements JsonSchemaWalkListener {
47
78
@ Override
48
79
public WalkFlow onWalkStart (WalkEvent walkEvent ) {
@@ -57,10 +88,10 @@ public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMess
57
88
}
58
89
59
90
private Map <String , Integer > collector () {
60
- Map <String , Integer > collector = (Map <String , Integer >) CollectorContext .getInstance ().get ("collector-451" );
91
+ Map <String , Integer > collector = (Map <String , Integer >) CollectorContext .getInstance ().get (COLLECTOR_ID );
61
92
if (collector == null ) {
62
93
collector = new HashMap <>();
63
- CollectorContext .getInstance ().add ("collector-451" , collector );
94
+ CollectorContext .getInstance ().add (COLLECTOR_ID , collector );
64
95
}
65
96
66
97
return collector ;
0 commit comments