Refined the test-cases and fixed a bug in GraphApiErrorResponseErrorHandler
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiExceptionTest.java
1 package de.juplo.facebook.errors;
2
3
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;
9 import org.junit.Test;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpHeaders;
13 import org.springframework.http.HttpStatus;
14
15
16
17 /**
18  *
19  * @author Kai Moritz
20  */
21 public class GraphApiExceptionTest
22 {
23   private static final Logger LOG =
24       LoggerFactory.getLogger(GraphApiExceptionTest.class);
25
26   private HttpStatus status = HttpStatus.I_AM_A_TEAPOT;
27   private HttpHeaders headers = HttpHeaders.EMPTY;
28
29
30   @Test
31   public void testError1()
32   {
33     LOG.info("testError1");
34
35     String str =
36         "{\n" +
37         "  \"error\":\n" +
38         "  {\n" +
39         "    \"message\": \"An unknown error has occurred.\",\n" +
40         "    \"type\": \"OAuthException\",\n" +
41         "    \"code\": 1\n" +
42         "  }\n" +
43         "}";
44     byte[] message = str.getBytes();
45     GraphApiException e = GraphApiException.create (status, headers, message);
46
47     assertTrue(e.getClass().equals(UnknownErrorException.class));
48     LOG.debug("{}", e.toString());
49   }
50
51   @Test
52   public void testError2()
53   {
54     LOG.info("testError2");
55
56
57     String str =
58         "{\n" +
59         "  \"error\":\n" +
60         "  {\n" +
61         "    \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
62         "    \"type\": \"OAuthException\",\n" +
63         "    \"code\": 2\n" +
64         "  }\n" +
65         "}";
66     byte[] message = str.getBytes();
67     GraphApiException e = GraphApiException.create (status, headers, message);
68
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());
74   }
75
76   @Test
77   public void testError4()
78   {
79     LOG.info("testError4");
80
81
82     String str =
83         "{\n" +
84         "    \"error\": {\n" +
85         "        \"code\": 4, \n" +
86         "        \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
87         "        \"is_transient\": true, \n" +
88         "        \"message\": \"(#4) Application request limit reached\", \n" +
89         "        \"type\": \"OAuthException\"\n" +
90         "    }\n" +
91         "}\n";
92     byte[] message = str.getBytes();
93     GraphApiException e = GraphApiException.create (status, headers, message);
94
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());
100   }
101
102   @Test
103   public void testError12()
104   {
105     LOG.info("testError12");
106
107
108     String str =
109         "{\n" +
110         "  \"error\":\n" +
111         "  {\n" +
112         "    \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
113         "    \"type\": \"OAuthException\",\n" +
114         "    \"code\": 12\n," +
115         "    \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
116         "  }\n" +
117         "}";
118     byte[] message = str.getBytes();
119     GraphApiException e = GraphApiException.create (status, headers, message);
120
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());
126   }
127
128   @Test
129   public void testError21()
130   {
131     LOG.info("testError21");
132
133
134     String str =
135         "{\n" +
136         "  \"error\":\n" +
137         "  {\n" +
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" +
140         "    \"code\": 21\n" +
141         "  }\n" +
142         "}";
143     byte[] message = str.getBytes();
144     GraphApiException e = GraphApiException.create (status, headers, message);
145
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());
151   }
152
153   @Test
154   public void testError100()
155   {
156     LOG.info("testError100");
157
158
159     String str =
160         "{\n" +
161         "  \"error\":\n" +
162         "  {\n" +
163         "    \"message\": \"Unsupported get request.\",\n" +
164         "    \"type\": \"GraphMethodException\",\n" +
165         "    \"code\": 100\n" +
166         "  }\n" +
167         "}";
168     byte[] message = str.getBytes();
169     GraphApiException e = GraphApiException.create (status, headers, message);
170
171
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());
177   }
178
179   @Test
180   public void testError102()
181   {
182     LOG.info("testError102");
183
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);
187
188
189     assertTrue(e.getClass().equals(UserAccessTokenRequiredException.class));
190     LOG.debug("{}", e.toString());
191   }
192
193   @Test
194   public void testError104()
195   {
196     LOG.info("testError104");
197
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);
201
202     assertTrue(e.getClass().equals(AccessTokenRequiredException.class));
203     LOG.debug("{}", e.toString());
204   }
205
206   @Test
207   public void testError190()
208   {
209     LOG.info("testError190");
210
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);
214
215     assertTrue(e.getClass().equals(AccessTokenExpiredException.class));
216     LOG.debug("{}", e.toString());
217   }
218
219   @Test
220   public void testError200()
221   {
222     LOG.info("testError200");
223
224     String str ="{\n" +
225         "  \"error\": {\n" +
226         "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
227         "    \"type\": \"OAuthException\",\n" +
228         "    \"code\": 200\n" +
229         "  }\n" +
230         "}";
231     byte[] message = str.getBytes();
232     GraphApiException e = GraphApiException.create (status, headers, message);
233
234     assertTrue(e.getClass().equals(ApplicationNotAuthorizedByUserException.class));
235     LOG.debug("{}", e.toString());
236   }
237
238   @Test
239   public void testError613()
240   {
241     LOG.info("testError613");
242
243
244     String str =
245         "{\n" +
246         "  \"error\":\n" +
247         "  {\n" +
248         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
249         "    \"type\": \"OAuthException\",\n" +
250         "    \"code\": 613\n" +
251         "  }\n" +
252         "}";
253     byte[] message = str.getBytes();
254     GraphApiException e = GraphApiException.create (status, headers, message);
255
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());
261   }
262
263   @Test
264   public void testError2200()
265   {
266     LOG.info("testError2200");
267
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);
271
272     assertTrue(e.getClass().equals(CallbackVerificationFailedException.class));
273     LOG.debug("{}", e.toString());
274   }
275
276   @Test
277   public void testUnmappedError()
278   {
279     LOG.info("testUnmappedError");
280
281
282     String str =
283         "{\n" +
284         "  \"error\":\n" +
285         "  {\n" +
286         "    \"message\": \"This error does not exist.\",\n" +
287         "    \"type\": \"NonexistentTypeException\",\n" +
288         "    \"code\": 999999999\n" +
289         "  }\n" +
290         "}";
291     byte[] message = str.getBytes();
292     GraphApiException e = GraphApiException.create (status, headers, message);
293
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());
298     try
299     {
300       Type type = e.getType();
301       LOG.error("unknown type: {}", type);
302       fail("unmapped type was resolved by enum: " + type);
303     }
304     catch (IllegalArgumentException ee) {}
305   }
306
307   @Test
308   public void testUnmappedErrors()
309   {
310     LOG.info("testUnmappedErrors");
311
312     String str;
313     byte[] message;
314     GraphApiException e;
315
316
317     str =
318         "{\n" +
319         "  \"error\":\n" +
320         "  {\n" +
321         "    \"message\": null,\n" +
322         "    \"type\": \"WhateverTypeException\",\n" +
323         "    \"code\": 999999999\n" +
324         "  }\n" +
325         "}";
326     message = str.getBytes();
327     e = GraphApiException.create (status, headers, message);
328
329     assertTrue(e.getClass().equals(UnmappedErrorException.class));
330     LOG.debug("{}", e.toString());
331     assertNull(e.getMessage());
332     try
333     {
334       Type type = e.getType();
335       LOG.error("unknown type: {}", type);
336       fail("unmapped type was resolved by enum: " + type);
337     }
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());
344
345
346     str =
347         "{\n" +
348         "  \"error\":\n" +
349         "  {\n" +
350         "    \"type\": \"WhateverTypeException\",\n" +
351         "    \"code\": 999999999\n" +
352         "  }\n" +
353         "}";
354     message = str.getBytes();
355     e = GraphApiException.create (status, headers, message);
356
357     assertTrue(e.getClass().equals(UnmappedErrorException.class));
358     LOG.debug("{}", e.toString());
359     assertNull(e.getMessage());
360     try
361     {
362       Type type = e.getType();
363       LOG.error("unknown type: {}", type);
364       fail("unmapped type was resolved by enum: " + type);
365     }
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());
372
373
374     str =
375         "{\n" +
376         "  \"error\":\n" +
377         "  {\n" +
378         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
379         "    \"type\": null,\n" +
380         "    \"code\": 999999999\n" +
381         "  }\n" +
382         "}";
383     message = str.getBytes();
384     e = GraphApiException.create (status, headers, message);
385
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());
395
396
397     str =
398         "{\n" +
399         "  \"error\":\n" +
400         "  {\n" +
401         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
402         "    \"code\": 999999999\n" +
403         "  }\n" +
404         "}";
405     message = str.getBytes();
406     e = GraphApiException.create (status, headers, message);
407
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());
417   }
418
419   @Test
420   public void testInvlalidErrors()
421   {
422     LOG.info("testInvalidErrors");
423
424     String str;
425     byte[] message;
426     GraphApiException e;
427
428
429     str =
430         "{\n" +
431         "  \"error\":\n" +
432         "  {\n" +
433         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
434         "    \"type\": \"Whatever\",\n" +
435         "    \"code\": \"some string\"\n" +
436         "  }\n" +
437         "}";
438     message = str.getBytes();
439     e = GraphApiException.create (status, headers, message);
440     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
441     LOG.debug("{}", e.toString());
442
443
444     str =
445         "{\n" +
446         "  \"error\":\n" +
447         "  {\n" +
448         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
449         "    \"type\": \"Whatever\",\n" +
450         "    \"code\": 9.9\n" +
451         "  }\n" +
452         "}";
453     message = str.getBytes();
454     e = GraphApiException.create (status, headers, message);
455     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
456     LOG.debug("{}", e.toString());
457
458
459     str =
460         "{\n" +
461         "  \"error\":\n" +
462         "  {\n" +
463         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
464         "    \"type\": \"Whatever\",\n" +
465         "    \"code\": null\n" +
466         "  }\n" +
467         "}";
468     message = str.getBytes();
469     e = GraphApiException.create (status, headers, message);
470     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
471     LOG.debug("{}", e.toString());
472
473
474     str =
475         "{\n" +
476         "  \"error\":\n" +
477         "  {\n" +
478         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
479         "    \"type\": \"Whatever\"\n" +
480         "  }\n" +
481         "}";
482     message = str.getBytes();
483     e = GraphApiException.create (status, headers, message);
484     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
485     LOG.debug("{}", e.toString());
486
487
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());
493
494
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());
500
501
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());
507
508
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());
514
515
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());
521
522
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());
528
529
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());
535
536
537     str ="{}";
538     message = str.getBytes();
539     e = GraphApiException.create (status, headers, message);
540     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
541     LOG.debug("{}", e.toString());
542
543
544     str ="";
545     message = str.getBytes();
546     e = GraphApiException.create (status, headers, message);
547     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
548     LOG.debug("{}", e.toString());
549   }
550 }