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