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.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.cp.*;
25  import org.apache.juneau.encoders.*;
26  import org.apache.juneau.httppart.*;
27  import org.apache.juneau.parser.*;
28  import org.apache.juneau.rest.*;
29  import org.apache.juneau.rest.arg.*;
30  import org.apache.juneau.rest.converter.*;
31  import org.apache.juneau.rest.debug.*;
32  import org.apache.juneau.rest.guard.*;
33  import org.apache.juneau.rest.logger.*;
34  import org.apache.juneau.rest.processor.*;
35  import org.apache.juneau.rest.staticfile.*;
36  import org.apache.juneau.rest.swagger.*;
37  import org.apache.juneau.serializer.*;
38  import org.junit.jupiter.api.*;
39  
40  class RestAnnotation_Test extends TestBase {
41  
42  	private static final String CNAME = RestAnnotation_Test.class.getName();
43  
44  	//------------------------------------------------------------------------------------------------------------------
45  	// Basic tests
46  	//------------------------------------------------------------------------------------------------------------------
47  
48  	Rest a1 = RestAnnotation.create()
49  		.disableContentParam("a")
50  		.allowedHeaderParams("b")
51  		.allowedMethodHeaders("c")
52  		.allowedMethodParams("d")
53  		.beanStore(BeanStore.class)
54  		.callLogger(CallLogger.class)
55  		.children(RestAnnotation_Test.class)
56  		.clientVersionHeader("e")
57  		.config("f")
58  		.consumes("g")
59  		.converters(RestConverter.class)
60  		.debug("h")
61  		.debugEnablement(DebugEnablement.class)
62  		.debugOn("i")
63  		.defaultAccept("j")
64  		.defaultCharset("k")
65  		.defaultContentType("l")
66  		.defaultRequestAttributes("m")
67  		.defaultRequestHeaders("n")
68  		.defaultResponseHeaders("o")
69  		.description("p")
70  		.encoders(Encoder.class)
71  		.guards(RestGuard.class)
72  		.maxInput("q")
73  		.messages("r")
74  		.on("s")
75  		.onClass(RestAnnotation_Test.class)
76  		.parsers(Parser.class)
77  		.partParser(HttpPartParser.class)
78  		.partSerializer(HttpPartSerializer.class)
79  		.path("t")
80  		.produces("u")
81  		.renderResponseStackTraces("v")
82  		.responseProcessors(ResponseProcessor.class)
83  		.restChildrenClass(RestChildren.class)
84  		.restOpArgs(RestOpArg.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 		.roleGuard("w")
137 		.rolesDeclared("x")
138 		.serializers(Serializer.class)
139 		.siteName("y")
140 		.staticFiles(StaticFiles.class)
141 		.swagger(SwaggerAnnotation.DEFAULT)
142 		.swaggerProvider(BasicSwaggerProvider.class)
143 		.title("z")
144 		.uriAuthority("aa")
145 		.uriContext("bb")
146 		.uriRelativity("cc")
147 		.uriResolution("dd")
148 		.build();
149 
150 	@Test void a01_basic() {
151 		assertBean(a1,
152 			"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,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",
153 			"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],w,x,[Serializer],y,StaticFiles,{{[],,,},[],{[],},{[],,},[],[],[],[],},BasicSwaggerProvider,[z],aa,bb,cc,dd");
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() {
186 		var c1 = RestAnnotation.create(C1.class).on(C2.class).build();
187 		var c2 = RestAnnotation.create("a").on("b").build();
188 
189 		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
190 		assertBean(c2, "on", "[a,b]");
191 	}
192 
193 	//------------------------------------------------------------------------------------------------------------------
194 	// Comparison with declared annotations.
195 	//------------------------------------------------------------------------------------------------------------------
196 
197 	@Rest(
198 		disableContentParam="a",
199 		allowedHeaderParams="b",
200 		allowedMethodHeaders="c",
201 		allowedMethodParams="d",
202 		beanStore=BeanStore.class,
203 		callLogger=CallLogger.class,
204 		children=RestAnnotation_Test.class,
205 		clientVersionHeader="e",
206 		config="f",
207 		consumes="g",
208 		converters=RestConverter.class,
209 		debug="h",
210 		debugEnablement=DebugEnablement.class,
211 		debugOn="i",
212 		defaultAccept="j",
213 		defaultCharset="k",
214 		defaultContentType="l",
215 		defaultRequestAttributes="m",
216 		defaultRequestHeaders="n",
217 		defaultResponseHeaders="o",
218 		description="p",
219 		encoders=Encoder.class,
220 		guards=RestGuard.class,
221 		maxInput="q",
222 		messages="r",
223 		on="s",
224 		onClass=RestAnnotation_Test.class,
225 		parsers=Parser.class,
226 		partParser=HttpPartParser.class,
227 		partSerializer=HttpPartSerializer.class,
228 		path="t",
229 		produces="u",
230 		renderResponseStackTraces="v",
231 		responseProcessors=ResponseProcessor.class,
232 		restChildrenClass=RestChildren.class,
233 		restOpArgs=RestOpArg.class,
234 		roleGuard="w",
235 		rolesDeclared="x",
236 		serializers=Serializer.class,
237 		siteName="y",
238 		staticFiles=StaticFiles.class,
239 		swagger=@Swagger,
240 		swaggerProvider=BasicSwaggerProvider.class,
241 		title="z",
242 		uriAuthority="aa",
243 		uriContext="bb",
244 		uriRelativity="cc",
245 		uriResolution="dd"
246 	)
247 	public static class D1 {}
248 	Rest d1 = D1.class.getAnnotationsByType(Rest.class)[0];
249 
250 	@Rest(
251 		disableContentParam="a",
252 		allowedHeaderParams="b",
253 		allowedMethodHeaders="c",
254 		allowedMethodParams="d",
255 		beanStore=BeanStore.class,
256 		callLogger=CallLogger.class,
257 		children=RestAnnotation_Test.class,
258 		clientVersionHeader="e",
259 		config="f",
260 		consumes="g",
261 		converters=RestConverter.class,
262 		debug="h",
263 		debugEnablement=DebugEnablement.class,
264 		debugOn="i",
265 		defaultAccept="j",
266 		defaultCharset="k",
267 		defaultContentType="l",
268 		defaultRequestAttributes="m",
269 		defaultRequestHeaders="n",
270 		defaultResponseHeaders="o",
271 		description="p",
272 		encoders=Encoder.class,
273 		guards=RestGuard.class,
274 		maxInput="q",
275 		messages="r",
276 		on="s",
277 		onClass=RestAnnotation_Test.class,
278 		parsers=Parser.class,
279 		partParser=HttpPartParser.class,
280 		partSerializer=HttpPartSerializer.class,
281 		path="t",
282 		produces="u",
283 		renderResponseStackTraces="v",
284 		responseProcessors=ResponseProcessor.class,
285 		restChildrenClass=RestChildren.class,
286 		restOpArgs=RestOpArg.class,
287 		roleGuard="w",
288 		rolesDeclared="x",
289 		serializers=Serializer.class,
290 		siteName="y",
291 		staticFiles=StaticFiles.class,
292 		swagger=@Swagger,
293 		swaggerProvider=BasicSwaggerProvider.class,
294 		title="z",
295 		uriAuthority="aa",
296 		uriContext="bb",
297 		uriRelativity="cc",
298 		uriResolution="dd"
299 	)
300 	public static class D2 {}
301 	Rest d2 = D2.class.getAnnotationsByType(Rest.class)[0];
302 
303 	@Test void d01_comparisonWithDeclarativeAnnotations() {
304 		assertEqualsAll(a1, d1, d2);
305 		assertNotEqualsAny(a1.hashCode(), 0, -1);
306 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
307 	}
308 }