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()
37 " \"message\": \"An unknown error has occurred.\",\n" +
38 " \"type\": \"OAuthException\",\n" +
42 byte[] message = str.getBytes();
43 GraphApiException e = GraphApiException.create (status, headers, message);
45 assertTrue(e.getClass().equals(UnknownErrorException.class));
46 LOG.debug("{}", e.toString());
50 public void testError2()
56 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
57 " \"type\": \"OAuthException\",\n" +
61 byte[] message = str.getBytes();
62 GraphApiException e = GraphApiException.create (status, headers, message);
64 assertTrue(e.getClass().equals(UnexpectedErrorException.class));
65 LOG.debug("{}", e.toString());
66 assertEquals(new Integer(2), e.getCode());
67 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
68 assertEquals(Type.OAuthException, e.getType());
72 public void testError4()
78 " \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
79 " \"is_transient\": true, \n" +
80 " \"message\": \"(#4) Application request limit reached\", \n" +
81 " \"type\": \"OAuthException\"\n" +
84 byte[] message = str.getBytes();
85 GraphApiException e = GraphApiException.create (status, headers, message);
87 assertTrue(e.getClass().equals(ApplicationRequestLimitReachedException.class));
88 LOG.debug("{}", e.toString());
89 assertEquals(new Integer(4), e.getCode());
90 assertEquals("(#4) Application request limit reached", e.getMessage());
91 assertEquals(Type.OAuthException, e.getType());
95 public void testError12()
101 " \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
102 " \"type\": \"OAuthException\",\n" +
104 " \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
107 byte[] message = str.getBytes();
108 GraphApiException e = GraphApiException.create (status, headers, message);
110 assertTrue(e.getClass().equals(DeprecatedException.class));
111 LOG.debug("{}", e.toString());
112 assertEquals(new Integer(12), e.getCode());
113 assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
114 assertEquals(Type.OAuthException, e.getType());
118 public void testError21()
124 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
125 " \"type\": \"OAuthException\",\n" +
129 byte[] message = str.getBytes();
130 GraphApiException e = GraphApiException.create (status, headers, message);
132 assertTrue(e.getClass().equals(PageMigratedException.class));
133 LOG.debug("{}", e.toString());
134 assertEquals(new Integer(21), e.getCode());
135 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
136 assertEquals(Type.OAuthException, e.getType());
140 public void testError100()
146 " \"message\": \"Unsupported get request.\",\n" +
147 " \"type\": \"GraphMethodException\",\n" +
151 byte[] message = str.getBytes();
152 GraphApiException e = GraphApiException.create (status, headers, message);
155 assertTrue(e.getClass().equals(UnsupportedGetRequestException.class));
156 LOG.debug("{}", e.toString());
157 assertEquals(new Integer(100), e.getCode());
158 assertEquals("Unsupported get request.", e.getMessage());
159 assertEquals(Type.GraphMethodException, e.getType());
163 public void testError102()
165 String str ="{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}";
166 byte[] message = str.getBytes();
167 GraphApiException e = GraphApiException.create (status, headers, message);
170 assertTrue(e.getClass().equals(UserAccessTokenRequiredException.class));
171 LOG.debug("{}", e.toString());
175 public void testError104()
177 String str ="{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}";
178 byte[] message = str.getBytes();
179 GraphApiException e = GraphApiException.create (status, headers, message);
181 assertTrue(e.getClass().equals(AccessTokenRequiredException.class));
182 LOG.debug("{}", e.toString());
186 public void testError190()
188 LOG.info("testError190");
190 String str ="{\"error\":{\"message\":\"Bad signature\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"Ay2OYQrINbXOCfQpBvoxDIw\"}}";
191 byte[] message = str.getBytes();
192 GraphApiException e = GraphApiException.create (status, headers, message);
194 assertTrue(e.getClass().equals(AccessTokenExpiredException.class));
195 LOG.debug("{}", e.toString());
199 public void testError200()
203 " \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
204 " \"type\": \"OAuthException\",\n" +
208 byte[] message = str.getBytes();
209 GraphApiException e = GraphApiException.create (status, headers, message);
211 assertTrue(e.getClass().equals(ApplicationNotAuthorizedByUserException.class));
212 LOG.debug("{}", e.toString());
216 public void testError613()
222 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
223 " \"type\": \"OAuthException\",\n" +
227 byte[] message = str.getBytes();
228 GraphApiException e = GraphApiException.create (status, headers, message);
230 assertTrue(e.getClass().equals(RateLimitExceededException.class));
231 LOG.debug("{}", e.toString());
232 assertEquals(new Integer(613), e.getCode());
233 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
234 assertEquals(Type.OAuthException, e.getType());
238 public void testError2200()
240 String str ="{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}";
241 byte[] message = str.getBytes();
242 GraphApiException e = GraphApiException.create (status, headers, message);
244 assertTrue(e.getClass().equals(CallbackVerificationFailedException.class));
245 LOG.debug("{}", e.toString());
249 public void testError2200()
251 String str ="{\"error\":{\"message\":\"An active access token must be used to query information about the current user.\",\"type\":\"OAuthException\",\"code\":2500,\"fbtrace_id\":\"AUmMHJlVYvTbQrBT11Dk104\"}}";
252 byte[] message = str.getBytes();
253 GraphApiException e = GraphApiException.create (status, headers, message);
255 assertTrue(e.getClass().equals(ActiveAccessTokenMissingException.class));
256 LOG.debug("{}", e.toString());
260 public void testUnmappedError()
266 " \"message\": \"This error does not exist.\",\n" +
267 " \"type\": \"NonexistentTypeException\",\n" +
268 " \"code\": 999999999\n" +
271 byte[] message = str.getBytes();
272 GraphApiException e = GraphApiException.create (status, headers, message);
274 assertTrue(e.getClass().equals(UnmappedErrorException.class));
275 LOG.debug("{}", e.toString());
276 assertEquals(new Integer(999999999), e.getCode());
277 assertEquals("This error does not exist.", e.getMessage());
280 Type type = e.getType();
281 LOG.error("unknown type: {}", type);
282 fail("unmapped type was resolved by enum: " + type);
284 catch (IllegalArgumentException ee) {}
288 public void testUnmappedErrors()
299 " \"message\": null,\n" +
300 " \"type\": \"WhateverTypeException\",\n" +
301 " \"code\": 999999999\n" +
304 message = str.getBytes();
305 e = GraphApiException.create (status, headers, message);
307 assertTrue(e.getClass().equals(UnmappedErrorException.class));
308 LOG.debug("{}", e.toString());
309 assertNull(e.getMessage());
312 Type type = e.getType();
313 LOG.error("unknown type: {}", type);
314 fail("unmapped type was resolved by enum: " + type);
316 catch (IllegalArgumentException ee) {}
317 assertEquals(new Integer(999999999), e.getCode());
318 assertNull(e.getSubCode());
319 assertNull(e.getUserTitle());
320 assertNull(e.getUserMessage());
321 assertNull(e.getTraceId());
328 " \"type\": \"WhateverTypeException\",\n" +
329 " \"code\": 999999999\n" +
332 message = str.getBytes();
333 e = GraphApiException.create (status, headers, message);
335 assertTrue(e.getClass().equals(UnmappedErrorException.class));
336 LOG.debug("{}", e.toString());
337 assertNull(e.getMessage());
340 Type type = e.getType();
341 LOG.error("unknown type: {}", type);
342 fail("unmapped type was resolved by enum: " + type);
344 catch (IllegalArgumentException ee) {}
345 assertEquals(new Integer(999999999), e.getCode());
346 assertNull(e.getSubCode());
347 assertNull(e.getUserTitle());
348 assertNull(e.getUserMessage());
349 assertNull(e.getTraceId());
356 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
357 " \"type\": null,\n" +
358 " \"code\": 999999999\n" +
361 message = str.getBytes();
362 e = GraphApiException.create (status, headers, message);
364 assertTrue(e.getClass().equals(UnmappedErrorException.class));
365 LOG.debug("{}", e.toString());
366 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
367 assertNull(e.getType());
368 assertEquals(new Integer(999999999), e.getCode());
369 assertNull(e.getSubCode());
370 assertNull(e.getUserTitle());
371 assertNull(e.getUserMessage());
372 assertNull(e.getTraceId());
379 " \"message\": \"An unmapped Graph-API-Exception.\",\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());
398 public void testInvlalidErrors()
409 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
410 " \"type\": \"Whatever\",\n" +
411 " \"code\": \"some string\"\n" +
414 message = str.getBytes();
415 e = GraphApiException.create (status, headers, message);
416 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
417 LOG.debug("{}", e.toString());
424 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
425 " \"type\": \"Whatever\",\n" +
429 message = str.getBytes();
430 e = GraphApiException.create (status, headers, message);
431 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
432 LOG.debug("{}", e.toString());
439 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
440 " \"type\": \"Whatever\",\n" +
441 " \"code\": null\n" +
444 message = str.getBytes();
445 e = GraphApiException.create (status, headers, message);
446 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
447 LOG.debug("{}", e.toString());
454 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
455 " \"type\": \"Whatever\"\n" +
458 message = str.getBytes();
459 e = GraphApiException.create (status, headers, message);
460 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
461 LOG.debug("{}", e.toString());
464 str ="{\"error\":{\"message\":null}}";
465 message = str.getBytes();
466 e = GraphApiException.create (status, headers, message);
467 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
468 LOG.debug("{}", e.toString());
471 str ="{\"error\":{\"type\":null}}";
472 message = str.getBytes();
473 e = GraphApiException.create (status, headers, message);
474 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
475 LOG.debug("{}", e.toString());
478 str ="{\"error\":{\"code\":null}}";
479 message = str.getBytes();
480 e = GraphApiException.create (status, headers, message);
481 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
482 LOG.debug("{}", e.toString());
485 str ="{\"error\":{}}";
486 message = str.getBytes();
487 e = GraphApiException.create (status, headers, message);
488 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
489 LOG.debug("{}", e.toString());
492 str ="{\"error\":\"some message\"}";
493 message = str.getBytes();
494 e = GraphApiException.create (status, headers, message);
495 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
496 LOG.debug("{}", e.toString());
499 str ="{\"error\":null}";
500 message = str.getBytes();
501 e = GraphApiException.create (status, headers, message);
502 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
503 LOG.debug("{}", e.toString());
506 str ="{\"some filed\":\"some message\"}";
507 message = str.getBytes();
508 e = GraphApiException.create (status, headers, message);
509 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
510 LOG.debug("{}", e.toString());
514 message = str.getBytes();
515 e = GraphApiException.create (status, headers, message);
516 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
517 LOG.debug("{}", e.toString());
521 message = str.getBytes();
522 e = GraphApiException.create (status, headers, message);
523 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
524 LOG.debug("{}", e.toString());