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 java.util.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.collections.*;
23  import org.apache.juneau.http.annotation.*;
24  import org.apache.juneau.rest.httppart.*;
25  import org.apache.juneau.rest.mock.*;
26  import org.junit.jupiter.api.*;
27  
28  class Restx_ReqAttrs_Test extends TestBase {
29  
30  	//------------------------------------------------------------------------------------------------------------------
31  	// Test properties inheritance.
32  	//------------------------------------------------------------------------------------------------------------------
33  
34  	@Rest(defaultRequestAttributes={"p1:v1","p2:v2"})
35  	public static class A {}
36  
37  	@Rest(defaultRequestAttributes={"p2:v2a","p3:v3","p4:v4"})
38  	public static class A1 extends A {}
39  
40  	@Rest
41  	public static class A2 extends A1 {
42  		@RestOp
43  		public JsonMap a1(RequestAttributes attrs) {
44  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} - NOSONAR
45  			return transform(attrs);
46  		}
47  		@RestOp(defaultRequestAttributes={"p4:v4a","p5:v5"})
48  		public JsonMap a2(RequestAttributes attrs, @HasQuery("override") boolean override) {
49  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false.
50  			// Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true.
51  			if (override) {
52  				attrs.set("p1", "x");
53  				attrs.set("p2", "x");
54  				attrs.set("p3", "x");
55  				attrs.set("p4", "x");
56  				attrs.set("p5", "x");
57  			}
58  			return transform(attrs);
59  		}
60  
61  		@RestGet
62  		public JsonMap b1(RequestAttributes attrs) {
63  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} - NOSONAR
64  			return transform(attrs);
65  		}
66  		@RestGet(defaultRequestAttributes={"p4:v4a","p5:v5"})
67  		public JsonMap b2(RequestAttributes attrs, @HasQuery("override") boolean override) {
68  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false.
69  			// Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true.
70  			if (override) {
71  				attrs.set("p1", "x");
72  				attrs.set("p2", "x");
73  				attrs.set("p3", "x");
74  				attrs.set("p4", "x");
75  				attrs.set("p5", "x");
76  			}
77  			return transform(attrs);
78  		}
79  
80  		@RestPut
81  		public JsonMap c1(RequestAttributes attrs) {
82  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} - NOSONAR
83  			return transform(attrs);
84  		}
85  		@RestPut(defaultRequestAttributes={"p4:v4a","p5:v5"})
86  		public JsonMap c2(RequestAttributes attrs, @HasQuery("override") boolean override) {
87  			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false.
88  			// Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true.
89  			if (override) {
90  				attrs.set("p1", "x");
91  				attrs.set("p2", "x");
92  				attrs.set("p3", "x");
93  				attrs.set("p4", "x");
94  				attrs.set("p5", "x");
95  			}
96  			return transform(attrs);
97  		}
98  
99  		@RestPost
100 		public JsonMap d1(RequestAttributes attrs) {
101 			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} - NOSONAR
102 			return transform(attrs);
103 		}
104 		@RestPost(defaultRequestAttributes={"p4:v4a","p5:v5"})
105 		public JsonMap d2(RequestAttributes attrs, @HasQuery("override") boolean override) {
106 			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false.
107 			// Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true.
108 			if (override) {
109 				attrs.set("p1", "x");
110 				attrs.set("p2", "x");
111 				attrs.set("p3", "x");
112 				attrs.set("p4", "x");
113 				attrs.set("p5", "x");
114 			}
115 			return transform(attrs);
116 		}
117 
118 		@RestDelete
119 		public JsonMap e1(RequestAttributes attrs) {
120 			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4'} - NOSONAR
121 			return transform(attrs);
122 		}
123 		@RestDelete(defaultRequestAttributes={"p4:v4a","p5:v5"})
124 		public JsonMap e2(RequestAttributes attrs, @HasQuery("override") boolean override) {
125 			// Should show {p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'} when override is false.
126 			// Should show {p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'} when override is true.
127 			if (override) {
128 				attrs.set("p1", "x");
129 				attrs.set("p2", "x");
130 				attrs.set("p3", "x");
131 				attrs.set("p4", "x");
132 				attrs.set("p5", "x");
133 			}
134 			return transform(attrs);
135 		}
136 
137 		private JsonMap transform(RequestAttributes attrs) {
138 			var m = new JsonMap();
139 			for (Map.Entry<String,Object> e : attrs.asMap().entrySet()) {
140 				if (e.getKey().startsWith("p"))
141 					m.put(e.getKey(), e.getValue());
142 			}
143 			return m;
144 		}
145 	}
146 
147 	@Test void a01_basic() throws Exception {
148 		var a = MockRestClient.build(A2.class);
149 
150 		a.get("/a1").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4'}");
151 		a.get("/a2").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'}");
152 		a.get("/a2?override").run().assertContent("{p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'}");
153 
154 		a.get("/b1").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4'}");
155 		a.get("/b2").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'}");
156 		a.get("/b2?override").run().assertContent("{p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'}");
157 
158 		a.put("/c1").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4'}");
159 		a.put("/c2").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'}");
160 		a.put("/c2?override").run().assertContent("{p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'}");
161 
162 		a.post("/d1").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4'}");
163 		a.post("/d2").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'}");
164 		a.post("/d2?override").run().assertContent("{p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'}");
165 
166 		a.delete("/e1").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4'}");
167 		a.delete("/e2").run().assertContent("{p1:'v1',p2:'v2a',p3:'v3',p4:'v4a',p5:'v5'}");
168 		a.delete("/e2?override").run().assertContent("{p1:'x',p2:'x',p3:'x',p4:'x',p5:'x'}");
169 	}
170 }