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