View Javadoc
1   // ***************************************************************************************************************************
2   // * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
3   // * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
4   // * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
5   // * with the License.  You may obtain a copy of the License at                                                              *
6   // *                                                                                                                         *
7   // *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
8   // *                                                                                                                         *
9   // * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
10  // * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
11  // * specific language governing permissions and limitations under the License.                                              *
12  // ***************************************************************************************************************************
13  package org.apache.juneau.bean.jsonschema;
14  
15  import static org.apache.juneau.commons.utils.CollectionUtils.*;
16  import static org.junit.jupiter.api.Assertions.*;
17  
18  import java.util.*;
19  
20  import org.apache.juneau.*;
21  import org.apache.juneau.json.*;
22  import org.junit.jupiter.api.*;
23  
24  /**
25   * Tests for JsonSchemaRef fluent setter overrides.
26   */
27  class JsonSchemaRef_Test extends TestBase {
28  
29  	@Test void a01_fluentChaining_basicSetters() {
30  		var r = new JsonSchemaRef("http://example.com/schema");
31  
32  		// Test that fluent methods return JsonSchemaRef (not JsonSchema)
33  		JsonSchemaRef result;
34  
35  		result = r.setName("myRef");
36  		assertSame(r, result);
37  		assertInstanceOf(JsonSchemaRef.class, result);
38  
39  		result = r.setTitle("My Reference");
40  		assertSame(r, result);
41  
42  		result = r.setDescription("Reference description");
43  		assertSame(r, result);
44  
45  		result = r.setType(JsonType.STRING);
46  		assertSame(r, result);
47  
48  		result = r.setId("http://example.com/id");
49  		assertSame(r, result);
50  	}
51  
52  	@Test void a02_fluentChaining_numericConstraints() {
53  		var r = new JsonSchemaRef("http://example.com/schema");
54  
55  		// Test numeric constraint methods
56  		JsonSchemaRef result;
57  
58  		result = r.setMultipleOf(5);
59  		assertSame(r, result);
60  		assertInstanceOf(JsonSchemaRef.class, result);
61  
62  		result = r.setMaximum(100);
63  		assertSame(r, result);
64  
65  		result = r.setMinimum(0);
66  		assertSame(r, result);
67  
68  		result = r.setExclusiveMaximum(99);
69  		assertSame(r, result);
70  
71  		result = r.setExclusiveMinimum(1);
72  		assertSame(r, result);
73  	}
74  
75  	@Test void a03_fluentChaining_stringConstraints() {
76  		var r = new JsonSchemaRef("http://example.com/schema");
77  
78  		// Test string constraint methods
79  		JsonSchemaRef result;
80  
81  		result = r.setMaxLength(50);
82  		assertSame(r, result);
83  		assertInstanceOf(JsonSchemaRef.class, result);
84  
85  		result = r.setMinLength(1);
86  		assertSame(r, result);
87  
88  		result = r.setPattern("^[a-z]+$");
89  		assertSame(r, result);
90  
91  		result = r.setContentMediaType("text/plain");
92  		assertSame(r, result);
93  
94  		result = r.setContentEncoding("base64");
95  		assertSame(r, result);
96  	}
97  
98  	@Test void a04_fluentChaining_arrayConstraints() {
99  		var r = new JsonSchemaRef("http://example.com/schema");
100 
101 		// Test array constraint methods
102 		JsonSchemaRef result;
103 
104 		result = r.setMaxItems(10);
105 		assertSame(r, result);
106 		assertInstanceOf(JsonSchemaRef.class, result);
107 
108 		result = r.setMinItems(1);
109 		assertSame(r, result);
110 
111 		result = r.setUniqueItems(true);
112 		assertSame(r, result);
113 
114 		result = r.addItems(new JsonSchema());
115 		assertSame(r, result);
116 
117 		result = r.addAdditionalItems(new JsonSchema());
118 		assertSame(r, result);
119 	}
120 
121 	@Test void a05_fluentChaining_objectConstraints() {
122 		var r = new JsonSchemaRef("http://example.com/schema");
123 
124 		// Test object constraint methods
125 		JsonSchemaRef result;
126 
127 		result = r.setMaxProperties(20);
128 		assertSame(r, result);
129 		assertInstanceOf(JsonSchemaRef.class, result);
130 
131 		result = r.setMinProperties(1);
132 		assertSame(r, result);
133 
134 		var props = new HashMap<String, JsonSchema>();
135 		result = r.setProperties(props);
136 		assertSame(r, result);
137 
138 		result = r.addProperties(new JsonSchemaProperty("field1"));
139 		assertSame(r, result);
140 	}
141 
142 	@Test void a06_fluentChaining_requiredAndEnum() {
143 		var r = new JsonSchemaRef("http://example.com/schema");
144 
145 		// Test required and enum methods
146 		JsonSchemaRef result;
147 
148 		result = r.addRequired("field1", "field2");
149 		assertSame(r, result);
150 		assertInstanceOf(JsonSchemaRef.class, result);
151 
152 		result = r.setRequired(l("field3"));
153 		assertSame(r, result);
154 
155 		result = r.addEnum("value1", "value2");
156 		assertSame(r, result);
157 
158 		result = r.setEnum(l("value3"));
159 		assertSame(r, result);
160 	}
161 
162 	@Test void a07_fluentChaining_schemaComposition() {
163 		var r = new JsonSchemaRef("http://example.com/schema");
164 
165 		// Test schema composition methods
166 		JsonSchemaRef result;
167 
168 		result = r.addAllOf(new JsonSchema());
169 		assertSame(r, result);
170 		assertInstanceOf(JsonSchemaRef.class, result);
171 
172 		result = r.setAllOf(l(new JsonSchema()));
173 		assertSame(r, result);
174 
175 		result = r.addAnyOf(new JsonSchema());
176 		assertSame(r, result);
177 
178 		result = r.setAnyOf(l(new JsonSchema()));
179 		assertSame(r, result);
180 
181 		result = r.addOneOf(new JsonSchema());
182 		assertSame(r, result);
183 
184 		result = r.setOneOf(l(new JsonSchema()));
185 		assertSame(r, result);
186 
187 		result = r.setNot(new JsonSchema());
188 		assertSame(r, result);
189 	}
190 
191 	@Test void a08_fluentChaining_metadata() {
192 		var r = new JsonSchemaRef("http://example.com/schema");
193 
194 		// Test metadata methods
195 		JsonSchemaRef result;
196 
197 		result = r.setReadOnly(true);
198 		assertSame(r, result);
199 		assertInstanceOf(JsonSchemaRef.class, result);
200 
201 		result = r.setWriteOnly(false);
202 		assertSame(r, result);
203 
204 		result = r.addExamples("example1", "example2");
205 		assertSame(r, result);
206 
207 		result = r.setExamples(l("example3"));
208 		assertSame(r, result);
209 
210 		result = r.setConst("constantValue");
211 		assertSame(r, result);
212 	}
213 
214 	@Test void a09_fluentChaining_complex() {
215 		// Test chaining multiple fluent calls
216 		var result = new JsonSchemaRef("http://example.com/user-schema")
217 			.setName("userRef")
218 			.setTitle("User Reference")
219 			.setDescription("Reference to user schema");
220 
221 		assertInstanceOf(JsonSchemaRef.class, result);
222 	}
223 
224 	@Test void a10_constructor_withUri() {
225 		var r = new JsonSchemaRef("http://example.com/schema");
226 
227 		assertInstanceOf(JsonSchemaRef.class, r);
228 	}
229 
230 	@Test void a11_output_basic() throws Exception {
231 		var r = new JsonSchemaRef("http://example.com/schema");
232 
233 		String json = JsonSerializer.DEFAULT.serialize(r);
234 
235 		assertTrue(json.contains("\"$ref\""));
236 		assertTrue(json.contains("http://example.com/schema"));
237 	}
238 
239 	@Test void a12_fluentChaining_definitions() {
240 		var r = new JsonSchemaRef("http://example.com/schema");
241 
242 		// Test definition methods
243 		JsonSchemaRef result;
244 
245 		result = r.addDefinition("def1", new JsonSchema());
246 		assertSame(r, result);
247 		assertInstanceOf(JsonSchemaRef.class, result);
248 
249 		result = r.setDefinitions(new HashMap<>());
250 		assertSame(r, result);
251 
252 		result = r.addDef("def2", new JsonSchema());
253 		assertSame(r, result);
254 	}
255 
256 	@Test void a13_fluentChaining_dependencies() {
257 		var r = new JsonSchemaRef("http://example.com/schema");
258 
259 		// Test dependency methods
260 		JsonSchemaRef result;
261 
262 		result = r.addDependency("dep1", new JsonSchema());
263 		assertSame(r, result);
264 		assertInstanceOf(JsonSchemaRef.class, result);
265 
266 		result = r.setDependencies(new HashMap<>());
267 		assertSame(r, result);
268 
269 		result = r.addDependentSchema("dep2", new JsonSchema());
270 		assertSame(r, result);
271 
272 		result = r.setDependentSchemas(new HashMap<>());
273 		assertSame(r, result);
274 
275 		result = r.addDependentRequired("dep3", l("field1"));
276 		assertSame(r, result);
277 
278 		result = r.setDependentRequired(new HashMap<>());
279 		assertSame(r, result);
280 	}
281 
282 	@Test void a14_fluentChaining_patternProperties() {
283 		var r = new JsonSchemaRef("http://example.com/schema");
284 
285 		// Test pattern property methods
286 		JsonSchemaRef result;
287 
288 		result = r.addPatternProperties(new JsonSchemaProperty("pattern1"));
289 		assertSame(r, result);
290 		assertInstanceOf(JsonSchemaRef.class, result);
291 
292 		result = r.setPatternProperties(new HashMap<>());
293 		assertSame(r, result);
294 	}
295 
296 	@Test void a15_fluentChaining_prefixItems() {
297 		var r = new JsonSchemaRef("http://example.com/schema");
298 
299 		// Test prefix items methods
300 		JsonSchemaRef result;
301 
302 		result = r.addPrefixItems(new JsonSchema());
303 		assertSame(r, result);
304 		assertInstanceOf(JsonSchemaRef.class, result);
305 
306 		result = r.setPrefixItems(new JsonSchemaArray());
307 		assertSame(r, result);
308 	}
309 
310 	@Test void a16_fluentChaining_unevaluated() {
311 		var r = new JsonSchemaRef("http://example.com/schema");
312 
313 		// Test unevaluated methods
314 		JsonSchemaRef result;
315 
316 		result = r.setUnevaluatedItems(new JsonSchema());
317 		assertSame(r, result);
318 		assertInstanceOf(JsonSchemaRef.class, result);
319 
320 		result = r.setUnevaluatedProperties(new JsonSchema());
321 		assertSame(r, result);
322 	}
323 
324 	@Test void a17_fluentChaining_addTypes() {
325 		var r = new JsonSchemaRef("http://example.com/schema");
326 
327 		// Test addTypes method
328 		JsonSchemaRef result = r.addTypes(JsonType.STRING, JsonType.NULL);
329 
330 		assertSame(r, result);
331 		assertInstanceOf(JsonSchemaRef.class, result);
332 	}
333 
334 	@Test void a18_getterMethods() {
335 		var r = new JsonSchemaRef("http://example.com/schema");
336 
337 		// Test getter methods that are overridden
338 		r.setNot(new JsonSchema().setType(JsonType.STRING));
339 		JsonSchema not = r.getNot();
340 		assertNotNull(not);
341 
342 		r.setUnevaluatedItems(new JsonSchema());
343 		JsonSchema unevaluated = r.getUnevaluatedItems();
344 		assertNotNull(unevaluated);
345 	}
346 
347 	@Test void a19_output_withAdditionalProperties() throws Exception {
348 		var r = new JsonSchemaRef("http://example.com/schema")
349 			.setTitle("User Schema")
350 			.setDescription("Schema for user objects");
351 
352 		String json = JsonSerializer.DEFAULT.serialize(r);
353 
354 		assertTrue(json.contains("\"$ref\""));
355 		assertTrue(json.contains("http://example.com/schema"));
356 		assertTrue(json.contains("\"title\":\"User Schema\""));
357 		assertTrue(json.contains("\"description\":\"Schema for user objects\""));
358 	}
359 }