• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.networknt.schema;
2 
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.junit.jupiter.api.Assertions;
6 import org.junit.jupiter.api.Test;
7 
8 import java.io.InputStream;
9 import java.util.Set;
10 
11 
12 public class Issue550Test {
getJsonSchemaFromStreamContentV7(String schemaPath)13     protected JsonSchema getJsonSchemaFromStreamContentV7(String schemaPath) {
14         InputStream schemaContent = getClass().getResourceAsStream(schemaPath);
15         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
16         return factory.getSchema(schemaContent);
17     }
18 
getJsonNodeFromStreamContent(String dataPath)19     protected JsonNode getJsonNodeFromStreamContent(String dataPath) throws Exception {
20         InputStream content = getClass().getResourceAsStream(dataPath);
21         ObjectMapper mapper = new ObjectMapper();
22         JsonNode node = mapper.readTree(content);
23         return node;
24     }
25 
26     @Test
testValidationMessageDoContainSchemaPath()27     void testValidationMessageDoContainSchemaPath() throws Exception {
28         String schemaPath = "/schema/issue500_1-v7.json";
29         String dataPath = "/data/issue500_1.json";
30         JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaPath);
31         JsonNode node = getJsonNodeFromStreamContent(dataPath);
32 
33         Set<ValidationMessage> errors = schema.validate(node);
34         ValidationMessage validationMessage = errors.stream().findFirst().get();
35 
36         Assertions.assertEquals("https://example.com/person.schema.json#/properties/age/minimum", validationMessage.getSchemaLocation().toString());
37         Assertions.assertEquals(1, errors.size());
38     }
39 
40     @Test
testValidationMessageDoContainSchemaPathForOneOf()41     void testValidationMessageDoContainSchemaPathForOneOf() throws Exception {
42         String schemaPath = "/schema/issue500_2-v7.json";
43         String dataPath = "/data/issue500_2.json";
44         JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaPath);
45         JsonNode node = getJsonNodeFromStreamContent(dataPath);
46 
47         Set<ValidationMessage> errors = schema.validate(node);
48         ValidationMessage validationMessage = errors.stream().findFirst().get();
49 
50         // Instead of capturing all subSchema within oneOf, a pointer to oneOf should be provided.
51         Assertions.assertEquals("https://example.com/person.schema.json#/oneOf", validationMessage.getSchemaLocation().toString());
52         Assertions.assertEquals(1, errors.size());
53     }
54 
55 }
56