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