1 package com.networknt.schema; 2 3 import static org.hamcrest.MatcherAssert.assertThat; 4 5 import java.io.InputStream; 6 import java.util.HashSet; 7 import java.util.Set; 8 9 import org.hamcrest.Matchers; 10 import org.junit.jupiter.api.Assertions; 11 import org.junit.jupiter.api.DisplayName; 12 import org.junit.jupiter.api.Test; 13 14 import com.fasterxml.jackson.databind.JsonNode; 15 import com.fasterxml.jackson.databind.ObjectMapper; 16 17 class Issue493Test 18 { 19 20 private static JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); 21 private static String schemaPath1 = "/schema/issue493.json"; 22 getJsonNodeFromJsonData(String jsonFilePath)23 private JsonNode getJsonNodeFromJsonData (String jsonFilePath) 24 throws Exception 25 { 26 InputStream content = getClass().getResourceAsStream(jsonFilePath); 27 ObjectMapper mapper = new ObjectMapper(); 28 return mapper.readTree(content); 29 } 30 31 @Test 32 @DisplayName("Test valid with required item only") testValidJson1()33 void testValidJson1 () 34 throws Exception 35 { 36 InputStream schemaInputStream = Issue493Test.class.getResourceAsStream(schemaPath1); 37 JsonSchema schema = factory.getSchema(schemaInputStream); 38 JsonNode node = getJsonNodeFromJsonData("/data/issue493-valid-1.json"); 39 Set<ValidationMessage> errors = schema.validate(node); 40 Assertions.assertTrue(errors.isEmpty()); 41 } 42 43 @Test 44 @DisplayName("Test valid with optional item") testValidJson2()45 void testValidJson2 () 46 throws Exception 47 { 48 InputStream schemaInputStream = Issue493Test.class.getResourceAsStream(schemaPath1); 49 JsonSchema schema = factory.getSchema(schemaInputStream); 50 JsonNode node = getJsonNodeFromJsonData("/data/issue493-valid-2.json"); 51 Set<ValidationMessage> errors = schema.validate(node); 52 Assertions.assertTrue(errors.isEmpty()); 53 } 54 55 @Test 56 @DisplayName("Test invalid with required item but wrong type") testInvalidJson1()57 void testInvalidJson1 () 58 throws Exception 59 { 60 InputStream schemaInputStream = Issue493Test.class.getResourceAsStream(schemaPath1); 61 JsonSchema schema = factory.getSchema(schemaInputStream); 62 JsonNode node = getJsonNodeFromJsonData("/data/issue493-invalid-1.json"); 63 Set<ValidationMessage> errors = schema.validate(node); 64 Assertions.assertEquals(2, errors.size()); 65 66 Set<String> allErrorMessages = new HashSet<>(); 67 errors.forEach(vm -> { 68 allErrorMessages.add(vm.getMessage()); 69 }); 70 assertThat(allErrorMessages, 71 Matchers.containsInAnyOrder("$.parameters[0].value: string found, integer expected", 72 "$.parameters[0].value: does not match the regex pattern ^\\{\\{.+\\}\\}$")); 73 } 74 75 @Test 76 @DisplayName("Test invalid with optional item but wrong type") testInvalidJson2()77 void testInvalidJson2 () 78 throws Exception 79 { 80 InputStream schemaInputStream = Issue493Test.class.getResourceAsStream(schemaPath1); 81 JsonSchema schema = factory.getSchema(schemaInputStream); 82 JsonNode node = getJsonNodeFromJsonData("/data/issue493-invalid-2.json"); 83 Set<ValidationMessage> errors = schema.validate(node); 84 Assertions.assertEquals(3, errors.size()); 85 86 Set<String> allErrorMessages = new HashSet<>(); 87 errors.forEach(vm -> { 88 allErrorMessages.add(vm.getMessage()); 89 }); 90 assertThat(allErrorMessages, Matchers.containsInAnyOrder( 91 "$.parameters[1].value: string found, integer expected", 92 "$.parameters[1].value: does not match the regex pattern ^\\{\\{.+\\}\\}$", 93 "$.parameters[1]: must be valid to one and only one schema, but 0 are valid" 94 )); 95 } 96 } 97