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 org.apache.juneau.*;
20  import org.apache.juneau.collections.*;
21  import org.apache.juneau.http.header.*;
22  import org.apache.juneau.rest.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.junit.jupiter.api.*;
25  
26  import jakarta.servlet.http.*;
27  
28  class RestPreCall_Test extends TestBase {
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// @RestPreCall
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	@Rest
35  	public static class A extends A_Parent {
36  		private boolean pre3Called;
37  		@RestPreCall
38  		public void pre3() {
39  			pre3Called = true;
40  		}
41  		@RestPreCall
42  		public void pre4(HttpServletRequest req, HttpServletResponse res) {
43  			res.setHeader("pre3-called", ""+pre3Called);
44  			pre3Called = false;
45  			if (res.getHeader("pre4-called") != null)
46  				throw new IllegalArgumentException("pre4 called multiple times.");
47  			res.setHeader("pre4-called", "true");
48  		}
49  		@RestGet(path="/")
50  		public JsonMap a(RestRequest req, RestResponse res) {
51  			return JsonMap.create()
52  				.append("1", res.getHeader("pre1-called"))
53  				.append("2", res.getHeader("pre2-called"))
54  				.append("3", res.getHeader("pre3-called"))
55  				.append("4", res.getHeader("pre4-called"));
56  		}
57  	}
58  
59  	public static class A_Parent {
60  		private boolean pre1Called;
61  		@RestPreCall
62  		public void pre1() {
63  			pre1Called = true;
64  		}
65  		@RestPreCall
66  		public void pre2(Accept accept, RestRequest req, RestResponse res) {
67  			res.setHeader("pre1-called", ""+pre1Called);
68  			pre1Called = false;
69  			if (res.getHeader("pre2-called") != null)
70  				throw new IllegalArgumentException("pre2 called multiple times.");
71  			res.setHeader("pre2-called", "true");
72  		}
73  	}
74  
75  	@Test void a01_preCall() throws Exception {
76  		var a = MockRestClient.build(A.class);
77  		a.get("/").run().assertContent("{'1':'true','2':'true','3':'true','4':'true'}");
78  	}
79  }