1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.rest;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.http.header.ContentType.*;
21
22 import java.io.*;
23
24 import org.apache.juneau.*;
25 import org.apache.juneau.http.annotation.*;
26 import org.apache.juneau.json.*;
27 import org.apache.juneau.rest.annotation.*;
28 import org.apache.juneau.rest.matcher.*;
29 import org.apache.juneau.rest.mock.*;
30 import org.junit.jupiter.api.*;
31
32 import jakarta.servlet.http.*;
33
34 class Rest_PredefinedStatusCodes_Test extends TestBase {
35
36
37
38
39 @Rest
40 public static class A {
41 @RestPut
42 public Reader a(@Content String b) {
43 return reader(b);
44 }
45 }
46
47 @Test void a01_OK() throws Exception {
48 var a = MockRestClient.build(A.class);
49 a.put("/a", "foo")
50 .run()
51 .assertStatus(200);
52 }
53
54
55
56
57
58 @Rest(parsers=JsonParser.class)
59 public static class B {
60 @RestPut
61 public String a(@Content B1 in) {
62 return "OK";
63 }
64 public static class B1 {
65 public String f1;
66 }
67 @RestPut
68 public String b(@Content B2 in) {
69 return "OK";
70 }
71 public static class B2 {
72 public int f1;
73 }
74 @RestPut
75 public String c(@Content B3 in) {
76 return "OK";
77 }
78 public static class B3 {
79 public int f1;
80 private B3(){}
81 }
82 @RestPut
83 public String d(@Content B4 in) {
84 return "OK";
85 }
86 public class B4 {
87 public B4(){ }
88 }
89 @RestPut
90 public String e(@Content B5 in) {
91 return "OK";
92 }
93 static class B5 {
94 public B5(){ }
95 }
96 @RestPut
97 public String f(@Content B6 in) {
98 return "OK";
99 }
100 public static class B6 {
101 public int f1;
102 private B6(){}
103 public static B6 valueOf(String s) {
104 throw new RuntimeException("Test error");
105 }
106 }
107 @RestPut(path="/g/{a1}")
108 public String g(@Query("p1") int t1, @Path("a1") int a1, @Header("h1") int h1) {
109 return "OK";
110 }
111 }
112
113 @Test void b01_badRequest() throws Exception {
114 var b = MockRestClient.buildLax(B.class);
115 b.put("/a?noTrace=true", "{f2:'foo'}", APPLICATION_JSON)
116 .run()
117 .assertStatus(400)
118 .assertContent().isContains(
119 "Unknown property 'f2' encountered while trying to parse into class"
120 );
121 b.put("/a?noTrace=true", "{f1:'foo', f2:'foo'}", APPLICATION_JSON)
122 .run()
123 .assertStatus(400)
124 .assertContent().isContains(
125 "Unknown property 'f2' encountered while trying to parse into class"
126 );
127 b.put("/b?noTrace=true", "{f1:'foo'}", APPLICATION_JSON)
128 .run()
129 .assertStatus(400)
130 .assertContent().isContains(
131 "NumberFormatException"
132 );
133 b.put("/c?noTrace=true", "{f1:1}", APPLICATION_JSON)
134 .run()
135 .assertStatus(400)
136 .assertContent().isContains(
137 "could not be instantiated"
138 );
139 b.put("/d?noTrace=true", "{f1:1}", APPLICATION_JSON)
140 .run()
141 .assertStatus(400)
142 .assertContent().isContains(
143 "could not be instantiated"
144 );
145 b.put("/e?noTrace=true", "{f1:1}", APPLICATION_JSON)
146 .run()
147 .assertStatus(400)
148 .assertContent().isContains(
149 "Class is not public"
150 );
151 b.put("/f?noTrace=true", "'foo'", APPLICATION_JSON)
152 .run()
153 .assertStatus(400)
154 .assertContent().isContains(
155 "Test error"
156 );
157 b.put("/g/123?noTrace=true&p1=foo", "'foo'", APPLICATION_JSON)
158 .run()
159 .assertStatus(400)
160 .assertContent().isContains(
161 "Could not parse query parameter 'p1'."
162 );
163 b.put("/g/foo?noTrace=true&p1=1", "'foo'", APPLICATION_JSON)
164 .run()
165 .assertStatus(400)
166 .assertContent().isContains(
167 "Could not parse path parameter 'a1'."
168 );
169 b.put("/g/123?noTrace=true&p1=1", "'foo'", APPLICATION_JSON)
170 .header("h1", "foo")
171 .run()
172 .assertStatus(400)
173 .assertContent().isContains(
174 "Could not parse header parameter 'h1'."
175 );
176 }
177
178
179
180
181
182 @Rest
183 public static class C {
184 @RestGet(path="/")
185 public String a() {
186 return "OK";
187 }
188 }
189 private static MockRestClient c = MockRestClient.buildLax(C.class);
190
191 @Test void c01_badPath() throws Exception {
192 c.get("/bad?noTrace=true")
193 .run()
194 .assertStatus(404)
195 .assertContent().isContains(
196 "Method 'GET' not found on resource with matching pattern on path '/bad'"
197 );
198 }
199 public void c02_badMethod() throws Exception {
200 c.put("?noTrace=true", null)
201 .run()
202 .assertStatus(405)
203 .assertContent().isContains(
204 "Method 'PUT' not found on resource."
205 );
206 }
207
208
209
210
211
212 @Rest
213 public static class D {
214 @RestGet(matchers=NeverMatcher.class)
215 public String d() {
216 return "OK";
217 }
218 public static class NeverMatcher extends RestMatcher {
219 @Override
220 public boolean matches(HttpServletRequest req) {
221 return false;
222 }
223 }
224 }
225 private static MockRestClient d = MockRestClient.buildLax(D.class);
226
227 @Test void d01() throws Exception {
228 d.get("/d?noTrace=true")
229 .run()
230 .assertStatus(412)
231 .assertContent().isContains(
232 "Method 'GET' not found on resource on path '/d' with matching matcher."
233 );
234 }
235 }