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