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