|
| 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 org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.net.URI; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | +public class Issue619Test extends BaseJsonSchemaValidatorTest { |
| 27 | + |
| 28 | + private JsonSchemaFactory factory; |
| 29 | + private JsonNode one; |
| 30 | + private JsonNode two; |
| 31 | + private JsonNode three; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + public void setup() throws Exception { |
| 35 | + factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
| 36 | + one = getJsonNodeFromStringContent("1"); |
| 37 | + two = getJsonNodeFromStringContent("2"); |
| 38 | + three = getJsonNodeFromStringContent("3"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void checkThatBundledSchemaLoadsAndValidatesCorrectly() throws Exception { |
| 43 | + JsonSchema rootSchema = factory.getSchema(new URI("resource:schema/issue619.json")); |
| 44 | + |
| 45 | + assertTrue(rootSchema.validate(one).isEmpty()); |
| 46 | + assertTrue(rootSchema.validate(two).isEmpty()); |
| 47 | + assertFalse(rootSchema.validate(three).isEmpty()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void checkThatSchemaThatReferencesBundledSchemaLoadsAndValidatesCorrectly() { |
| 52 | + JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json\" }"); |
| 53 | + |
| 54 | + assertTrue(referencingRootSchema.validate(one).isEmpty()); |
| 55 | + assertTrue(referencingRootSchema.validate(two).isEmpty()); |
| 56 | + assertFalse(referencingRootSchema.validate(three).isEmpty()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void checkThatUriThatPointsToEmptyFragmentLoadsAndValidatesCorrectly() throws Exception { |
| 61 | + JsonSchema rootSchema = factory.getSchema(new URI("resource:schema/issue619.json#")); |
| 62 | + |
| 63 | + assertTrue(rootSchema.validate(one).isEmpty()); |
| 64 | + assertTrue(rootSchema.validate(two).isEmpty()); |
| 65 | + assertFalse(rootSchema.validate(three).isEmpty()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void checkThatSchemaThatReferencesEmptyFragmentLoadsAndValidatesCorrectly() { |
| 70 | + JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#\" }"); |
| 71 | + |
| 72 | + assertTrue(referencingRootSchema.validate(one).isEmpty()); |
| 73 | + assertTrue(referencingRootSchema.validate(two).isEmpty()); |
| 74 | + assertFalse(referencingRootSchema.validate(three).isEmpty()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void loadingSchemaWithUriThatPointsToOneShouldOnlyValidateOne() throws Exception { |
| 79 | + JsonSchema oneSchema = factory.getSchema(new URI("resource:schema/issue619.json#/definitions/one")); |
| 80 | + |
| 81 | + assertTrue(oneSchema.validate(one).isEmpty()); |
| 82 | + assertFalse(oneSchema.validate(two).isEmpty()); |
| 83 | + assertFalse(oneSchema.validate(three).isEmpty()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void referencingSchemaWithUriThatPointsToTwoShouldOnlyValidateTwo() { |
| 88 | + JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/two\" }"); |
| 89 | + |
| 90 | + assertFalse(referencingTwoSchema.validate(one).isEmpty()); |
| 91 | + assertTrue(referencingTwoSchema.validate(two).isEmpty()); |
| 92 | + assertFalse(referencingTwoSchema.validate(three).isEmpty()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void loadingSchemaWithUriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne() throws Exception { |
| 97 | + JsonSchema oneSchema = factory.getSchema(new URI("resource:schema/issue619.json#/definitions/refToOne")); |
| 98 | + |
| 99 | + assertTrue(oneSchema.validate(one).isEmpty()); |
| 100 | + assertFalse(oneSchema.validate(two).isEmpty()); |
| 101 | + assertFalse(oneSchema.validate(three).isEmpty()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void referencingSchemaWithUriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne() { |
| 106 | + JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/refToOne\" }"); |
| 107 | + |
| 108 | + assertTrue(referencingTwoSchema.validate(one).isEmpty()); |
| 109 | + assertFalse(referencingTwoSchema.validate(two).isEmpty()); |
| 110 | + assertFalse(referencingTwoSchema.validate(three).isEmpty()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void loadingSchemaWithUriThatPointsToSchemaThatDoesNotExistShouldFail() { |
| 115 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(new URI("resource:data/schema-that-does-not-exist.json#/definitions/something"))); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void referencingSchemaWithUriThatPointsToSchemaThatDoesNotExistShouldFail() { |
| 120 | + JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:data/schema-that-does-not-exist.json#/definitions/something\" }"); |
| 121 | + |
| 122 | + assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void loadingSchemaWithUriThatPointsToNodeThatDoesNotExistShouldFail() { |
| 127 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(new URI("resource:schema/issue619.json#/definitions/node-that-does-not-exist"))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void referencingSchemaWithUriThatPointsToNodeThatDoesNotExistShouldFail() { |
| 132 | + JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/node-that-does-not-exist\" }"); |
| 133 | + |
| 134 | + assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one)); |
| 135 | + } |
| 136 | +} |
0 commit comments