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 testError613()
282 log.info("testError613");
285 requestFactory.setBody(
289 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
290 " \"type\": \"OAuthException\",\n" +
297 clientTemplate.getForObject("ANY", SOME.class);
298 fail("The expected exception was not thrown");
300 catch(RateLimitExceededException e)
302 log.debug("{}", e.toString());
303 assertEquals(new Integer(613), e.getCode());
304 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
305 assertEquals(Type.OAuthException, e.getType());
310 public void testError2200()
312 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
316 clientTemplate.getForObject("ANY", SOME.class);
317 fail("The expected exception was not thrown");
319 catch(CallbackVerificationFailedException e)
321 log.debug("{}", e.toString());
322 assertEquals(new Integer(2200), e.getCode());
323 assertEquals("(#2200) callback verification failed: ", e.getMessage());
324 assertEquals(Type.OAuthException, e.getType());
325 assertEquals("ESLjoZKvPXg", e.getTraceId());
330 public void testUnmappedError()
332 log.info("testUnmappedError");
335 requestFactory.setBody(
339 " \"message\": \"This error does not exist.\",\n" +
340 " \"type\": \"NonexistentTypeException\",\n" +
341 " \"code\": 999999999\n" +
347 clientTemplate.getForObject("ANY", SOME.class);
348 fail("The expected exception was not thrown");
350 catch(GraphApiException e)
352 log.debug("{}", e.toString());
353 assertEquals(new Integer(999999999), e.getCode());
354 assertEquals("This error does not exist.", e.getMessage());
357 Type type = e.getType();
358 log.error("unknown type: {}", type);
359 fail("unmapped type was resolved by enum: " + type);
361 catch (IllegalArgumentException ee) {}
366 public void testUnmappedErrors()
368 log.info("testUnmappedErrors");
371 requestFactory.setBody(
375 " \"message\": null,\n" +
376 " \"type\": \"WhateverTypeException\",\n" +
377 " \"code\": 999999999\n" +
383 clientTemplate.getForObject("ANY", SOME.class);
384 fail("The expected exception was not thrown");
386 catch(UnmappedErrorException e)
388 log.debug("{}", e.toString());
389 assertNull(e.getMessage());
392 Type type = e.getType();
393 log.error("unknown type: {}", type);
394 fail("unmapped type was resolved by enum: " + type);
396 catch (IllegalArgumentException ee) {}
397 assertEquals(new Integer(999999999), e.getCode());
398 assertNull(e.getSubCode());
399 assertNull(e.getUserTitle());
400 assertNull(e.getUserMessage());
401 assertNull(e.getTraceId());
405 fail("A wrong exception was thrown: " + e.toString());
409 requestFactory.setBody(
413 " \"type\": \"WhateverTypeException\",\n" +
414 " \"code\": 999999999\n" +
420 clientTemplate.getForObject("ANY", SOME.class);
421 fail("The expected exception was not thrown");
423 catch(UnmappedErrorException e)
425 log.debug("{}", e.toString());
426 assertNull(e.getMessage());
429 Type type = e.getType();
430 log.error("unknown type: {}", type);
431 fail("unmapped type was resolved by enum: " + type);
433 catch (IllegalArgumentException ee) {}
434 assertEquals(new Integer(999999999), e.getCode());
435 assertNull(e.getSubCode());
436 assertNull(e.getUserTitle());
437 assertNull(e.getUserMessage());
438 assertNull(e.getTraceId());
442 fail("A wrong exception was thrown: " + e.toString());
446 requestFactory.setBody(
450 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
451 " \"type\": null,\n" +
452 " \"code\": 999999999\n" +
458 clientTemplate.getForObject("ANY", SOME.class);
459 fail("The expected exception was not thrown");
461 catch(UnmappedErrorException e)
463 log.debug("{}", e.toString());
464 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
465 assertNull(e.getType());
466 assertEquals(new Integer(999999999), e.getCode());
467 assertNull(e.getSubCode());
468 assertNull(e.getUserTitle());
469 assertNull(e.getUserMessage());
470 assertNull(e.getTraceId());
474 fail("A wrong exception was thrown: " + e.toString());
478 requestFactory.setBody(
482 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
483 " \"code\": 999999999\n" +
489 clientTemplate.getForObject("ANY", SOME.class);
490 fail("The expected exception was not thrown");
492 catch(UnmappedErrorException e)
494 log.debug("{}", e.toString());
495 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
496 assertNull(e.getType());
497 assertEquals(new Integer(999999999), e.getCode());
498 assertNull(e.getSubCode());
499 assertNull(e.getUserTitle());
500 assertNull(e.getUserMessage());
501 assertNull(e.getTraceId());
505 fail("A wrong exception was thrown: " + e.toString());
510 public void testInvlalidErrors()
512 log.info("testInvalidErrors");
515 requestFactory.setBody(
519 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
520 " \"type\": \"Whatever\",\n" +
521 " \"code\": \"some string\"\n" +
527 clientTemplate.getForObject("ANY", SOME.class);
528 fail("The expected exception was not thrown");
530 catch(HttpClientErrorException e)
532 log.debug("{}", e.toString());
536 fail("A wrong exception was thrown: " + e.toString());
540 requestFactory.setBody(
544 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
545 " \"type\": \"Whatever\",\n" +
552 clientTemplate.getForObject("ANY", SOME.class);
553 fail("The expected exception was not thrown");
555 catch(HttpClientErrorException e)
557 log.debug("{}", e.toString());
561 fail("A wrong exception was thrown: " + e.toString());
565 requestFactory.setBody(
569 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
570 " \"type\": \"Whatever\",\n" +
571 " \"code\": null\n" +
577 clientTemplate.getForObject("ANY", SOME.class);
578 fail("The expected exception was not thrown");
580 catch(HttpClientErrorException e)
582 log.debug("{}", e.toString());
586 fail("A wrong exception was thrown: " + e.toString());
590 requestFactory.setBody(
594 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
595 " \"type\": \"Whatever\"\n" +
601 clientTemplate.getForObject("ANY", SOME.class);
602 fail("The expected exception was not thrown");
604 catch(HttpClientErrorException e)
606 log.debug("{}", e.toString());
610 fail("A wrong exception was thrown: " + e.toString());
614 requestFactory.setBody("{\"error\":{\"message\":null}}");
618 clientTemplate.getForObject("ANY", SOME.class);
619 fail("The expected exception was not thrown");
621 catch(HttpClientErrorException e)
623 log.debug("{}", e.toString());
627 fail("A wrong exception was thrown: " + e.toString());
631 requestFactory.setBody("{\"error\":{\"type\":null}}");
635 clientTemplate.getForObject("ANY", SOME.class);
636 fail("The expected exception was not thrown");
638 catch(HttpClientErrorException e)
640 log.debug("{}", e.toString());
644 fail("A wrong exception was thrown: " + e.toString());
648 requestFactory.setBody("{\"error\":{\"code\":null}}");
652 clientTemplate.getForObject("ANY", SOME.class);
653 fail("The expected exception was not thrown");
655 catch(HttpClientErrorException e)
657 log.debug("{}", e.toString());
661 fail("A wrong exception was thrown: " + e.toString());
665 requestFactory.setBody("{\"error\":{}}");
669 clientTemplate.getForObject("ANY", SOME.class);
670 fail("The expected exception was not thrown");
672 catch(HttpClientErrorException e)
674 log.debug("{}", e.toString());
678 fail("A wrong exception was thrown: " + e.toString());
682 requestFactory.setBody("{\"error\":\"some message\"}");
686 clientTemplate.getForObject("ANY", SOME.class);
687 fail("The expected exception was not thrown");
689 catch(HttpClientErrorException e)
691 log.debug("{}", e.toString());
695 fail("A wrong exception was thrown: " + e.toString());
699 requestFactory.setBody("{\"error\":null}");
703 clientTemplate.getForObject("ANY", SOME.class);
704 fail("The expected exception was not thrown");
706 catch(HttpClientErrorException e)
708 log.debug("{}", e.toString());
712 fail("A wrong exception was thrown: " + e.toString());
716 requestFactory.setBody("{\"some filed\":\"some message\"}");
720 clientTemplate.getForObject("ANY", SOME.class);
721 fail("The expected exception was not thrown");
723 catch(HttpClientErrorException e)
725 log.debug("{}", e.toString());
729 fail("A wrong exception was thrown: " + e.toString());
733 requestFactory.setBody("{}");
737 clientTemplate.getForObject("ANY", SOME.class);
738 fail("The expected exception was not thrown");
740 catch(HttpClientErrorException e)
742 log.debug("{}", e.toString());
746 fail("A wrong exception was thrown: " + e.toString());
750 requestFactory.setBody("");
754 clientTemplate.getForObject("ANY", SOME.class);
755 fail("The expected exception was not thrown");
757 catch(HttpClientErrorException e)
759 log.debug("{}", e.toString());
763 fail("A wrong exception was thrown: " + e.toString());
771 requestFactory = new MockClientHttpRequestFactory();
772 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
773 requestFactory.addHeader("Content-Type", "application/json");
775 clientTemplate = new RestTemplate();
776 clientTemplate.setRequestFactory(requestFactory);
777 clientTemplate.setErrorHandler(
778 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())