2200: An active access token must be used
[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     String str =
34         "{\n" +
35         "  \"error\":\n" +
36         "  {\n" +
37         "    \"message\": \"An unknown error has occurred.\",\n" +
38         "    \"type\": \"OAuthException\",\n" +
39         "    \"code\": 1\n" +
40         "  }\n" +
41         "}";
42     byte[] message = str.getBytes();
43     GraphApiException e = GraphApiException.create (status, headers, message);
44
45     assertTrue(e.getClass().equals(UnknownErrorException.class));
46     LOG.debug("{}", e.toString());
47   }
48
49   @Test
50   public void testError2()
51   {
52     String str =
53         "{\n" +
54         "  \"error\":\n" +
55         "  {\n" +
56         "    \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
57         "    \"type\": \"OAuthException\",\n" +
58         "    \"code\": 2\n" +
59         "  }\n" +
60         "}";
61     byte[] message = str.getBytes();
62     GraphApiException e = GraphApiException.create (status, headers, message);
63
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());
69   }
70
71   @Test
72   public void testError4()
73   {
74     String str =
75         "{\n" +
76         "    \"error\": {\n" +
77         "        \"code\": 4, \n" +
78         "        \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
79         "        \"is_transient\": true, \n" +
80         "        \"message\": \"(#4) Application request limit reached\", \n" +
81         "        \"type\": \"OAuthException\"\n" +
82         "    }\n" +
83         "}\n";
84     byte[] message = str.getBytes();
85     GraphApiException e = GraphApiException.create (status, headers, message);
86
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());
92   }
93
94   @Test
95   public void testError12()
96   {
97     String str =
98         "{\n" +
99         "  \"error\":\n" +
100         "  {\n" +
101         "    \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
102         "    \"type\": \"OAuthException\",\n" +
103         "    \"code\": 12\n," +
104         "    \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
105         "  }\n" +
106         "}";
107     byte[] message = str.getBytes();
108     GraphApiException e = GraphApiException.create (status, headers, message);
109
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());
115   }
116
117   @Test
118   public void testError21()
119   {
120     String str =
121         "{\n" +
122         "  \"error\":\n" +
123         "  {\n" +
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" +
126         "    \"code\": 21\n" +
127         "  }\n" +
128         "}";
129     byte[] message = str.getBytes();
130     GraphApiException e = GraphApiException.create (status, headers, message);
131
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());
137   }
138
139   @Test
140   public void testError100()
141   {
142     String str =
143         "{\n" +
144         "  \"error\":\n" +
145         "  {\n" +
146         "    \"message\": \"Unsupported get request.\",\n" +
147         "    \"type\": \"GraphMethodException\",\n" +
148         "    \"code\": 100\n" +
149         "  }\n" +
150         "}";
151     byte[] message = str.getBytes();
152     GraphApiException e = GraphApiException.create (status, headers, message);
153
154
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());
160   }
161
162   @Test
163   public void testError102()
164   {
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);
168
169
170     assertTrue(e.getClass().equals(UserAccessTokenRequiredException.class));
171     LOG.debug("{}", e.toString());
172   }
173
174   @Test
175   public void testError104()
176   {
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);
180
181     assertTrue(e.getClass().equals(AccessTokenRequiredException.class));
182     LOG.debug("{}", e.toString());
183   }
184
185   @Test
186   public void testError190()
187   {
188     LOG.info("testError190");
189
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);
193
194     assertTrue(e.getClass().equals(AccessTokenExpiredException.class));
195     LOG.debug("{}", e.toString());
196   }
197
198   @Test
199   public void testError200()
200   {
201     String str ="{\n" +
202         "  \"error\": {\n" +
203         "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
204         "    \"type\": \"OAuthException\",\n" +
205         "    \"code\": 200\n" +
206         "  }\n" +
207         "}";
208     byte[] message = str.getBytes();
209     GraphApiException e = GraphApiException.create (status, headers, message);
210
211     assertTrue(e.getClass().equals(ApplicationNotAuthorizedByUserException.class));
212     LOG.debug("{}", e.toString());
213   }
214
215   @Test
216   public void testError613()
217   {
218     String str =
219         "{\n" +
220         "  \"error\":\n" +
221         "  {\n" +
222         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
223         "    \"type\": \"OAuthException\",\n" +
224         "    \"code\": 613\n" +
225         "  }\n" +
226         "}";
227     byte[] message = str.getBytes();
228     GraphApiException e = GraphApiException.create (status, headers, message);
229
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());
235   }
236
237   @Test
238   public void testError2200()
239   {
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);
243
244     assertTrue(e.getClass().equals(CallbackVerificationFailedException.class));
245     LOG.debug("{}", e.toString());
246   }
247
248   @Test
249   public void testError2200()
250   {
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);
254
255     assertTrue(e.getClass().equals(ActiveAccessTokenMissingException.class));
256     LOG.debug("{}", e.toString());
257   }
258
259   @Test
260   public void testUnmappedError()
261   {
262     String str =
263         "{\n" +
264         "  \"error\":\n" +
265         "  {\n" +
266         "    \"message\": \"This error does not exist.\",\n" +
267         "    \"type\": \"NonexistentTypeException\",\n" +
268         "    \"code\": 999999999\n" +
269         "  }\n" +
270         "}";
271     byte[] message = str.getBytes();
272     GraphApiException e = GraphApiException.create (status, headers, message);
273
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());
278     try
279     {
280       Type type = e.getType();
281       LOG.error("unknown type: {}", type);
282       fail("unmapped type was resolved by enum: " + type);
283     }
284     catch (IllegalArgumentException ee) {}
285   }
286
287   @Test
288   public void testUnmappedErrors()
289   {
290     String str;
291     byte[] message;
292     GraphApiException e;
293
294
295     str =
296         "{\n" +
297         "  \"error\":\n" +
298         "  {\n" +
299         "    \"message\": null,\n" +
300         "    \"type\": \"WhateverTypeException\",\n" +
301         "    \"code\": 999999999\n" +
302         "  }\n" +
303         "}";
304     message = str.getBytes();
305     e = GraphApiException.create (status, headers, message);
306
307     assertTrue(e.getClass().equals(UnmappedErrorException.class));
308     LOG.debug("{}", e.toString());
309     assertNull(e.getMessage());
310     try
311     {
312       Type type = e.getType();
313       LOG.error("unknown type: {}", type);
314       fail("unmapped type was resolved by enum: " + type);
315     }
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());
322
323
324     str =
325         "{\n" +
326         "  \"error\":\n" +
327         "  {\n" +
328         "    \"type\": \"WhateverTypeException\",\n" +
329         "    \"code\": 999999999\n" +
330         "  }\n" +
331         "}";
332     message = str.getBytes();
333     e = GraphApiException.create (status, headers, message);
334
335     assertTrue(e.getClass().equals(UnmappedErrorException.class));
336     LOG.debug("{}", e.toString());
337     assertNull(e.getMessage());
338     try
339     {
340       Type type = e.getType();
341       LOG.error("unknown type: {}", type);
342       fail("unmapped type was resolved by enum: " + type);
343     }
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());
350
351
352     str =
353         "{\n" +
354         "  \"error\":\n" +
355         "  {\n" +
356         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
357         "    \"type\": null,\n" +
358         "    \"code\": 999999999\n" +
359         "  }\n" +
360         "}";
361     message = str.getBytes();
362     e = GraphApiException.create (status, headers, message);
363
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());
373
374
375     str =
376         "{\n" +
377         "  \"error\":\n" +
378         "  {\n" +
379         "    \"message\": \"An unmapped Graph-API-Exception.\",\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   @Test
398   public void testInvlalidErrors()
399   {
400     String str;
401     byte[] message;
402     GraphApiException e;
403
404
405     str =
406         "{\n" +
407         "  \"error\":\n" +
408         "  {\n" +
409         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
410         "    \"type\": \"Whatever\",\n" +
411         "    \"code\": \"some string\"\n" +
412         "  }\n" +
413         "}";
414     message = str.getBytes();
415     e = GraphApiException.create (status, headers, message);
416     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
417     LOG.debug("{}", e.toString());
418
419
420     str =
421         "{\n" +
422         "  \"error\":\n" +
423         "  {\n" +
424         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
425         "    \"type\": \"Whatever\",\n" +
426         "    \"code\": 9.9\n" +
427         "  }\n" +
428         "}";
429     message = str.getBytes();
430     e = GraphApiException.create (status, headers, message);
431     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
432     LOG.debug("{}", e.toString());
433
434
435     str =
436         "{\n" +
437         "  \"error\":\n" +
438         "  {\n" +
439         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
440         "    \"type\": \"Whatever\",\n" +
441         "    \"code\": null\n" +
442         "  }\n" +
443         "}";
444     message = str.getBytes();
445     e = GraphApiException.create (status, headers, message);
446     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
447     LOG.debug("{}", e.toString());
448
449
450     str =
451         "{\n" +
452         "  \"error\":\n" +
453         "  {\n" +
454         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
455         "    \"type\": \"Whatever\"\n" +
456         "  }\n" +
457         "}";
458     message = str.getBytes();
459     e = GraphApiException.create (status, headers, message);
460     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
461     LOG.debug("{}", e.toString());
462
463
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());
469
470
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());
476
477
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());
483
484
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());
490
491
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());
497
498
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());
504
505
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());
511
512
513     str ="{}";
514     message = str.getBytes();
515     e = GraphApiException.create (status, headers, message);
516     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
517     LOG.debug("{}", e.toString());
518
519
520     str ="";
521     message = str.getBytes();
522     e = GraphApiException.create (status, headers, message);
523     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
524     LOG.debug("{}", e.toString());
525   }
526 }