|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +import java.io.InputStream; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +class Issue491Test { |
| 17 | + |
| 18 | + private static JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); |
| 19 | + private static String schemaPath1 = "/schema/issue491-v7.json"; |
| 20 | + private static String schemaPath2 = "/schema/issue491_2-v7.json"; |
| 21 | + private static String schemaPath3 = "/schema/issue491_3-v7.json"; |
| 22 | + |
| 23 | + private JsonNode getJsonNodeFromJsonData(String jsonFilePath) throws Exception { |
| 24 | + InputStream content = getClass().getResourceAsStream(jsonFilePath); |
| 25 | + ObjectMapper mapper = new ObjectMapper(); |
| 26 | + return mapper.readTree(content); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("Test valid oneOf option 1") |
| 31 | + void testValidJson1() throws Exception { |
| 32 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); |
| 33 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 34 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-1.json"); |
| 35 | + Set<ValidationMessage> errors = schema.validate(node); |
| 36 | + Assertions.assertTrue(errors.isEmpty()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("Test valid oneOf option 2") |
| 41 | + void testValidJson2() throws Exception { |
| 42 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); |
| 43 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 44 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); |
| 45 | + Set<ValidationMessage> errors = schema.validate(node); |
| 46 | + Assertions.assertTrue(errors.isEmpty()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("Test valid oneOf option 1") |
| 51 | + void testValidJson3() throws Exception { |
| 52 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); |
| 53 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 54 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-3.json"); |
| 55 | + Set<ValidationMessage> errors = schema.validate(node); |
| 56 | + Assertions.assertTrue(errors.isEmpty()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("Test valid oneOf option 2") |
| 61 | + void testValidJson4() throws Exception { |
| 62 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); |
| 63 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 64 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); |
| 65 | + Set<ValidationMessage> errors = schema.validate(node); |
| 66 | + Assertions.assertTrue(errors.isEmpty()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @DisplayName("Test valid oneOf option 1") |
| 71 | + void testValidJson5() throws Exception { |
| 72 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); |
| 73 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 74 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-4.json"); |
| 75 | + Set<ValidationMessage> errors = schema.validate(node); |
| 76 | + Assertions.assertTrue(errors.isEmpty()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @DisplayName("Test valid oneOf option 2") |
| 81 | + void testValidJson6() throws Exception { |
| 82 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); |
| 83 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 84 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); |
| 85 | + Set<ValidationMessage> errors = schema.validate(node); |
| 86 | + Assertions.assertTrue(errors.isEmpty()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @DisplayName("Test invalid oneOf option 1 - wrong type") |
| 91 | + void testInvalidJson1() throws Exception { |
| 92 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); |
| 93 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 94 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-1.json"); |
| 95 | + Set<ValidationMessage> errors = schema.validate(node); |
| 96 | + Assertions.assertEquals(2, errors.size()); |
| 97 | + Assertions.assertEquals("$.search.searchAge.age: string found, integer expected", errors.iterator().next().getMessage()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @DisplayName("Test invalid oneOf option 2 - wrong type") |
| 102 | + void testInvalidJson2() throws Exception { |
| 103 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); |
| 104 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 105 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-2.json"); |
| 106 | + Set<ValidationMessage> errors = schema.validate(node); |
| 107 | + Assertions.assertEquals(2, errors.size()); |
| 108 | + Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + @DisplayName("Test invalid oneOf option 1 - wrong type") |
| 113 | + void testInvalidJson3() throws Exception { |
| 114 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); |
| 115 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 116 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-3.json"); |
| 117 | + Set<ValidationMessage> errors = schema.validate(node); |
| 118 | + Assertions.assertEquals(2, errors.size()); |
| 119 | + Assertions.assertEquals("$.search.byAge.age: string found, integer expected", errors.iterator().next().getMessage()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @DisplayName("Test invalid oneOf option 2 - wrong type") |
| 124 | + void testInvalidJson4() throws Exception { |
| 125 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); |
| 126 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 127 | + JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-2.json"); |
| 128 | + Set<ValidationMessage> errors = schema.validate(node); |
| 129 | + Assertions.assertEquals(2, errors.size()); |
| 130 | + Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage()); |
| 131 | + } |
| 132 | + |
| 133 | + @ParameterizedTest |
| 134 | + @MethodSource("parametersProvider") |
| 135 | + @DisplayName("Test invalid oneOf option - wrong types or values") |
| 136 | + void testInvalidJson5(String jsonPath, String expectedError) throws Exception { |
| 137 | + InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); |
| 138 | + JsonSchema schema = factory.getSchema(schemaInputStream); |
| 139 | + JsonNode node = getJsonNodeFromJsonData(jsonPath); |
| 140 | + Set<ValidationMessage> errors = schema.validate(node); |
| 141 | + Assertions.assertEquals(2, errors.size()); |
| 142 | + Assertions.assertEquals(expectedError, errors.iterator().next().getMessage()); |
| 143 | + } |
| 144 | + |
| 145 | + private static Stream<Arguments> parametersProvider() { |
| 146 | + return Stream.of( |
| 147 | + Arguments.of("/data/issue491-invalid-4.json", "$.search.age: string found, integer expected"), |
| 148 | + Arguments.of("/data/issue491-invalid-2.json", "$.search.name: integer found, string expected"), |
| 149 | + Arguments.of("/data/issue491-invalid-5.json", "$.search.age: must have a maximum value of 150"), |
| 150 | + Arguments.of("/data/issue491-invalid-6.json", "$.search.name: may only be 20 characters long") |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments