• 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 
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 
23 import static com.networknt.schema.BaseJsonSchemaValidatorTest.getJsonNodeFromStringContent;
24 
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 
29 public class Issue619Test extends HTTPServiceSupport {
30 
31     private JsonSchemaFactory factory;
32     private JsonNode one;
33     private JsonNode two;
34     private JsonNode three;
35 
36     @BeforeEach
setup()37     public void setup() throws Exception {
38         factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
39         one = getJsonNodeFromStringContent("1");
40         two = getJsonNodeFromStringContent("2");
41         three = getJsonNodeFromStringContent("3");
42     }
43 
44     @Test
bundledSchemaLoadsAndValidatesCorrectly_Ref()45     public void bundledSchemaLoadsAndValidatesCorrectly_Ref() {
46         JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json\" }");
47 
48         assertTrue(referencingRootSchema.validate(one).isEmpty());
49         assertTrue(referencingRootSchema.validate(two).isEmpty());
50         assertFalse(referencingRootSchema.validate(three).isEmpty());
51     }
52 
53     @Test
bundledSchemaLoadsAndValidatesCorrectly_Uri()54     public void bundledSchemaLoadsAndValidatesCorrectly_Uri() throws Exception {
55         JsonSchema rootSchema = factory.getSchema(SchemaLocation.of("resource:schema/issue619.json"));
56 
57         assertTrue(rootSchema.validate(one).isEmpty());
58         assertTrue(rootSchema.validate(two).isEmpty());
59         assertFalse(rootSchema.validate(three).isEmpty());
60     }
61 
62     @Test
uriWithEmptyFragment_Ref()63     public void uriWithEmptyFragment_Ref() {
64         JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#\" }");
65 
66         assertTrue(referencingRootSchema.validate(one).isEmpty());
67         assertTrue(referencingRootSchema.validate(two).isEmpty());
68         assertFalse(referencingRootSchema.validate(three).isEmpty());
69     }
70 
71     @Test
uriWithEmptyFragment_Uri()72     public void uriWithEmptyFragment_Uri() throws Exception {
73         JsonSchema rootSchema = factory.getSchema(SchemaLocation.of("resource:schema/issue619.json#"));
74 
75         assertTrue(rootSchema.validate(one).isEmpty());
76         assertTrue(rootSchema.validate(two).isEmpty());
77         assertFalse(rootSchema.validate(three).isEmpty());
78     }
79 
80     @Test
uriThatPointsToTwoShouldOnlyValidateTwo_Ref()81     public void uriThatPointsToTwoShouldOnlyValidateTwo_Ref() {
82         JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/two\" }");
83 
84         assertFalse(referencingTwoSchema.validate(one).isEmpty());
85         assertTrue(referencingTwoSchema.validate(two).isEmpty());
86         assertFalse(referencingTwoSchema.validate(three).isEmpty());
87     }
88 
89     @Test
uriThatPointsToOneShouldOnlyValidateOne_Uri()90     public void uriThatPointsToOneShouldOnlyValidateOne_Uri() throws Exception {
91         JsonSchema oneSchema = factory.getSchema(SchemaLocation.of("resource:schema/issue619.json#/definitions/one"));
92 
93         assertTrue(oneSchema.validate(one).isEmpty());
94         assertFalse(oneSchema.validate(two).isEmpty());
95         assertFalse(oneSchema.validate(three).isEmpty());
96     }
97 
98     @Test
uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Ref()99     public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Ref() {
100         JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/refToOne\" }");
101 
102         assertTrue(referencingTwoSchema.validate(one).isEmpty());
103         assertFalse(referencingTwoSchema.validate(two).isEmpty());
104         assertFalse(referencingTwoSchema.validate(three).isEmpty());
105     }
106 
107     @Test
uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Uri()108     public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Uri() throws Exception {
109         JsonSchema oneSchema = factory.getSchema(SchemaLocation.of("resource:schema/issue619.json#/definitions/refToOne"));
110 
111         assertTrue(oneSchema.validate(one).isEmpty());
112         assertFalse(oneSchema.validate(two).isEmpty());
113         assertFalse(oneSchema.validate(three).isEmpty());
114     }
115 
116     @Test
uriThatPointsToSchemaWithIdThatHasDifferentUri_Ref()117     public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Ref() throws Exception {
118         JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
119         JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
120 
121         JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:tests/draft4/refRemote.json#/3/schema\" }");
122         assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty());
123         assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty());
124     }
125 
126     @Test
uriThatPointsToSchemaWithIdThatHasDifferentUri_Uri()127     public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Uri() throws Exception {
128         JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
129         JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");
130 
131         JsonSchema schemaWithIdFromUri = factory.getSchema(SchemaLocation.of("resource:tests/draft4/refRemote.json#/3/schema"));
132         assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty());
133         assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty());
134     }
135 
136     @Test
uriThatPointsToSchemaThatDoesNotExistShouldFail_Ref()137     public void uriThatPointsToSchemaThatDoesNotExistShouldFail_Ref() {
138         JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:data/schema-that-does-not-exist.json#/definitions/something\" }");
139 
140         assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one));
141     }
142 
143     @Test
uriThatPointsToSchemaThatDoesNotExistShouldFail_Uri()144     public void uriThatPointsToSchemaThatDoesNotExistShouldFail_Uri() {
145         assertThrows(JsonSchemaException.class, () -> factory.getSchema(SchemaLocation.of("resource:data/schema-that-does-not-exist.json#/definitions/something")));
146     }
147 
148     @Test
uriThatPointsToNodeThatDoesNotExistShouldFail_Ref()149     public void uriThatPointsToNodeThatDoesNotExistShouldFail_Ref() {
150         JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/node-that-does-not-exist\" }");
151 
152         assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one));
153     }
154 
155     @Test
uriThatPointsToNodeThatDoesNotExistShouldFail_Uri()156     public void uriThatPointsToNodeThatDoesNotExistShouldFail_Uri() {
157         // This test failed before adding the 10-millisecond sleep. IllegalStateException is returned with recursive update error. This only happens on my faster desktop
158         // computer during 'maven clean install'. It passes within the IDE on the same computer. It passes on my slower laptop. It passes on Travis CI.
159         try {
160             Thread.sleep(10);
161         } catch (InterruptedException e) {
162             e.printStackTrace();
163         }
164         assertThrows(JsonSchemaException.class, () -> factory.getSchema(SchemaLocation.of("resource:schema/issue619.json#/definitions/node-that-does-not-exist")));
165     }
166 }
167