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.http.header.*;
20  import org.apache.juneau.rest.*;
21  import org.apache.juneau.rest.mock.*;
22  import org.junit.jupiter.api.*;
23  
24  import jakarta.servlet.http.*;
25  
26  class RestPostCall_Test {
27  
28  	//------------------------------------------------------------------------------------------------------------------
29  	// @RestPostCall
30  	//------------------------------------------------------------------------------------------------------------------
31  
32  	@Rest
33  	public static class A extends A_Parent {
34  		private boolean post3Called;
35  		@RestPostCall
36  		public void post3() {
37  			post3Called = true;
38  		}
39  		@RestPostCall
40  		public void post4(HttpServletRequest req, HttpServletResponse res) {
41  			res.setHeader("post3-called", ""+post3Called);
42  			post3Called = false;
43  			if (res.getHeader("post4-called") != null)
44  				throw new IllegalArgumentException("post4 called multiple times.");
45  			res.setHeader("post4-called", "true");
46  		}
47  		@RestGet(path="/")
48  		public String a() {
49  			return "OK";
50  		}
51  	}
52  
53  	public static class A_Parent {
54  		private boolean post1Called;
55  		@RestPostCall
56  		public void post1() {
57  			post1Called = true;
58  		}
59  		@RestPostCall
60  		public void post2(Accept accept, RestRequest req, RestResponse res) {
61  			res.setHeader("post1-called", ""+post1Called);
62  			post1Called = false;
63  			if (res.getHeader("post2-called") != null)
64  				throw new IllegalArgumentException("post2 called multiple times.");
65  			res.setHeader("post2-called", "true");
66  		}
67  	}
68  
69  	@Test void a01_postCall() throws Exception {
70  		var a = MockRestClient.build(A.class);
71  		a.get("/").run()
72  			.assertHeader("post1-called").is("true")
73  			.assertHeader("post2-called").is("true")
74  			.assertHeader("post3-called").is("true")
75  			.assertHeader("post4-called").is("true");
76  	}
77  }