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.junit.jupiter.api.Assertions.*;
21
22 import org.apache.juneau.*;
23 import org.apache.juneau.http.response.*;
24 import org.apache.juneau.json.*;
25 import org.apache.juneau.parser.*;
26 import org.apache.juneau.rest.annotation.*;
27 import org.apache.juneau.rest.beans.*;
28 import org.apache.juneau.rest.mock.*;
29 import org.junit.jupiter.api.*;
30
31 class RestOp_Throws_Test extends TestBase {
32
33
34
35
36 @Rest
37 public static class A {
38 @RestGet public void badRequest() { throw new BadRequest(); }
39 @RestGet public void conflict() { throw new Conflict(); }
40 @RestGet public void expectationFailed() { throw new ExpectationFailed(); }
41 @RestGet public void failedDependency() { throw new FailedDependency(); }
42 @RestGet public void forbidden() { throw new Forbidden(); }
43 @RestGet public void gone() { throw new Gone(); }
44 @RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported(); }
45 @RestGet public void insufficientStorage() { throw new InsufficientStorage(); }
46 @RestGet public void internalServerError() { throw new InternalServerError(); }
47 @RestGet public void lengthRequired() { throw new LengthRequired(); }
48 @RestGet public void locked() { throw new Locked(); }
49 @RestGet public void loopDetected() { throw new LoopDetected(); }
50 @RestGet public void methodNotAllowed() { throw new MethodNotAllowed(); }
51 @RestGet public void misdirectedRequest() { throw new MisdirectedRequest(); }
52 @RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired(); }
53 @RestGet public void notAcceptable() { throw new NotAcceptable(); }
54 @RestGet public void notExtended() { throw new NotExtended(); }
55 @RestGet public void notFound() { throw new NotFound(); }
56 @RestGet public void notImplemented() { throw new NotImplemented(); }
57 @RestGet public void payloadTooLarge() { throw new PayloadTooLarge(); }
58 @RestGet public void preconditionFailed() { throw new PreconditionFailed(); }
59 @RestGet public void preconditionRequired() { throw new PreconditionRequired(); }
60 @RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable(); }
61 @RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge(); }
62 @RestGet public void serviceUnavailable() { throw new ServiceUnavailable(); }
63 @RestGet public void tooManyRequests() { throw new TooManyRequests(); }
64 @RestGet public void unauthorized() { throw new Unauthorized(); }
65 @RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons(); }
66 @RestGet public void unprocessableEntity() { throw new UnprocessableEntity(); }
67 @RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType(); }
68 @RestGet public void upgradeRequired() { throw new UpgradeRequired(); }
69 @RestGet public void uriTooLong() { throw new UriTooLong(); }
70 @RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates(); }
71 }
72
73 @Test void a01_noArgs() throws Exception {
74 var a = MockRestClient.buildLax(A.class);
75 a.get("/badRequest")
76 .run()
77 .assertStatus(400)
78 .assertContent("Bad Request");
79 a.get("/conflict")
80 .run()
81 .assertStatus(409)
82 .assertContent("Conflict");
83 a.get("/expectationFailed")
84 .run()
85 .assertStatus(417)
86 .assertContent("Expectation Failed");
87 a.get("/failedDependency")
88 .run()
89 .assertStatus(424)
90 .assertContent("Failed Dependency");
91 a.get("/forbidden")
92 .run()
93 .assertStatus(403)
94 .assertContent("Forbidden");
95 a.get("/gone")
96 .run()
97 .assertStatus(410)
98 .assertContent("Gone");
99 a.get("/httpVersionNotSupported")
100 .run()
101 .assertStatus(505)
102 .assertContent("Http Version Not Supported");
103 a.get("/insufficientStorage")
104 .run()
105 .assertStatus(507)
106 .assertContent("Insufficient Storage");
107 a.get("/internalServerError")
108 .run()
109 .assertStatus(500)
110 .assertContent("Internal Server Error");
111 a.get("/lengthRequired")
112 .run()
113 .assertStatus(411)
114 .assertContent("Length Required");
115 a.get("/locked")
116 .run()
117 .assertStatus(423)
118 .assertContent("Locked");
119 a.get("/loopDetected")
120 .run()
121 .assertStatus(508)
122 .assertContent("Loop Detected");
123 a.get("/methodNotAllowed")
124 .run()
125 .assertStatus(405)
126 .assertContent("Method Not Allowed");
127 a.get("/misdirectedRequest")
128 .run()
129 .assertStatus(421)
130 .assertContent("Misdirected Request");
131 a.get("/networkAuthenticationRequired")
132 .run()
133 .assertStatus(511)
134 .assertContent("Network Authentication Required");
135 a.get("/notAcceptable")
136 .run()
137 .assertStatus(406)
138 .assertContent("Not Acceptable");
139 a.get("/notExtended")
140 .run()
141 .assertStatus(510)
142 .assertContent("Not Extended");
143 a.get("/notFound")
144 .run()
145 .assertStatus(404)
146 .assertContent("Not Found");
147 a.get("/notImplemented")
148 .run()
149 .assertStatus(501)
150 .assertContent("Not Implemented");
151 a.get("/payloadTooLarge")
152 .run()
153 .assertStatus(413)
154 .assertContent("Payload Too Large");
155 a.get("/preconditionFailed")
156 .run()
157 .assertStatus(412)
158 .assertContent("Precondition Failed");
159 a.get("/preconditionRequired")
160 .run()
161 .assertStatus(428)
162 .assertContent("Precondition Required");
163 a.get("/rangeNotSatisfiable")
164 .run()
165 .assertStatus(416)
166 .assertContent("Range Not Satisfiable");
167 a.get("/requestHeaderFieldsTooLarge")
168 .run()
169 .assertStatus(431)
170 .assertContent("Request Header Fields Too Large");
171 a.get("/serviceUnavailable")
172 .run()
173 .assertStatus(503)
174 .assertContent("Service Unavailable");
175 a.get("/tooManyRequests")
176 .run()
177 .assertStatus(429)
178 .assertContent("Too Many Requests");
179 a.get("/unauthorized")
180 .run()
181 .assertStatus(401)
182 .assertContent("Unauthorized");
183 a.get("/unavailableForLegalReasons")
184 .run()
185 .assertStatus(451)
186 .assertContent("Unavailable For Legal Reasons");
187 a.get("/unprocessableEntity")
188 .run()
189 .assertStatus(422)
190 .assertContent("Unprocessable Entity");
191 a.get("/unsupportedMediaType")
192 .run()
193 .assertStatus(415)
194 .assertContent("Unsupported Media Type");
195 a.get("/upgradeRequired")
196 .run()
197 .assertStatus(426)
198 .assertContent("Upgrade Required");
199 a.get("/uriTooLong")
200 .run()
201 .assertStatus(414)
202 .assertContent("URI Too Long");
203 a.get("/variantAlsoNegotiates")
204 .run()
205 .assertStatus(506)
206 .assertContent("Variant Also Negotiates");
207 }
208
209
210
211
212
213 @Rest
214 public static class B {
215 @RestGet public void badRequest() { throw new BadRequest("foo {0}", "bar"); }
216 @RestGet public void conflict() { throw new Conflict("foo {0}", "bar"); }
217 @RestGet public void expectationFailed() { throw new ExpectationFailed("foo {0}", "bar"); }
218 @RestGet public void failedDependency() { throw new FailedDependency("foo {0}", "bar"); }
219 @RestGet public void forbidden() { throw new Forbidden("foo {0}", "bar"); }
220 @RestGet public void gone() { throw new Gone("foo {0}", "bar"); }
221 @RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported("foo {0}", "bar"); }
222 @RestGet public void insufficientStorage() { throw new InsufficientStorage("foo {0}", "bar"); }
223 @RestGet public void internalServerError() { throw new InternalServerError("foo {0}", "bar"); }
224 @RestGet public void lengthRequired() { throw new LengthRequired("foo {0}", "bar"); }
225 @RestGet public void locked() { throw new Locked("foo {0}", "bar"); }
226 @RestGet public void loopDetected() { throw new LoopDetected("foo {0}", "bar"); }
227 @RestGet public void methodNotAllowed() { throw new MethodNotAllowed("foo {0}", "bar"); }
228 @RestGet public void misdirectedRequest() { throw new MisdirectedRequest("foo {0}", "bar"); }
229 @RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired("foo {0}", "bar"); }
230 @RestGet public void notAcceptable() { throw new NotAcceptable("foo {0}", "bar"); }
231 @RestGet public void notExtended() { throw new NotExtended("foo {0}", "bar"); }
232 @RestGet public void notFound() { throw new NotFound("foo {0}", "bar"); }
233 @RestGet public void notImplemented() { throw new NotImplemented("foo {0}", "bar"); }
234 @RestGet public void payloadTooLarge() { throw new PayloadTooLarge("foo {0}", "bar"); }
235 @RestGet public void preconditionFailed() { throw new PreconditionFailed("foo {0}", "bar"); }
236 @RestGet public void preconditionRequired() { throw new PreconditionRequired("foo {0}", "bar"); }
237 @RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable("foo {0}", "bar"); }
238 @RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge("foo {0}", "bar"); }
239 @RestGet public void serviceUnavailable() { throw new ServiceUnavailable("foo {0}", "bar"); }
240 @RestGet public void tooManyRequests() { throw new TooManyRequests("foo {0}", "bar"); }
241 @RestGet public void unauthorized() { throw new Unauthorized("foo {0}", "bar"); }
242 @RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons("foo {0}", "bar"); }
243 @RestGet public void unprocessableEntity() { throw new UnprocessableEntity("foo {0}", "bar"); }
244 @RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType("foo {0}", "bar"); }
245 @RestGet public void upgradeRequired() { throw new UpgradeRequired("foo {0}", "bar"); }
246 @RestGet public void uriTooLong() { throw new UriTooLong("foo {0}", "bar"); }
247 @RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates("foo {0}", "bar"); }
248 }
249
250 @Test void b01_userSpecifiedMessage() throws Exception {
251 var b = MockRestClient.buildLax(B.class);
252 b.get("/badRequest")
253 .run()
254 .assertStatus(400)
255 .assertContent("foo bar");
256 b.get("/conflict")
257 .run()
258 .assertStatus(409)
259 .assertContent("foo bar");
260 b.get("/expectationFailed")
261 .run()
262 .assertStatus(417)
263 .assertContent("foo bar");
264 b.get("/failedDependency")
265 .run()
266 .assertStatus(424)
267 .assertContent("foo bar");
268 b.get("/forbidden")
269 .run()
270 .assertStatus(403)
271 .assertContent("foo bar");
272 b.get("/gone")
273 .run()
274 .assertStatus(410)
275 .assertContent("foo bar");
276 b.get("/httpVersionNotSupported")
277 .run()
278 .assertStatus(505)
279 .assertContent("foo bar");
280 b.get("/insufficientStorage")
281 .run()
282 .assertStatus(507)
283 .assertContent("foo bar");
284 b.get("/internalServerError")
285 .run()
286 .assertStatus(500)
287 .assertContent("foo bar");
288 b.get("/lengthRequired")
289 .run()
290 .assertStatus(411)
291 .assertContent("foo bar");
292 b.get("/locked")
293 .run()
294 .assertStatus(423)
295 .assertContent("foo bar");
296 b.get("/loopDetected")
297 .run()
298 .assertStatus(508)
299 .assertContent("foo bar");
300 b.get("/methodNotAllowed")
301 .run()
302 .assertStatus(405)
303 .assertContent("foo bar");
304 b.get("/misdirectedRequest")
305 .run()
306 .assertStatus(421)
307 .assertContent("foo bar");
308 b.get("/networkAuthenticationRequired")
309 .run()
310 .assertStatus(511)
311 .assertContent("foo bar");
312 b.get("/notAcceptable")
313 .run()
314 .assertStatus(406)
315 .assertContent("foo bar");
316 b.get("/notExtended")
317 .run()
318 .assertStatus(510)
319 .assertContent("foo bar");
320 b.get("/notFound")
321 .run()
322 .assertStatus(404)
323 .assertContent("foo bar");
324 b.get("/notImplemented")
325 .run()
326 .assertStatus(501)
327 .assertContent("foo bar");
328 b.get("/payloadTooLarge")
329 .run()
330 .assertStatus(413)
331 .assertContent("foo bar");
332 b.get("/preconditionFailed")
333 .run()
334 .assertStatus(412)
335 .assertContent("foo bar");
336 b.get("/preconditionRequired")
337 .run()
338 .assertStatus(428)
339 .assertContent("foo bar");
340 b.get("/rangeNotSatisfiable")
341 .run()
342 .assertStatus(416)
343 .assertContent("foo bar");
344 b.get("/requestHeaderFieldsTooLarge")
345 .run()
346 .assertStatus(431)
347 .assertContent("foo bar");
348 b.get("/serviceUnavailable")
349 .run()
350 .assertStatus(503)
351 .assertContent("foo bar");
352 b.get("/tooManyRequests")
353 .run()
354 .assertStatus(429)
355 .assertContent("foo bar");
356 b.get("/unauthorized")
357 .run()
358 .assertStatus(401)
359 .assertContent("foo bar");
360 b.get("/unavailableForLegalReasons")
361 .run()
362 .assertStatus(451)
363 .assertContent("foo bar");
364 b.get("/unprocessableEntity")
365 .run()
366 .assertStatus(422)
367 .assertContent("foo bar");
368 b.get("/unsupportedMediaType")
369 .run()
370 .assertStatus(415)
371 .assertContent("foo bar");
372 b.get("/upgradeRequired")
373 .run()
374 .assertStatus(426)
375 .assertContent("foo bar");
376 b.get("/uriTooLong")
377 .run()
378 .assertStatus(414)
379 .assertContent("foo bar");
380 b.get("/variantAlsoNegotiates")
381 .run()
382 .assertStatus(506)
383 .assertContent("foo bar");
384 }
385
386
387
388
389
390 static final Throwable t = new Throwable("foo");
391
392 @Rest
393 public static class C {
394 @RestGet public void badRequest() { throw new BadRequest(t); }
395 @RestGet public void conflict() { throw new Conflict(t); }
396 @RestGet public void expectationFailed() { throw new ExpectationFailed(t); }
397 @RestGet public void failedDependency() { throw new FailedDependency(t); }
398 @RestGet public void forbidden() { throw new Forbidden(t); }
399 @RestGet public void gone() { throw new Gone(t); }
400 @RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported(t); }
401 @RestGet public void insufficientStorage() { throw new InsufficientStorage(t); }
402 @RestGet public void internalServerError() { throw new InternalServerError(t); }
403 @RestGet public void lengthRequired() { throw new LengthRequired(t); }
404 @RestGet public void locked() { throw new Locked(t); }
405 @RestGet public void loopDetected() { throw new LoopDetected(t); }
406 @RestGet public void methodNotAllowed() { throw new MethodNotAllowed(t); }
407 @RestGet public void misdirectedRequest() { throw new MisdirectedRequest(t); }
408 @RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired(t); }
409 @RestGet public void notAcceptable() { throw new NotAcceptable(t); }
410 @RestGet public void notExtended() { throw new NotExtended(t); }
411 @RestGet public void notFound() { throw new NotFound(t); }
412 @RestGet public void notImplemented() { throw new NotImplemented(t); }
413 @RestGet public void payloadTooLarge() { throw new PayloadTooLarge(t); }
414 @RestGet public void preconditionFailed() { throw new PreconditionFailed(t); }
415 @RestGet public void preconditionRequired() { throw new PreconditionRequired(t); }
416 @RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable(t); }
417 @RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge(t); }
418 @RestGet public void serviceUnavailable() { throw new ServiceUnavailable(t); }
419 @RestGet public void tooManyRequests() { throw new TooManyRequests(t); }
420 @RestGet public void unauthorized() { throw new Unauthorized(t); }
421 @RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons(t); }
422 @RestGet public void unprocessableEntity() { throw new UnprocessableEntity(t); }
423 @RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType(t); }
424 @RestGet public void upgradeRequired() { throw new UpgradeRequired(t); }
425 @RestGet public void uriTooLong() { throw new UriTooLong(t); }
426 @RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates(t); }
427 }
428
429 @Test void c01_nestedThrowable() throws Exception {
430 var c = MockRestClient.buildLax(C.class);
431 c.get("/badRequest")
432 .run()
433 .assertStatus(400)
434 .assertContent("foo");
435 c.get("/conflict")
436 .run()
437 .assertStatus(409)
438 .assertContent("foo");
439 c.get("/expectationFailed")
440 .run()
441 .assertStatus(417)
442 .assertContent("foo");
443 c.get("/failedDependency")
444 .run()
445 .assertStatus(424)
446 .assertContent("foo");
447 c.get("/forbidden")
448 .run()
449 .assertStatus(403)
450 .assertContent("foo");
451 c.get("/gone")
452 .run()
453 .assertStatus(410)
454 .assertContent("foo");
455 c.get("/httpVersionNotSupported")
456 .run()
457 .assertStatus(505)
458 .assertContent("foo");
459 c.get("/insufficientStorage")
460 .run()
461 .assertStatus(507)
462 .assertContent("foo");
463 c.get("/internalServerError")
464 .run()
465 .assertStatus(500)
466 .assertContent("foo");
467 c.get("/lengthRequired")
468 .run()
469 .assertStatus(411)
470 .assertContent("foo");
471 c.get("/locked")
472 .run()
473 .assertStatus(423)
474 .assertContent("foo");
475 c.get("/loopDetected")
476 .run()
477 .assertStatus(508)
478 .assertContent("foo");
479 c.get("/methodNotAllowed")
480 .run()
481 .assertStatus(405)
482 .assertContent("foo");
483 c.get("/misdirectedRequest")
484 .run()
485 .assertStatus(421)
486 .assertContent("foo");
487 c.get("/networkAuthenticationRequired")
488 .run()
489 .assertStatus(511)
490 .assertContent("foo");
491 c.get("/notAcceptable")
492 .run()
493 .assertStatus(406)
494 .assertContent("foo");
495 c.get("/notExtended")
496 .run()
497 .assertStatus(510)
498 .assertContent("foo");
499 c.get("/notFound")
500 .run()
501 .assertStatus(404)
502 .assertContent("foo");
503 c.get("/notImplemented")
504 .run()
505 .assertStatus(501)
506 .assertContent("foo");
507 c.get("/payloadTooLarge")
508 .run()
509 .assertStatus(413)
510 .assertContent("foo");
511 c.get("/preconditionFailed").
512 run()
513 .assertStatus(412)
514 .assertContent("foo");
515 c.get("/preconditionRequired")
516 .run()
517 .assertStatus(428)
518 .assertContent("foo");
519 c.get("/rangeNotSatisfiable")
520 .run()
521 .assertStatus(416)
522 .assertContent("foo");
523 c.get("/requestHeaderFieldsTooLarge")
524 .run()
525 .assertStatus(431)
526 .assertContent("foo");
527 c.get("/serviceUnavailable")
528 .run()
529 .assertStatus(503)
530 .assertContent("foo");
531 c.get("/tooManyRequests")
532 .run()
533 .assertStatus(429)
534 .assertContent("foo");
535 c.get("/unauthorized")
536 .run()
537 .assertStatus(401)
538 .assertContent("foo");
539 c.get("/unavailableForLegalReasons")
540 .run()
541 .assertStatus(451)
542 .assertContent("foo");
543 c.get("/unprocessableEntity")
544 .run()
545 .assertStatus(422)
546 .assertContent("foo");
547 c.get("/unsupportedMediaType")
548 .run()
549 .assertStatus(415)
550 .assertContent("foo");
551 c.get("/upgradeRequired")
552 .run()
553 .assertStatus(426)
554 .assertContent("foo");
555 c.get("/uriTooLong")
556 .run()
557 .assertStatus(414)
558 .assertContent("foo");
559 c.get("/variantAlsoNegotiates")
560 .run()
561 .assertStatus(506)
562 .assertContent("foo");
563 }
564
565
566
567
568
569 @Rest
570 public static class D {
571 @RestGet public void badRequest() { throw new BadRequest(t, "foo {0}", "bar"); }
572 @RestGet public void conflict() { throw new Conflict(t, "foo {0}", "bar"); }
573 @RestGet public void expectationFailed() { throw new ExpectationFailed(t, "foo {0}", "bar"); }
574 @RestGet public void failedDependency() { throw new FailedDependency(t, "foo {0}", "bar"); }
575 @RestGet public void forbidden() { throw new Forbidden(t, "foo {0}", "bar"); }
576 @RestGet public void gone() { throw new Gone(t, "foo {0}", "bar"); }
577 @RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported(t, "foo {0}", "bar"); }
578 @RestGet public void insufficientStorage() { throw new InsufficientStorage(t, "foo {0}", "bar"); }
579 @RestGet public void internalServerError() { throw new InternalServerError(t, "foo {0}", "bar"); }
580 @RestGet public void lengthRequired() { throw new LengthRequired(t, "foo {0}", "bar"); }
581 @RestGet public void locked() { throw new Locked(t, "foo {0}", "bar"); }
582 @RestGet public void loopDetected() { throw new LoopDetected(t, "foo {0}", "bar"); }
583 @RestGet public void methodNotAllowed() { throw new MethodNotAllowed(t, "foo {0}", "bar"); }
584 @RestGet public void misdirectedRequest() { throw new MisdirectedRequest(t, "foo {0}", "bar"); }
585 @RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired(t, "foo {0}", "bar"); }
586 @RestGet public void notAcceptable() { throw new NotAcceptable(t, "foo {0}", "bar"); }
587 @RestGet public void notExtended() { throw new NotExtended(t, "foo {0}", "bar"); }
588 @RestGet public void notFound() { throw new NotFound(t, "foo {0}", "bar"); }
589 @RestGet public void notImplemented() { throw new NotImplemented(t, "foo {0}", "bar"); }
590 @RestGet public void payloadTooLarge() { throw new PayloadTooLarge(t, "foo {0}", "bar"); }
591 @RestGet public void preconditionFailed() { throw new PreconditionFailed(t, "foo {0}", "bar"); }
592 @RestGet public void preconditionRequired() { throw new PreconditionRequired(t, "foo {0}", "bar"); }
593 @RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable(t, "foo {0}", "bar"); }
594 @RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge(t, "foo {0}", "bar"); }
595 @RestGet public void serviceUnavailable() { throw new ServiceUnavailable(t, "foo {0}", "bar"); }
596 @RestGet public void tooManyRequests() { throw new TooManyRequests(t, "foo {0}", "bar"); }
597 @RestGet public void unauthorized() { throw new Unauthorized(t, "foo {0}", "bar"); }
598 @RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons(t, "foo {0}", "bar"); }
599 @RestGet public void unprocessableEntity() { throw new UnprocessableEntity(t, "foo {0}", "bar"); }
600 @RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType(t, "foo {0}", "bar"); }
601 @RestGet public void upgradeRequired() { throw new UpgradeRequired(t, "foo {0}", "bar"); }
602 @RestGet public void uriTooLong() { throw new UriTooLong(t, "foo {0}", "bar"); }
603 @RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates(t, "foo {0}", "bar"); }
604 }
605
606 @Test void d01_nestedThrowableWithMessage() throws Exception {
607 var d = MockRestClient.buildLax(D.class);
608 d.get("/badRequest")
609 .run()
610 .assertStatus(400)
611 .assertContent("foo bar");
612 d.get("/conflict")
613 .run()
614 .assertStatus(409)
615 .assertContent("foo bar");
616 d.get("/expectationFailed")
617 .run()
618 .assertStatus(417)
619 .assertContent("foo bar");
620 d.get("/failedDependency")
621 .run()
622 .assertStatus(424)
623 .assertContent("foo bar");
624 d.get("/forbidden")
625 .run()
626 .assertStatus(403)
627 .assertContent("foo bar");
628 d.get("/gone")
629 .run()
630 .assertStatus(410)
631 .assertContent("foo bar");
632 d.get("/httpVersionNotSupported")
633 .run()
634 .assertStatus(505)
635 .assertContent("foo bar");
636 d.get("/insufficientStorage")
637 .run()
638 .assertStatus(507)
639 .assertContent("foo bar");
640 d.get("/internalServerError")
641 .run()
642 .assertStatus(500)
643 .assertContent("foo bar");
644 d.get("/lengthRequired")
645 .run()
646 .assertStatus(411)
647 .assertContent("foo bar");
648 d.get("/locked")
649 .run()
650 .assertStatus(423)
651 .assertContent("foo bar");
652 d.get("/loopDetected")
653 .run()
654 .assertStatus(508)
655 .assertContent("foo bar");
656 d.get("/methodNotAllowed")
657 .run()
658 .assertStatus(405)
659 .assertContent("foo bar");
660 d.get("/misdirectedRequest")
661 .run()
662 .assertStatus(421)
663 .assertContent("foo bar");
664 d.get("/networkAuthenticationRequired")
665 .run()
666 .assertStatus(511)
667 .assertContent("foo bar");
668 d.get("/notAcceptable")
669 .run()
670 .assertStatus(406)
671 .assertContent("foo bar");
672 d.get("/notExtended")
673 .run()
674 .assertStatus(510)
675 .assertContent("foo bar");
676 d.get("/notFound")
677 .run()
678 .assertStatus(404)
679 .assertContent("foo bar");
680 d.get("/notImplemented")
681 .run()
682 .assertStatus(501)
683 .assertContent("foo bar");
684 d.get("/payloadTooLarge")
685 .run()
686 .assertStatus(413)
687 .assertContent("foo bar");
688 d.get("/preconditionFailed")
689 .run()
690 .assertStatus(412)
691 .assertContent("foo bar");
692 d.get("/preconditionRequired")
693 .run()
694 .assertStatus(428)
695 .assertContent("foo bar");
696 d.get("/rangeNotSatisfiable")
697 .run()
698 .assertStatus(416)
699 .assertContent("foo bar");
700 d.get("/requestHeaderFieldsTooLarge")
701 .run()
702 .assertStatus(431)
703 .assertContent("foo bar");
704 d.get("/serviceUnavailable")
705 .run()
706 .assertStatus(503)
707 .assertContent("foo bar");
708 d.get("/tooManyRequests")
709 .run()
710 .assertStatus(429)
711 .assertContent("foo bar");
712 d.get("/unauthorized")
713 .run()
714 .assertStatus(401)
715 .assertContent("foo bar");
716 d.get("/unavailableForLegalReasons")
717 .run()
718 .assertStatus(451)
719 .assertContent("foo bar");
720 d.get("/unprocessableEntity")
721 .run()
722 .assertStatus(422)
723 .assertContent("foo bar");
724 d.get("/unsupportedMediaType")
725 .run()
726 .assertStatus(415)
727 .assertContent("foo bar");
728 d.get("/upgradeRequired")
729 .run()
730 .assertStatus(426)
731 .assertContent("foo bar");
732 d.get("/uriTooLong")
733 .run()
734 .assertStatus(414)
735 .assertContent("foo bar");
736 d.get("/variantAlsoNegotiates")
737 .run()
738 .assertStatus(506)
739 .assertContent("foo bar");
740 }
741
742
743
744
745
746 @Rest(serializers=Json5Serializer.class)
747 public static class E {
748 @RestGet public void badRequest() { throw new BadRequest(t, "foo {0}", "bar"); }
749 }
750
751 @Test void e01_useAcceptForSerialization() throws Exception {
752 var e = MockRestClient.buildLax(E.class);
753 e.get("/badRequest")
754 .json()
755 .run()
756 .assertStatus(400)
757 .assertContent("foo bar");
758 }
759
760
761
762
763
764 @Rest
765 public static class F {
766 @RestGet public void badRequest() throws BadRequest {}
767 @RestGet public void conflict() throws Conflict {}
768 @RestGet public void expectationFailed() throws ExpectationFailed {}
769 @RestGet public void failedDependency() throws FailedDependency {}
770 @RestGet public void forbidden() throws Forbidden {}
771 @RestGet public void gone() throws Gone {}
772 @RestGet public void httpVersionNotSupported() throws HttpVersionNotSupported {}
773 @RestGet public void insufficientStorage() throws InsufficientStorage {}
774 @RestGet public void internalServerError() throws InternalServerError {}
775 @RestGet public void lengthRequired() throws LengthRequired {}
776 @RestGet public void locked() throws Locked {}
777 @RestGet public void loopDetected() throws LoopDetected {}
778 @RestGet public void methodNotAllowed() throws MethodNotAllowed {}
779 @RestGet public void misdirectedRequest() throws MisdirectedRequest {}
780 @RestGet public void networkAuthenticationRequired() throws NetworkAuthenticationRequired {}
781 @RestGet public void notAcceptable() throws NotAcceptable {}
782 @RestGet public void notExtended() throws NotExtended {}
783 @RestGet public void notFound() throws NotFound {}
784 @RestGet public void notImplemented() throws NotImplemented {}
785 @RestGet public void payloadTooLarge() throws PayloadTooLarge {}
786 @RestGet public void preconditionFailed() throws PreconditionFailed {}
787 @RestGet public void preconditionRequired() throws PreconditionRequired {}
788 @RestGet public void rangeNotSatisfiable() throws RangeNotSatisfiable {}
789 @RestGet public void requestHeaderFieldsTooLarge() throws RequestHeaderFieldsTooLarge {}
790 @RestGet public void serviceUnavailable() throws ServiceUnavailable {}
791 @RestGet public void tooManyRequests() throws TooManyRequests {}
792 @RestGet public void unauthorized() throws Unauthorized {}
793 @RestGet public void unavailableForLegalReasons() throws UnavailableForLegalReasons {}
794 @RestGet public void unprocessableEntity() throws UnprocessableEntity {}
795 @RestGet public void unsupportedMediaType() throws UnsupportedMediaType {}
796 @RestGet public void upgradeRequired() throws UpgradeRequired {}
797 @RestGet public void uriTooLong() throws UriTooLong {}
798 @RestGet public void variantAlsoNegotiates() throws VariantAlsoNegotiates {}
799 }
800
801 @Test void f01_swagger() {
802 var paths = getSwagger(F.class).getPaths();
803
804 assertEquals(BadRequest.REASON_PHRASE, paths.get("/badRequest").get("get").getResponse(BadRequest.STATUS_CODE).getDescription());
805 assertEquals(Conflict.REASON_PHRASE, paths.get("/conflict").get("get").getResponse(Conflict.STATUS_CODE).getDescription());
806 assertEquals(ExpectationFailed.REASON_PHRASE, paths.get("/expectationFailed").get("get").getResponse(ExpectationFailed.STATUS_CODE).getDescription());
807 assertEquals(FailedDependency.REASON_PHRASE, paths.get("/failedDependency").get("get").getResponse(FailedDependency.STATUS_CODE).getDescription());
808 assertEquals(Forbidden.REASON_PHRASE, paths.get("/forbidden").get("get").getResponse(Forbidden.STATUS_CODE).getDescription());
809 assertEquals(Gone.REASON_PHRASE, paths.get("/gone").get("get").getResponse(Gone.STATUS_CODE).getDescription());
810 assertEquals(HttpVersionNotSupported.REASON_PHRASE, paths.get("/httpVersionNotSupported").get("get").getResponse(HttpVersionNotSupported.STATUS_CODE).getDescription());
811 assertEquals(InsufficientStorage.REASON_PHRASE, paths.get("/insufficientStorage").get("get").getResponse(InsufficientStorage.STATUS_CODE).getDescription());
812 assertEquals(InternalServerError.REASON_PHRASE, paths.get("/internalServerError").get("get").getResponse(InternalServerError.STATUS_CODE).getDescription());
813 assertEquals(LengthRequired.REASON_PHRASE, paths.get("/lengthRequired").get("get").getResponse(LengthRequired.STATUS_CODE).getDescription());
814 assertEquals(Locked.REASON_PHRASE, paths.get("/locked").get("get").getResponse(Locked.STATUS_CODE).getDescription());
815 assertEquals(LoopDetected.REASON_PHRASE, paths.get("/loopDetected").get("get").getResponse(LoopDetected.STATUS_CODE).getDescription());
816 assertEquals(MethodNotAllowed.REASON_PHRASE, paths.get("/methodNotAllowed").get("get").getResponse(MethodNotAllowed.STATUS_CODE).getDescription());
817 assertEquals(MisdirectedRequest.REASON_PHRASE, paths.get("/misdirectedRequest").get("get").getResponse(MisdirectedRequest.STATUS_CODE).getDescription());
818 assertEquals(NetworkAuthenticationRequired.REASON_PHRASE, paths.get("/networkAuthenticationRequired").get("get").getResponse(NetworkAuthenticationRequired.STATUS_CODE).getDescription());
819 assertEquals(NotAcceptable.REASON_PHRASE, paths.get("/notAcceptable").get("get").getResponse(NotAcceptable.STATUS_CODE).getDescription());
820 assertEquals(NotExtended.REASON_PHRASE, paths.get("/notExtended").get("get").getResponse(NotExtended.STATUS_CODE).getDescription());
821 assertEquals(NotFound.REASON_PHRASE, paths.get("/notFound").get("get").getResponse(NotFound.STATUS_CODE).getDescription());
822 assertEquals(NotImplemented.REASON_PHRASE, paths.get("/notImplemented").get("get").getResponse(NotImplemented.STATUS_CODE).getDescription());
823 assertEquals(PayloadTooLarge.REASON_PHRASE, paths.get("/payloadTooLarge").get("get").getResponse(PayloadTooLarge.STATUS_CODE).getDescription());
824 assertEquals(PreconditionFailed.REASON_PHRASE, paths.get("/preconditionFailed").get("get").getResponse(PreconditionFailed.STATUS_CODE).getDescription());
825 assertEquals(PreconditionRequired.REASON_PHRASE, paths.get("/preconditionRequired").get("get").getResponse(PreconditionRequired.STATUS_CODE).getDescription());
826 assertEquals(RangeNotSatisfiable.REASON_PHRASE, paths.get("/rangeNotSatisfiable").get("get").getResponse(RangeNotSatisfiable.STATUS_CODE).getDescription());
827 assertEquals(RequestHeaderFieldsTooLarge.REASON_PHRASE, paths.get("/requestHeaderFieldsTooLarge").get("get").getResponse(RequestHeaderFieldsTooLarge.STATUS_CODE).getDescription());
828 assertEquals(ServiceUnavailable.REASON_PHRASE, paths.get("/serviceUnavailable").get("get").getResponse(ServiceUnavailable.STATUS_CODE).getDescription());
829 assertEquals(TooManyRequests.REASON_PHRASE, paths.get("/tooManyRequests").get("get").getResponse(TooManyRequests.STATUS_CODE).getDescription());
830 assertEquals(Unauthorized.REASON_PHRASE, paths.get("/unauthorized").get("get").getResponse(Unauthorized.STATUS_CODE).getDescription());
831 assertEquals(UnavailableForLegalReasons.REASON_PHRASE, paths.get("/unavailableForLegalReasons").get("get").getResponse(UnavailableForLegalReasons.STATUS_CODE).getDescription());
832 assertEquals(UnprocessableEntity.REASON_PHRASE, paths.get("/unprocessableEntity").get("get").getResponse(UnprocessableEntity.STATUS_CODE).getDescription());
833 assertEquals(UnsupportedMediaType.REASON_PHRASE, paths.get("/unsupportedMediaType").get("get").getResponse(UnsupportedMediaType.STATUS_CODE).getDescription());
834 assertEquals(UpgradeRequired.REASON_PHRASE, paths.get("/upgradeRequired").get("get").getResponse(UpgradeRequired.STATUS_CODE).getDescription());
835 assertEquals(UriTooLong.REASON_PHRASE, paths.get("/uriTooLong").get("get").getResponse(UriTooLong.STATUS_CODE).getDescription());
836 assertEquals(VariantAlsoNegotiates.REASON_PHRASE, paths.get("/variantAlsoNegotiates").get("get").getResponse(VariantAlsoNegotiates.STATUS_CODE).getDescription());
837 }
838
839
840
841
842
843 @Rest
844 public static class G {
845 @RestGet
846 public SeeOtherRoot a() throws Exception { throw new NotFound(); }
847 }
848
849 @Test void g01_thrownObjectDoesntMatchReturnType() throws Exception {
850 var g = MockRestClient.buildLax(G.class);
851 g.get("/a")
852 .run()
853 .assertStatus(404);
854 }
855
856
857
858
859
860 @Rest
861 public static class H {
862 @RestGet
863 public void a() throws Exception {
864 throw new ParseException("Test");
865 }
866 }
867
868 @Test void h01_parseExceptionCausesBadRequest() throws Exception {
869 var h = MockRestClient.buildLax(H.class);
870 h.get("/a")
871 .run()
872 .assertStatus(400);
873 }
874 }