• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Network New Technologies Inc.
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 com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Test;
22 
23 import java.io.InputStream;
24 import java.util.Set;
25 
26 public class Issue255Test {
getJsonSchemaFromStreamContent(InputStream schemaContent)27     protected JsonSchema getJsonSchemaFromStreamContent(InputStream schemaContent) {
28         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
29         return factory.getSchema(schemaContent);
30     }
31 
getJsonNodeFromStreamContent(InputStream content)32     protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
33         ObjectMapper mapper = new ObjectMapper();
34         JsonNode node = mapper.readTree(content);
35         return node;
36     }
37 
38     @Test
shouldFailWhenRequiredPropertiesDoNotExistInReferencedSubSchema()39     public void shouldFailWhenRequiredPropertiesDoNotExistInReferencedSubSchema() throws Exception {
40         String schemaPath = "/draft2019-09/issue255.json";
41         String dataPath = "/data/issue255.json";
42         InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
43         JsonSchema schema = getJsonSchemaFromStreamContent(schemaInputStream);
44         InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
45         JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
46         Set<ValidationMessage> errors = schema.validate(node);
47         Assertions.assertEquals(2, errors.size());
48     }
49 }
50