|
| 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 io.undertow.Undertow; |
| 20 | +import io.undertow.server.handlers.resource.FileResourceManager; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.net.URI; |
| 26 | + |
| 27 | +import static io.undertow.Handlers.resource; |
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
| 29 | + |
| 30 | +public class Issue619Test extends BaseJsonSchemaValidatorTest { |
| 31 | + |
| 32 | + private JsonSchemaFactory factory; |
| 33 | + private JsonNode one; |
| 34 | + private JsonNode two; |
| 35 | + private JsonNode three; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setup() throws Exception { |
| 39 | + factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
| 40 | + one = getJsonNodeFromStringContent("1"); |
| 41 | + two = getJsonNodeFromStringContent("2"); |
| 42 | + three = getJsonNodeFromStringContent("3"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void bundledSchemaLoadsAndValidatesCorrectly_Ref() { |
| 47 | + JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json\" }"); |
| 48 | + |
| 49 | + assertTrue(referencingRootSchema.validate(one).isEmpty()); |
| 50 | + assertTrue(referencingRootSchema.validate(two).isEmpty()); |
| 51 | + assertFalse(referencingRootSchema.validate(three).isEmpty()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void bundledSchemaLoadsAndValidatesCorrectly_Uri() throws Exception { |
| 56 | + JsonSchema rootSchema = factory.getSchema(new URI("resource:schema/issue619.json")); |
| 57 | + |
| 58 | + assertTrue(rootSchema.validate(one).isEmpty()); |
| 59 | + assertTrue(rootSchema.validate(two).isEmpty()); |
| 60 | + assertFalse(rootSchema.validate(three).isEmpty()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void uriWithEmptyFragment_Ref() { |
| 65 | + JsonSchema referencingRootSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#\" }"); |
| 66 | + |
| 67 | + assertTrue(referencingRootSchema.validate(one).isEmpty()); |
| 68 | + assertTrue(referencingRootSchema.validate(two).isEmpty()); |
| 69 | + assertFalse(referencingRootSchema.validate(three).isEmpty()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void uriWithEmptyFragment_Uri() throws Exception { |
| 74 | + JsonSchema rootSchema = factory.getSchema(new URI("resource:schema/issue619.json#")); |
| 75 | + |
| 76 | + assertTrue(rootSchema.validate(one).isEmpty()); |
| 77 | + assertTrue(rootSchema.validate(two).isEmpty()); |
| 78 | + assertFalse(rootSchema.validate(three).isEmpty()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void uriThatPointsToTwoShouldOnlyValidateTwo_Ref() { |
| 83 | + JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/two\" }"); |
| 84 | + |
| 85 | + assertFalse(referencingTwoSchema.validate(one).isEmpty()); |
| 86 | + assertTrue(referencingTwoSchema.validate(two).isEmpty()); |
| 87 | + assertFalse(referencingTwoSchema.validate(three).isEmpty()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void uriThatPointsToOneShouldOnlyValidateOne_Uri() throws Exception { |
| 92 | + JsonSchema oneSchema = factory.getSchema(new URI("resource:schema/issue619.json#/definitions/one")); |
| 93 | + |
| 94 | + assertTrue(oneSchema.validate(one).isEmpty()); |
| 95 | + assertFalse(oneSchema.validate(two).isEmpty()); |
| 96 | + assertFalse(oneSchema.validate(three).isEmpty()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Ref() { |
| 101 | + JsonSchema referencingTwoSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/refToOne\" }"); |
| 102 | + |
| 103 | + assertTrue(referencingTwoSchema.validate(one).isEmpty()); |
| 104 | + assertFalse(referencingTwoSchema.validate(two).isEmpty()); |
| 105 | + assertFalse(referencingTwoSchema.validate(three).isEmpty()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Uri() throws Exception { |
| 110 | + JsonSchema oneSchema = factory.getSchema(new URI("resource:schema/issue619.json#/definitions/refToOne")); |
| 111 | + |
| 112 | + assertTrue(oneSchema.validate(one).isEmpty()); |
| 113 | + assertFalse(oneSchema.validate(two).isEmpty()); |
| 114 | + assertFalse(oneSchema.validate(three).isEmpty()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Ref() throws Exception { |
| 119 | + runLocalServer(() -> { |
| 120 | + JsonNode oneArray = getJsonNodeFromStringContent("[[1]]"); |
| 121 | + JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]"); |
| 122 | + |
| 123 | + JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:draft4/refRemote.json#/3/schema\" }"); |
| 124 | + assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty()); |
| 125 | + assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty()); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Uri() throws Exception { |
| 131 | + runLocalServer(() -> { |
| 132 | + JsonNode oneArray = getJsonNodeFromStringContent("[[1]]"); |
| 133 | + JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]"); |
| 134 | + |
| 135 | + JsonSchema schemaWithIdFromUri = factory.getSchema(new URI("resource:draft4/refRemote.json#/3/schema")); |
| 136 | + assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty()); |
| 137 | + assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty()); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + private interface ThrowingRunnable { |
| 142 | + void run() throws Exception; |
| 143 | + } |
| 144 | + |
| 145 | + private void runLocalServer(ThrowingRunnable actualTest) throws Exception { |
| 146 | + Undertow server = Undertow.builder() |
| 147 | + .addHttpListener(1234, "localhost") |
| 148 | + .setHandler(resource(new FileResourceManager( |
| 149 | + new File("./src/test/resources/remotes"), 100))) |
| 150 | + .build(); |
| 151 | + try { |
| 152 | + server.start(); |
| 153 | + |
| 154 | + actualTest.run(); |
| 155 | + |
| 156 | + } finally { |
| 157 | + try { |
| 158 | + Thread.sleep(100); |
| 159 | + } catch (InterruptedException ignored) { |
| 160 | + Thread.currentThread().interrupt(); |
| 161 | + } |
| 162 | + server.stop(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void uriThatPointsToSchemaThatDoesNotExistShouldFail_Ref() { |
| 168 | + JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:data/schema-that-does-not-exist.json#/definitions/something\" }"); |
| 169 | + |
| 170 | + assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one)); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void uriThatPointsToSchemaThatDoesNotExistShouldFail_Uri() { |
| 175 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(new URI("resource:data/schema-that-does-not-exist.json#/definitions/something"))); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void uriThatPointsToNodeThatDoesNotExistShouldFail_Ref() { |
| 180 | + JsonSchema referencingNonexistentSchema = factory.getSchema("{ \"$ref\": \"resource:schema/issue619.json#/definitions/node-that-does-not-exist\" }"); |
| 181 | + |
| 182 | + assertThrows(JsonSchemaException.class, () -> referencingNonexistentSchema.validate(one)); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void uriThatPointsToNodeThatDoesNotExistShouldFail_Uri() { |
| 187 | + assertThrows(JsonSchemaException.class, () -> factory.getSchema(new URI("resource:schema/issue619.json#/definitions/node-that-does-not-exist"))); |
| 188 | + } |
| 189 | +} |
0 commit comments