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