|
3 | 3 | import com.fasterxml.jackson.core.JsonProcessingException;
|
4 | 4 | import com.fasterxml.jackson.databind.JsonNode;
|
5 | 5 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 6 | +import org.junit.Assert; |
6 | 7 | import org.junit.Test;
|
7 | 8 |
|
8 | 9 | import java.util.Set;
|
9 | 10 |
|
10 | 11 | public class UnknownMetaSchemaTest {
|
11 | 12 |
|
12 |
| - private String schema = |
13 |
| - "{\n" + |
14 |
| - " \"$schema\": \"https://json-schema.org/draft-07/schema\",\n" + |
15 |
| - " \"title\": \"thingModel\",\n" + |
16 |
| - " \"description\": \"description of thing\",\n" + |
17 |
| - " \"type\": \"object\",\n" + |
18 |
| - " \"properties\": {\n" + |
19 |
| - " \"data\": {\n" + |
20 |
| - " \"type\": \"integer\"\n" + |
21 |
| - " }\n" + |
22 |
| - " },\n" + |
23 |
| - " \"required\": [\n" + |
24 |
| - " \"data\"\n" + |
25 |
| - " ]\n" + |
26 |
| - "}"; |
27 |
| - |
28 |
| - private String json = |
29 |
| - "{\n" + |
30 |
| - " \"data\": 1\n" + |
31 |
| - "}"; |
| 13 | + private String schema1 = "{\"$schema\":\"http://json-schema.org/draft-07/schema\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}"; |
| 14 | + private String schema2 = "{\"$schema\":\"https://json-schema.org/draft-07/schema\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}"; |
| 15 | + private String schema3 = "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"title\":\"thingModel\",\"description\":\"description of thing\",\"type\":\"object\",\"properties\":{\"data\":{\"type\":\"integer\"},\"required\":[\"data\"]}}"; |
32 | 16 |
|
| 17 | + private String json = "{\"data\":1}"; |
33 | 18 |
|
34 | 19 | @Test
|
35 |
| - public void test() throws JsonProcessingException { |
| 20 | + public void testSchema1() throws JsonProcessingException { |
36 | 21 | ObjectMapper mapper = new ObjectMapper();
|
37 | 22 | JsonNode jsonNode = mapper.readTree(this.json);
|
38 | 23 |
|
39 | 24 | JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
|
40 |
| - JsonSchema jsonSchema = factory.getSchema(schema); |
| 25 | + JsonSchema jsonSchema = factory.getSchema(schema1); |
41 | 26 |
|
42 | 27 | Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
|
43 | 28 | for(ValidationMessage error:errors) {
|
44 | 29 | System.out.println(error.getMessage());
|
45 | 30 | }
|
46 | 31 | }
|
| 32 | + |
| 33 | + @Test |
| 34 | + public void testSchema2() throws JsonProcessingException { |
| 35 | + ObjectMapper mapper = new ObjectMapper(); |
| 36 | + JsonNode jsonNode = mapper.readTree(this.json); |
| 37 | + |
| 38 | + JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); |
| 39 | + JsonSchema jsonSchema = factory.getSchema(schema2); |
| 40 | + |
| 41 | + Set<ValidationMessage> errors = jsonSchema.validate(jsonNode); |
| 42 | + for(ValidationMessage error:errors) { |
| 43 | + System.out.println(error.getMessage()); |
| 44 | + } |
| 45 | + } |
| 46 | + @Test |
| 47 | + public void testSchema3() throws JsonProcessingException { |
| 48 | + ObjectMapper mapper = new ObjectMapper(); |
| 49 | + JsonNode jsonNode = mapper.readTree(this.json); |
| 50 | + |
| 51 | + JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); |
| 52 | + JsonSchema jsonSchema = factory.getSchema(schema3); |
| 53 | + |
| 54 | + Set<ValidationMessage> errors = jsonSchema.validate(jsonNode); |
| 55 | + for(ValidationMessage error:errors) { |
| 56 | + System.out.println(error.getMessage()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testNormalize() throws JsonSchemaException { |
| 62 | + String uri01 = "http://json-schema.org/draft-07/schema"; |
| 63 | + String uri02 = "http://json-schema.org/draft-07/schema#"; |
| 64 | + String uri03 = "http://json-schema.org/draft-07/schema?key=value"; |
| 65 | + String uri04 = "http://json-schema.org/draft-07/schema?key=value&key2=value2"; |
| 66 | + String expected = "https://json-schema.org/draft-07/schema"; |
| 67 | + Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri01)); |
| 68 | + Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri02)); |
| 69 | + Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri03)); |
| 70 | + Assert.assertEquals(expected, JsonSchemaFactory.normalizeMetaSchemaUri(uri04)); |
| 71 | + |
| 72 | + } |
47 | 73 | }
|
0 commit comments