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 testError21()
94 log.info("testError21");
97 requestFactory.setBody(
101 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
102 " \"type\": \"OAuthException\",\n" +
109 clientTemplate.getForObject("ANY", SOME.class);
110 fail("The expected exception was not thrown");
112 catch(PageMigratedException e)
114 log.debug("{}", e.toString());
115 assertEquals(new Integer(21), e.getCode());
116 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
117 assertEquals(Type.OAuthException, e.getType());
122 public void testError100()
124 log.info("testError100");
127 requestFactory.setBody(
131 " \"message\": \"Unsupported get request.\",\n" +
132 " \"type\": \"GraphMethodException\",\n" +
139 clientTemplate.getForObject("ANY", SOME.class);
140 fail("The expected exception was not thrown");
142 catch(UnsupportedGetRequestException e)
144 log.debug("{}", e.toString());
145 assertEquals(new Integer(100), e.getCode());
146 assertEquals("Unsupported get request.", e.getMessage());
147 assertEquals(Type.GraphMethodException, e.getType());
152 public void testError102()
154 log.info("testError102");
156 requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
160 clientTemplate.getForObject("ANY", SOME.class);
161 fail("The expected exception was not thrown");
163 catch(UserAccessTokenRequiredException e)
165 log.debug("{}", e.toString());
166 assertEquals(new Integer(102), e.getCode());
167 assertEquals("A user access token is required to request this resource.", e.getMessage());
168 assertEquals(Type.OAuthException, e.getType());
169 assertEquals("DhdMyf23Ki7", e.getTraceId());
174 public void testError104()
176 log.info("testError104");
178 requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
182 clientTemplate.getForObject("ANY", SOME.class);
183 fail("The expected exception was not thrown");
185 catch(AccessTokenRequiredException e)
187 log.debug("{}", e.toString());
188 assertEquals(new Integer(104), e.getCode());
189 assertEquals("An access token is required to request this resource.", e.getMessage());
190 assertEquals(Type.OAuthException, e.getType());
191 assertEquals("E2Jjkj5++LL", e.getTraceId());
196 public void testError613()
198 log.info("testError613");
201 requestFactory.setBody(
205 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
206 " \"type\": \"OAuthException\",\n" +
213 clientTemplate.getForObject("ANY", SOME.class);
214 fail("The expected exception was not thrown");
216 catch(RateLimitExceededException e)
218 log.debug("{}", e.toString());
219 assertEquals(new Integer(613), e.getCode());
220 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
221 assertEquals(Type.OAuthException, e.getType());
226 public void testError2200()
228 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
232 clientTemplate.getForObject("ANY", SOME.class);
233 fail("The expected exception was not thrown");
235 catch(CallbackVerificationFailedException e)
237 log.debug("{}", e.toString());
238 assertEquals(new Integer(2200), e.getCode());
239 assertEquals("(#2200) callback verification failed: ", e.getMessage());
240 assertEquals(Type.OAuthException, e.getType());
241 assertEquals("ESLjoZKvPXg", e.getTraceId());
246 public void testUnmappedError()
248 log.info("testUnmappedError");
251 requestFactory.setBody(
255 " \"message\": \"This error does not exist.\",\n" +
256 " \"type\": \"NonexistentTypeException\",\n" +
257 " \"code\": 999999999\n" +
263 clientTemplate.getForObject("ANY", SOME.class);
264 fail("The expected exception was not thrown");
266 catch(GraphApiException e)
268 log.debug("{}", e.toString());
269 assertEquals(new Integer(999999999), e.getCode());
270 assertEquals("This error does not exist.", e.getMessage());
273 Type type = e.getType();
274 log.error("unknown type: {}", type);
275 fail("unmapped type was resolved by enum: " + type);
277 catch (IllegalArgumentException ee) {}
282 public void testUnmappedErrors()
284 log.info("testUnmappedErrors");
287 requestFactory.setBody(
291 " \"message\": null,\n" +
292 " \"type\": \"WhateverTypeException\",\n" +
293 " \"code\": 999999999\n" +
299 clientTemplate.getForObject("ANY", SOME.class);
300 fail("The expected exception was not thrown");
302 catch(UnmappedErrorException e)
304 log.debug("{}", e.toString());
305 assertNull(e.getMessage());
308 Type type = e.getType();
309 log.error("unknown type: {}", type);
310 fail("unmapped type was resolved by enum: " + type);
312 catch (IllegalArgumentException ee) {}
313 assertEquals(new Integer(999999999), e.getCode());
314 assertNull(e.getSubCode());
315 assertNull(e.getUserTitle());
316 assertNull(e.getUserMessage());
317 assertNull(e.getTraceId());
321 fail("A wrong exception was thrown: " + e.toString());
325 requestFactory.setBody(
329 " \"type\": \"WhateverTypeException\",\n" +
330 " \"code\": 999999999\n" +
336 clientTemplate.getForObject("ANY", SOME.class);
337 fail("The expected exception was not thrown");
339 catch(UnmappedErrorException e)
341 log.debug("{}", e.toString());
342 assertNull(e.getMessage());
345 Type type = e.getType();
346 log.error("unknown type: {}", type);
347 fail("unmapped type was resolved by enum: " + type);
349 catch (IllegalArgumentException ee) {}
350 assertEquals(new Integer(999999999), e.getCode());
351 assertNull(e.getSubCode());
352 assertNull(e.getUserTitle());
353 assertNull(e.getUserMessage());
354 assertNull(e.getTraceId());
358 fail("A wrong exception was thrown: " + e.toString());
362 requestFactory.setBody(
366 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
367 " \"type\": null,\n" +
368 " \"code\": 999999999\n" +
374 clientTemplate.getForObject("ANY", SOME.class);
375 fail("The expected exception was not thrown");
377 catch(UnmappedErrorException e)
379 log.debug("{}", e.toString());
380 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
381 assertNull(e.getType());
382 assertEquals(new Integer(999999999), e.getCode());
383 assertNull(e.getSubCode());
384 assertNull(e.getUserTitle());
385 assertNull(e.getUserMessage());
386 assertNull(e.getTraceId());
390 fail("A wrong exception was thrown: " + e.toString());
394 requestFactory.setBody(
398 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
399 " \"code\": 999999999\n" +
405 clientTemplate.getForObject("ANY", SOME.class);
406 fail("The expected exception was not thrown");
408 catch(UnmappedErrorException e)
410 log.debug("{}", e.toString());
411 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
412 assertNull(e.getType());
413 assertEquals(new Integer(999999999), e.getCode());
414 assertNull(e.getSubCode());
415 assertNull(e.getUserTitle());
416 assertNull(e.getUserMessage());
417 assertNull(e.getTraceId());
421 fail("A wrong exception was thrown: " + e.toString());
426 public void testInvlalidErrors()
428 log.info("testInvalidErrors");
431 requestFactory.setBody(
435 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
436 " \"type\": \"Whatever\",\n" +
437 " \"code\": \"some string\"\n" +
443 clientTemplate.getForObject("ANY", SOME.class);
444 fail("The expected exception was not thrown");
446 catch(HttpClientErrorException e)
448 log.debug("{}", e.toString());
452 fail("A wrong exception was thrown: " + e.toString());
456 requestFactory.setBody(
460 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
461 " \"type\": \"Whatever\",\n" +
468 clientTemplate.getForObject("ANY", SOME.class);
469 fail("The expected exception was not thrown");
471 catch(HttpClientErrorException e)
473 log.debug("{}", e.toString());
477 fail("A wrong exception was thrown: " + e.toString());
481 requestFactory.setBody(
485 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
486 " \"type\": \"Whatever\",\n" +
487 " \"code\": null\n" +
493 clientTemplate.getForObject("ANY", SOME.class);
494 fail("The expected exception was not thrown");
496 catch(HttpClientErrorException e)
498 log.debug("{}", e.toString());
502 fail("A wrong exception was thrown: " + e.toString());
506 requestFactory.setBody(
510 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
511 " \"type\": \"Whatever\"\n" +
517 clientTemplate.getForObject("ANY", SOME.class);
518 fail("The expected exception was not thrown");
520 catch(HttpClientErrorException e)
522 log.debug("{}", e.toString());
526 fail("A wrong exception was thrown: " + e.toString());
530 requestFactory.setBody("{\"error\":{\"message\":null}}");
534 clientTemplate.getForObject("ANY", SOME.class);
535 fail("The expected exception was not thrown");
537 catch(HttpClientErrorException e)
539 log.debug("{}", e.toString());
543 fail("A wrong exception was thrown: " + e.toString());
547 requestFactory.setBody("{\"error\":{\"type\":null}}");
551 clientTemplate.getForObject("ANY", SOME.class);
552 fail("The expected exception was not thrown");
554 catch(HttpClientErrorException e)
556 log.debug("{}", e.toString());
560 fail("A wrong exception was thrown: " + e.toString());
564 requestFactory.setBody("{\"error\":{\"code\":null}}");
568 clientTemplate.getForObject("ANY", SOME.class);
569 fail("The expected exception was not thrown");
571 catch(HttpClientErrorException e)
573 log.debug("{}", e.toString());
577 fail("A wrong exception was thrown: " + e.toString());
581 requestFactory.setBody("{\"error\":{}}");
585 clientTemplate.getForObject("ANY", SOME.class);
586 fail("The expected exception was not thrown");
588 catch(HttpClientErrorException e)
590 log.debug("{}", e.toString());
594 fail("A wrong exception was thrown: " + e.toString());
598 requestFactory.setBody("{\"error\":\"some message\"}");
602 clientTemplate.getForObject("ANY", SOME.class);
603 fail("The expected exception was not thrown");
605 catch(HttpClientErrorException e)
607 log.debug("{}", e.toString());
611 fail("A wrong exception was thrown: " + e.toString());
615 requestFactory.setBody("{\"error\":null}");
619 clientTemplate.getForObject("ANY", SOME.class);
620 fail("The expected exception was not thrown");
622 catch(HttpClientErrorException e)
624 log.debug("{}", e.toString());
628 fail("A wrong exception was thrown: " + e.toString());
632 requestFactory.setBody("{\"some filed\":\"some message\"}");
636 clientTemplate.getForObject("ANY", SOME.class);
637 fail("The expected exception was not thrown");
639 catch(HttpClientErrorException e)
641 log.debug("{}", e.toString());
645 fail("A wrong exception was thrown: " + e.toString());
649 requestFactory.setBody("{}");
653 clientTemplate.getForObject("ANY", SOME.class);
654 fail("The expected exception was not thrown");
656 catch(HttpClientErrorException e)
658 log.debug("{}", e.toString());
662 fail("A wrong exception was thrown: " + e.toString());
666 requestFactory.setBody("");
670 clientTemplate.getForObject("ANY", SOME.class);
671 fail("The expected exception was not thrown");
673 catch(HttpClientErrorException e)
675 log.debug("{}", e.toString());
679 fail("A wrong exception was thrown: " + e.toString());
687 requestFactory = new MockClientHttpRequestFactory();
688 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
689 requestFactory.addHeader("Content-Type", "application/json");
691 clientTemplate = new RestTemplate();
692 clientTemplate.setRequestFactory(requestFactory);
693 clientTemplate.setErrorHandler(
694 new GraphApiErrorHandler(clientTemplate.getErrorHandler())