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 JsonSchemaPropertySimpleArray fluent setter overrides.
26   */
27  class JsonSchemaPropertySimpleArray_Test extends TestBase {
28  
29  	@Test void a01_fluentChaining_basicSetters() {
30  		var p = new JsonSchemaPropertySimpleArray("myArray", JsonType.STRING);
31  
32  		// Test that fluent methods return JsonSchemaPropertySimpleArray (not JsonSchemaProperty or JsonSchema)
33  		JsonSchemaPropertySimpleArray result;
34  
35  		result = p.setName("renamedArray");
36  		assertSame(p, result);
37  		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
38  
39  		result = p.setTitle("My Array");
40  		assertSame(p, result);
41  
42  		result = p.setDescription("Array description");
43  		assertSame(p, result);
44  
45  		result = p.setId("http://example.com/schema");
46  		assertSame(p, result);
47  	}
48  
49  	@Test void a02_fluentChaining_arrayConstraints() {
50  		var p = new JsonSchemaPropertySimpleArray("items", JsonType.NUMBER);
51  
52  		// Test array constraint methods
53  		JsonSchemaPropertySimpleArray result;
54  
55  		result = p.setMaxItems(10);
56  		assertSame(p, result);
57  		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
58  
59  		result = p.setMinItems(1);
60  		assertSame(p, result);
61  
62  		result = p.setUniqueItems(true);
63  		assertSame(p, result);
64  
65  		result = p.addItems(new JsonSchema());
66  		assertSame(p, result);
67  
68  		result = p.addAdditionalItems(new JsonSchema());
69  		assertSame(p, result);
70  	}
71  
72  	@Test void a03_fluentChaining_numericConstraints() {
73  		var p = new JsonSchemaPropertySimpleArray("numbers", JsonType.INTEGER);
74  
75  		// Test numeric constraint methods
76  		JsonSchemaPropertySimpleArray result;
77  
78  		result = p.setMultipleOf(5);
79  		assertSame(p, result);
80  		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
81  
82  		result = p.setMaximum(100);
83  		assertSame(p, result);
84  
85  		result = p.setMinimum(0);
86  		assertSame(p, result);
87  	}
88  
89  	@Test void a04_fluentChaining_stringConstraints() {
90  		var p = new JsonSchemaPropertySimpleArray("tags", JsonType.STRING);
91  
92  		// Test string constraint methods
93  		JsonSchemaPropertySimpleArray result;
94  
95  		result = p.setMaxLength(50);
96  		assertSame(p, result);
97  		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
98  
99  		result = p.setMinLength(1);
100 		assertSame(p, result);
101 
102 		result = p.setPattern("^[a-z]+$");
103 		assertSame(p, result);
104 	}
105 
106 	@Test void a05_fluentChaining_metadata() {
107 		var p = new JsonSchemaPropertySimpleArray("data", JsonType.OBJECT);
108 
109 		// Test metadata methods
110 		JsonSchemaPropertySimpleArray result;
111 
112 		result = p.setReadOnly(true);
113 		assertSame(p, result);
114 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
115 
116 		result = p.setWriteOnly(false);
117 		assertSame(p, result);
118 
119 		result = p.addExamples("example1", "example2");
120 		assertSame(p, result);
121 
122 		result = p.setConst("constantValue");
123 		assertSame(p, result);
124 	}
125 
126 	@Test void a06_fluentChaining_requiredAndEnum() {
127 		var p = new JsonSchemaPropertySimpleArray("values", JsonType.STRING);
128 
129 		// Test required and enum methods
130 		JsonSchemaPropertySimpleArray result;
131 
132 		result = p.addRequired("field1", "field2");
133 		assertSame(p, result);
134 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
135 
136 		result = p.addEnum("value1", "value2");
137 		assertSame(p, result);
138 	}
139 
140 	@Test void a07_fluentChaining_schemaComposition() {
141 		var p = new JsonSchemaPropertySimpleArray("composite", JsonType.ANY);
142 
143 		// Test schema composition methods
144 		JsonSchemaPropertySimpleArray result;
145 
146 		result = p.addAllOf(new JsonSchema());
147 		assertSame(p, result);
148 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
149 
150 		result = p.addAnyOf(new JsonSchema());
151 		assertSame(p, result);
152 
153 		result = p.addOneOf(new JsonSchema());
154 		assertSame(p, result);
155 
156 		result = p.setNot(new JsonSchema());
157 		assertSame(p, result);
158 	}
159 
160 	@Test void a08_fluentChaining_complex() {
161 		// Test chaining multiple fluent calls
162 		var result = new JsonSchemaPropertySimpleArray("usernames", JsonType.STRING)
163 			.setMinItems(1)
164 			.setMaxItems(10)
165 			.setMinLength(3)
166 			.setMaxLength(20)
167 			.setPattern("^[a-zA-Z0-9_]+$")
168 			.setDescription("List of usernames");
169 
170 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
171 	}
172 
173 	@Test void a09_constructor_withNameAndType() {
174 		var p = new JsonSchemaPropertySimpleArray("myArray", JsonType.STRING);
175 
176 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, p);
177 	}
178 
179 	@Test void a10_output_basic() throws Exception {
180 		var p = new JsonSchemaPropertySimpleArray("tags", JsonType.STRING)
181 			.setMinItems(1)
182 			.setMaxItems(5);
183 
184 		String json = JsonSerializer.DEFAULT.serialize(p);
185 
186 		assertTrue(json.contains("\"type\":\"array\""));
187 		assertTrue(json.contains("\"minItems\":1"));
188 		assertTrue(json.contains("\"maxItems\":5"));
189 		assertTrue(json.contains("\"items\""));
190 	}
191 
192 	@Test void a11_fluentChaining_definitions() {
193 		var p = new JsonSchemaPropertySimpleArray("array", JsonType.STRING);
194 
195 		// Test definition methods
196 		JsonSchemaPropertySimpleArray result;
197 
198 		result = p.addDefinition("def1", new JsonSchema());
199 		assertSame(p, result);
200 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
201 
202 		result = p.setDefinitions(new HashMap<>());
203 		assertSame(p, result);
204 	}
205 
206 	@Test void a12_fluentChaining_properties() {
207 		var p = new JsonSchemaPropertySimpleArray("objects", JsonType.OBJECT);
208 
209 		// Test property methods
210 		JsonSchemaPropertySimpleArray result;
211 
212 		result = p.addProperties(new JsonSchemaProperty("field1"));
213 		assertSame(p, result);
214 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
215 
216 		result = p.setProperties(new HashMap<>());
217 		assertSame(p, result);
218 	}
219 
220 	@Test void a13_fluentChaining_dependencies() {
221 		var p = new JsonSchemaPropertySimpleArray("data", JsonType.ANY);
222 
223 		// Test dependency methods
224 		JsonSchemaPropertySimpleArray result;
225 
226 		result = p.addDependency("dep1", new JsonSchema());
227 		assertSame(p, result);
228 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
229 
230 		result = p.addDependentSchema("dep2", new JsonSchema());
231 		assertSame(p, result);
232 
233 		result = p.addDependentRequired("dep3", l("field1"));
234 		assertSame(p, result);
235 	}
236 
237 	@Test void a14_fluentChaining_prefixItems() {
238 		var p = new JsonSchemaPropertySimpleArray("tuple", JsonType.ANY);
239 
240 		// Test prefix items methods
241 		JsonSchemaPropertySimpleArray result;
242 
243 		result = p.addPrefixItems(new JsonSchema());
244 		assertSame(p, result);
245 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
246 
247 		result = p.setPrefixItems(new JsonSchemaArray());
248 		assertSame(p, result);
249 	}
250 
251 	@Test void a15_fluentChaining_unevaluated() {
252 		var p = new JsonSchemaPropertySimpleArray("array", JsonType.ANY);
253 
254 		// Test unevaluated methods
255 		JsonSchemaPropertySimpleArray result;
256 
257 		result = p.setUnevaluatedItems(new JsonSchema());
258 		assertSame(p, result);
259 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
260 
261 		result = p.setUnevaluatedProperties(new JsonSchema());
262 		assertSame(p, result);
263 	}
264 
265 	@Test void a16_output_withConstraints() throws Exception {
266 		var p = new JsonSchemaPropertySimpleArray("ids", JsonType.INTEGER)
267 			.setMinItems(1)
268 			.setUniqueItems(true)
269 			.setDescription("List of unique IDs");
270 
271 		String json = JsonSerializer.DEFAULT.serialize(p);
272 
273 		assertTrue(json.contains("\"type\":\"array\""));
274 		assertTrue(json.contains("\"minItems\":1"));
275 		assertTrue(json.contains("\"uniqueItems\":true"));
276 		assertTrue(json.contains("\"description\":\"List of unique IDs\""));
277 	}
278 
279 	@Test void a17_fluentChaining_addTypes() {
280 		var p = new JsonSchemaPropertySimpleArray("mixed", JsonType.ANY);
281 
282 		// Test addTypes method
283 		JsonSchemaPropertySimpleArray result = p.addTypes(JsonType.STRING, JsonType.NULL);
284 
285 		assertSame(p, result);
286 		assertInstanceOf(JsonSchemaPropertySimpleArray.class, result);
287 	}
288 }