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 testUnmappedError()
255 " \"message\": \"This error does not exist.\",\n" +
256 " \"type\": \"NonexistentTypeException\",\n" +
257 " \"code\": 999999999\n" +
260 byte[] message = str.getBytes();
261 GraphApiException e = GraphApiException.create (status, headers, message);
263 assertTrue(e.getClass().equals(UnmappedErrorException.class));
264 LOG.debug("{}", e.toString());
265 assertEquals(new Integer(999999999), e.getCode());
266 assertEquals("This error does not exist.", e.getMessage());
269 Type type = e.getType();
270 LOG.error("unknown type: {}", type);
271 fail("unmapped type was resolved by enum: " + type);
273 catch (IllegalArgumentException ee) {}
277 public void testUnmappedErrors()
288 " \"message\": null,\n" +
289 " \"type\": \"WhateverTypeException\",\n" +
290 " \"code\": 999999999\n" +
293 message = str.getBytes();
294 e = GraphApiException.create (status, headers, message);
296 assertTrue(e.getClass().equals(UnmappedErrorException.class));
297 LOG.debug("{}", e.toString());
298 assertNull(e.getMessage());
301 Type type = e.getType();
302 LOG.error("unknown type: {}", type);
303 fail("unmapped type was resolved by enum: " + type);
305 catch (IllegalArgumentException ee) {}
306 assertEquals(new Integer(999999999), e.getCode());
307 assertNull(e.getSubCode());
308 assertNull(e.getUserTitle());
309 assertNull(e.getUserMessage());
310 assertNull(e.getTraceId());
317 " \"type\": \"WhateverTypeException\",\n" +
318 " \"code\": 999999999\n" +
321 message = str.getBytes();
322 e = GraphApiException.create (status, headers, message);
324 assertTrue(e.getClass().equals(UnmappedErrorException.class));
325 LOG.debug("{}", e.toString());
326 assertNull(e.getMessage());
329 Type type = e.getType();
330 LOG.error("unknown type: {}", type);
331 fail("unmapped type was resolved by enum: " + type);
333 catch (IllegalArgumentException ee) {}
334 assertEquals(new Integer(999999999), e.getCode());
335 assertNull(e.getSubCode());
336 assertNull(e.getUserTitle());
337 assertNull(e.getUserMessage());
338 assertNull(e.getTraceId());
345 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
346 " \"type\": null,\n" +
347 " \"code\": 999999999\n" +
350 message = str.getBytes();
351 e = GraphApiException.create (status, headers, message);
353 assertTrue(e.getClass().equals(UnmappedErrorException.class));
354 LOG.debug("{}", e.toString());
355 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
356 assertNull(e.getType());
357 assertEquals(new Integer(999999999), e.getCode());
358 assertNull(e.getSubCode());
359 assertNull(e.getUserTitle());
360 assertNull(e.getUserMessage());
361 assertNull(e.getTraceId());
368 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
369 " \"code\": 999999999\n" +
372 message = str.getBytes();
373 e = GraphApiException.create (status, headers, message);
375 assertTrue(e.getClass().equals(UnmappedErrorException.class));
376 LOG.debug("{}", e.toString());
377 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
378 assertNull(e.getType());
379 assertEquals(new Integer(999999999), e.getCode());
380 assertNull(e.getSubCode());
381 assertNull(e.getUserTitle());
382 assertNull(e.getUserMessage());
383 assertNull(e.getTraceId());
387 public void testInvlalidErrors()
398 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
399 " \"type\": \"Whatever\",\n" +
400 " \"code\": \"some string\"\n" +
403 message = str.getBytes();
404 e = GraphApiException.create (status, headers, message);
405 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
406 LOG.debug("{}", e.toString());
413 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
414 " \"type\": \"Whatever\",\n" +
418 message = str.getBytes();
419 e = GraphApiException.create (status, headers, message);
420 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
421 LOG.debug("{}", e.toString());
428 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
429 " \"type\": \"Whatever\",\n" +
430 " \"code\": null\n" +
433 message = str.getBytes();
434 e = GraphApiException.create (status, headers, message);
435 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
436 LOG.debug("{}", e.toString());
443 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
444 " \"type\": \"Whatever\"\n" +
447 message = str.getBytes();
448 e = GraphApiException.create (status, headers, message);
449 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
450 LOG.debug("{}", e.toString());
453 str ="{\"error\":{\"message\":null}}";
454 message = str.getBytes();
455 e = GraphApiException.create (status, headers, message);
456 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
457 LOG.debug("{}", e.toString());
460 str ="{\"error\":{\"type\":null}}";
461 message = str.getBytes();
462 e = GraphApiException.create (status, headers, message);
463 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
464 LOG.debug("{}", e.toString());
467 str ="{\"error\":{\"code\":null}}";
468 message = str.getBytes();
469 e = GraphApiException.create (status, headers, message);
470 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
471 LOG.debug("{}", e.toString());
474 str ="{\"error\":{}}";
475 message = str.getBytes();
476 e = GraphApiException.create (status, headers, message);
477 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
478 LOG.debug("{}", e.toString());
481 str ="{\"error\":\"some message\"}";
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\":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 ="{\"some filed\":\"some message\"}";
496 message = str.getBytes();
497 e = GraphApiException.create (status, headers, message);
498 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
499 LOG.debug("{}", e.toString());
503 message = str.getBytes();
504 e = GraphApiException.create (status, headers, message);
505 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
506 LOG.debug("{}", e.toString());
510 message = str.getBytes();
511 e = GraphApiException.create (status, headers, message);
512 assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
513 LOG.debug("{}", e.toString());