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 testError613()
260 log.info("testError613");
263 requestFactory.setBody(
267 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
268 " \"type\": \"OAuthException\",\n" +
275 clientTemplate.getForObject("ANY", SOME.class);
276 fail("The expected exception was not thrown");
278 catch(RateLimitExceededException e)
280 log.debug("{}", e.toString());
281 assertEquals(new Integer(613), e.getCode());
282 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
283 assertEquals(Type.OAuthException, e.getType());
288 public void testError2200()
290 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
294 clientTemplate.getForObject("ANY", SOME.class);
295 fail("The expected exception was not thrown");
297 catch(CallbackVerificationFailedException e)
299 log.debug("{}", e.toString());
300 assertEquals(new Integer(2200), e.getCode());
301 assertEquals("(#2200) callback verification failed: ", e.getMessage());
302 assertEquals(Type.OAuthException, e.getType());
303 assertEquals("ESLjoZKvPXg", e.getTraceId());
308 public void testUnmappedError()
310 log.info("testUnmappedError");
313 requestFactory.setBody(
317 " \"message\": \"This error does not exist.\",\n" +
318 " \"type\": \"NonexistentTypeException\",\n" +
319 " \"code\": 999999999\n" +
325 clientTemplate.getForObject("ANY", SOME.class);
326 fail("The expected exception was not thrown");
328 catch(GraphApiException e)
330 log.debug("{}", e.toString());
331 assertEquals(new Integer(999999999), e.getCode());
332 assertEquals("This error does not exist.", e.getMessage());
335 Type type = e.getType();
336 log.error("unknown type: {}", type);
337 fail("unmapped type was resolved by enum: " + type);
339 catch (IllegalArgumentException ee) {}
344 public void testUnmappedErrors()
346 log.info("testUnmappedErrors");
349 requestFactory.setBody(
353 " \"message\": null,\n" +
354 " \"type\": \"WhateverTypeException\",\n" +
355 " \"code\": 999999999\n" +
361 clientTemplate.getForObject("ANY", SOME.class);
362 fail("The expected exception was not thrown");
364 catch(UnmappedErrorException e)
366 log.debug("{}", e.toString());
367 assertNull(e.getMessage());
370 Type type = e.getType();
371 log.error("unknown type: {}", type);
372 fail("unmapped type was resolved by enum: " + type);
374 catch (IllegalArgumentException ee) {}
375 assertEquals(new Integer(999999999), e.getCode());
376 assertNull(e.getSubCode());
377 assertNull(e.getUserTitle());
378 assertNull(e.getUserMessage());
379 assertNull(e.getTraceId());
383 fail("A wrong exception was thrown: " + e.toString());
387 requestFactory.setBody(
391 " \"type\": \"WhateverTypeException\",\n" +
392 " \"code\": 999999999\n" +
398 clientTemplate.getForObject("ANY", SOME.class);
399 fail("The expected exception was not thrown");
401 catch(UnmappedErrorException e)
403 log.debug("{}", e.toString());
404 assertNull(e.getMessage());
407 Type type = e.getType();
408 log.error("unknown type: {}", type);
409 fail("unmapped type was resolved by enum: " + type);
411 catch (IllegalArgumentException ee) {}
412 assertEquals(new Integer(999999999), e.getCode());
413 assertNull(e.getSubCode());
414 assertNull(e.getUserTitle());
415 assertNull(e.getUserMessage());
416 assertNull(e.getTraceId());
420 fail("A wrong exception was thrown: " + e.toString());
424 requestFactory.setBody(
428 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
429 " \"type\": null,\n" +
430 " \"code\": 999999999\n" +
436 clientTemplate.getForObject("ANY", SOME.class);
437 fail("The expected exception was not thrown");
439 catch(UnmappedErrorException e)
441 log.debug("{}", e.toString());
442 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
443 assertNull(e.getType());
444 assertEquals(new Integer(999999999), e.getCode());
445 assertNull(e.getSubCode());
446 assertNull(e.getUserTitle());
447 assertNull(e.getUserMessage());
448 assertNull(e.getTraceId());
452 fail("A wrong exception was thrown: " + e.toString());
456 requestFactory.setBody(
460 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
461 " \"code\": 999999999\n" +
467 clientTemplate.getForObject("ANY", SOME.class);
468 fail("The expected exception was not thrown");
470 catch(UnmappedErrorException e)
472 log.debug("{}", e.toString());
473 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
474 assertNull(e.getType());
475 assertEquals(new Integer(999999999), e.getCode());
476 assertNull(e.getSubCode());
477 assertNull(e.getUserTitle());
478 assertNull(e.getUserMessage());
479 assertNull(e.getTraceId());
483 fail("A wrong exception was thrown: " + e.toString());
488 public void testInvlalidErrors()
490 log.info("testInvalidErrors");
493 requestFactory.setBody(
497 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
498 " \"type\": \"Whatever\",\n" +
499 " \"code\": \"some string\"\n" +
505 clientTemplate.getForObject("ANY", SOME.class);
506 fail("The expected exception was not thrown");
508 catch(HttpClientErrorException e)
510 log.debug("{}", e.toString());
514 fail("A wrong exception was thrown: " + e.toString());
518 requestFactory.setBody(
522 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
523 " \"type\": \"Whatever\",\n" +
530 clientTemplate.getForObject("ANY", SOME.class);
531 fail("The expected exception was not thrown");
533 catch(HttpClientErrorException e)
535 log.debug("{}", e.toString());
539 fail("A wrong exception was thrown: " + e.toString());
543 requestFactory.setBody(
547 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
548 " \"type\": \"Whatever\",\n" +
549 " \"code\": null\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" +
579 clientTemplate.getForObject("ANY", SOME.class);
580 fail("The expected exception was not thrown");
582 catch(HttpClientErrorException e)
584 log.debug("{}", e.toString());
588 fail("A wrong exception was thrown: " + e.toString());
592 requestFactory.setBody("{\"error\":{\"message\":null}}");
596 clientTemplate.getForObject("ANY", SOME.class);
597 fail("The expected exception was not thrown");
599 catch(HttpClientErrorException e)
601 log.debug("{}", e.toString());
605 fail("A wrong exception was thrown: " + e.toString());
609 requestFactory.setBody("{\"error\":{\"type\":null}}");
613 clientTemplate.getForObject("ANY", SOME.class);
614 fail("The expected exception was not thrown");
616 catch(HttpClientErrorException e)
618 log.debug("{}", e.toString());
622 fail("A wrong exception was thrown: " + e.toString());
626 requestFactory.setBody("{\"error\":{\"code\":null}}");
630 clientTemplate.getForObject("ANY", SOME.class);
631 fail("The expected exception was not thrown");
633 catch(HttpClientErrorException e)
635 log.debug("{}", e.toString());
639 fail("A wrong exception was thrown: " + e.toString());
643 requestFactory.setBody("{\"error\":{}}");
647 clientTemplate.getForObject("ANY", SOME.class);
648 fail("The expected exception was not thrown");
650 catch(HttpClientErrorException e)
652 log.debug("{}", e.toString());
656 fail("A wrong exception was thrown: " + e.toString());
660 requestFactory.setBody("{\"error\":\"some message\"}");
664 clientTemplate.getForObject("ANY", SOME.class);
665 fail("The expected exception was not thrown");
667 catch(HttpClientErrorException e)
669 log.debug("{}", e.toString());
673 fail("A wrong exception was thrown: " + e.toString());
677 requestFactory.setBody("{\"error\":null}");
681 clientTemplate.getForObject("ANY", SOME.class);
682 fail("The expected exception was not thrown");
684 catch(HttpClientErrorException e)
686 log.debug("{}", e.toString());
690 fail("A wrong exception was thrown: " + e.toString());
694 requestFactory.setBody("{\"some filed\":\"some message\"}");
698 clientTemplate.getForObject("ANY", SOME.class);
699 fail("The expected exception was not thrown");
701 catch(HttpClientErrorException e)
703 log.debug("{}", e.toString());
707 fail("A wrong exception was thrown: " + e.toString());
711 requestFactory.setBody("{}");
715 clientTemplate.getForObject("ANY", SOME.class);
716 fail("The expected exception was not thrown");
718 catch(HttpClientErrorException e)
720 log.debug("{}", e.toString());
724 fail("A wrong exception was thrown: " + e.toString());
728 requestFactory.setBody("");
732 clientTemplate.getForObject("ANY", SOME.class);
733 fail("The expected exception was not thrown");
735 catch(HttpClientErrorException e)
737 log.debug("{}", e.toString());
741 fail("A wrong exception was thrown: " + e.toString());
749 requestFactory = new MockClientHttpRequestFactory();
750 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
751 requestFactory.addHeader("Content-Type", "application/json");
753 clientTemplate = new RestTemplate();
754 clientTemplate.setRequestFactory(requestFactory);
755 clientTemplate.setErrorHandler(
756 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())