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 testError104()
154 log.info("testError104");
156 requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
160 clientTemplate.getForObject("ANY", SOME.class);
161 fail("The expected exception was not thrown");
163 catch(AccessTokenRequiredException e)
165 log.debug("{}", e.toString());
166 assertEquals(new Integer(104), e.getCode());
167 assertEquals("An access token is required to request this resource.", e.getMessage());
168 assertEquals(Type.OAuthException, e.getType());
169 assertEquals("E2Jjkj5++LL", e.getTraceId());
174 public void testError613()
176 log.info("testError613");
179 requestFactory.setBody(
183 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
184 " \"type\": \"OAuthException\",\n" +
191 clientTemplate.getForObject("ANY", SOME.class);
192 fail("The expected exception was not thrown");
194 catch(RateExceededException e)
196 log.debug("{}", e.toString());
197 assertEquals(new Integer(613), e.getCode());
198 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
199 assertEquals(Type.OAuthException, e.getType());
204 public void testError2200()
206 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
210 clientTemplate.getForObject("ANY", SOME.class);
211 fail("The expected exception was not thrown");
213 catch(CallbackVerificationFailedException e)
215 log.debug("{}", e.toString());
216 assertEquals(new Integer(2200), e.getCode());
217 assertEquals("(#2200) callback verification failed: ", e.getMessage());
218 assertEquals(Type.OAuthException, e.getType());
219 assertEquals("ESLjoZKvPXg", e.getTraceId());
224 public void testUnmappedError()
226 log.info("testUnmappedError");
229 requestFactory.setBody(
233 " \"message\": \"This error does not exist.\",\n" +
234 " \"type\": \"NonexistentTypeException\",\n" +
235 " \"code\": 999999999\n" +
241 clientTemplate.getForObject("ANY", SOME.class);
242 fail("The expected exception was not thrown");
244 catch(GraphApiException e)
246 log.debug("{}", e.toString());
247 assertEquals(new Integer(999999999), e.getCode());
248 assertEquals("This error does not exist.", e.getMessage());
251 Type type = e.getType();
252 log.error("unknown type: {}", type);
253 fail("unmapped type was resolved by enum: " + type);
255 catch (IllegalArgumentException ee) {}
260 public void testUnmappedErrors()
262 log.info("testUnmappedErrors");
265 requestFactory.setBody(
269 " \"message\": null,\n" +
270 " \"type\": \"WhateverTypeException\",\n" +
271 " \"code\": 999999999\n" +
277 clientTemplate.getForObject("ANY", SOME.class);
278 fail("The expected exception was not thrown");
280 catch(UnmappedErrorException e)
282 log.debug("{}", e.toString());
283 assertNull(e.getMessage());
286 Type type = e.getType();
287 log.error("unknown type: {}", type);
288 fail("unmapped type was resolved by enum: " + type);
290 catch (IllegalArgumentException ee) {}
291 assertEquals(new Integer(999999999), e.getCode());
292 assertNull(e.getSubCode());
293 assertNull(e.getUserTitle());
294 assertNull(e.getUserMessage());
295 assertNull(e.getTraceId());
299 fail("A wrong exception was thrown: " + e.toString());
303 requestFactory.setBody(
307 " \"type\": \"WhateverTypeException\",\n" +
308 " \"code\": 999999999\n" +
314 clientTemplate.getForObject("ANY", SOME.class);
315 fail("The expected exception was not thrown");
317 catch(UnmappedErrorException e)
319 log.debug("{}", e.toString());
320 assertNull(e.getMessage());
323 Type type = e.getType();
324 log.error("unknown type: {}", type);
325 fail("unmapped type was resolved by enum: " + type);
327 catch (IllegalArgumentException ee) {}
328 assertEquals(new Integer(999999999), e.getCode());
329 assertNull(e.getSubCode());
330 assertNull(e.getUserTitle());
331 assertNull(e.getUserMessage());
332 assertNull(e.getTraceId());
336 fail("A wrong exception was thrown: " + e.toString());
340 requestFactory.setBody(
344 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
345 " \"type\": null,\n" +
346 " \"code\": 999999999\n" +
352 clientTemplate.getForObject("ANY", SOME.class);
353 fail("The expected exception was not thrown");
355 catch(UnmappedErrorException e)
357 log.debug("{}", e.toString());
358 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
359 assertNull(e.getType());
360 assertEquals(new Integer(999999999), e.getCode());
361 assertNull(e.getSubCode());
362 assertNull(e.getUserTitle());
363 assertNull(e.getUserMessage());
364 assertNull(e.getTraceId());
368 fail("A wrong exception was thrown: " + e.toString());
372 requestFactory.setBody(
376 " \"message\": \"An unmapped Graph-API-Exception.\",\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 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
390 assertNull(e.getType());
391 assertEquals(new Integer(999999999), e.getCode());
392 assertNull(e.getSubCode());
393 assertNull(e.getUserTitle());
394 assertNull(e.getUserMessage());
395 assertNull(e.getTraceId());
399 fail("A wrong exception was thrown: " + e.toString());
404 public void testInvlalidErrors()
406 log.info("testInvalidErrors");
409 requestFactory.setBody(
413 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
414 " \"type\": \"Whatever\",\n" +
415 " \"code\": \"some string\"\n" +
421 clientTemplate.getForObject("ANY", SOME.class);
422 fail("The expected exception was not thrown");
424 catch(HttpClientErrorException e)
426 log.debug("{}", e.toString());
430 fail("A wrong exception was thrown: " + e.toString());
434 requestFactory.setBody(
438 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
439 " \"type\": \"Whatever\",\n" +
446 clientTemplate.getForObject("ANY", SOME.class);
447 fail("The expected exception was not thrown");
449 catch(HttpClientErrorException e)
451 log.debug("{}", e.toString());
455 fail("A wrong exception was thrown: " + e.toString());
459 requestFactory.setBody(
463 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
464 " \"type\": \"Whatever\",\n" +
465 " \"code\": null\n" +
471 clientTemplate.getForObject("ANY", SOME.class);
472 fail("The expected exception was not thrown");
474 catch(HttpClientErrorException e)
476 log.debug("{}", e.toString());
480 fail("A wrong exception was thrown: " + e.toString());
484 requestFactory.setBody(
488 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
489 " \"type\": \"Whatever\"\n" +
495 clientTemplate.getForObject("ANY", SOME.class);
496 fail("The expected exception was not thrown");
498 catch(HttpClientErrorException e)
500 log.debug("{}", e.toString());
504 fail("A wrong exception was thrown: " + e.toString());
508 requestFactory.setBody("{\"error\":{\"message\":null}}");
512 clientTemplate.getForObject("ANY", SOME.class);
513 fail("The expected exception was not thrown");
515 catch(HttpClientErrorException e)
517 log.debug("{}", e.toString());
521 fail("A wrong exception was thrown: " + e.toString());
525 requestFactory.setBody("{\"error\":{\"type\":null}}");
529 clientTemplate.getForObject("ANY", SOME.class);
530 fail("The expected exception was not thrown");
532 catch(HttpClientErrorException e)
534 log.debug("{}", e.toString());
538 fail("A wrong exception was thrown: " + e.toString());
542 requestFactory.setBody("{\"error\":{\"code\":null}}");
546 clientTemplate.getForObject("ANY", SOME.class);
547 fail("The expected exception was not thrown");
549 catch(HttpClientErrorException e)
551 log.debug("{}", e.toString());
555 fail("A wrong exception was thrown: " + e.toString());
559 requestFactory.setBody("{\"error\":{}}");
563 clientTemplate.getForObject("ANY", SOME.class);
564 fail("The expected exception was not thrown");
566 catch(HttpClientErrorException e)
568 log.debug("{}", e.toString());
572 fail("A wrong exception was thrown: " + e.toString());
576 requestFactory.setBody("{\"error\":\"some message\"}");
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("{\"error\":null}");
597 clientTemplate.getForObject("ANY", SOME.class);
598 fail("The expected exception was not thrown");
600 catch(HttpClientErrorException e)
602 log.debug("{}", e.toString());
606 fail("A wrong exception was thrown: " + e.toString());
610 requestFactory.setBody("{\"some filed\":\"some message\"}");
614 clientTemplate.getForObject("ANY", SOME.class);
615 fail("The expected exception was not thrown");
617 catch(HttpClientErrorException e)
619 log.debug("{}", e.toString());
623 fail("A wrong exception was thrown: " + e.toString());
627 requestFactory.setBody("{}");
631 clientTemplate.getForObject("ANY", SOME.class);
632 fail("The expected exception was not thrown");
634 catch(HttpClientErrorException e)
636 log.debug("{}", e.toString());
640 fail("A wrong exception was thrown: " + e.toString());
644 requestFactory.setBody("");
648 clientTemplate.getForObject("ANY", SOME.class);
649 fail("The expected exception was not thrown");
651 catch(HttpClientErrorException e)
653 log.debug("{}", e.toString());
657 fail("A wrong exception was thrown: " + e.toString());
665 requestFactory = new MockClientHttpRequestFactory();
666 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
667 requestFactory.addHeader("Content-Type", "application/json");
669 clientTemplate = new RestTemplate();
670 clientTemplate.setRequestFactory(requestFactory);
671 clientTemplate.setErrorHandler(
672 new GraphApiErrorHandler(clientTemplate.getErrorHandler())