1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.bean.jsonschema;
18
19 import static org.junit.jupiter.api.Assertions.*;
20
21 import java.net.*;
22
23 import org.apache.juneau.*;
24 import org.apache.juneau.json.*;
25 import org.junit.jupiter.api.*;
26
27 @SuppressWarnings("deprecation")
28 public class JsonSchema_Test extends TestBase {
29
30 @Test void a01_schema1() throws Exception {
31 var s = JsonSerializer.create().json5().ws().sortProperties().build();
32 var p = JsonParser.DEFAULT;
33
34 var expected = """
35 {
36 '$schema': 'http:
37 additionalItems: [
38 {
39 type: 'number'
40 }
41 ],
42 additionalProperties: {
43 '$ref': 'http:
44 },
45 allOf: [
46 {
47 '$ref': 'http:
48 }
49 ],
50 anyOf: [
51 {
52 '$ref': 'http:
53 }
54 ],
55 definitions: {
56 definition: {
57 '$ref': 'http:
58 }
59 },
60 dependencies: {
61 dependency: {
62 '$ref': 'http:
63 }
64 },
65 description: 'description',
66 'enum': [
67 'enum'
68 ],
69 exclusiveMaximum: 10,
70 exclusiveMinimum: 1,
71 id: 'http:
72 items: [
73 {
74 type: 'number'
75 }
76 ],
77 maxItems: 6,
78 maxLength: 4,
79 maxProperties: 8,
80 maximum: 2,
81 minItems: 7,
82 minLength: 5,
83 minProperties: 9,
84 minimum: 3,
85 multipleOf: 1,
86 not: {
87 '$ref': 'http:
88 },
89 oneOf: [
90 {
91 '$ref': 'http:
92 }
93 ],
94 pattern: '/pattern/',
95 patternProperties: {
96 '/pattern/': {
97 type: 'number'
98 }
99 },
100 properties: {
101 property: {
102 type: 'number'
103 }
104 },
105 required: [
106 'required'
107 ],
108 title: 'title',
109 type: 'number',
110 uniqueItems: true
111 }""";
112
113 var t = getTest1();
114 var r = s.serialize(t);
115 assertEquals(expected, r);
116 var t2 = p.parse(r, JsonSchema.class);
117 r = s.serialize(t2);
118 assertEquals(expected, r);
119 }
120
121 @Test void a02_schema2() throws Exception {
122 var s = JsonSerializer.create().json5().ws().sortProperties().build();
123 var p = JsonParser.DEFAULT;
124
125 var expected = """
126 {
127 '$schema': 'http:
128 additionalItems: true,
129 additionalProperties: true,
130 definitions: {
131 definition: {
132 id: 'http:
133 }
134 },
135 id: 'http:
136 items: [
137 {
138 '$ref': 'http:
139 }
140 ],
141 type: [
142 'string',
143 'number'
144 ]
145 }""";
146
147 var t = getTest2();
148 var r = s.serialize(t);
149 assertEquals(expected, r);
150 var t2 = p.parse(r, JsonSchema.class);
151 r = s.serialize(t2);
152 assertEquals(expected, r);
153 }
154
155 @Test void a03_toString() throws Exception {
156 var s = JsonSerializer.create().json5().ws().sortProperties().build();
157 var p = JsonParser.DEFAULT;
158
159 var expected = """
160 {
161 '$schema': 'http:
162 additionalItems: [
163 {
164 type: 'number'
165 }
166 ],
167 additionalProperties: {
168 '$ref': 'http:
169 },
170 allOf: [
171 {
172 '$ref': 'http:
173 }
174 ],
175 anyOf: [
176 {
177 '$ref': 'http:
178 }
179 ],
180 definitions: {
181 definition: {
182 '$ref': 'http:
183 }
184 },
185 dependencies: {
186 dependency: {
187 '$ref': 'http:
188 }
189 },
190 description: 'description',
191 'enum': [
192 'enum'
193 ],
194 exclusiveMaximum: 10,
195 exclusiveMinimum: 1,
196 id: 'http:
197 items: [
198 {
199 type: 'number'
200 }
201 ],
202 maxItems: 6,
203 maxLength: 4,
204 maxProperties: 8,
205 maximum: 2,
206 minItems: 7,
207 minLength: 5,
208 minProperties: 9,
209 minimum: 3,
210 multipleOf: 1,
211 not: {
212 '$ref': 'http:
213 },
214 oneOf: [
215 {
216 '$ref': 'http:
217 }
218 ],
219 pattern: '/pattern/',
220 patternProperties: {
221 '/pattern/': {
222 type: 'number'
223 }
224 },
225 properties: {
226 property: {
227 type: 'number'
228 }
229 },
230 required: [
231 'required'
232 ],
233 title: 'title',
234 type: 'number',
235 uniqueItems: true
236 }""";
237
238 var t = getTest1();
239 var r = t.toString();
240 var t2 = p.parse(r, JsonSchema.class);
241 r = s.serialize(t2);
242 assertEquals(expected, r);
243 }
244
245
246
247 public static JsonSchema getTest1() {
248 return new JsonSchema()
249 .setId("http://id")
250 .setSchemaVersionUri("http://schemaVersionUri")
251 .setTitle("title")
252 .setDescription("description")
253 .setType(JsonType.NUMBER)
254 .addDefinition("definition", new JsonSchemaRef("http://definition"))
255 .addProperties(new JsonSchemaProperty("property", JsonType.NUMBER))
256 .addPatternProperties(new JsonSchemaProperty("/pattern/", JsonType.NUMBER))
257 .addDependency("dependency", new JsonSchemaRef("http://dependency"))
258 .addItems(new JsonSchema().setType(JsonType.NUMBER))
259 .setMultipleOf(1)
260 .setMaximum(2)
261 .setExclusiveMaximum(Integer.valueOf(10))
262 .setMinimum(3)
263 .setExclusiveMinimum(Integer.valueOf(1))
264 .setMaxLength(4)
265 .setMinLength(5)
266 .setPattern("/pattern/")
267 .addAdditionalItems(new JsonSchemaProperty("additionalItem", JsonType.NUMBER))
268 .setMaxItems(6)
269 .setMinItems(7)
270 .setUniqueItems(true)
271 .setMaxProperties(8)
272 .setMinProperties(9)
273 .addRequired("required")
274 .setAdditionalProperties(new JsonSchemaRef("http://additionalProperty"))
275 .addEnum("enum")
276 .addAllOf(new JsonSchemaRef("http://allOf"))
277 .addAnyOf(new JsonSchemaRef("http://anyOf"))
278 .addOneOf(new JsonSchemaRef("http://oneOf"))
279 .setNot(new JsonSchemaRef("http://not"))
280 ;
281 }
282
283
284 public static JsonSchema getTest2() {
285 return new JsonSchema()
286 .setId(URI.create("http://id"))
287 .setSchemaVersionUri(URI.create("http://schemaVersionUri"))
288 .setType(new JsonTypeArray(JsonType.STRING, JsonType.NUMBER))
289 .addDefinition("definition", new JsonSchema().setId("http://definition"))
290 .setItems(new JsonSchemaArray(new JsonSchemaRef("http://items")))
291 .setAdditionalItems(Boolean.TRUE)
292 .setAdditionalProperties(Boolean.TRUE);
293 }
294 }