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.encoders.*;
24  import org.apache.juneau.parser.*;
25  import org.apache.juneau.rest.converter.*;
26  import org.apache.juneau.rest.guard.*;
27  import org.apache.juneau.rest.matcher.*;
28  import org.apache.juneau.serializer.*;
29  import org.junit.jupiter.api.*;
30  
31  class RestOpAnnotation_Test extends TestBase {
32  
33  	private static final String CNAME = RestOpAnnotation_Test.class.getName();
34  
35  	//------------------------------------------------------------------------------------------------------------------
36  	// Basic tests
37  	//------------------------------------------------------------------------------------------------------------------
38  
39  	RestOp a1 = RestOpAnnotation.create()
40  		.clientVersion("a")
41  		.consumes("b")
42  		.converters(RestConverter.class)
43  		.debug("c")
44  		.defaultAccept("d")
45  		.defaultCharset("e")
46  		.defaultContentType("f")
47  		.defaultRequestFormData("g")
48  		.defaultRequestQueryData("h")
49  		.defaultRequestAttributes("i")
50  		.defaultRequestHeaders("j")
51  		.defaultResponseHeaders("k")
52  		.description("l")
53  		.encoders(Encoder.class)
54  		.guards(RestGuard.class)
55  		.matchers(RestMatcher.class)
56  		.maxInput("m")
57  		.method("n")
58  		.on("o")
59  		.parsers(Parser.class)
60  		.path("p")
61  		.produces("q")
62  		.roleGuard("r")
63  		.rolesDeclared("s")
64  		.serializers(Serializer.class)
65  		.summary("t")
66  		.swagger(OpSwaggerAnnotation.DEFAULT)
67  		.value("u")
68  		.build();
69  
70  	RestOp a2 = RestOpAnnotation.create()
71  		.clientVersion("a")
72  		.consumes("b")
73  		.converters(RestConverter.class)
74  		.debug("c")
75  		.defaultAccept("d")
76  		.defaultCharset("e")
77  		.defaultContentType("f")
78  		.defaultRequestFormData("g")
79  		.defaultRequestQueryData("h")
80  		.defaultRequestAttributes("i")
81  		.defaultRequestHeaders("j")
82  		.defaultResponseHeaders("k")
83  		.description("l")
84  		.encoders(Encoder.class)
85  		.guards(RestGuard.class)
86  		.matchers(RestMatcher.class)
87  		.maxInput("m")
88  		.method("n")
89  		.on("o")
90  		.parsers(Parser.class)
91  		.path("p")
92  		.produces("q")
93  		.roleGuard("r")
94  		.rolesDeclared("s")
95  		.serializers(Serializer.class)
96  		.summary("t")
97  		.swagger(OpSwaggerAnnotation.DEFAULT)
98  		.value("u")
99  		.build();
100 
101 	@Test void a01_basic() {
102 		assertBean(a1,
103 			"clientVersion,consumes,converters,debug,defaultAccept,defaultCharset,defaultContentType,defaultRequestAttributes,defaultRequestFormData,defaultRequestHeaders,defaultRequestQueryData,defaultResponseHeaders,description,encoders,guards,matchers,maxInput,method,on,parsers,path,produces,roleGuard,rolesDeclared,serializers,summary,swagger{consumes,deprecated,description,externalDocs{description,url},operationId,parameters,produces,responses,schemes,summary,tags,value},value",
104 			"a,[b],[RestConverter],c,d,e,f,[i],[g],[j],[h],[k],[l],[Encoder],[RestGuard],[RestMatcher],m,n,[o],[Parser],[p],[q],r,s,[Serializer],t,{[],,[],{[],},,[],[],[],[],[],[],[]},u");
105 	}
106 
107 	@Test void a02_testEquivalency() {
108 		assertEquals(a2, a1);
109 		assertNotEqualsAny(a1.hashCode(), 0, -1);
110 		assertEquals(a1.hashCode(), a2.hashCode());
111 	}
112 
113 	//------------------------------------------------------------------------------------------------------------------
114 	// PropertyStore equivalency.
115 	//------------------------------------------------------------------------------------------------------------------
116 
117 	@Test void b01_testEquivalencyInPropertyStores() {
118 		var bc1 = BeanContext.create().annotations(a1).build();
119 		var bc2 = BeanContext.create().annotations(a2).build();
120 		assertSame(bc1, bc2);
121 	}
122 
123 	//------------------------------------------------------------------------------------------------------------------
124 	// Other methods.
125 	//------------------------------------------------------------------------------------------------------------------
126 
127 	public static class C1 {
128 		public int f1;
129 		public void m1() {}  // NOSONAR
130 	}
131 	public static class C2 {
132 		public int f2;
133 		public void m2() {}  // NOSONAR
134 	}
135 
136 	@Test void c01_otherMethods() throws Exception {
137 		var c4 = RestOpAnnotation.create().on(C1.class.getMethod("m1")).on(C2.class.getMethod("m2")).build();
138 
139 		assertBean(c4, "on", "["+CNAME+"$C1.m1(),"+CNAME+"$C2.m2()]");
140 	}
141 
142 	//------------------------------------------------------------------------------------------------------------------
143 	// Comparison with declared annotations.
144 	//------------------------------------------------------------------------------------------------------------------
145 
146 	public interface D1 {
147 
148 		@RestOp(
149 			clientVersion="a",
150 			consumes="b",
151 			converters=RestConverter.class,
152 			debug="c",
153 			defaultAccept="d",
154 			defaultCharset="e",
155 			defaultContentType="f",
156 			defaultRequestFormData="g",
157 			defaultRequestQueryData="h",
158 			defaultRequestAttributes="i",
159 			defaultRequestHeaders="j",
160 			defaultResponseHeaders="k",
161 			description="l",
162 			encoders=Encoder.class,
163 			guards=RestGuard.class,
164 			matchers=RestMatcher.class,
165 			maxInput="m",
166 			method="n",
167 			on="o",
168 			parsers=Parser.class,
169 			path="p",
170 			produces="q",
171 			roleGuard="r",
172 			rolesDeclared="s",
173 			serializers=Serializer.class,
174 			summary="t",
175 			swagger=@OpSwagger,
176 			value="u"
177 		)
178 		void m1();
179 
180 		@RestOp(
181 			clientVersion="a",
182 			consumes="b",
183 			converters=RestConverter.class,
184 			debug="c",
185 			defaultAccept="d",
186 			defaultCharset="e",
187 			defaultContentType="f",
188 			defaultRequestFormData="g",
189 			defaultRequestQueryData="h",
190 			defaultRequestAttributes="i",
191 			defaultRequestHeaders="j",
192 			defaultResponseHeaders="k",
193 			description="l",
194 			encoders=Encoder.class,
195 			guards=RestGuard.class,
196 			matchers=RestMatcher.class,
197 			maxInput="m",
198 			method="n",
199 			on="o",
200 			parsers=Parser.class,
201 			path="p",
202 			produces="q",
203 			roleGuard="r",
204 			rolesDeclared="s",
205 			serializers=Serializer.class,
206 			summary="t",
207 			swagger=@OpSwagger,
208 			value="u"
209 		)
210 		void m2();
211 	}
212 
213 	RestOp d1, d2;
214 	{
215 		try {
216 			d1 = D1.class.getMethod("m1").getAnnotationsByType(RestOp.class)[0];
217 			d2 = D1.class.getMethod("m2").getAnnotationsByType(RestOp.class)[0];
218 
219 		} catch (Exception e) {
220 			throw new RuntimeException(e);
221 		}
222 	}
223 
224 	@Test void d01_comparisonWithDeclarativeAnnotations() {
225 		assertEqualsAll(a1, d1, d2);
226 		assertNotEqualsAny(a1.hashCode(), 0, -1);
227 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
228 	}
229 }