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