• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 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 
17 package com.networknt.schema;
18 
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 
22 import org.junit.jupiter.api.Test;
23 
24 import java.io.IOException;
25 import java.net.URL;
26 import java.util.Set;
27 
28 import static org.junit.jupiter.api.Assertions.*;
29 
30 public class V4JsonSchemaTest extends HTTPServiceSupport {
31 
32     protected ObjectMapper mapper = new ObjectMapper();
33 
34     @Test(/* expected = java.lang.StackOverflowError.class */)
testLoadingWithId()35     public void testLoadingWithId() throws Exception {
36         URL url = new URL("http://localhost:1234/self_ref/selfRef.json");
37         JsonNode schemaJson = mapper.readTree(url);
38         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
39         @SuppressWarnings("unused")
40         JsonSchema schema = factory.getSchema(schemaJson);
41     }
42 
43     /**
44      * Although, the data file has three errors, but only on is reported
45      */
46     @Test
testFailFast_AllErrors()47     public void testFailFast_AllErrors() throws IOException {
48         Set<ValidationMessage> messages = validateFailingFastSchemaFor("extra/product/product.schema.json",
49                 "extra/product/product-all-errors-data.json");
50         assertEquals(1, messages.size());
51     }
52 
53     /**
54      * File contains only one error and that is reported.
55      */
56     @Test
testFailFast_OneErrors()57     public void testFailFast_OneErrors() throws IOException {
58         Set<ValidationMessage> messages = validateFailingFastSchemaFor("extra/product/product.schema.json",
59                 "extra/product/product-one-error-data.json");
60         assertEquals(1, messages.size());
61     }
62 
63     /**
64      * Although, the file contains two errors, but only one is reported
65      */
66     @Test
testFailFast_TwoErrors()67     public void testFailFast_TwoErrors() throws IOException {
68         Set<ValidationMessage> messages = validateFailingFastSchemaFor("extra/product/product.schema.json",
69                 "extra/product/product-two-errors-data.json");
70         assertEquals(1, messages.size());
71     }
72 
73     /**
74      * The file contains no errors, in ths case
75      * {@link Set}&lt;{@link ValidationMessage}&gt; must be empty
76      */
77     @Test
testFailFast_NoErrors()78     public void testFailFast_NoErrors() throws IOException {
79         final Set<ValidationMessage> messages = validateFailingFastSchemaFor("extra/product/product.schema.json",
80                 "extra/product/product-no-errors-data.json");
81         assertTrue(messages.isEmpty());
82     }
83 
validateFailingFastSchemaFor(final String schemaFileName, final String dataFileName)84     private Set<ValidationMessage> validateFailingFastSchemaFor(final String schemaFileName, final String dataFileName) throws IOException {
85         final ObjectMapper objectMapper = new ObjectMapper();
86         final JsonNode schema = getJsonNodeFromResource(objectMapper, schemaFileName);
87         final JsonNode dataFile = getJsonNodeFromResource(objectMapper, dataFileName);
88         final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
89         config.setFailFast(true);
90         return JsonSchemaFactory
91             .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4))
92             .jsonMapper(objectMapper)
93             .build()
94             .getSchema(schema, config)
95             .validate(dataFile);
96     }
97 
getJsonNodeFromResource(final ObjectMapper mapper, final String locationInTestResources)98     private JsonNode getJsonNodeFromResource(final ObjectMapper mapper, final String locationInTestResources) throws IOException {
99         return mapper.readTree(
100             Thread.currentThread().getContextClassLoader()
101                 .getResourceAsStream("draft4" + System.getProperty("file.separator") + locationInTestResources));
102 
103     }
104 }
105