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 testError12()
94 log.info("testError12");
97 requestFactory.setBody(
101 " \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
102 " \"type\": \"OAuthException\",\n" +
104 " \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
110 clientTemplate.getForObject("ANY", SOME.class);
111 fail("The expected exception was not thrown");
113 catch(DeprecatedException e)
115 log.debug("{}", e.toString());
116 assertEquals(new Integer(12), e.getCode());
117 assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
118 assertEquals(Type.OAuthException, e.getType());
123 public void testError21()
125 log.info("testError21");
128 requestFactory.setBody(
132 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
133 " \"type\": \"OAuthException\",\n" +
140 clientTemplate.getForObject("ANY", SOME.class);
141 fail("The expected exception was not thrown");
143 catch(PageMigratedException e)
145 log.debug("{}", e.toString());
146 assertEquals(new Integer(21), e.getCode());
147 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
148 assertEquals(Type.OAuthException, e.getType());
153 public void testError100()
155 log.info("testError100");
158 requestFactory.setBody(
162 " \"message\": \"Unsupported get request.\",\n" +
163 " \"type\": \"GraphMethodException\",\n" +
170 clientTemplate.getForObject("ANY", SOME.class);
171 fail("The expected exception was not thrown");
173 catch(UnsupportedGetRequestException e)
175 log.debug("{}", e.toString());
176 assertEquals(new Integer(100), e.getCode());
177 assertEquals("Unsupported get request.", e.getMessage());
178 assertEquals(Type.GraphMethodException, e.getType());
183 public void testError102()
185 log.info("testError102");
187 requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
191 clientTemplate.getForObject("ANY", SOME.class);
192 fail("The expected exception was not thrown");
194 catch(UserAccessTokenRequiredException e)
196 log.debug("{}", e.toString());
197 assertEquals(new Integer(102), e.getCode());
198 assertEquals("A user access token is required to request this resource.", e.getMessage());
199 assertEquals(Type.OAuthException, e.getType());
200 assertEquals("DhdMyf23Ki7", e.getTraceId());
205 public void testError104()
207 log.info("testError104");
209 requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
213 clientTemplate.getForObject("ANY", SOME.class);
214 fail("The expected exception was not thrown");
216 catch(AccessTokenRequiredException e)
218 log.debug("{}", e.toString());
219 assertEquals(new Integer(104), e.getCode());
220 assertEquals("An access token is required to request this resource.", e.getMessage());
221 assertEquals(Type.OAuthException, e.getType());
222 assertEquals("E2Jjkj5++LL", e.getTraceId());
227 public void testError613()
229 log.info("testError613");
232 requestFactory.setBody(
236 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
237 " \"type\": \"OAuthException\",\n" +
244 clientTemplate.getForObject("ANY", SOME.class);
245 fail("The expected exception was not thrown");
247 catch(RateLimitExceededException e)
249 log.debug("{}", e.toString());
250 assertEquals(new Integer(613), e.getCode());
251 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
252 assertEquals(Type.OAuthException, e.getType());
257 public void testError2200()
259 requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
263 clientTemplate.getForObject("ANY", SOME.class);
264 fail("The expected exception was not thrown");
266 catch(CallbackVerificationFailedException e)
268 log.debug("{}", e.toString());
269 assertEquals(new Integer(2200), e.getCode());
270 assertEquals("(#2200) callback verification failed: ", e.getMessage());
271 assertEquals(Type.OAuthException, e.getType());
272 assertEquals("ESLjoZKvPXg", e.getTraceId());
277 public void testUnmappedError()
279 log.info("testUnmappedError");
282 requestFactory.setBody(
286 " \"message\": \"This error does not exist.\",\n" +
287 " \"type\": \"NonexistentTypeException\",\n" +
288 " \"code\": 999999999\n" +
294 clientTemplate.getForObject("ANY", SOME.class);
295 fail("The expected exception was not thrown");
297 catch(GraphApiException e)
299 log.debug("{}", e.toString());
300 assertEquals(new Integer(999999999), e.getCode());
301 assertEquals("This error does not exist.", e.getMessage());
304 Type type = e.getType();
305 log.error("unknown type: {}", type);
306 fail("unmapped type was resolved by enum: " + type);
308 catch (IllegalArgumentException ee) {}
313 public void testUnmappedErrors()
315 log.info("testUnmappedErrors");
318 requestFactory.setBody(
322 " \"message\": null,\n" +
323 " \"type\": \"WhateverTypeException\",\n" +
324 " \"code\": 999999999\n" +
330 clientTemplate.getForObject("ANY", SOME.class);
331 fail("The expected exception was not thrown");
333 catch(UnmappedErrorException e)
335 log.debug("{}", e.toString());
336 assertNull(e.getMessage());
339 Type type = e.getType();
340 log.error("unknown type: {}", type);
341 fail("unmapped type was resolved by enum: " + type);
343 catch (IllegalArgumentException ee) {}
344 assertEquals(new Integer(999999999), e.getCode());
345 assertNull(e.getSubCode());
346 assertNull(e.getUserTitle());
347 assertNull(e.getUserMessage());
348 assertNull(e.getTraceId());
352 fail("A wrong exception was thrown: " + e.toString());
356 requestFactory.setBody(
360 " \"type\": \"WhateverTypeException\",\n" +
361 " \"code\": 999999999\n" +
367 clientTemplate.getForObject("ANY", SOME.class);
368 fail("The expected exception was not thrown");
370 catch(UnmappedErrorException e)
372 log.debug("{}", e.toString());
373 assertNull(e.getMessage());
376 Type type = e.getType();
377 log.error("unknown type: {}", type);
378 fail("unmapped type was resolved by enum: " + type);
380 catch (IllegalArgumentException ee) {}
381 assertEquals(new Integer(999999999), e.getCode());
382 assertNull(e.getSubCode());
383 assertNull(e.getUserTitle());
384 assertNull(e.getUserMessage());
385 assertNull(e.getTraceId());
389 fail("A wrong exception was thrown: " + e.toString());
393 requestFactory.setBody(
397 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
398 " \"type\": null,\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());
425 requestFactory.setBody(
429 " \"message\": \"An unmapped Graph-API-Exception.\",\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());
457 public void testInvlalidErrors()
459 log.info("testInvalidErrors");
462 requestFactory.setBody(
466 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
467 " \"type\": \"Whatever\",\n" +
468 " \"code\": \"some string\"\n" +
474 clientTemplate.getForObject("ANY", SOME.class);
475 fail("The expected exception was not thrown");
477 catch(HttpClientErrorException e)
479 log.debug("{}", e.toString());
483 fail("A wrong exception was thrown: " + e.toString());
487 requestFactory.setBody(
491 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
492 " \"type\": \"Whatever\",\n" +
499 clientTemplate.getForObject("ANY", SOME.class);
500 fail("The expected exception was not thrown");
502 catch(HttpClientErrorException e)
504 log.debug("{}", e.toString());
508 fail("A wrong exception was thrown: " + e.toString());
512 requestFactory.setBody(
516 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
517 " \"type\": \"Whatever\",\n" +
518 " \"code\": null\n" +
524 clientTemplate.getForObject("ANY", SOME.class);
525 fail("The expected exception was not thrown");
527 catch(HttpClientErrorException e)
529 log.debug("{}", e.toString());
533 fail("A wrong exception was thrown: " + e.toString());
537 requestFactory.setBody(
541 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
542 " \"type\": \"Whatever\"\n" +
548 clientTemplate.getForObject("ANY", SOME.class);
549 fail("The expected exception was not thrown");
551 catch(HttpClientErrorException e)
553 log.debug("{}", e.toString());
557 fail("A wrong exception was thrown: " + e.toString());
561 requestFactory.setBody("{\"error\":{\"message\":null}}");
565 clientTemplate.getForObject("ANY", SOME.class);
566 fail("The expected exception was not thrown");
568 catch(HttpClientErrorException e)
570 log.debug("{}", e.toString());
574 fail("A wrong exception was thrown: " + e.toString());
578 requestFactory.setBody("{\"error\":{\"type\":null}}");
582 clientTemplate.getForObject("ANY", SOME.class);
583 fail("The expected exception was not thrown");
585 catch(HttpClientErrorException e)
587 log.debug("{}", e.toString());
591 fail("A wrong exception was thrown: " + e.toString());
595 requestFactory.setBody("{\"error\":{\"code\":null}}");
599 clientTemplate.getForObject("ANY", SOME.class);
600 fail("The expected exception was not thrown");
602 catch(HttpClientErrorException e)
604 log.debug("{}", e.toString());
608 fail("A wrong exception was thrown: " + e.toString());
612 requestFactory.setBody("{\"error\":{}}");
616 clientTemplate.getForObject("ANY", SOME.class);
617 fail("The expected exception was not thrown");
619 catch(HttpClientErrorException e)
621 log.debug("{}", e.toString());
625 fail("A wrong exception was thrown: " + e.toString());
629 requestFactory.setBody("{\"error\":\"some message\"}");
633 clientTemplate.getForObject("ANY", SOME.class);
634 fail("The expected exception was not thrown");
636 catch(HttpClientErrorException e)
638 log.debug("{}", e.toString());
642 fail("A wrong exception was thrown: " + e.toString());
646 requestFactory.setBody("{\"error\":null}");
650 clientTemplate.getForObject("ANY", SOME.class);
651 fail("The expected exception was not thrown");
653 catch(HttpClientErrorException e)
655 log.debug("{}", e.toString());
659 fail("A wrong exception was thrown: " + e.toString());
663 requestFactory.setBody("{\"some filed\":\"some message\"}");
667 clientTemplate.getForObject("ANY", SOME.class);
668 fail("The expected exception was not thrown");
670 catch(HttpClientErrorException e)
672 log.debug("{}", e.toString());
676 fail("A wrong exception was thrown: " + e.toString());
680 requestFactory.setBody("{}");
684 clientTemplate.getForObject("ANY", SOME.class);
685 fail("The expected exception was not thrown");
687 catch(HttpClientErrorException e)
689 log.debug("{}", e.toString());
693 fail("A wrong exception was thrown: " + e.toString());
697 requestFactory.setBody("");
701 clientTemplate.getForObject("ANY", SOME.class);
702 fail("The expected exception was not thrown");
704 catch(HttpClientErrorException e)
706 log.debug("{}", e.toString());
710 fail("A wrong exception was thrown: " + e.toString());
718 requestFactory = new MockClientHttpRequestFactory();
719 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
720 requestFactory.addHeader("Content-Type", "application/json");
722 clientTemplate = new RestTemplate();
723 clientTemplate.setRequestFactory(requestFactory);
724 clientTemplate.setErrorHandler(
725 new GraphApiErrorHandler(clientTemplate.getErrorHandler())