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;
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  	// Basic sanity tests
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 	// User-specified message
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 
251 	@Test void b01_userSpecifiedMessage() throws Exception {
252 		var b = MockRestClient.buildLax(B.class);
253 		b.get("/badRequest")
254 			.run()
255 			.assertStatus(400)
256 			.assertContent("foo bar");
257 		b.get("/conflict")
258 			.run()
259 			.assertStatus(409)
260 			.assertContent("foo bar");
261 		b.get("/expectationFailed")
262 			.run()
263 			.assertStatus(417)
264 			.assertContent("foo bar");
265 		b.get("/failedDependency")
266 			.run()
267 			.assertStatus(424)
268 			.assertContent("foo bar");
269 		b.get("/forbidden")
270 			.run()
271 			.assertStatus(403)
272 			.assertContent("foo bar");
273 		b.get("/gone")
274 			.run()
275 			.assertStatus(410)
276 			.assertContent("foo bar");
277 		b.get("/httpVersionNotSupported")
278 			.run()
279 			.assertStatus(505)
280 			.assertContent("foo bar");
281 		b.get("/insufficientStorage")
282 			.run()
283 			.assertStatus(507)
284 			.assertContent("foo bar");
285 		b.get("/internalServerError")
286 			.run()
287 			.assertStatus(500)
288 			.assertContent("foo bar");
289 		b.get("/lengthRequired")
290 			.run()
291 			.assertStatus(411)
292 			.assertContent("foo bar");
293 		b.get("/locked")
294 			.run()
295 			.assertStatus(423)
296 			.assertContent("foo bar");
297 		b.get("/loopDetected")
298 			.run()
299 			.assertStatus(508)
300 			.assertContent("foo bar");
301 		b.get("/methodNotAllowed")
302 			.run()
303 			.assertStatus(405)
304 			.assertContent("foo bar");
305 		b.get("/misdirectedRequest")
306 			.run()
307 			.assertStatus(421)
308 			.assertContent("foo bar");
309 		b.get("/networkAuthenticationRequired")
310 			.run()
311 			.assertStatus(511)
312 			.assertContent("foo bar");
313 		b.get("/notAcceptable")
314 			.run()
315 			.assertStatus(406)
316 			.assertContent("foo bar");
317 		b.get("/notExtended")
318 			.run()
319 			.assertStatus(510)
320 			.assertContent("foo bar");
321 		b.get("/notFound")
322 			.run()
323 			.assertStatus(404)
324 			.assertContent("foo bar");
325 		b.get("/notImplemented")
326 			.run()
327 			.assertStatus(501)
328 			.assertContent("foo bar");
329 		b.get("/payloadTooLarge")
330 			.run()
331 			.assertStatus(413)
332 			.assertContent("foo bar");
333 		b.get("/preconditionFailed")
334 			.run()
335 			.assertStatus(412)
336 			.assertContent("foo bar");
337 		b.get("/preconditionRequired")
338 			.run()
339 			.assertStatus(428)
340 			.assertContent("foo bar");
341 		b.get("/rangeNotSatisfiable")
342 			.run()
343 			.assertStatus(416)
344 			.assertContent("foo bar");
345 		b.get("/requestHeaderFieldsTooLarge")
346 			.run()
347 			.assertStatus(431)
348 			.assertContent("foo bar");
349 		b.get("/serviceUnavailable")
350 			.run()
351 			.assertStatus(503)
352 			.assertContent("foo bar");
353 		b.get("/tooManyRequests")
354 			.run()
355 			.assertStatus(429)
356 			.assertContent("foo bar");
357 		b.get("/unauthorized")
358 			.run()
359 			.assertStatus(401)
360 			.assertContent("foo bar");
361 		b.get("/unavailableForLegalReasons")
362 			.run()
363 			.assertStatus(451)
364 			.assertContent("foo bar");
365 		b.get("/unprocessableEntity")
366 			.run()
367 			.assertStatus(422)
368 			.assertContent("foo bar");
369 		b.get("/unsupportedMediaType")
370 			.run()
371 			.assertStatus(415)
372 			.assertContent("foo bar");
373 		b.get("/upgradeRequired")
374 			.run()
375 			.assertStatus(426)
376 			.assertContent("foo bar");
377 		b.get("/uriTooLong")
378 			.run()
379 			.assertStatus(414)
380 			.assertContent("foo bar");
381 		b.get("/variantAlsoNegotiates")
382 			.run()
383 			.assertStatus(506)
384 			.assertContent("foo bar");
385 	}
386 
387 	//-----------------------------------------------------------------------------------------------------------------
388 	// Throwable
389 	//-----------------------------------------------------------------------------------------------------------------
390 
391 	static final Throwable t = new Throwable("foo");
392 
393 	@Rest
394 	public static class C {
395 		@RestGet public void badRequest() { throw new BadRequest(t); }
396 		@RestGet public void conflict() { throw new Conflict(t); }
397 		@RestGet public void expectationFailed() { throw new ExpectationFailed(t); }
398 		@RestGet public void failedDependency() { throw new FailedDependency(t); }
399 		@RestGet public void forbidden() { throw new Forbidden(t); }
400 		@RestGet public void gone() { throw new Gone(t); }
401 		@RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported(t); }
402 		@RestGet public void insufficientStorage() { throw new InsufficientStorage(t); }
403 		@RestGet public void internalServerError() { throw new InternalServerError(t); }
404 		@RestGet public void lengthRequired() { throw new LengthRequired(t); }
405 		@RestGet public void locked() { throw new Locked(t); }
406 		@RestGet public void loopDetected() { throw new LoopDetected(t); }
407 		@RestGet public void methodNotAllowed() { throw new MethodNotAllowed(t); }
408 		@RestGet public void misdirectedRequest() { throw new MisdirectedRequest(t); }
409 		@RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired(t); }
410 		@RestGet public void notAcceptable() { throw new NotAcceptable(t); }
411 		@RestGet public void notExtended() { throw new NotExtended(t); }
412 		@RestGet public void notFound() { throw new NotFound(t); }
413 		@RestGet public void notImplemented() { throw new NotImplemented(t); }
414 		@RestGet public void payloadTooLarge() { throw new PayloadTooLarge(t); }
415 		@RestGet public void preconditionFailed() { throw new PreconditionFailed(t); }
416 		@RestGet public void preconditionRequired() { throw new PreconditionRequired(t); }
417 		@RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable(t); }
418 		@RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge(t); }
419 		@RestGet public void serviceUnavailable() { throw new ServiceUnavailable(t); }
420 		@RestGet public void tooManyRequests() { throw new TooManyRequests(t); }
421 		@RestGet public void unauthorized() { throw new Unauthorized(t); }
422 		@RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons(t); }
423 		@RestGet public void unprocessableEntity() { throw new UnprocessableEntity(t); }
424 		@RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType(t); }
425 		@RestGet public void upgradeRequired() { throw new UpgradeRequired(t); }
426 		@RestGet public void uriTooLong() { throw new UriTooLong(t); }
427 		@RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates(t); }
428 	}
429 
430 	@Test void c01_nestedThrowable() throws Exception {
431 		var c = MockRestClient.buildLax(C.class);
432 		c.get("/badRequest")
433 			.run()
434 			.assertStatus(400)
435 			.assertContent("foo");
436 		c.get("/conflict")
437 			.run()
438 			.assertStatus(409)
439 			.assertContent("foo");
440 		c.get("/expectationFailed")
441 			.run()
442 			.assertStatus(417)
443 			.assertContent("foo");
444 		c.get("/failedDependency")
445 			.run()
446 			.assertStatus(424)
447 			.assertContent("foo");
448 		c.get("/forbidden")
449 			.run()
450 			.assertStatus(403)
451 			.assertContent("foo");
452 		c.get("/gone")
453 			.run()
454 			.assertStatus(410)
455 			.assertContent("foo");
456 		c.get("/httpVersionNotSupported")
457 			.run()
458 			.assertStatus(505)
459 			.assertContent("foo");
460 		c.get("/insufficientStorage")
461 			.run()
462 			.assertStatus(507)
463 			.assertContent("foo");
464 		c.get("/internalServerError")
465 			.run()
466 			.assertStatus(500)
467 			.assertContent("foo");
468 		c.get("/lengthRequired")
469 			.run()
470 			.assertStatus(411)
471 			.assertContent("foo");
472 		c.get("/locked")
473 			.run()
474 			.assertStatus(423)
475 			.assertContent("foo");
476 		c.get("/loopDetected")
477 			.run()
478 			.assertStatus(508)
479 			.assertContent("foo");
480 		c.get("/methodNotAllowed")
481 			.run()
482 			.assertStatus(405)
483 			.assertContent("foo");
484 		c.get("/misdirectedRequest")
485 			.run()
486 			.assertStatus(421)
487 			.assertContent("foo");
488 		c.get("/networkAuthenticationRequired")
489 			.run()
490 			.assertStatus(511)
491 			.assertContent("foo");
492 		c.get("/notAcceptable")
493 			.run()
494 			.assertStatus(406)
495 			.assertContent("foo");
496 		c.get("/notExtended")
497 			.run()
498 			.assertStatus(510)
499 			.assertContent("foo");
500 		c.get("/notFound")
501 			.run()
502 			.assertStatus(404)
503 			.assertContent("foo");
504 		c.get("/notImplemented")
505 			.run()
506 			.assertStatus(501)
507 			.assertContent("foo");
508 		c.get("/payloadTooLarge")
509 			.run()
510 			.assertStatus(413)
511 			.assertContent("foo");
512 		c.get("/preconditionFailed").
513 			run()
514 			.assertStatus(412)
515 			.assertContent("foo");
516 		c.get("/preconditionRequired")
517 			.run()
518 			.assertStatus(428)
519 			.assertContent("foo");
520 		c.get("/rangeNotSatisfiable")
521 			.run()
522 			.assertStatus(416)
523 			.assertContent("foo");
524 		c.get("/requestHeaderFieldsTooLarge")
525 			.run()
526 			.assertStatus(431)
527 			.assertContent("foo");
528 		c.get("/serviceUnavailable")
529 			.run()
530 			.assertStatus(503)
531 			.assertContent("foo");
532 		c.get("/tooManyRequests")
533 			.run()
534 			.assertStatus(429)
535 			.assertContent("foo");
536 		c.get("/unauthorized")
537 			.run()
538 			.assertStatus(401)
539 			.assertContent("foo");
540 		c.get("/unavailableForLegalReasons")
541 			.run()
542 			.assertStatus(451)
543 			.assertContent("foo");
544 		c.get("/unprocessableEntity")
545 			.run()
546 			.assertStatus(422)
547 			.assertContent("foo");
548 		c.get("/unsupportedMediaType")
549 			.run()
550 			.assertStatus(415)
551 			.assertContent("foo");
552 		c.get("/upgradeRequired")
553 			.run()
554 			.assertStatus(426)
555 			.assertContent("foo");
556 		c.get("/uriTooLong")
557 			.run()
558 			.assertStatus(414)
559 			.assertContent("foo");
560 		c.get("/variantAlsoNegotiates")
561 			.run()
562 			.assertStatus(506)
563 			.assertContent("foo");
564 	}
565 
566 	//-----------------------------------------------------------------------------------------------------------------
567 	// Throwable with message
568 	//-----------------------------------------------------------------------------------------------------------------
569 
570 	@Rest
571 	public static class D {
572 		@RestGet public void badRequest() { throw new BadRequest(t, "foo {0}", "bar"); }
573 		@RestGet public void conflict() { throw new Conflict(t, "foo {0}", "bar"); }
574 		@RestGet public void expectationFailed() { throw new ExpectationFailed(t, "foo {0}", "bar"); }
575 		@RestGet public void failedDependency() { throw new FailedDependency(t, "foo {0}", "bar"); }
576 		@RestGet public void forbidden() { throw new Forbidden(t, "foo {0}", "bar"); }
577 		@RestGet public void gone() { throw new Gone(t, "foo {0}", "bar"); }
578 		@RestGet public void httpVersionNotSupported() { throw new HttpVersionNotSupported(t, "foo {0}", "bar"); }
579 		@RestGet public void insufficientStorage() { throw new InsufficientStorage(t, "foo {0}", "bar"); }
580 		@RestGet public void internalServerError() { throw new InternalServerError(t, "foo {0}", "bar"); }
581 		@RestGet public void lengthRequired() { throw new LengthRequired(t, "foo {0}", "bar"); }
582 		@RestGet public void locked() { throw new Locked(t, "foo {0}", "bar"); }
583 		@RestGet public void loopDetected() { throw new LoopDetected(t, "foo {0}", "bar"); }
584 		@RestGet public void methodNotAllowed() { throw new MethodNotAllowed(t, "foo {0}", "bar"); }
585 		@RestGet public void misdirectedRequest() { throw new MisdirectedRequest(t, "foo {0}", "bar"); }
586 		@RestGet public void networkAuthenticationRequired() { throw new NetworkAuthenticationRequired(t, "foo {0}", "bar"); }
587 		@RestGet public void notAcceptable() { throw new NotAcceptable(t, "foo {0}", "bar"); }
588 		@RestGet public void notExtended() { throw new NotExtended(t, "foo {0}", "bar"); }
589 		@RestGet public void notFound() { throw new NotFound(t, "foo {0}", "bar"); }
590 		@RestGet public void notImplemented() { throw new NotImplemented(t, "foo {0}", "bar"); }
591 		@RestGet public void payloadTooLarge() { throw new PayloadTooLarge(t, "foo {0}", "bar"); }
592 		@RestGet public void preconditionFailed() { throw new PreconditionFailed(t, "foo {0}", "bar"); }
593 		@RestGet public void preconditionRequired() { throw new PreconditionRequired(t, "foo {0}", "bar"); }
594 		@RestGet public void rangeNotSatisfiable() { throw new RangeNotSatisfiable(t, "foo {0}", "bar"); }
595 		@RestGet public void requestHeaderFieldsTooLarge() { throw new RequestHeaderFieldsTooLarge(t, "foo {0}", "bar"); }
596 		@RestGet public void serviceUnavailable() { throw new ServiceUnavailable(t, "foo {0}", "bar"); }
597 		@RestGet public void tooManyRequests() { throw new TooManyRequests(t, "foo {0}", "bar"); }
598 		@RestGet public void unauthorized() { throw new Unauthorized(t, "foo {0}", "bar"); }
599 		@RestGet public void unavailableForLegalReasons() { throw new UnavailableForLegalReasons(t, "foo {0}", "bar"); }
600 		@RestGet public void unprocessableEntity() { throw new UnprocessableEntity(t, "foo {0}", "bar"); }
601 		@RestGet public void unsupportedMediaType() { throw new UnsupportedMediaType(t, "foo {0}", "bar"); }
602 		@RestGet public void upgradeRequired() { throw new UpgradeRequired(t, "foo {0}", "bar"); }
603 		@RestGet public void uriTooLong() { throw new UriTooLong(t, "foo {0}", "bar"); }
604 		@RestGet public void variantAlsoNegotiates() { throw new VariantAlsoNegotiates(t, "foo {0}", "bar"); }
605 	}
606 
607 	@Test void d01_nestedThrowableWithMessage() throws Exception {
608 		var d = MockRestClient.buildLax(D.class);
609 		d.get("/badRequest")
610 			.run()
611 			.assertStatus(400)
612 			.assertContent("foo bar");
613 		d.get("/conflict")
614 			.run()
615 			.assertStatus(409)
616 			.assertContent("foo bar");
617 		d.get("/expectationFailed")
618 			.run()
619 			.assertStatus(417)
620 			.assertContent("foo bar");
621 		d.get("/failedDependency")
622 			.run()
623 			.assertStatus(424)
624 			.assertContent("foo bar");
625 		d.get("/forbidden")
626 			.run()
627 			.assertStatus(403)
628 			.assertContent("foo bar");
629 		d.get("/gone")
630 			.run()
631 			.assertStatus(410)
632 			.assertContent("foo bar");
633 		d.get("/httpVersionNotSupported")
634 			.run()
635 			.assertStatus(505)
636 			.assertContent("foo bar");
637 		d.get("/insufficientStorage")
638 			.run()
639 			.assertStatus(507)
640 			.assertContent("foo bar");
641 		d.get("/internalServerError")
642 			.run()
643 			.assertStatus(500)
644 			.assertContent("foo bar");
645 		d.get("/lengthRequired")
646 			.run()
647 			.assertStatus(411)
648 			.assertContent("foo bar");
649 		d.get("/locked")
650 			.run()
651 			.assertStatus(423)
652 			.assertContent("foo bar");
653 		d.get("/loopDetected")
654 			.run()
655 			.assertStatus(508)
656 			.assertContent("foo bar");
657 		d.get("/methodNotAllowed")
658 			.run()
659 			.assertStatus(405)
660 			.assertContent("foo bar");
661 		d.get("/misdirectedRequest")
662 			.run()
663 			.assertStatus(421)
664 			.assertContent("foo bar");
665 		d.get("/networkAuthenticationRequired")
666 			.run()
667 			.assertStatus(511)
668 			.assertContent("foo bar");
669 		d.get("/notAcceptable")
670 			.run()
671 			.assertStatus(406)
672 			.assertContent("foo bar");
673 		d.get("/notExtended")
674 			.run()
675 			.assertStatus(510)
676 			.assertContent("foo bar");
677 		d.get("/notFound")
678 			.run()
679 			.assertStatus(404)
680 			.assertContent("foo bar");
681 		d.get("/notImplemented")
682 			.run()
683 			.assertStatus(501)
684 			.assertContent("foo bar");
685 		d.get("/payloadTooLarge")
686 			.run()
687 			.assertStatus(413)
688 			.assertContent("foo bar");
689 		d.get("/preconditionFailed")
690 			.run()
691 			.assertStatus(412)
692 			.assertContent("foo bar");
693 		d.get("/preconditionRequired")
694 			.run()
695 			.assertStatus(428)
696 			.assertContent("foo bar");
697 		d.get("/rangeNotSatisfiable")
698 			.run()
699 			.assertStatus(416)
700 			.assertContent("foo bar");
701 		d.get("/requestHeaderFieldsTooLarge")
702 			.run()
703 			.assertStatus(431)
704 			.assertContent("foo bar");
705 		d.get("/serviceUnavailable")
706 			.run()
707 			.assertStatus(503)
708 			.assertContent("foo bar");
709 		d.get("/tooManyRequests")
710 			.run()
711 			.assertStatus(429)
712 			.assertContent("foo bar");
713 		d.get("/unauthorized")
714 			.run()
715 			.assertStatus(401)
716 			.assertContent("foo bar");
717 		d.get("/unavailableForLegalReasons")
718 			.run()
719 			.assertStatus(451)
720 			.assertContent("foo bar");
721 		d.get("/unprocessableEntity")
722 			.run()
723 			.assertStatus(422)
724 			.assertContent("foo bar");
725 		d.get("/unsupportedMediaType")
726 			.run()
727 			.assertStatus(415)
728 			.assertContent("foo bar");
729 		d.get("/upgradeRequired")
730 			.run()
731 			.assertStatus(426)
732 			.assertContent("foo bar");
733 		d.get("/uriTooLong")
734 			.run()
735 			.assertStatus(414)
736 			.assertContent("foo bar");
737 		d.get("/variantAlsoNegotiates")
738 			.run()
739 			.assertStatus(506)
740 			.assertContent("foo bar");
741 	}
742 
743 	//-----------------------------------------------------------------------------------------------------------------
744 	// Should use Accept language for serialization.
745 	//-----------------------------------------------------------------------------------------------------------------
746 
747 	@Rest(serializers=Json5Serializer.class)
748 	public static class E {
749 		@RestGet public void badRequest() { throw new BadRequest(t, "foo {0}", "bar"); }
750 	}
751 
752 	@Test void e01_useAcceptForSerialization() throws Exception {
753 		var e = MockRestClient.buildLax(E.class);
754 		e.get("/badRequest")
755 			.json()
756 			.run()
757 			.assertStatus(400)
758 			.assertContent("foo bar");
759 	}
760 
761 	//-----------------------------------------------------------------------------------------------------------------
762 	// Test Swagger
763 	//-----------------------------------------------------------------------------------------------------------------
764 
765 	@Rest
766 	public static class F {
767 		@RestGet public void badRequest() throws BadRequest {}  // NOSONAR
768 		@RestGet public void conflict() throws Conflict {}  // NOSONAR
769 		@RestGet public void expectationFailed() throws ExpectationFailed {}  // NOSONAR
770 		@RestGet public void failedDependency() throws FailedDependency {}  // NOSONAR
771 		@RestGet public void forbidden() throws Forbidden {}  // NOSONAR
772 		@RestGet public void gone() throws Gone {}  // NOSONAR
773 		@RestGet public void httpVersionNotSupported() throws HttpVersionNotSupported {}  // NOSONAR
774 		@RestGet public void insufficientStorage() throws InsufficientStorage {}  // NOSONAR
775 		@RestGet public void internalServerError() throws InternalServerError {}  // NOSONAR
776 		@RestGet public void lengthRequired() throws LengthRequired {}  // NOSONAR
777 		@RestGet public void locked() throws Locked {}  // NOSONAR
778 		@RestGet public void loopDetected() throws LoopDetected {}  // NOSONAR
779 		@RestGet public void methodNotAllowed() throws MethodNotAllowed {}  // NOSONAR
780 		@RestGet public void misdirectedRequest() throws MisdirectedRequest {}  // NOSONAR
781 		@RestGet public void networkAuthenticationRequired() throws NetworkAuthenticationRequired {}  // NOSONAR
782 		@RestGet public void notAcceptable() throws NotAcceptable {}  // NOSONAR
783 		@RestGet public void notExtended() throws NotExtended {}  // NOSONAR
784 		@RestGet public void notFound() throws NotFound {}  // NOSONAR
785 		@RestGet public void notImplemented() throws NotImplemented {}  // NOSONAR
786 		@RestGet public void payloadTooLarge() throws PayloadTooLarge {}  // NOSONAR
787 		@RestGet public void preconditionFailed() throws PreconditionFailed {}  // NOSONAR
788 		@RestGet public void preconditionRequired() throws PreconditionRequired {}  // NOSONAR
789 		@RestGet public void rangeNotSatisfiable() throws RangeNotSatisfiable {}  // NOSONAR
790 		@RestGet public void requestHeaderFieldsTooLarge() throws RequestHeaderFieldsTooLarge {}  // NOSONAR
791 		@RestGet public void serviceUnavailable() throws ServiceUnavailable {}  // NOSONAR
792 		@RestGet public void tooManyRequests() throws TooManyRequests {}  // NOSONAR
793 		@RestGet public void unauthorized() throws Unauthorized {}  // NOSONAR
794 		@RestGet public void unavailableForLegalReasons() throws UnavailableForLegalReasons {}  // NOSONAR
795 		@RestGet public void unprocessableEntity() throws UnprocessableEntity {}  // NOSONAR
796 		@RestGet public void unsupportedMediaType() throws UnsupportedMediaType {}  // NOSONAR
797 		@RestGet public void upgradeRequired() throws UpgradeRequired {}  // NOSONAR
798 		@RestGet public void uriTooLong() throws UriTooLong {}  // NOSONAR
799 		@RestGet public void variantAlsoNegotiates() throws VariantAlsoNegotiates {}  // NOSONAR
800 	}
801 
802 	@Test void f01_swagger() {
803 		var paths = getSwagger(F.class).getPaths();
804 
805 		assertEquals(BadRequest.REASON_PHRASE, paths.get("/badRequest").get("get").getResponse(BadRequest.STATUS_CODE).getDescription());
806 		assertEquals(Conflict.REASON_PHRASE, paths.get("/conflict").get("get").getResponse(Conflict.STATUS_CODE).getDescription());
807 		assertEquals(ExpectationFailed.REASON_PHRASE, paths.get("/expectationFailed").get("get").getResponse(ExpectationFailed.STATUS_CODE).getDescription());
808 		assertEquals(FailedDependency.REASON_PHRASE, paths.get("/failedDependency").get("get").getResponse(FailedDependency.STATUS_CODE).getDescription());
809 		assertEquals(Forbidden.REASON_PHRASE, paths.get("/forbidden").get("get").getResponse(Forbidden.STATUS_CODE).getDescription());
810 		assertEquals(Gone.REASON_PHRASE, paths.get("/gone").get("get").getResponse(Gone.STATUS_CODE).getDescription());
811 		assertEquals(HttpVersionNotSupported.REASON_PHRASE, paths.get("/httpVersionNotSupported").get("get").getResponse(HttpVersionNotSupported.STATUS_CODE).getDescription());
812 		assertEquals(InsufficientStorage.REASON_PHRASE, paths.get("/insufficientStorage").get("get").getResponse(InsufficientStorage.STATUS_CODE).getDescription());
813 		assertEquals(InternalServerError.REASON_PHRASE, paths.get("/internalServerError").get("get").getResponse(InternalServerError.STATUS_CODE).getDescription());
814 		assertEquals(LengthRequired.REASON_PHRASE, paths.get("/lengthRequired").get("get").getResponse(LengthRequired.STATUS_CODE).getDescription());
815 		assertEquals(Locked.REASON_PHRASE, paths.get("/locked").get("get").getResponse(Locked.STATUS_CODE).getDescription());
816 		assertEquals(LoopDetected.REASON_PHRASE, paths.get("/loopDetected").get("get").getResponse(LoopDetected.STATUS_CODE).getDescription());
817 		assertEquals(MethodNotAllowed.REASON_PHRASE, paths.get("/methodNotAllowed").get("get").getResponse(MethodNotAllowed.STATUS_CODE).getDescription());
818 		assertEquals(MisdirectedRequest.REASON_PHRASE, paths.get("/misdirectedRequest").get("get").getResponse(MisdirectedRequest.STATUS_CODE).getDescription());
819 		assertEquals(NetworkAuthenticationRequired.REASON_PHRASE, paths.get("/networkAuthenticationRequired").get("get").getResponse(NetworkAuthenticationRequired.STATUS_CODE).getDescription());
820 		assertEquals(NotAcceptable.REASON_PHRASE, paths.get("/notAcceptable").get("get").getResponse(NotAcceptable.STATUS_CODE).getDescription());
821 		assertEquals(NotExtended.REASON_PHRASE, paths.get("/notExtended").get("get").getResponse(NotExtended.STATUS_CODE).getDescription());
822 		assertEquals(NotFound.REASON_PHRASE, paths.get("/notFound").get("get").getResponse(NotFound.STATUS_CODE).getDescription());
823 		assertEquals(NotImplemented.REASON_PHRASE, paths.get("/notImplemented").get("get").getResponse(NotImplemented.STATUS_CODE).getDescription());
824 		assertEquals(PayloadTooLarge.REASON_PHRASE, paths.get("/payloadTooLarge").get("get").getResponse(PayloadTooLarge.STATUS_CODE).getDescription());
825 		assertEquals(PreconditionFailed.REASON_PHRASE, paths.get("/preconditionFailed").get("get").getResponse(PreconditionFailed.STATUS_CODE).getDescription());
826 		assertEquals(PreconditionRequired.REASON_PHRASE, paths.get("/preconditionRequired").get("get").getResponse(PreconditionRequired.STATUS_CODE).getDescription());
827 		assertEquals(RangeNotSatisfiable.REASON_PHRASE, paths.get("/rangeNotSatisfiable").get("get").getResponse(RangeNotSatisfiable.STATUS_CODE).getDescription());
828 		assertEquals(RequestHeaderFieldsTooLarge.REASON_PHRASE, paths.get("/requestHeaderFieldsTooLarge").get("get").getResponse(RequestHeaderFieldsTooLarge.STATUS_CODE).getDescription());
829 		assertEquals(ServiceUnavailable.REASON_PHRASE, paths.get("/serviceUnavailable").get("get").getResponse(ServiceUnavailable.STATUS_CODE).getDescription());
830 		assertEquals(TooManyRequests.REASON_PHRASE, paths.get("/tooManyRequests").get("get").getResponse(TooManyRequests.STATUS_CODE).getDescription());
831 		assertEquals(Unauthorized.REASON_PHRASE, paths.get("/unauthorized").get("get").getResponse(Unauthorized.STATUS_CODE).getDescription());
832 		assertEquals(UnavailableForLegalReasons.REASON_PHRASE, paths.get("/unavailableForLegalReasons").get("get").getResponse(UnavailableForLegalReasons.STATUS_CODE).getDescription());
833 		assertEquals(UnprocessableEntity.REASON_PHRASE, paths.get("/unprocessableEntity").get("get").getResponse(UnprocessableEntity.STATUS_CODE).getDescription());
834 		assertEquals(UnsupportedMediaType.REASON_PHRASE, paths.get("/unsupportedMediaType").get("get").getResponse(UnsupportedMediaType.STATUS_CODE).getDescription());
835 		assertEquals(UpgradeRequired.REASON_PHRASE, paths.get("/upgradeRequired").get("get").getResponse(UpgradeRequired.STATUS_CODE).getDescription());
836 		assertEquals(UriTooLong.REASON_PHRASE, paths.get("/uriTooLong").get("get").getResponse(UriTooLong.STATUS_CODE).getDescription());
837 		assertEquals(VariantAlsoNegotiates.REASON_PHRASE, paths.get("/variantAlsoNegotiates").get("get").getResponse(VariantAlsoNegotiates.STATUS_CODE).getDescription());
838 	}
839 
840 	//-----------------------------------------------------------------------------------------------------------------
841 	// Thrown object doesn't match return type.
842 	//-----------------------------------------------------------------------------------------------------------------
843 
844 	@Rest
845 	public static class G {
846 		@RestGet
847 		public SeeOtherRoot a() throws Exception { throw new NotFound(); }
848 	}
849 
850 	@Test void g01_thrownObjectDoesntMatchReturnType() throws Exception {
851 		var g = MockRestClient.buildLax(G.class);
852 		g.get("/a")
853 			.run()
854 			.assertStatus(404);
855 	}
856 
857 	//-----------------------------------------------------------------------------------------------------------------
858 	// ParseException should produce BadRequest
859 	//-----------------------------------------------------------------------------------------------------------------
860 
861 	@Rest
862 	public static class H {
863 		@RestGet
864 		public void a() throws Exception {
865 			throw new ParseException("Test");
866 		}
867 	}
868 
869 	@Test void h01_parseExceptionCausesBadRequest() throws Exception {
870 		var h = MockRestClient.buildLax(H.class);
871 		h.get("/a")
872 			.run()
873 			.assertStatus(400);
874 	}
875 }