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 JsonSchemaProperty fluent setter overrides.
26   */
27  class JsonSchemaProperty_Test extends TestBase {
28  
29  	@Test void a01_fluentChaining_basicSetters() {
30  		var p = new JsonSchemaProperty();
31  
32  		// Test that fluent methods return JsonSchemaProperty (not JsonSchema)
33  		JsonSchemaProperty result;
34  
35  		result = p.setName("myProperty");
36  		assertSame(p, result);
37  		assertInstanceOf(JsonSchemaProperty.class, result);
38  
39  		result = p.setTitle("My Property");
40  		assertSame(p, result);
41  
42  		result = p.setDescription("Property description");
43  		assertSame(p, result);
44  
45  		result = p.setType(JsonType.STRING);
46  		assertSame(p, result);
47  
48  		result = p.setId("http://example.com/schema");
49  		assertSame(p, result);
50  	}
51  
52  	@Test void a02_fluentChaining_numericConstraints() {
53  		var p = new JsonSchemaProperty();
54  
55  		// Test numeric constraint methods
56  		JsonSchemaProperty result;
57  
58  		result = p.setMultipleOf(5);
59  		assertSame(p, result);
60  		assertInstanceOf(JsonSchemaProperty.class, result);
61  
62  		result = p.setMaximum(100);
63  		assertSame(p, result);
64  
65  		result = p.setMinimum(0);
66  		assertSame(p, result);
67  
68  		result = p.setExclusiveMaximum(99);
69  		assertSame(p, result);
70  
71  		result = p.setExclusiveMinimum(1);
72  		assertSame(p, result);
73  	}
74  
75  	@Test void a03_fluentChaining_stringConstraints() {
76  		var p = new JsonSchemaProperty();
77  
78  		// Test string constraint methods
79  		JsonSchemaProperty result;
80  
81  		result = p.setMaxLength(50);
82  		assertSame(p, result);
83  		assertInstanceOf(JsonSchemaProperty.class, result);
84  
85  		result = p.setMinLength(1);
86  		assertSame(p, result);
87  
88  		result = p.setPattern("^[a-z]+$");
89  		assertSame(p, result);
90  
91  		result = p.setContentMediaType("text/plain");
92  		assertSame(p, result);
93  
94  		result = p.setContentEncoding("base64");
95  		assertSame(p, result);
96  	}
97  
98  	@Test void a04_fluentChaining_arrayConstraints() {
99  		var p = new JsonSchemaProperty();
100 
101 		// Test array constraint methods
102 		JsonSchemaProperty result;
103 
104 		result = p.setMaxItems(10);
105 		assertSame(p, result);
106 		assertInstanceOf(JsonSchemaProperty.class, result);
107 
108 		result = p.setMinItems(1);
109 		assertSame(p, result);
110 
111 		result = p.setUniqueItems(true);
112 		assertSame(p, result);
113 
114 		result = p.addItems(new JsonSchema());
115 		assertSame(p, result);
116 
117 		result = p.addAdditionalItems(new JsonSchema());
118 		assertSame(p, result);
119 	}
120 
121 	@Test void a05_fluentChaining_objectConstraints() {
122 		var p = new JsonSchemaProperty();
123 
124 		// Test object constraint methods
125 		JsonSchemaProperty result;
126 
127 		result = p.setMaxProperties(20);
128 		assertSame(p, result);
129 		assertInstanceOf(JsonSchemaProperty.class, result);
130 
131 		result = p.setMinProperties(1);
132 		assertSame(p, result);
133 
134 		var props = new HashMap<String, JsonSchema>();
135 		result = p.setProperties(props);
136 		assertSame(p, result);
137 
138 		result = p.addProperties(new JsonSchemaProperty("field1"));
139 		assertSame(p, result);
140 	}
141 
142 	@Test void a06_fluentChaining_requiredAndEnum() {
143 		var p = new JsonSchemaProperty();
144 
145 		// Test required and enum methods
146 		JsonSchemaProperty result;
147 
148 		result = p.addRequired("field1", "field2");
149 		assertSame(p, result);
150 		assertInstanceOf(JsonSchemaProperty.class, result);
151 
152 		result = p.setRequired(l("field3"));
153 		assertSame(p, result);
154 
155 		result = p.addEnum("value1", "value2");
156 		assertSame(p, result);
157 
158 		result = p.setEnum(l("value3"));
159 		assertSame(p, result);
160 	}
161 
162 	@Test void a07_fluentChaining_schemaComposition() {
163 		var p = new JsonSchemaProperty();
164 
165 		// Test schema composition methods
166 		JsonSchemaProperty result;
167 
168 		result = p.addAllOf(new JsonSchema());
169 		assertSame(p, result);
170 		assertInstanceOf(JsonSchemaProperty.class, result);
171 
172 		result = p.setAllOf(l(new JsonSchema()));
173 		assertSame(p, result);
174 
175 		result = p.addAnyOf(new JsonSchema());
176 		assertSame(p, result);
177 
178 		result = p.setAnyOf(l(new JsonSchema()));
179 		assertSame(p, result);
180 
181 		result = p.addOneOf(new JsonSchema());
182 		assertSame(p, result);
183 
184 		result = p.setOneOf(l(new JsonSchema()));
185 		assertSame(p, result);
186 
187 		result = p.setNot(new JsonSchema());
188 		assertSame(p, result);
189 	}
190 
191 	@Test void a08_fluentChaining_metadata() {
192 		var p = new JsonSchemaProperty();
193 
194 		// Test metadata methods
195 		JsonSchemaProperty result;
196 
197 		result = p.setReadOnly(true);
198 		assertSame(p, result);
199 		assertInstanceOf(JsonSchemaProperty.class, result);
200 
201 		result = p.setWriteOnly(false);
202 		assertSame(p, result);
203 
204 		result = p.addExamples("example1", "example2");
205 		assertSame(p, result);
206 
207 		result = p.setExamples(l("example3"));
208 		assertSame(p, result);
209 
210 		result = p.setConst("constantValue");
211 		assertSame(p, result);
212 	}
213 
214 	@Test void a09_fluentChaining_complex() {
215 		// Test chaining multiple fluent calls
216 		var result = new JsonSchemaProperty()
217 			.setName("username")
218 			.setType(JsonType.STRING)
219 			.setMinLength(3)
220 			.setMaxLength(20)
221 			.setPattern("^[a-zA-Z0-9_]+$")
222 			.setDescription("User's login name");
223 
224 		assertInstanceOf(JsonSchemaProperty.class, result);
225 	}
226 
227 	@Test void a10_constructor_withName() {
228 		var p = new JsonSchemaProperty("myProperty");
229 
230 		assertInstanceOf(JsonSchemaProperty.class, p);
231 	}
232 
233 	@Test void a11_constructor_withNameAndType() {
234 		var p = new JsonSchemaProperty("myProperty", JsonType.STRING);
235 
236 		assertInstanceOf(JsonSchemaProperty.class, p);
237 	}
238 
239 	@Test void a12_output_basic() throws Exception {
240 		var p = new JsonSchemaProperty("name", JsonType.STRING)
241 			.setMinLength(1)
242 			.setMaxLength(50);
243 
244 		String json = JsonSerializer.DEFAULT.serialize(p);
245 
246 		// Verify basic schema properties are serialized
247 		assertTrue(json.contains("\"type\":\"string\""));
248 		assertTrue(json.contains("\"minLength\":1"));
249 		assertTrue(json.contains("\"maxLength\":50"));
250 	}
251 
252 	@Test void a13_fluentChaining_definitions() {
253 		var p = new JsonSchemaProperty();
254 
255 		// Test definition methods
256 		JsonSchemaProperty result;
257 
258 		result = p.addDefinition("def1", new JsonSchema());
259 		assertSame(p, result);
260 		assertInstanceOf(JsonSchemaProperty.class, result);
261 
262 		result = p.setDefinitions(new HashMap<>());
263 		assertSame(p, result);
264 
265 		result = p.addDef("def2", new JsonSchema());
266 		assertSame(p, result);
267 	}
268 
269 	@Test void a14_fluentChaining_dependencies() {
270 		var p = new JsonSchemaProperty();
271 
272 		// Test dependency methods
273 		JsonSchemaProperty result;
274 
275 		result = p.addDependency("dep1", new JsonSchema());
276 		assertSame(p, result);
277 		assertInstanceOf(JsonSchemaProperty.class, result);
278 
279 		result = p.setDependencies(new HashMap<>());
280 		assertSame(p, result);
281 
282 		result = p.addDependentSchema("dep2", new JsonSchema());
283 		assertSame(p, result);
284 
285 		result = p.setDependentSchemas(new HashMap<>());
286 		assertSame(p, result);
287 
288 		result = p.addDependentRequired("dep3", l("field1"));
289 		assertSame(p, result);
290 
291 		result = p.setDependentRequired(new HashMap<>());
292 		assertSame(p, result);
293 	}
294 
295 	@Test void a15_fluentChaining_patternProperties() {
296 		var p = new JsonSchemaProperty();
297 
298 		// Test pattern property methods
299 		JsonSchemaProperty result;
300 
301 		result = p.addPatternProperties(new JsonSchemaProperty("pattern1"));
302 		assertSame(p, result);
303 		assertInstanceOf(JsonSchemaProperty.class, result);
304 
305 		result = p.setPatternProperties(new HashMap<>());
306 		assertSame(p, result);
307 	}
308 
309 	@Test void a16_fluentChaining_prefixItems() {
310 		var p = new JsonSchemaProperty();
311 
312 		// Test prefix items methods
313 		JsonSchemaProperty result;
314 
315 		result = p.addPrefixItems(new JsonSchema());
316 		assertSame(p, result);
317 		assertInstanceOf(JsonSchemaProperty.class, result);
318 
319 		result = p.setPrefixItems(new JsonSchemaArray());
320 		assertSame(p, result);
321 	}
322 
323 	@Test void a17_fluentChaining_unevaluated() {
324 		var p = new JsonSchemaProperty();
325 
326 		// Test unevaluated methods
327 		JsonSchemaProperty result;
328 
329 		result = p.setUnevaluatedItems(new JsonSchema());
330 		assertSame(p, result);
331 		assertInstanceOf(JsonSchemaProperty.class, result);
332 
333 		result = p.setUnevaluatedProperties(new JsonSchema());
334 		assertSame(p, result);
335 	}
336 
337 	@Test void a18_fluentChaining_addTypes() {
338 		var p = new JsonSchemaProperty();
339 
340 		// Test addTypes method
341 		JsonSchemaProperty result = p.addTypes(JsonType.STRING, JsonType.NULL);
342 
343 		assertSame(p, result);
344 		assertInstanceOf(JsonSchemaProperty.class, result);
345 	}
346 
347 	@Test void a19_getterMethods() {
348 		var p = new JsonSchemaProperty();
349 
350 		// Test getter methods that are overridden
351 		p.setNot(new JsonSchema().setType(JsonType.STRING));
352 		JsonSchema not = p.getNot();
353 		assertNotNull(not);
354 
355 		p.setUnevaluatedItems(new JsonSchema());
356 		JsonSchema unevaluated = p.getUnevaluatedItems();
357 		assertNotNull(unevaluated);
358 	}
359 }