190: Access token expired
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiErrorHandlerTest.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.fail;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.web.client.HttpClientErrorException;
14 import org.springframework.web.client.RestTemplate;
15
16
17
18 /**
19  *
20  * @author Kai Moritz
21  */
22 public class GraphApiErrorHandlerTest
23 {
24   private static final Logger log =
25       LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
26
27   private RestTemplate clientTemplate;
28   private MockClientHttpRequestFactory requestFactory;
29
30
31   @Test
32   public void testError1()
33   {
34     log.info("testError1");
35
36
37     requestFactory.setBody(
38         "{\n" +
39         "  \"error\":\n" +
40         "  {\n" +
41         "    \"message\": \"An unknown error has occurred.\",\n" +
42         "    \"type\": \"OAuthException\",\n" +
43         "    \"code\": 1\n" +
44         "  }\n" +
45         "}");
46
47     try
48     {
49       clientTemplate.getForObject("ANY", SOME.class);
50       fail("The expected exception was not thrown");
51     }
52     catch(UnknownErrorException e)
53     {
54       log.debug("{}", e.toString());
55       assertEquals(new Integer(1), e.getCode());
56       assertEquals("An unknown error has occurred.", e.getMessage());
57       assertEquals(Type.OAuthException, e.getType());
58     }
59   }
60
61   @Test
62   public void testError2()
63   {
64     log.info("testError2");
65
66
67     requestFactory.setBody(
68         "{\n" +
69         "  \"error\":\n" +
70         "  {\n" +
71         "    \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
72         "    \"type\": \"OAuthException\",\n" +
73         "    \"code\": 2\n" +
74         "  }\n" +
75         "}");
76
77     try
78     {
79       clientTemplate.getForObject("ANY", SOME.class);
80       fail("The expected exception was not thrown");
81     }
82     catch(UnexpectedErrorException e)
83     {
84       log.debug("{}", e.toString());
85       assertEquals(new Integer(2), e.getCode());
86       assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
87       assertEquals(Type.OAuthException, e.getType());
88     }
89   }
90
91   @Test
92   public void testError4()
93   {
94     log.info("testError4");
95
96
97     requestFactory.setBody(
98         "{\n" +
99         "    \"error\": {\n" +
100         "        \"code\": 4, \n" +
101         "        \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
102         "        \"is_transient\": true, \n" +
103         "        \"message\": \"(#4) Application request limit reached\", \n" +
104         "        \"type\": \"OAuthException\"\n" +
105         "    }\n" +
106         "}\n");
107
108     try
109     {
110       clientTemplate.getForObject("ANY", SOME.class);
111       fail("The expected exception was not thrown");
112     }
113     catch(ApplicationRequestLimitReachedException e)
114     {
115       log.debug("{}", e.toString());
116       assertEquals(new Integer(4), e.getCode());
117       assertEquals("(#4) Application request limit reached", e.getMessage());
118       assertEquals(Type.OAuthException, e.getType());
119     }
120   }
121
122   @Test
123   public void testError12()
124   {
125     log.info("testError12");
126
127
128     requestFactory.setBody(
129         "{\n" +
130         "  \"error\":\n" +
131         "  {\n" +
132         "    \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
133         "    \"type\": \"OAuthException\",\n" +
134         "    \"code\": 12\n," +
135         "    \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
136         "  }\n" +
137         "}");
138
139     try
140     {
141       clientTemplate.getForObject("ANY", SOME.class);
142       fail("The expected exception was not thrown");
143     }
144     catch(DeprecatedException e)
145     {
146       log.debug("{}", e.toString());
147       assertEquals(new Integer(12), e.getCode());
148       assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
149       assertEquals(Type.OAuthException, e.getType());
150     }
151   }
152
153   @Test
154   public void testError21()
155   {
156     log.info("testError21");
157
158
159     requestFactory.setBody(
160         "{\n" +
161         "  \"error\":\n" +
162         "  {\n" +
163         "    \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID\",\n" +
164         "    \"type\": \"OAuthException\",\n" +
165         "    \"code\": 21\n" +
166         "  }\n" +
167         "}");
168
169     try
170     {
171       clientTemplate.getForObject("ANY", SOME.class);
172       fail("The expected exception was not thrown");
173     }
174     catch(PageMigratedException e)
175     {
176       log.debug("{}", e.toString());
177       assertEquals(new Integer(21), e.getCode());
178       assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID", e.getMessage());
179       assertEquals(Type.OAuthException, e.getType());
180     }
181   }
182
183   @Test
184   public void testError100()
185   {
186     log.info("testError100");
187
188
189     requestFactory.setBody(
190         "{\n" +
191         "  \"error\":\n" +
192         "  {\n" +
193         "    \"message\": \"Unsupported get request.\",\n" +
194         "    \"type\": \"GraphMethodException\",\n" +
195         "    \"code\": 100\n" +
196         "  }\n" +
197         "}");
198
199     try
200     {
201       clientTemplate.getForObject("ANY", SOME.class);
202       fail("The expected exception was not thrown");
203     }
204     catch(UnsupportedGetRequestException e)
205     {
206       log.debug("{}", e.toString());
207       assertEquals(new Integer(100), e.getCode());
208       assertEquals("Unsupported get request.", e.getMessage());
209       assertEquals(Type.GraphMethodException, e.getType());
210     }
211   }
212
213   @Test
214   public void testError102()
215   {
216     log.info("testError102");
217
218     requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
219
220     try
221     {
222       clientTemplate.getForObject("ANY", SOME.class);
223       fail("The expected exception was not thrown");
224     }
225     catch(UserAccessTokenRequiredException e)
226     {
227       log.debug("{}", e.toString());
228       assertEquals(new Integer(102), e.getCode());
229       assertEquals("A user access token is required to request this resource.", e.getMessage());
230       assertEquals(Type.OAuthException, e.getType());
231       assertEquals("DhdMyf23Ki7", e.getTraceId());
232     }
233   }
234
235   @Test
236   public void testError104()
237   {
238     log.info("testError104");
239
240     requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
241
242     try
243     {
244       clientTemplate.getForObject("ANY", SOME.class);
245       fail("The expected exception was not thrown");
246     }
247     catch(AccessTokenRequiredException e)
248     {
249       log.debug("{}", e.toString());
250       assertEquals(new Integer(104), e.getCode());
251       assertEquals("An access token is required to request this resource.", e.getMessage());
252       assertEquals(Type.OAuthException, e.getType());
253       assertEquals("E2Jjkj5++LL", e.getTraceId());
254     }
255   }
256
257   @Test
258   public void testError190()
259   {
260     log.info("testError190");
261
262     requestFactory.setBody("{\"error\":{\"message\":\"Bad signature\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"Ay2OYQrINbXOCfQpBvoxDIw\"}}");
263
264     try
265     {
266       clientTemplate.getForObject("ANY", SOME.class);
267       fail("The expected exception was not thrown");
268     }
269     catch(AccessTokenExpiredException e)
270     {
271       log.debug("{}", e.toString());
272       assertEquals(new Integer(190), e.getCode());
273       assertEquals("Bad signature", e.getMessage());
274       assertEquals(Type.OAuthException, e.getType());
275       assertEquals("Ay2OYQrINbXOCfQpBvoxDIw", e.getTraceId());
276     }
277   }
278
279   @Test
280   public void testError613()
281   {
282     log.info("testError613");
283
284
285     requestFactory.setBody(
286         "{\n" +
287         "  \"error\":\n" +
288         "  {\n" +
289         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
290         "    \"type\": \"OAuthException\",\n" +
291         "    \"code\": 613\n" +
292         "  }\n" +
293         "}");
294
295     try
296     {
297       clientTemplate.getForObject("ANY", SOME.class);
298       fail("The expected exception was not thrown");
299     }
300     catch(RateLimitExceededException e)
301     {
302       log.debug("{}", e.toString());
303       assertEquals(new Integer(613), e.getCode());
304       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
305       assertEquals(Type.OAuthException, e.getType());
306     }
307   }
308
309   @Test
310   public void testError2200()
311   {
312     requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
313     
314     try
315     {
316       clientTemplate.getForObject("ANY", SOME.class);
317       fail("The expected exception was not thrown");
318     }
319     catch(CallbackVerificationFailedException e)
320     {
321       log.debug("{}", e.toString());
322       assertEquals(new Integer(2200), e.getCode());
323       assertEquals("(#2200) callback verification failed: ", e.getMessage());
324       assertEquals(Type.OAuthException, e.getType());
325       assertEquals("ESLjoZKvPXg", e.getTraceId());
326     }
327   }
328
329   @Test
330   public void testUnmappedError()
331   {
332     log.info("testUnmappedError");
333
334
335     requestFactory.setBody(
336         "{\n" +
337         "  \"error\":\n" +
338         "  {\n" +
339         "    \"message\": \"This error does not exist.\",\n" +
340         "    \"type\": \"NonexistentTypeException\",\n" +
341         "    \"code\": 999999999\n" +
342         "  }\n" +
343         "}");
344
345     try
346     {
347       clientTemplate.getForObject("ANY", SOME.class);
348       fail("The expected exception was not thrown");
349     }
350     catch(GraphApiException e)
351     {
352       log.debug("{}", e.toString());
353       assertEquals(new Integer(999999999), e.getCode());
354       assertEquals("This error does not exist.", e.getMessage());
355       try
356       {
357         Type type = e.getType();
358         log.error("unknown type: {}", type);
359         fail("unmapped type was resolved by enum: " + type);
360       }
361       catch (IllegalArgumentException ee) {}
362     }
363   }
364
365   @Test
366   public void testUnmappedErrors()
367   {
368     log.info("testUnmappedErrors");
369
370
371     requestFactory.setBody(
372         "{\n" +
373         "  \"error\":\n" +
374         "  {\n" +
375         "    \"message\": null,\n" +
376         "    \"type\": \"WhateverTypeException\",\n" +
377         "    \"code\": 999999999\n" +
378         "  }\n" +
379         "}");
380
381     try
382     {
383       clientTemplate.getForObject("ANY", SOME.class);
384       fail("The expected exception was not thrown");
385     }
386     catch(UnmappedErrorException e)
387     {
388       log.debug("{}", e.toString());
389       assertNull(e.getMessage());
390       try
391       {
392         Type type = e.getType();
393         log.error("unknown type: {}", type);
394         fail("unmapped type was resolved by enum: " + type);
395       }
396       catch (IllegalArgumentException ee) {}
397       assertEquals(new Integer(999999999), e.getCode());
398       assertNull(e.getSubCode());
399       assertNull(e.getUserTitle());
400       assertNull(e.getUserMessage());
401       assertNull(e.getTraceId());
402     }
403     catch(Exception e)
404     {
405       fail("A wrong exception was thrown: " + e.toString());
406     }
407
408
409     requestFactory.setBody(
410         "{\n" +
411         "  \"error\":\n" +
412         "  {\n" +
413         "    \"type\": \"WhateverTypeException\",\n" +
414         "    \"code\": 999999999\n" +
415         "  }\n" +
416         "}");
417
418     try
419     {
420       clientTemplate.getForObject("ANY", SOME.class);
421       fail("The expected exception was not thrown");
422     }
423     catch(UnmappedErrorException e)
424     {
425       log.debug("{}", e.toString());
426       assertNull(e.getMessage());
427       try
428       {
429         Type type = e.getType();
430         log.error("unknown type: {}", type);
431         fail("unmapped type was resolved by enum: " + type);
432       }
433       catch (IllegalArgumentException ee) {}
434       assertEquals(new Integer(999999999), e.getCode());
435       assertNull(e.getSubCode());
436       assertNull(e.getUserTitle());
437       assertNull(e.getUserMessage());
438       assertNull(e.getTraceId());
439     }
440     catch(Exception e)
441     {
442       fail("A wrong exception was thrown: " + e.toString());
443     }
444
445
446     requestFactory.setBody(
447         "{\n" +
448         "  \"error\":\n" +
449         "  {\n" +
450         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
451         "    \"type\": null,\n" +
452         "    \"code\": 999999999\n" +
453         "  }\n" +
454         "}");
455
456     try
457     {
458       clientTemplate.getForObject("ANY", SOME.class);
459       fail("The expected exception was not thrown");
460     }
461     catch(UnmappedErrorException e)
462     {
463       log.debug("{}", e.toString());
464       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
465       assertNull(e.getType());
466       assertEquals(new Integer(999999999), e.getCode());
467       assertNull(e.getSubCode());
468       assertNull(e.getUserTitle());
469       assertNull(e.getUserMessage());
470       assertNull(e.getTraceId());
471     }
472     catch(Exception e)
473     {
474       fail("A wrong exception was thrown: " + e.toString());
475     }
476
477
478     requestFactory.setBody(
479         "{\n" +
480         "  \"error\":\n" +
481         "  {\n" +
482         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
483         "    \"code\": 999999999\n" +
484         "  }\n" +
485         "}");
486
487     try
488     {
489       clientTemplate.getForObject("ANY", SOME.class);
490       fail("The expected exception was not thrown");
491     }
492     catch(UnmappedErrorException e)
493     {
494       log.debug("{}", e.toString());
495       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
496       assertNull(e.getType());
497       assertEquals(new Integer(999999999), e.getCode());
498       assertNull(e.getSubCode());
499       assertNull(e.getUserTitle());
500       assertNull(e.getUserMessage());
501       assertNull(e.getTraceId());
502     }
503     catch(Exception e)
504     {
505       fail("A wrong exception was thrown: " + e.toString());
506     }
507   }
508
509   @Test
510   public void testInvlalidErrors()
511   {
512     log.info("testInvalidErrors");
513
514
515     requestFactory.setBody(
516         "{\n" +
517         "  \"error\":\n" +
518         "  {\n" +
519         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
520         "    \"type\": \"Whatever\",\n" +
521         "    \"code\": \"some string\"\n" +
522         "  }\n" +
523         "}");
524
525     try
526     {
527       clientTemplate.getForObject("ANY", SOME.class);
528       fail("The expected exception was not thrown");
529     }
530     catch(HttpClientErrorException e)
531     {
532       log.debug("{}", e.toString());
533     }
534     catch(Exception e)
535     {
536       fail("A wrong exception was thrown: " + e.toString());
537     }
538
539
540     requestFactory.setBody(
541         "{\n" +
542         "  \"error\":\n" +
543         "  {\n" +
544         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
545         "    \"type\": \"Whatever\",\n" +
546         "    \"code\": 9.9\n" +
547         "  }\n" +
548         "}");
549
550     try
551     {
552       clientTemplate.getForObject("ANY", SOME.class);
553       fail("The expected exception was not thrown");
554     }
555     catch(HttpClientErrorException e)
556     {
557       log.debug("{}", e.toString());
558     }
559     catch(Exception e)
560     {
561       fail("A wrong exception was thrown: " + e.toString());
562     }
563
564
565     requestFactory.setBody(
566         "{\n" +
567         "  \"error\":\n" +
568         "  {\n" +
569         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
570         "    \"type\": \"Whatever\",\n" +
571         "    \"code\": null\n" +
572         "  }\n" +
573         "}");
574
575     try
576     {
577       clientTemplate.getForObject("ANY", SOME.class);
578       fail("The expected exception was not thrown");
579     }
580     catch(HttpClientErrorException e)
581     {
582       log.debug("{}", e.toString());
583     }
584     catch(Exception e)
585     {
586       fail("A wrong exception was thrown: " + e.toString());
587     }
588
589
590     requestFactory.setBody(
591         "{\n" +
592         "  \"error\":\n" +
593         "  {\n" +
594         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
595         "    \"type\": \"Whatever\"\n" +
596         "  }\n" +
597         "}");
598
599     try
600     {
601       clientTemplate.getForObject("ANY", SOME.class);
602       fail("The expected exception was not thrown");
603     }
604     catch(HttpClientErrorException e)
605     {
606       log.debug("{}", e.toString());
607     }
608     catch(Exception e)
609     {
610       fail("A wrong exception was thrown: " + e.toString());
611     }
612
613
614     requestFactory.setBody("{\"error\":{\"message\":null}}");
615
616     try
617     {
618       clientTemplate.getForObject("ANY", SOME.class);
619       fail("The expected exception was not thrown");
620     }
621     catch(HttpClientErrorException e)
622     {
623       log.debug("{}", e.toString());
624     }
625     catch(Exception e)
626     {
627       fail("A wrong exception was thrown: " + e.toString());
628     }
629
630
631     requestFactory.setBody("{\"error\":{\"type\":null}}");
632
633     try
634     {
635       clientTemplate.getForObject("ANY", SOME.class);
636       fail("The expected exception was not thrown");
637     }
638     catch(HttpClientErrorException e)
639     {
640       log.debug("{}", e.toString());
641     }
642     catch(Exception e)
643     {
644       fail("A wrong exception was thrown: " + e.toString());
645     }
646
647
648     requestFactory.setBody("{\"error\":{\"code\":null}}");
649
650     try
651     {
652       clientTemplate.getForObject("ANY", SOME.class);
653       fail("The expected exception was not thrown");
654     }
655     catch(HttpClientErrorException e)
656     {
657       log.debug("{}", e.toString());
658     }
659     catch(Exception e)
660     {
661       fail("A wrong exception was thrown: " + e.toString());
662     }
663
664
665     requestFactory.setBody("{\"error\":{}}");
666
667     try
668     {
669       clientTemplate.getForObject("ANY", SOME.class);
670       fail("The expected exception was not thrown");
671     }
672     catch(HttpClientErrorException e)
673     {
674       log.debug("{}", e.toString());
675     }
676     catch(Exception e)
677     {
678       fail("A wrong exception was thrown: " + e.toString());
679     }
680
681
682     requestFactory.setBody("{\"error\":\"some message\"}");
683
684     try
685     {
686       clientTemplate.getForObject("ANY", SOME.class);
687       fail("The expected exception was not thrown");
688     }
689     catch(HttpClientErrorException e)
690     {
691       log.debug("{}", e.toString());
692     }
693     catch(Exception e)
694     {
695       fail("A wrong exception was thrown: " + e.toString());
696     }
697
698
699     requestFactory.setBody("{\"error\":null}");
700
701     try
702     {
703       clientTemplate.getForObject("ANY", SOME.class);
704       fail("The expected exception was not thrown");
705     }
706     catch(HttpClientErrorException e)
707     {
708       log.debug("{}", e.toString());
709     }
710     catch(Exception e)
711     {
712       fail("A wrong exception was thrown: " + e.toString());
713     }
714
715
716     requestFactory.setBody("{\"some filed\":\"some message\"}");
717
718     try
719     {
720       clientTemplate.getForObject("ANY", SOME.class);
721       fail("The expected exception was not thrown");
722     }
723     catch(HttpClientErrorException e)
724     {
725       log.debug("{}", e.toString());
726     }
727     catch(Exception e)
728     {
729       fail("A wrong exception was thrown: " + e.toString());
730     }
731
732
733     requestFactory.setBody("{}");
734
735     try
736     {
737       clientTemplate.getForObject("ANY", SOME.class);
738       fail("The expected exception was not thrown");
739     }
740     catch(HttpClientErrorException e)
741     {
742       log.debug("{}", e.toString());
743     }
744     catch(Exception e)
745     {
746       fail("A wrong exception was thrown: " + e.toString());
747     }
748
749
750     requestFactory.setBody("");
751
752     try
753     {
754       clientTemplate.getForObject("ANY", SOME.class);
755       fail("The expected exception was not thrown");
756     }
757     catch(HttpClientErrorException e)
758     {
759       log.debug("{}", e.toString());
760     }
761     catch(Exception e)
762     {
763       fail("A wrong exception was thrown: " + e.toString());
764     }
765   }
766
767
768   @Before
769   public void setUp()
770   {
771     requestFactory = new MockClientHttpRequestFactory();
772     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
773     requestFactory.addHeader("Content-Type", "application/json");
774
775     clientTemplate = new RestTemplate();
776     clientTemplate.setRequestFactory(requestFactory);
777     clientTemplate.setErrorHandler(
778         new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
779         );
780   }
781
782
783   static class SOME
784   {
785   }
786 }