xref: /aosp_15_r20/external/json-schema-validator/src/test/java/com/networknt/schema/Issue475Test.java (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1 /*
2  * Copyright (c) 2024 the original author or authors.
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 static org.junit.jupiter.api.Assertions.assertEquals;
19 
20 import java.util.Set;
21 
22 import org.junit.jupiter.api.Test;
23 
24 import com.networknt.schema.SpecVersion.VersionFlag;
25 import com.networknt.schema.serialization.JsonMapperFactory;
26 
27 /**
28  * Tests for validation of schema against meta schema.
29  */
30 public class Issue475Test {
31     private static final String VALID_INPUT = "{  \n"
32             + "  \"type\": \"object\",  \n"
33             + "  \"properties\": {    \n"
34             + "    \"key\": { \n"
35             + "      \"title\" : \"My key\", \n"
36             + "      \"type\": \"array\" \n"
37             + "    } \n"
38             + "  }\n"
39             + "}";
40 
41     private static final String INVALID_INPUT = "{  \n"
42             + "  \"type\": \"object\",  \n"
43             + "  \"properties\": {    \n"
44             + "    \"key\": { \n"
45             + "      \"title\" : \"My key\", \n"
46             + "      \"type\": \"blabla\" \n"
47             + "    } \n"
48             + "  }\n"
49             + "}";
50 
51     @Test
draft4()52     public void draft4() throws Exception {
53         JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V4, builder -> builder
54                 .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("http://json-schema.org", "classpath:")));
55         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
56         config.setPathType(PathType.JSON_POINTER);
57         JsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V4), config);
58 
59         Set<ValidationMessage> assertions = schema.validate(JsonMapperFactory.getInstance().readTree(INVALID_INPUT));
60         assertEquals(2, assertions.size());
61 
62         assertions = schema.validate(JsonMapperFactory.getInstance().readTree(VALID_INPUT));
63         assertEquals(0, assertions.size());
64     }
65 
66     @Test
draft6()67     public void draft6() throws Exception {
68         JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V6, builder -> builder
69                 .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("http://json-schema.org", "classpath:")));
70         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
71         config.setPathType(PathType.JSON_POINTER);
72         JsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V6), config);
73 
74         Set<ValidationMessage> assertions = schema.validate(JsonMapperFactory.getInstance().readTree(INVALID_INPUT));
75         assertEquals(2, assertions.size());
76 
77         assertions = schema.validate(JsonMapperFactory.getInstance().readTree(VALID_INPUT));
78         assertEquals(0, assertions.size());
79     }
80 
81     @Test
draft7()82     public void draft7() throws Exception {
83         JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V7, builder -> builder
84                 .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("http://json-schema.org", "classpath:")));
85         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
86         config.setPathType(PathType.JSON_POINTER);
87         JsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V7), config);
88 
89         Set<ValidationMessage> assertions = schema.validate(JsonMapperFactory.getInstance().readTree(INVALID_INPUT));
90         assertEquals(2, assertions.size());
91 
92         assertions = schema.validate(JsonMapperFactory.getInstance().readTree(VALID_INPUT));
93         assertEquals(0, assertions.size());
94     }
95 
96     @Test
draft201909()97     public void draft201909() throws Exception {
98         JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V201909, builder -> builder
99                 .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("https://json-schema.org", "classpath:")));
100         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
101         config.setPathType(PathType.JSON_POINTER);
102         JsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V201909), config);
103 
104         Set<ValidationMessage> assertions = schema.validate(JsonMapperFactory.getInstance().readTree(INVALID_INPUT));
105         assertEquals(2, assertions.size());
106 
107         assertions = schema.validate(JsonMapperFactory.getInstance().readTree(VALID_INPUT));
108         assertEquals(0, assertions.size());
109     }
110 
111     @Test
draft202012()112     public void draft202012() throws Exception {
113         JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder
114                 .schemaMappers(schemaMappers -> schemaMappers.mapPrefix("https://json-schema.org", "classpath:")));
115         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
116         config.setPathType(PathType.JSON_POINTER);
117         JsonSchema schema = jsonSchemaFactory.getSchema(SchemaLocation.of(SchemaId.V202012), config);
118 
119         Set<ValidationMessage> assertions = schema.validate(JsonMapperFactory.getInstance().readTree(INVALID_INPUT));
120         assertEquals(2, assertions.size());
121 
122         assertions = schema.validate(JsonMapperFactory.getInstance().readTree(VALID_INPUT));
123         assertEquals(0, assertions.size());
124     }
125 }
126