55import com .networknt .schema .walk .JsonSchemaWalkListener ;
66import com .networknt .schema .walk .WalkEvent ;
77import com .networknt .schema .walk .WalkFlow ;
8+ import org .junit .jupiter .api .AfterEach ;
89import org .junit .jupiter .api .Assertions ;
10+ import org .junit .jupiter .api .Disabled ;
911import org .junit .jupiter .api .Test ;
1012
1113import java .io .InputStream ;
1719 * Validating anyOf walker
1820 */
1921public class Issue451Test {
22+
23+ private static final String COLLECTOR_ID = "collector-451" ;
24+
2025 protected JsonSchema getJsonSchemaFromStreamContentV7 (InputStream schemaContent ) {
2126 JsonSchemaFactory factory = JsonSchemaFactory .getInstance (SpecVersion .VersionFlag .V7 );
2227 SchemaValidatorsConfig svc = new SchemaValidatorsConfig ();
@@ -29,20 +34,46 @@ protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exce
2934 return mapper .readTree (content );
3035 }
3136
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+
3356 @ 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 ) {
3565 String schemaPath = "/schema/issue451-v7.json" ;
3666 InputStream schemaInputStream = getClass ().getResourceAsStream (schemaPath );
3767 JsonSchema schema = getJsonSchemaFromStreamContentV7 (schemaInputStream );
3868
39- schema .walk (null , false );
69+ schema .walk (data , shouldValidate );
4070
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 );
4272 Assertions .assertEquals (2 , collector .get ("#/definitions/definition1/properties/a" ));
4373 Assertions .assertEquals (2 , collector .get ("#/definitions/definition2/properties/x" ));
4474 }
4575
76+
4677 private static class CountingWalker implements JsonSchemaWalkListener {
4778 @ Override
4879 public WalkFlow onWalkStart (WalkEvent walkEvent ) {
@@ -57,10 +88,10 @@ public void onWalkEnd(WalkEvent walkEvent, Set<ValidationMessage> validationMess
5788 }
5889
5990 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 );
6192 if (collector == null ) {
6293 collector = new HashMap <>();
63- CollectorContext .getInstance ().add ("collector-451" , collector );
94+ CollectorContext .getInstance ().add (COLLECTOR_ID , collector );
6495 }
6596
6697 return collector ;
0 commit comments