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.rest.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.cp.*;
24  import org.apache.juneau.encoders.*;
25  import org.apache.juneau.httppart.*;
26  import org.apache.juneau.parser.*;
27  import org.apache.juneau.rest.*;
28  import org.apache.juneau.rest.arg.*;
29  import org.apache.juneau.rest.converter.*;
30  import org.apache.juneau.rest.debug.*;
31  import org.apache.juneau.rest.guard.*;
32  import org.apache.juneau.rest.logger.*;
33  import org.apache.juneau.rest.processor.*;
34  import org.apache.juneau.rest.staticfile.*;
35  import org.apache.juneau.rest.swagger.*;
36  import org.apache.juneau.serializer.*;
37  import org.junit.jupiter.api.*;
38  
39  class RestAnnotation_Test extends TestBase {
40  
41  	private static final String CNAME = RestAnnotation_Test.class.getName();
42  
43  	//------------------------------------------------------------------------------------------------------------------
44  	// Basic tests
45  	//------------------------------------------------------------------------------------------------------------------
46  
47  	Rest a1 = RestAnnotation.create()
48  		.disableContentParam("a")
49  		.allowedHeaderParams("b")
50  		.allowedMethodHeaders("c")
51  		.allowedMethodParams("d")
52  		.beanStore(BeanStore.class)
53  		.callLogger(CallLogger.class)
54  		.children(RestAnnotation_Test.class)
55  		.clientVersionHeader("e")
56  		.config("f")
57  		.consumes("g")
58  		.converters(RestConverter.class)
59  		.debug("h")
60  		.debugEnablement(DebugEnablement.class)
61  		.debugOn("i")
62  		.defaultAccept("j")
63  		.defaultCharset("k")
64  		.defaultContentType("l")
65  		.defaultRequestAttributes("m")
66  		.defaultRequestHeaders("n")
67  		.defaultResponseHeaders("o")
68  		.description("p")
69  		.encoders(Encoder.class)
70  		.guards(RestGuard.class)
71  		.maxInput("q")
72  		.messages("r")
73  		.on("s")
74  		.onClass(RestAnnotation_Test.class)
75  		.parsers(Parser.class)
76  		.partParser(HttpPartParser.class)
77  		.partSerializer(HttpPartSerializer.class)
78  		.path("t")
79  		.produces("u")
80  		.renderResponseStackTraces("v")
81  		.responseProcessors(ResponseProcessor.class)
82  		.restChildrenClass(RestChildren.class)
83  		.restOpArgs(RestOpArg.class)
84  		.restOperationsClass(RestOperations.class)
85  		.roleGuard("w")
86  		.rolesDeclared("x")
87  		.serializers(Serializer.class)
88  		.siteName("y")
89  		.staticFiles(StaticFiles.class)
90  		.swagger(SwaggerAnnotation.DEFAULT)
91  		.swaggerProvider(BasicSwaggerProvider.class)
92  		.title("z")
93  		.uriAuthority("aa")
94  		.uriContext("bb")
95  		.uriRelativity("cc")
96  		.uriResolution("dd")
97  		.build();
98  
99  	Rest a2 = RestAnnotation.create()
100 		.disableContentParam("a")
101 		.allowedHeaderParams("b")
102 		.allowedMethodHeaders("c")
103 		.allowedMethodParams("d")
104 		.beanStore(BeanStore.class)
105 		.callLogger(CallLogger.class)
106 		.children(RestAnnotation_Test.class)
107 		.clientVersionHeader("e")
108 		.config("f")
109 		.consumes("g")
110 		.converters(RestConverter.class)
111 		.debug("h")
112 		.debugEnablement(DebugEnablement.class)
113 		.debugOn("i")
114 		.defaultAccept("j")
115 		.defaultCharset("k")
116 		.defaultContentType("l")
117 		.defaultRequestAttributes("m")
118 		.defaultRequestHeaders("n")
119 		.defaultResponseHeaders("o")
120 		.description("p")
121 		.encoders(Encoder.class)
122 		.guards(RestGuard.class)
123 		.maxInput("q")
124 		.messages("r")
125 		.on("s")
126 		.onClass(RestAnnotation_Test.class)
127 		.parsers(Parser.class)
128 		.partParser(HttpPartParser.class)
129 		.partSerializer(HttpPartSerializer.class)
130 		.path("t")
131 		.produces("u")
132 		.renderResponseStackTraces("v")
133 		.responseProcessors(ResponseProcessor.class)
134 		.restChildrenClass(RestChildren.class)
135 		.restOpArgs(RestOpArg.class)
136 		.restOperationsClass(RestOperations.class)
137 		.roleGuard("w")
138 		.rolesDeclared("x")
139 		.serializers(Serializer.class)
140 		.siteName("y")
141 		.staticFiles(StaticFiles.class)
142 		.swagger(SwaggerAnnotation.DEFAULT)
143 		.swaggerProvider(BasicSwaggerProvider.class)
144 		.title("z")
145 		.uriAuthority("aa")
146 		.uriContext("bb")
147 		.uriRelativity("cc")
148 		.uriResolution("dd")
149 		.build();
150 
151 	@Test void a01_basic() {
152 		assertBean(a1,
153 			"allowedHeaderParams,allowedMethodHeaders,allowedMethodParams,beanStore,callLogger,children,clientVersionHeader,config,consumes,converters,debug,debugEnablement,debugOn,defaultAccept,defaultCharset,defaultContentType,defaultRequestAttributes,defaultRequestHeaders,defaultResponseHeaders,description,disableContentParam,encoders,guards,maxInput,messages,on,onClass,parsers,partParser,partSerializer,path,produces,renderResponseStackTraces,responseProcessors,restChildrenClass,restOpArgs,restOperationsClass,roleGuard,rolesDeclared,serializers,siteName,staticFiles,swagger{contact{description,email,name,url},description,externalDocs{description,url},license{description,name,url},tags,termsOfService,title,value,version},swaggerProvider,title,uriAuthority,uriContext,uriRelativity,uriResolution",
154 			"b,c,d,BeanStore,CallLogger,[RestAnnotation_Test],e,f,[g],[RestConverter],h,DebugEnablement,i,j,k,l,[m],[n],[o],[p],a,[Encoder],[RestGuard],q,r,[s],[RestAnnotation_Test],[Parser],HttpPartParser,HttpPartSerializer,t,[u],v,[ResponseProcessor],RestChildren,[RestOpArg],RestOperations,w,x,[Serializer],y,StaticFiles,{{[],,,},[],{[],},{[],,},[],[],[],[],},BasicSwaggerProvider,[z],aa,bb,cc,dd");
155 	}
156 
157 	@Test void a02_testEquivalency() {
158 		assertEquals(a2, a1);
159 		assertNotEqualsAny(a1.hashCode(), 0, -1);
160 		assertEquals(a1.hashCode(), a2.hashCode());
161 	}
162 
163 	//------------------------------------------------------------------------------------------------------------------
164 	// PropertyStore equivalency.
165 	//------------------------------------------------------------------------------------------------------------------
166 
167 	@Test void b01_testEquivalencyInPropertyStores() {
168 		var bc1 = BeanContext.create().annotations(a1).build();
169 		var bc2 = BeanContext.create().annotations(a2).build();
170 		assertSame(bc1, bc2);
171 	}
172 
173 	//------------------------------------------------------------------------------------------------------------------
174 	// Other methods.
175 	//------------------------------------------------------------------------------------------------------------------
176 
177 	public static class C1 {
178 		public int f1;
179 		public void m1() {}  // NOSONAR
180 	}
181 	public static class C2 {
182 		public int f2;
183 		public void m2() {}  // NOSONAR
184 	}
185 
186 	@Test void c01_otherMethods() {
187 		var c1 = RestAnnotation.create(C1.class).on(C2.class).build();
188 		var c2 = RestAnnotation.create("a").on("b").build();
189 
190 		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
191 		assertBean(c2, "on", "[a,b]");
192 	}
193 
194 	//------------------------------------------------------------------------------------------------------------------
195 	// Comparison with declared annotations.
196 	//------------------------------------------------------------------------------------------------------------------
197 
198 	@Rest(
199 		disableContentParam="a",
200 		allowedHeaderParams="b",
201 		allowedMethodHeaders="c",
202 		allowedMethodParams="d",
203 		beanStore=BeanStore.class,
204 		callLogger=CallLogger.class,
205 		children=RestAnnotation_Test.class,
206 		clientVersionHeader="e",
207 		config="f",
208 		consumes="g",
209 		converters=RestConverter.class,
210 		debug="h",
211 		debugEnablement=DebugEnablement.class,
212 		debugOn="i",
213 		defaultAccept="j",
214 		defaultCharset="k",
215 		defaultContentType="l",
216 		defaultRequestAttributes="m",
217 		defaultRequestHeaders="n",
218 		defaultResponseHeaders="o",
219 		description="p",
220 		encoders=Encoder.class,
221 		guards=RestGuard.class,
222 		maxInput="q",
223 		messages="r",
224 		on="s",
225 		onClass=RestAnnotation_Test.class,
226 		parsers=Parser.class,
227 		partParser=HttpPartParser.class,
228 		partSerializer=HttpPartSerializer.class,
229 		path="t",
230 		produces="u",
231 		renderResponseStackTraces="v",
232 		responseProcessors=ResponseProcessor.class,
233 		restChildrenClass=RestChildren.class,
234 		restOpArgs=RestOpArg.class,
235 		restOperationsClass=RestOperations.class,
236 		roleGuard="w",
237 		rolesDeclared="x",
238 		serializers=Serializer.class,
239 		siteName="y",
240 		staticFiles=StaticFiles.class,
241 		swagger=@Swagger,
242 		swaggerProvider=BasicSwaggerProvider.class,
243 		title="z",
244 		uriAuthority="aa",
245 		uriContext="bb",
246 		uriRelativity="cc",
247 		uriResolution="dd"
248 	)
249 	public static class D1 {}
250 	Rest d1 = D1.class.getAnnotationsByType(Rest.class)[0];
251 
252 	@Rest(
253 		disableContentParam="a",
254 		allowedHeaderParams="b",
255 		allowedMethodHeaders="c",
256 		allowedMethodParams="d",
257 		beanStore=BeanStore.class,
258 		callLogger=CallLogger.class,
259 		children=RestAnnotation_Test.class,
260 		clientVersionHeader="e",
261 		config="f",
262 		consumes="g",
263 		converters=RestConverter.class,
264 		debug="h",
265 		debugEnablement=DebugEnablement.class,
266 		debugOn="i",
267 		defaultAccept="j",
268 		defaultCharset="k",
269 		defaultContentType="l",
270 		defaultRequestAttributes="m",
271 		defaultRequestHeaders="n",
272 		defaultResponseHeaders="o",
273 		description="p",
274 		encoders=Encoder.class,
275 		guards=RestGuard.class,
276 		maxInput="q",
277 		messages="r",
278 		on="s",
279 		onClass=RestAnnotation_Test.class,
280 		parsers=Parser.class,
281 		partParser=HttpPartParser.class,
282 		partSerializer=HttpPartSerializer.class,
283 		path="t",
284 		produces="u",
285 		renderResponseStackTraces="v",
286 		responseProcessors=ResponseProcessor.class,
287 		restChildrenClass=RestChildren.class,
288 		restOpArgs=RestOpArg.class,
289 		restOperationsClass=RestOperations.class,
290 		roleGuard="w",
291 		rolesDeclared="x",
292 		serializers=Serializer.class,
293 		siteName="y",
294 		staticFiles=StaticFiles.class,
295 		swagger=@Swagger,
296 		swaggerProvider=BasicSwaggerProvider.class,
297 		title="z",
298 		uriAuthority="aa",
299 		uriContext="bb",
300 		uriRelativity="cc",
301 		uriResolution="dd"
302 	)
303 	public static class D2 {}
304 	Rest d2 = D2.class.getAnnotationsByType(Rest.class)[0];
305 
306 	@Test void d01_comparisonWithDeclarativeAnnotations() {
307 		assertEqualsAll(a1, d1, d2);
308 		assertNotEqualsAny(a1.hashCode(), 0, -1);
309 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
310 	}
311 }