1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
45 return transform(attrs);
46 }
47 @RestOp(defaultRequestAttributes={"p4:v4a","p5:v5"})
48 public JsonMap a2(RequestAttributes attrs, @HasQuery("override") boolean override) {
49
50
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
64 return transform(attrs);
65 }
66 @RestGet(defaultRequestAttributes={"p4:v4a","p5:v5"})
67 public JsonMap b2(RequestAttributes attrs, @HasQuery("override") boolean override) {
68
69
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
83 return transform(attrs);
84 }
85 @RestPut(defaultRequestAttributes={"p4:v4a","p5:v5"})
86 public JsonMap c2(RequestAttributes attrs, @HasQuery("override") boolean override) {
87
88
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
102 return transform(attrs);
103 }
104 @RestPost(defaultRequestAttributes={"p4:v4a","p5:v5"})
105 public JsonMap d2(RequestAttributes attrs, @HasQuery("override") boolean override) {
106
107
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
121 return transform(attrs);
122 }
123 @RestDelete(defaultRequestAttributes={"p4:v4a","p5:v5"})
124 public JsonMap e2(RequestAttributes attrs, @HasQuery("override") boolean override) {
125
126
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 }