View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.jsonschema.annotation;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.annotation.*;
24  import org.junit.jupiter.api.*;
25  
26  class SchemaAnnotation_Test extends TestBase {
27  
28  	private static final String CNAME = SchemaAnnotation_Test.class.getName();
29  
30  	private static class X1 {}
31  
32  	//------------------------------------------------------------------------------------------------------------------
33  	// Basic tests
34  	//------------------------------------------------------------------------------------------------------------------
35  
36  	Schema a1 = SchemaAnnotation.create()
37  		._default("a")
38  		._enum("b")
39  		.$ref("c")
40  		.additionalProperties("d")
41  		.allOf("e")
42  		.cf("f")
43  		.collectionFormat("g")
44  		.d("h")
45  		.description("i")
46  		.df("j")
47  		.discriminator("k")
48  		.e("l")
49  		.emax(true)
50  		.emin(true)
51  		.exclusiveMaximum(true)
52  		.exclusiveMinimum(true)
53  		.externalDocs(ExternalDocsAnnotation.DEFAULT)
54  		.f("m")
55  		.format("n")
56  		.ignore(true)
57  		.items(ItemsAnnotation.DEFAULT)
58  		.max("o")
59  		.maxi(1)
60  		.maximum("p")
61  		.maxItems(2)
62  		.maxl(3)
63  		.maxLength(4)
64  		.maxp(5)
65  		.maxProperties(6)
66  		.min("q")
67  		.mini(7)
68  		.minimum("r")
69  		.minItems(8)
70  		.minl(9)
71  		.minLength(10)
72  		.minp(11)
73  		.minProperties(12)
74  		.mo("s")
75  		.multipleOf("t")
76  		.on("u")
77  		.onClass(X1.class)
78  		.p("v")
79  		.pattern("w")
80  		.properties("x")
81  		.r(true)
82  		.readOnly(true)
83  		.required(true)
84  		.ro(true)
85  		.t("z")
86  		.title("aa")
87  		.type("bb")
88  		.ui(true)
89  		.uniqueItems(true)
90  		.xml("cc")
91  		.build();
92  
93  	Schema a2 = SchemaAnnotation.create()
94  		._default("a")
95  		._enum("b")
96  		.$ref("c")
97  		.additionalProperties("d")
98  		.allOf("e")
99  		.cf("f")
100 		.collectionFormat("g")
101 		.d("h")
102 		.description("i")
103 		.df("j")
104 		.discriminator("k")
105 		.e("l")
106 		.emax(true)
107 		.emin(true)
108 		.exclusiveMaximum(true)
109 		.exclusiveMinimum(true)
110 		.externalDocs(ExternalDocsAnnotation.DEFAULT)
111 		.f("m")
112 		.format("n")
113 		.ignore(true)
114 		.items(ItemsAnnotation.DEFAULT)
115 		.max("o")
116 		.maxi(1)
117 		.maximum("p")
118 		.maxItems(2)
119 		.maxl(3)
120 		.maxLength(4)
121 		.maxp(5)
122 		.maxProperties(6)
123 		.min("q")
124 		.mini(7)
125 		.minimum("r")
126 		.minItems(8)
127 		.minl(9)
128 		.minLength(10)
129 		.minp(11)
130 		.minProperties(12)
131 		.mo("s")
132 		.multipleOf("t")
133 		.on("u")
134 		.onClass(X1.class)
135 		.p("v")
136 		.pattern("w")
137 		.properties("x")
138 		.r(true)
139 		.readOnly(true)
140 		.required(true)
141 		.ro(true)
142 		.t("z")
143 		.title("aa")
144 		.type("bb")
145 		.ui(true)
146 		.uniqueItems(true)
147 		.xml("cc")
148 		.build();
149 
150 	@Test void a01_basic() {
151 		assertBean(a1,
152 			"$ref,_default,_enum,additionalProperties,aev,allOf,allowEmptyValue,cf,collectionFormat,d,description,df,discriminator,e,emax,emin,exclusiveMaximum,exclusiveMinimum,externalDocs{description,url},f,format,ignore,items{$ref,_default,_enum,cf,collectionFormat,description,df,e,emax,emin,exclusiveMaximum,exclusiveMinimum,f,format,items{$ref,_default,_enum,cf,collectionFormat,description,df,e,emax,emin,exclusiveMaximum,exclusiveMinimum,f,format,items,max,maxItems,maxLength,maxi,maximum,maxl,min,minItems,minLength,mini,minimum,minl,mo,multipleOf,p,pattern,t,type,ui,uniqueItems},max,maxItems,maxLength,maxi,maximum,maxl,min,minItems,minLength,mini,minimum,minl,mo,multipleOf,p,pattern,t,type,ui,uniqueItems},max,maxItems,maxLength,maxProperties,maxi,maximum,maxl,maxp,min,minItems,minLength,minProperties,mini,minimum,minl,minp,mo,multipleOf,on,onClass,p,pattern,properties,r,readOnly,required,ro,sie,skipIfEmpty,t,title,type,ui,uniqueItems,xml",
153 			"c,[a],[b],[d],false,[e],false,f,g,[h],[i],[j],k,[l],true,true,true,true,{[],},m,n,true,{,[],[],,,[],[],[],false,false,false,false,,,{,[],[],,,[],[],[],false,false,false,false,,,[],,-1,-1,-1,,-1,,-1,-1,-1,,-1,,,,,,,false,false},,-1,-1,-1,,-1,,-1,-1,-1,,-1,,,,,,,false,false},o,2,4,6,1,p,3,5,q,8,10,12,7,r,9,11,s,t,[u],[X1],v,w,[x],true,true,true,true,false,false,z,aa,bb,true,true,[cc]");
154 	}
155 
156 	@Test void a02_testEquivalency() {
157 		assertEquals(a2, a1);
158 		assertNotEqualsAny(a1.hashCode(), 0, -1);
159 		assertEquals(a1.hashCode(), a2.hashCode());
160 	}
161 
162 	//------------------------------------------------------------------------------------------------------------------
163 	// PropertyStore equivalency.
164 	//------------------------------------------------------------------------------------------------------------------
165 
166 	@Test void b01_testEquivalencyInPropertyStores() {
167 		var bc1 = BeanContext.create().annotations(a1).build();
168 		var bc2 = BeanContext.create().annotations(a2).build();
169 		assertSame(bc1, bc2);
170 	}
171 
172 	//------------------------------------------------------------------------------------------------------------------
173 	// Other methods.
174 	//------------------------------------------------------------------------------------------------------------------
175 
176 	public static class C1 {
177 		public int f1;
178 		public void m1() {}  // NOSONAR
179 	}
180 	public static class C2 {
181 		public int f2;
182 		public void m2() {}  // NOSONAR
183 	}
184 
185 	@Test void c01_otherMethods() throws Exception {
186 		var c1 = SchemaAnnotation.create(C1.class).on(C2.class).build();
187 		var c2 = SchemaAnnotation.create("a").on("b").build();
188 		var c3 = SchemaAnnotation.create().on(C1.class.getField("f1")).on(C2.class.getField("f2")).build();
189 		var c4 = SchemaAnnotation.create().on(C1.class.getMethod("m1")).on(C2.class.getMethod("m2")).build();
190 
191 		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
192 		assertBean(c2, "on", "[a,b]");
193 		assertBean(c3, "on", "["+CNAME+"$C1.f1,"+CNAME+"$C2.f2]");
194 		assertBean(c4, "on", "["+CNAME+"$C1.m1(),"+CNAME+"$C2.m2()]");
195 	}
196 
197 	//------------------------------------------------------------------------------------------------------------------
198 	// Comparison with declared annotations.
199 	//------------------------------------------------------------------------------------------------------------------
200 
201 	@Schema(
202 		_default="a",
203 		_enum="b",
204 		$ref="c",
205 		additionalProperties="d",
206 		allOf="e",
207 		cf="f",
208 		collectionFormat="g",
209 		d="h",
210 		description="i",
211 		df="j",
212 		discriminator="k",
213 		e="l",
214 		emax=true,
215 		emin=true,
216 		exclusiveMaximum=true,
217 		exclusiveMinimum=true,
218 		externalDocs=@ExternalDocs,
219 		f="m",
220 		format="n",
221 		ignore=true,
222 		items=@Items,
223 		max="o",
224 		maxi=1,
225 		maximum="p",
226 		maxItems=2,
227 		maxl=3,
228 		maxLength=4,
229 		maxp=5,
230 		maxProperties=6,
231 		min="q",
232 		mini=7,
233 		minimum="r",
234 		minItems=8,
235 		minl=9,
236 		minLength=10,
237 		minp=11,
238 		minProperties=12,
239 		mo="s",
240 		multipleOf="t",
241 		on="u",
242 		onClass=X1.class,
243 		p="v",
244 		pattern="w",
245 		properties="x",
246 		r=true,
247 		readOnly=true,
248 		required=true,
249 		ro=true,
250 		t="z",
251 		title="aa",
252 		type="bb",
253 		ui=true,
254 		uniqueItems=true,
255 		xml="cc"
256 	)
257 	public static class D1 {}
258 	Schema d1 = D1.class.getAnnotationsByType(Schema.class)[0];
259 
260 	@Schema(
261 		_default="a",
262 		_enum="b",
263 		$ref="c",
264 		additionalProperties="d",
265 		allOf="e",
266 		cf="f",
267 		collectionFormat="g",
268 		d="h",
269 		description="i",
270 		df="j",
271 		discriminator="k",
272 		e="l",
273 		emax=true,
274 		emin=true,
275 		exclusiveMaximum=true,
276 		exclusiveMinimum=true,
277 		externalDocs=@ExternalDocs,
278 		f="m",
279 		format="n",
280 		ignore=true,
281 		items=@Items,
282 		max="o",
283 		maxi=1,
284 		maximum="p",
285 		maxItems=2,
286 		maxl=3,
287 		maxLength=4,
288 		maxp=5,
289 		maxProperties=6,
290 		min="q",
291 		mini=7,
292 		minimum="r",
293 		minItems=8,
294 		minl=9,
295 		minLength=10,
296 		minp=11,
297 		minProperties=12,
298 		mo="s",
299 		multipleOf="t",
300 		on="u",
301 		onClass=X1.class,
302 		p="v",
303 		pattern="w",
304 		properties="x",
305 		r=true,
306 		readOnly=true,
307 		required=true,
308 		ro=true,
309 		t="z",
310 		title="aa",
311 		type="bb",
312 		ui=true,
313 		uniqueItems=true,
314 		xml="cc"
315 	)
316 	public static class D2 {}
317 	Schema d2 = D2.class.getAnnotationsByType(Schema.class)[0];
318 
319 	@Test void d01_comparisonWithDeclarativeAnnotations() {
320 		assertEqualsAll(a1, d1, d2);
321 		assertNotEqualsAny(a1.hashCode(), 0, -1);
322 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
323 	}
324 }