1 package de.juplo.facebook.errors;
4 import de.juplo.facebook.errors.GraphApiException.Type;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.fail;
8 import org.junit.Before;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.web.client.HttpClientErrorException;
14 import org.springframework.web.client.RestTemplate;
22 public class GraphApiErrorHandlerTest
24 private static final Logger log =
25 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
27 private RestTemplate clientTemplate;
28 private MockClientHttpRequestFactory requestFactory;
32 public void testError1()
34 log.info("testError1");
37 requestFactory.setBody(
41 " \"message\": \"An unknown error has occurred.\",\n" +
42 " \"type\": \"OAuthException\",\n" +
49 clientTemplate.getForObject("ANY", SOME.class);
50 fail("The expected exception was not thrown");
52 catch(UnknownErrorException e)
54 log.debug("{}", e.toString());
55 assertEquals(new Integer(1), e.getCode());
56 assertEquals("An unknown error has occurred.", e.getMessage());
57 assertEquals(Type.OAuthException, e.getType());
62 public void testError2()
64 log.info("testError2");
67 requestFactory.setBody(
71 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
72 " \"type\": \"OAuthException\",\n" +
79 clientTemplate.getForObject("ANY", SOME.class);
80 fail("The expected exception was not thrown");
82 catch(UnexpectedErrorException e)
84 log.debug("{}", e.toString());
85 assertEquals(new Integer(2), e.getCode());
86 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
87 assertEquals(Type.OAuthException, e.getType());
92 public void testError4()
94 log.info("testError4");
97 requestFactory.setBody(
101 " \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
102 " \"is_transient\": true, \n" +
103 " \"message\": \"(#4) Application request limit reached\", \n" +
104 " \"type\": \"OAuthException\"\n" +
110 clientTemplate.getForObject("ANY", SOME.class);
111 fail("The expected exception was not thrown");
113 catch(ApplicationRequestLimitReachedException e)
115 log.debug("{}", e.toString());
116 assertEquals(new Integer(4), e.getCode());
117 assertEquals("(#4) Application request limit reached", e.getMessage());
118 assertEquals(Type.OAuthException, e.getType());
123 public void testError12()
125 log.info("testError12");
128 requestFactory.setBody(
132 " \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
133 " \"type\": \"OAuthException\",\n" +
135 " \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
141 clientTemplate.getForObject("ANY", SOME.class);
142 fail("The expected exception was not thrown");
144 catch(DeprecatedException e)
146 log.debug("{}", e.toString());
147 assertEquals(new Integer(12), e.getCode());
148 assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
149 assertEquals(Type.OAuthException, e.getType());
154 public void testError21()
156 log.info("testError21");
159 requestFactory.setBody(
163 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
164 " \"type\": \"OAuthException\",\n" +
171 clientTemplate.getForObject("ANY", SOME.class);
172 fail("The expected exception was not thrown");
174 catch(PageMigratedException e)
176 log.debug("{}", e.toString());
177 assertEquals(new Integer(21), e.getCode());
178 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
179 assertEquals(Type.OAuthException, e.getType());
184 public void testError100()
186 log.info("testError100");
189 requestFactory.setBody(
193 " \"message\": \"Unsupported get request.\",\n" +
194 " \"type\": \"GraphMethodException\",\n" +
201 clientTemplate.getForObject("ANY", SOME.class);
202 fail("The expected exception was not thrown");
204 catch(UnsupportedGetRequestException e)
206 log.debug("{}", e.toString());
207 assertEquals(new Integer(100), e.getCode());
208 assertEquals("Unsupported get request.", e.getMessage());
209 assertEquals(Type.GraphMethodException, e.getType());
214 public void testError102()
216 log.info("testError102");
218 requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
222 clientTemplate.getForObject("ANY", SOME.class);
223 fail("The expected exception was not thrown");
225 catch(UserAccessTokenRequiredException e)
227 log.debug("{}", e.toString());
228 assertEquals(new Integer(102), e.getCode());
229 assertEquals("A user access token is required to request this resource.", e.getMessage());
230 assertEquals(Type.OAuthException, e.getType());
231 assertEquals("DhdMyf23Ki7", e.getTraceId());
236 public void testError104()
238 log.info("testError104");
240 requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
244 clientTemplate.getForObject("ANY", SOME.class);
245 fail("The expected exception was not thrown");
247 catch(AccessTokenRequiredException e)
249 log.debug("{}", e.toString());
250 assertEquals(new Integer(104), e.getCode());
251 assertEquals("An access token is required to request this resource.", e.getMessage());
252 assertEquals(Type.OAuthException, e.getType());
253 assertEquals("E2Jjkj5++LL", e.getTraceId());
258 public void testError190()
260 log.info("testError190");
262 requestFactory.setBody("{\"error\":{\"message\":\"Bad signature\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"Ay2OYQrINbXOCfQpBvoxDIw\"}}");
266 clientTemplate.getForObject("ANY", SOME.class);
267 fail("The expected exception was not thrown");
269 catch(AccessTokenExpiredException e)
271 log.debug("{}", e.toString());
272 assertEquals(new Integer(190), e.getCode());
273 assertEquals("Bad signature", e.getMessage());
274 assertEquals(Type.OAuthException, e.getType());
275 assertEquals("Ay2OYQrINbXOCfQpBvoxDIw", e.getTraceId());
280 public void testError200()
282 log.info("testError200");
284 requestFactory.setBody("{\n" +
286 " \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
287 " \"type\": \"OAuthException\",\n" +
294 clientTemplate.getForObject("ANY", SOME.class);
295 fail("The expected exception was not thrown");
297 catch(ApplicationNotAuthorizedByUserException e)
299 log.debug("{}", e.toString());
300 assertEquals(new Integer(200), e.getCode());
301 assertEquals("(#200) The user hasn't authorized the application to perform this action", e.getMessage());
302 assertEquals(Type.OAuthException, e.getType());
303 assertNull(e.getTraceId());
308 public void testError613()
310 log.info("testError613");
313 requestFactory.setBody(
317 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
318 " \"type\": \"OAuthException\",\n" +
325 clientTemplate.getForObject("ANY", SOME.class);
326 fail("The expected exception was not thrown");
328 catch(RateLimitExceededException e)
330 log.debug("{}", e.toString());
331 assertEquals(new Integer(613), e.getCode());
332 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
333 assertEquals(Type.OAuthException, e.getType());
338 public void testError2200()
340 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
344 clientTemplate.getForObject("ANY", SOME.class);
345 fail("The expected exception was not thrown");
347 catch(CallbackVerificationFailedException e)
349 log.debug("{}", e.toString());
350 assertEquals(new Integer(2200), e.getCode());
351 assertEquals("(#2200) callback verification failed: ", e.getMessage());
352 assertEquals(Type.OAuthException, e.getType());
353 assertEquals("ESLjoZKvPXg", e.getTraceId());
358 public void testUnmappedError()
360 log.info("testUnmappedError");
363 requestFactory.setBody(
367 " \"message\": \"This error does not exist.\",\n" +
368 " \"type\": \"NonexistentTypeException\",\n" +
369 " \"code\": 999999999\n" +
375 clientTemplate.getForObject("ANY", SOME.class);
376 fail("The expected exception was not thrown");
378 catch(GraphApiException e)
380 log.debug("{}", e.toString());
381 assertEquals(new Integer(999999999), e.getCode());
382 assertEquals("This error does not exist.", e.getMessage());
385 Type type = e.getType();
386 log.error("unknown type: {}", type);
387 fail("unmapped type was resolved by enum: " + type);
389 catch (IllegalArgumentException ee) {}
394 public void testUnmappedErrors()
396 log.info("testUnmappedErrors");
399 requestFactory.setBody(
403 " \"message\": null,\n" +
404 " \"type\": \"WhateverTypeException\",\n" +
405 " \"code\": 999999999\n" +
411 clientTemplate.getForObject("ANY", SOME.class);
412 fail("The expected exception was not thrown");
414 catch(UnmappedErrorException e)
416 log.debug("{}", e.toString());
417 assertNull(e.getMessage());
420 Type type = e.getType();
421 log.error("unknown type: {}", type);
422 fail("unmapped type was resolved by enum: " + type);
424 catch (IllegalArgumentException ee) {}
425 assertEquals(new Integer(999999999), e.getCode());
426 assertNull(e.getSubCode());
427 assertNull(e.getUserTitle());
428 assertNull(e.getUserMessage());
429 assertNull(e.getTraceId());
433 fail("A wrong exception was thrown: " + e.toString());
437 requestFactory.setBody(
441 " \"type\": \"WhateverTypeException\",\n" +
442 " \"code\": 999999999\n" +
448 clientTemplate.getForObject("ANY", SOME.class);
449 fail("The expected exception was not thrown");
451 catch(UnmappedErrorException e)
453 log.debug("{}", e.toString());
454 assertNull(e.getMessage());
457 Type type = e.getType();
458 log.error("unknown type: {}", type);
459 fail("unmapped type was resolved by enum: " + type);
461 catch (IllegalArgumentException ee) {}
462 assertEquals(new Integer(999999999), e.getCode());
463 assertNull(e.getSubCode());
464 assertNull(e.getUserTitle());
465 assertNull(e.getUserMessage());
466 assertNull(e.getTraceId());
470 fail("A wrong exception was thrown: " + e.toString());
474 requestFactory.setBody(
478 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
479 " \"type\": null,\n" +
480 " \"code\": 999999999\n" +
486 clientTemplate.getForObject("ANY", SOME.class);
487 fail("The expected exception was not thrown");
489 catch(UnmappedErrorException e)
491 log.debug("{}", e.toString());
492 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
493 assertNull(e.getType());
494 assertEquals(new Integer(999999999), e.getCode());
495 assertNull(e.getSubCode());
496 assertNull(e.getUserTitle());
497 assertNull(e.getUserMessage());
498 assertNull(e.getTraceId());
502 fail("A wrong exception was thrown: " + e.toString());
506 requestFactory.setBody(
510 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
511 " \"code\": 999999999\n" +
517 clientTemplate.getForObject("ANY", SOME.class);
518 fail("The expected exception was not thrown");
520 catch(UnmappedErrorException e)
522 log.debug("{}", e.toString());
523 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
524 assertNull(e.getType());
525 assertEquals(new Integer(999999999), e.getCode());
526 assertNull(e.getSubCode());
527 assertNull(e.getUserTitle());
528 assertNull(e.getUserMessage());
529 assertNull(e.getTraceId());
533 fail("A wrong exception was thrown: " + e.toString());
538 public void testInvlalidErrors()
540 log.info("testInvalidErrors");
543 requestFactory.setBody(
547 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
548 " \"type\": \"Whatever\",\n" +
549 " \"code\": \"some string\"\n" +
555 clientTemplate.getForObject("ANY", SOME.class);
556 fail("The expected exception was not thrown");
558 catch(HttpClientErrorException e)
560 log.debug("{}", e.toString());
564 fail("A wrong exception was thrown: " + e.toString());
568 requestFactory.setBody(
572 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
573 " \"type\": \"Whatever\",\n" +
580 clientTemplate.getForObject("ANY", SOME.class);
581 fail("The expected exception was not thrown");
583 catch(HttpClientErrorException e)
585 log.debug("{}", e.toString());
589 fail("A wrong exception was thrown: " + e.toString());
593 requestFactory.setBody(
597 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
598 " \"type\": \"Whatever\",\n" +
599 " \"code\": null\n" +
605 clientTemplate.getForObject("ANY", SOME.class);
606 fail("The expected exception was not thrown");
608 catch(HttpClientErrorException e)
610 log.debug("{}", e.toString());
614 fail("A wrong exception was thrown: " + e.toString());
618 requestFactory.setBody(
622 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
623 " \"type\": \"Whatever\"\n" +
629 clientTemplate.getForObject("ANY", SOME.class);
630 fail("The expected exception was not thrown");
632 catch(HttpClientErrorException e)
634 log.debug("{}", e.toString());
638 fail("A wrong exception was thrown: " + e.toString());
642 requestFactory.setBody("{\"error\":{\"message\":null}}");
646 clientTemplate.getForObject("ANY", SOME.class);
647 fail("The expected exception was not thrown");
649 catch(HttpClientErrorException e)
651 log.debug("{}", e.toString());
655 fail("A wrong exception was thrown: " + e.toString());
659 requestFactory.setBody("{\"error\":{\"type\":null}}");
663 clientTemplate.getForObject("ANY", SOME.class);
664 fail("The expected exception was not thrown");
666 catch(HttpClientErrorException e)
668 log.debug("{}", e.toString());
672 fail("A wrong exception was thrown: " + e.toString());
676 requestFactory.setBody("{\"error\":{\"code\":null}}");
680 clientTemplate.getForObject("ANY", SOME.class);
681 fail("The expected exception was not thrown");
683 catch(HttpClientErrorException e)
685 log.debug("{}", e.toString());
689 fail("A wrong exception was thrown: " + e.toString());
693 requestFactory.setBody("{\"error\":{}}");
697 clientTemplate.getForObject("ANY", SOME.class);
698 fail("The expected exception was not thrown");
700 catch(HttpClientErrorException e)
702 log.debug("{}", e.toString());
706 fail("A wrong exception was thrown: " + e.toString());
710 requestFactory.setBody("{\"error\":\"some message\"}");
714 clientTemplate.getForObject("ANY", SOME.class);
715 fail("The expected exception was not thrown");
717 catch(HttpClientErrorException e)
719 log.debug("{}", e.toString());
723 fail("A wrong exception was thrown: " + e.toString());
727 requestFactory.setBody("{\"error\":null}");
731 clientTemplate.getForObject("ANY", SOME.class);
732 fail("The expected exception was not thrown");
734 catch(HttpClientErrorException e)
736 log.debug("{}", e.toString());
740 fail("A wrong exception was thrown: " + e.toString());
744 requestFactory.setBody("{\"some filed\":\"some message\"}");
748 clientTemplate.getForObject("ANY", SOME.class);
749 fail("The expected exception was not thrown");
751 catch(HttpClientErrorException e)
753 log.debug("{}", e.toString());
757 fail("A wrong exception was thrown: " + e.toString());
761 requestFactory.setBody("{}");
765 clientTemplate.getForObject("ANY", SOME.class);
766 fail("The expected exception was not thrown");
768 catch(HttpClientErrorException e)
770 log.debug("{}", e.toString());
774 fail("A wrong exception was thrown: " + e.toString());
778 requestFactory.setBody("");
782 clientTemplate.getForObject("ANY", SOME.class);
783 fail("The expected exception was not thrown");
785 catch(HttpClientErrorException e)
787 log.debug("{}", e.toString());
791 fail("A wrong exception was thrown: " + e.toString());
799 requestFactory = new MockClientHttpRequestFactory();
800 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
801 requestFactory.addHeader("Content-Type", "application/json");
803 clientTemplate = new RestTemplate();
804 clientTemplate.setRequestFactory(requestFactory);
805 clientTemplate.setErrorHandler(
806 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())