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 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
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
43 return transform(attrs);
44 }
45 @RestOp(defaultRequestAttributes={"p4:v4a","p5:v5"})
46 public JsonMap a2(RequestAttributes attrs, @HasQuery("override") boolean override) {
47
48
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
62 return transform(attrs);
63 }
64 @RestGet(defaultRequestAttributes={"p4:v4a","p5:v5"})
65 public JsonMap b2(RequestAttributes attrs, @HasQuery("override") boolean override) {
66
67
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
81 return transform(attrs);
82 }
83 @RestPut(defaultRequestAttributes={"p4:v4a","p5:v5"})
84 public JsonMap c2(RequestAttributes attrs, @HasQuery("override") boolean override) {
85
86
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
100 return transform(attrs);
101 }
102 @RestPost(defaultRequestAttributes={"p4:v4a","p5:v5"})
103 public JsonMap d2(RequestAttributes attrs, @HasQuery("override") boolean override) {
104
105
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
119 return transform(attrs);
120 }
121 @RestDelete(defaultRequestAttributes={"p4:v4a","p5:v5"})
122 public JsonMap e2(RequestAttributes attrs, @HasQuery("override") boolean override) {
123
124
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 }