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.assertTrue;
8 import static org.junit.Assert.fail;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpHeaders;
13 import org.springframework.http.HttpStatus;
21 public class GraphApiExceptionTest
23 private static final Logger LOG =
24 LoggerFactory.getLogger(GraphApiExceptionTest.class);
26 private HttpStatus status = HttpStatus.I_AM_A_TEAPOT;
27 private HttpHeaders headers = HttpHeaders.EMPTY;
31 public void testError1()
33 LOG.info("testError1");
39 " \"message\": \"An unknown error has occurred.\",\n" +
40 " \"type\": \"OAuthException\",\n" +
44 byte[] message = str.getBytes();
45 GraphApiException e = GraphApiException.create (status, headers, message);
47 assertTrue(e.getClass().equals(UnknownErrorException.class));
48 LOG.debug("{}", e.toString());
52 public void testError2()
54 LOG.info("testError2");
61 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
62 " \"type\": \"OAuthException\",\n" +
66 byte[] message = str.getBytes();
67 GraphApiException e = GraphApiException.create (status, headers, message);
69 assertTrue(e.getClass().equals(UnexpectedErrorException.class));
70 LOG.debug("{}", e.toString());
71 assertEquals(new Integer(2), e.getCode());
72 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
73 assertEquals(Type.OAuthException, e.getType());
77 public void testError4()
79 LOG.info("testError4");
86 " \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
87 " \"is_transient\": true, \n" +
88 " \"message\": \"(#4) Application request limit reached\", \n" +
89 " \"type\": \"OAuthException\"\n" +
92 byte[] message = str.getBytes();
93 GraphApiException e = GraphApiException.create (status, headers, message);
95 assertTrue(e.getClass().equals(ApplicationRequestLimitReachedException.class));
96 LOG.debug("{}", e.toString());
97 assertEquals(new Integer(4), e.getCode());
98 assertEquals("(#4) Application request limit reached", e.getMessage());
99 assertEquals(Type.OAuthException, e.getType());
103 public void testError12()
105 LOG.info("testError12");
112 " \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
113 " \"type\": \"OAuthException\",\n" +
115 " \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
118 byte[] message = str.getBytes();
119 GraphApiException e = GraphApiException.create (status, headers, message);
121 assertTrue(e.getClass().equals(DeprecatedException.class));
122 LOG.debug("{}", e.toString());
123 assertEquals(new Integer(12), e.getCode());
124 assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
125 assertEquals(Type.OAuthException, e.getType());
129 public void testError21()
131 LOG.info("testError21");
138 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
139 " \"type\": \"OAuthException\",\n" +
143 byte[] message = str.getBytes();
144 GraphApiException e = GraphApiException.create (status, headers, message);
146 assertTrue(e.getClass().equals(PageMigratedException.class));
147 LOG.debug("{}", e.toString());
148 assertEquals(new Integer(21), e.getCode());
149 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
150 assertEquals(Type.OAuthException, e.getType());
154 public void testError100()
156 LOG.info("testError100");
163 " \"message\": \"Unsupported get request.\",\n" +
164 " \"type\": \"GraphMethodException\",\n" +
168 byte[] message = str.getBytes();
169 GraphApiException e = GraphApiException.create (status, headers, message);
172 assertTrue(e.getClass().equals(UnsupportedGetRequestException.class));
173 LOG.debug("{}", e.toString());
174 assertEquals(new Integer(100), e.getCode());
175 assertEquals("Unsupported get request.", e.getMessage());
176 assertEquals(Type.GraphMethodException, e.getType());
180 public void testError102()
182 LOG.info("testError102");
184 String str ="{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}";
185 byte[] message = str.getBytes();
186 GraphApiException e = GraphApiException.create (status, headers, message);
189 assertTrue(e.getClass().equals(UserAccessTokenRequiredException.class));
190 LOG.debug("{}", e.toString());
194 public void testError104()
196 LOG.info("testError104");
198 String str ="{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}";
199 byte[] message = str.getBytes();
200 GraphApiException e = GraphApiException.create (status, headers, message);
202 assertTrue(e.getClass().equals(AccessTokenRequiredException.class));
203 LOG.debug("{}", e.toString());
207 public void testError190()
209 LOG.info("testError190");
211 String str ="{\"error\":{\"message\":\"Bad signature\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"Ay2OYQrINbXOCfQpBvoxDIw\"}}";
212 byte[] message = str.getBytes();
213 GraphApiException e = GraphApiException.create (status, headers, message);
215 assertTrue(e.getClass().equals(AccessTokenExpiredException.class));
216 LOG.debug("{}", e.toString());
220 public void testError200()
222 LOG.info("testError200");
226 " \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
227 " \"type\": \"OAuthException\",\n" +
231 byte[] message = str.getBytes();
232 GraphApiException e = GraphApiException.create (status, headers, message);
234 assertTrue(e.getClass().equals(ApplicationNotAuthorizedByUserException.class));
235 LOG.debug("{}", e.toString());
239 public void testError613()
241 LOG.info("testError613");
248 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
249 " \"type\": \"OAuthException\",\n" +
253 byte[] message = str.getBytes();
254 GraphApiException e = GraphApiException.create (status, headers, message);
256 assertTrue(e.getClass().equals(RateLimitExceededException.class));
257 LOG.debug("{}", e.toString());
258 assertEquals(new Integer(613), e.getCode());
259 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
260 assertEquals(Type.OAuthException, e.getType());
264 public void testError2200()
266 LOG.info("testError2200");
268 String str ="{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}";
269 byte[] message = str.getBytes();
270 GraphApiException e = GraphApiException.create (status, headers, message);
272 assertTrue(e.getClass().equals(CallbackVerificationFailedException.class));
273 LOG.debug("{}", e.toString());
277 public void testUnmappedError()
279 LOG.info("testUnmappedError");
286 " \"message\": \"This error does not exist.\",\n" +
287 " \"type\": \"NonexistentTypeException\",\n" +
288 " \"code\": 999999999\n" +
291 byte[] message = str.getBytes();
292 GraphApiException e = GraphApiException.create (status, headers, message);
294 assertTrue(e.getClass().equals(UnmappedErrorException.class));
295 LOG.debug("{}", e.toString());
296 assertEquals(new Integer(999999999), e.getCode());
297 assertEquals("This error does not exist.", e.getMessage());
300 Type type = e.getType();
301 LOG.error("unknown type: {}", type);
302 fail("unmapped type was resolved by enum: " + type);
304 catch (IllegalArgumentException ee) {}
308 public void testUnmappedErrors()
310 LOG.info("testUnmappedErrors");
321 " \"message\": null,\n" +
322 " \"type\": \"WhateverTypeException\",\n" +
323 " \"code\": 999999999\n" +
326 message = str.getBytes();
327 e = GraphApiException.create (status, headers, message);
329 assertTrue(e.getClass().equals(UnmappedErrorException.class));
330 LOG.debug("{}", e.toString());
331 assertNull(e.getMessage());
334 Type type = e.getType();
335 LOG.error("unknown type: {}", type);
336 fail("unmapped type was resolved by enum: " + type);
338 catch (IllegalArgumentException ee) {}
339 assertEquals(new Integer(999999999), e.getCode());
340 assertNull(e.getSubCode());
341 assertNull(e.getUserTitle());
342 assertNull(e.getUserMessage());
343 assertNull(e.getTraceId());
350 " \"type\": \"WhateverTypeException\",\n" +
351 " \"code\": 999999999\n" +
354 message = str.getBytes();
355 e = GraphApiException.create (status, headers, message);
357 assertTrue(e.getClass().equals(UnmappedErrorException.class));
358 LOG.debug("{}", e.toString());
359 assertNull(e.getMessage());
362 Type type = e.getType();
363 LOG.error("unknown type: {}", type);
364 fail("unmapped type was resolved by enum: " + type);
366 catch (IllegalArgumentException ee) {}
367 assertEquals(new Integer(999999999), e.getCode());
368 assertNull(e.getSubCode());
369 assertNull(e.getUserTitle());
370 assertNull(e.getUserMessage());
371 assertNull(e.getTraceId());
378 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
379 " \"type\": null,\n" +
380 " \"code\": 999999999\n" +
383 message = str.getBytes();
384 e = GraphApiException.create (status, headers, message);
386 assertTrue(e.getClass().equals(UnmappedErrorException.class));
387 LOG.debug("{}", e.toString());
388 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
389 assertNull(e.getType());
390 assertEquals(new Integer(999999999), e.getCode());
391 assertNull(e.getSubCode());
392 assertNull(e.getUserTitle());
393 assertNull(e.getUserMessage());
394 assertNull(e.getTraceId());
401 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
402 " \"code\": 999999999\n" +
405 message = str.getBytes();
406 e = GraphApiException.create (status, headers, message);
408 assertTrue(e.getClass().equals(UnmappedErrorException.class));
409 LOG.debug("{}", e.toString());
410 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
411 assertNull(e.getType());
412 assertEquals(new Integer(999999999), e.getCode());
413 assertNull(e.getSubCode());
414 assertNull(e.getUserTitle());
415 assertNull(e.getUserMessage());
416 assertNull(e.getTraceId());
420 public void testInvlalidErrors()
422 LOG.info("testInvalidErrors");
433 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
434 " \"type\": \"Whatever\",\n" +
435 " \"code\": \"some string\"\n" +
438 message = str.getBytes();
439 e = GraphApiException.create (status, headers, message);
440 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
441 LOG.debug("{}", e.toString());
448 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
449 " \"type\": \"Whatever\",\n" +
453 message = str.getBytes();
454 e = GraphApiException.create (status, headers, message);
455 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
456 LOG.debug("{}", e.toString());
463 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
464 " \"type\": \"Whatever\",\n" +
465 " \"code\": null\n" +
468 message = str.getBytes();
469 e = GraphApiException.create (status, headers, message);
470 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
471 LOG.debug("{}", e.toString());
478 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
479 " \"type\": \"Whatever\"\n" +
482 message = str.getBytes();
483 e = GraphApiException.create (status, headers, message);
484 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
485 LOG.debug("{}", e.toString());
488 str ="{\"error\":{\"message\":null}}";
489 message = str.getBytes();
490 e = GraphApiException.create (status, headers, message);
491 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
492 LOG.debug("{}", e.toString());
495 str ="{\"error\":{\"type\":null}}";
496 message = str.getBytes();
497 e = GraphApiException.create (status, headers, message);
498 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
499 LOG.debug("{}", e.toString());
502 str ="{\"error\":{\"code\":null}}";
503 message = str.getBytes();
504 e = GraphApiException.create (status, headers, message);
505 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
506 LOG.debug("{}", e.toString());
509 str ="{\"error\":{}}";
510 message = str.getBytes();
511 e = GraphApiException.create (status, headers, message);
512 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
513 LOG.debug("{}", e.toString());
516 str ="{\"error\":\"some message\"}";
517 message = str.getBytes();
518 e = GraphApiException.create (status, headers, message);
519 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
520 LOG.debug("{}", e.toString());
523 str ="{\"error\":null}";
524 message = str.getBytes();
525 e = GraphApiException.create (status, headers, message);
526 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
527 LOG.debug("{}", e.toString());
530 str ="{\"some filed\":\"some message\"}";
531 message = str.getBytes();
532 e = GraphApiException.create (status, headers, message);
533 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
534 LOG.debug("{}", e.toString());
538 message = str.getBytes();
539 e = GraphApiException.create (status, headers, message);
540 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
541 LOG.debug("{}", e.toString());
545 message = str.getBytes();
546 e = GraphApiException.create (status, headers, message);
547 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
548 LOG.debug("{}", e.toString());