|
| 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.BeforeAll; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.io.InputStream; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +public class Issue470Test { |
| 14 | + |
| 15 | + private static JsonSchema schema; |
| 16 | + |
| 17 | + @BeforeAll |
| 18 | + static void init() { |
| 19 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); |
| 20 | + String schemaPath = "/schema/issue470-v7.json"; |
| 21 | + InputStream schemaInputStream = Issue470Test.class.getResourceAsStream(schemaPath); |
| 22 | + schema = factory.getSchema(schemaInputStream); |
| 23 | + } |
| 24 | + |
| 25 | + private JsonNode getJsonNodeFromJsonData(String jsonFilePath) throws Exception { |
| 26 | + InputStream content = getClass().getResourceAsStream(jsonFilePath); |
| 27 | + ObjectMapper mapper = new ObjectMapper(); |
| 28 | + return mapper.readTree(content); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("Test valid oneOf option 1") |
| 33 | + public void testValidJson1() throws Exception { |
| 34 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-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 | + public void testValidJson2() throws Exception { |
| 42 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-valid-2.json"); |
| 43 | + Set<ValidationMessage> errors = schema.validate(node); |
| 44 | + Assertions.assertTrue(errors.isEmpty()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("Test invalid oneOf option 1 - wrong type") |
| 49 | + public void testInvalidJson1() throws Exception { |
| 50 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-invalid-1.json"); |
| 51 | + Set<ValidationMessage> errors = schema.validate(node); |
| 52 | + Assertions.assertEquals(1, errors.size()); |
| 53 | + Assertions.assertEquals("$.search.byName.name: integer found, string expected", errors.iterator().next().getMessage()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("Test invalid oneOf option 1 - invalid value") |
| 58 | + public void testInvalidJson2() throws Exception { |
| 59 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-invalid-2.json"); |
| 60 | + Set<ValidationMessage> errors = schema.validate(node); |
| 61 | + Assertions.assertEquals(1, errors.size()); |
| 62 | + Assertions.assertEquals("$.search.byName.name: may only be 20 characters long", errors.iterator().next().getMessage()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("Test invalid oneOf option 2 - wrong type") |
| 67 | + public void testInvalidJson3() throws Exception { |
| 68 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-invalid-3.json"); |
| 69 | + Set<ValidationMessage> errors = schema.validate(node); |
| 70 | + Assertions.assertEquals(1, errors.size()); |
| 71 | + Assertions.assertEquals("$.search.byAge.age: string found, integer expected", errors.iterator().next().getMessage()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("Test invalid oneOf option 2 - invalid value") |
| 76 | + public void testInvalidJson4() throws Exception { |
| 77 | + JsonNode node = getJsonNodeFromJsonData("/data/issue470-invalid-4.json"); |
| 78 | + Set<ValidationMessage> errors = schema.validate(node); |
| 79 | + Assertions.assertEquals(1, errors.size()); |
| 80 | + Assertions.assertEquals("$.search.byAge.age: must have a maximum value of 150", errors.iterator().next().getMessage()); |
| 81 | + } |
| 82 | +} |
0 commit comments