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