200: The user hasn't authorized the application to perform this action
[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 testError200()
281   {
282     log.info("testError200");
283
284     requestFactory.setBody("{\n" +
285         "  \"error\": {\n" +
286         "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
287         "    \"type\": \"OAuthException\",\n" +
288         "    \"code\": 200\n" +
289         "  }\n" +
290         "}");
291
292     try
293     {
294       clientTemplate.getForObject("ANY", SOME.class);
295       fail("The expected exception was not thrown");
296     }
297     catch(ApplicationNotAuthorizedByUserException e)
298     {
299       log.debug("{}", e.toString());
300       assertEquals(new Integer(200), e.getCode());
301       assertEquals("(#200) The user hasn't authorized the application to perform this action", e.getMessage());
302       assertEquals(Type.OAuthException, e.getType());
303       assertNull(e.getTraceId());
304     }
305   }
306
307   @Test
308   public void testError613()
309   {
310     log.info("testError613");
311
312
313     requestFactory.setBody(
314         "{\n" +
315         "  \"error\":\n" +
316         "  {\n" +
317         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
318         "    \"type\": \"OAuthException\",\n" +
319         "    \"code\": 613\n" +
320         "  }\n" +
321         "}");
322
323     try
324     {
325       clientTemplate.getForObject("ANY", SOME.class);
326       fail("The expected exception was not thrown");
327     }
328     catch(RateLimitExceededException e)
329     {
330       log.debug("{}", e.toString());
331       assertEquals(new Integer(613), e.getCode());
332       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
333       assertEquals(Type.OAuthException, e.getType());
334     }
335   }
336
337   @Test
338   public void testError2200()
339   {
340     requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
341     
342     try
343     {
344       clientTemplate.getForObject("ANY", SOME.class);
345       fail("The expected exception was not thrown");
346     }
347     catch(CallbackVerificationFailedException e)
348     {
349       log.debug("{}", e.toString());
350       assertEquals(new Integer(2200), e.getCode());
351       assertEquals("(#2200) callback verification failed: ", e.getMessage());
352       assertEquals(Type.OAuthException, e.getType());
353       assertEquals("ESLjoZKvPXg", e.getTraceId());
354     }
355   }
356
357   @Test
358   public void testUnmappedError()
359   {
360     log.info("testUnmappedError");
361
362
363     requestFactory.setBody(
364         "{\n" +
365         "  \"error\":\n" +
366         "  {\n" +
367         "    \"message\": \"This error does not exist.\",\n" +
368         "    \"type\": \"NonexistentTypeException\",\n" +
369         "    \"code\": 999999999\n" +
370         "  }\n" +
371         "}");
372
373     try
374     {
375       clientTemplate.getForObject("ANY", SOME.class);
376       fail("The expected exception was not thrown");
377     }
378     catch(GraphApiException e)
379     {
380       log.debug("{}", e.toString());
381       assertEquals(new Integer(999999999), e.getCode());
382       assertEquals("This error does not exist.", e.getMessage());
383       try
384       {
385         Type type = e.getType();
386         log.error("unknown type: {}", type);
387         fail("unmapped type was resolved by enum: " + type);
388       }
389       catch (IllegalArgumentException ee) {}
390     }
391   }
392
393   @Test
394   public void testUnmappedErrors()
395   {
396     log.info("testUnmappedErrors");
397
398
399     requestFactory.setBody(
400         "{\n" +
401         "  \"error\":\n" +
402         "  {\n" +
403         "    \"message\": null,\n" +
404         "    \"type\": \"WhateverTypeException\",\n" +
405         "    \"code\": 999999999\n" +
406         "  }\n" +
407         "}");
408
409     try
410     {
411       clientTemplate.getForObject("ANY", SOME.class);
412       fail("The expected exception was not thrown");
413     }
414     catch(UnmappedErrorException e)
415     {
416       log.debug("{}", e.toString());
417       assertNull(e.getMessage());
418       try
419       {
420         Type type = e.getType();
421         log.error("unknown type: {}", type);
422         fail("unmapped type was resolved by enum: " + type);
423       }
424       catch (IllegalArgumentException ee) {}
425       assertEquals(new Integer(999999999), e.getCode());
426       assertNull(e.getSubCode());
427       assertNull(e.getUserTitle());
428       assertNull(e.getUserMessage());
429       assertNull(e.getTraceId());
430     }
431     catch(Exception e)
432     {
433       fail("A wrong exception was thrown: " + e.toString());
434     }
435
436
437     requestFactory.setBody(
438         "{\n" +
439         "  \"error\":\n" +
440         "  {\n" +
441         "    \"type\": \"WhateverTypeException\",\n" +
442         "    \"code\": 999999999\n" +
443         "  }\n" +
444         "}");
445
446     try
447     {
448       clientTemplate.getForObject("ANY", SOME.class);
449       fail("The expected exception was not thrown");
450     }
451     catch(UnmappedErrorException e)
452     {
453       log.debug("{}", e.toString());
454       assertNull(e.getMessage());
455       try
456       {
457         Type type = e.getType();
458         log.error("unknown type: {}", type);
459         fail("unmapped type was resolved by enum: " + type);
460       }
461       catch (IllegalArgumentException ee) {}
462       assertEquals(new Integer(999999999), e.getCode());
463       assertNull(e.getSubCode());
464       assertNull(e.getUserTitle());
465       assertNull(e.getUserMessage());
466       assertNull(e.getTraceId());
467     }
468     catch(Exception e)
469     {
470       fail("A wrong exception was thrown: " + e.toString());
471     }
472
473
474     requestFactory.setBody(
475         "{\n" +
476         "  \"error\":\n" +
477         "  {\n" +
478         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
479         "    \"type\": null,\n" +
480         "    \"code\": 999999999\n" +
481         "  }\n" +
482         "}");
483
484     try
485     {
486       clientTemplate.getForObject("ANY", SOME.class);
487       fail("The expected exception was not thrown");
488     }
489     catch(UnmappedErrorException e)
490     {
491       log.debug("{}", e.toString());
492       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
493       assertNull(e.getType());
494       assertEquals(new Integer(999999999), e.getCode());
495       assertNull(e.getSubCode());
496       assertNull(e.getUserTitle());
497       assertNull(e.getUserMessage());
498       assertNull(e.getTraceId());
499     }
500     catch(Exception e)
501     {
502       fail("A wrong exception was thrown: " + e.toString());
503     }
504
505
506     requestFactory.setBody(
507         "{\n" +
508         "  \"error\":\n" +
509         "  {\n" +
510         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
511         "    \"code\": 999999999\n" +
512         "  }\n" +
513         "}");
514
515     try
516     {
517       clientTemplate.getForObject("ANY", SOME.class);
518       fail("The expected exception was not thrown");
519     }
520     catch(UnmappedErrorException e)
521     {
522       log.debug("{}", e.toString());
523       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
524       assertNull(e.getType());
525       assertEquals(new Integer(999999999), e.getCode());
526       assertNull(e.getSubCode());
527       assertNull(e.getUserTitle());
528       assertNull(e.getUserMessage());
529       assertNull(e.getTraceId());
530     }
531     catch(Exception e)
532     {
533       fail("A wrong exception was thrown: " + e.toString());
534     }
535   }
536
537   @Test
538   public void testInvlalidErrors()
539   {
540     log.info("testInvalidErrors");
541
542
543     requestFactory.setBody(
544         "{\n" +
545         "  \"error\":\n" +
546         "  {\n" +
547         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
548         "    \"type\": \"Whatever\",\n" +
549         "    \"code\": \"some string\"\n" +
550         "  }\n" +
551         "}");
552
553     try
554     {
555       clientTemplate.getForObject("ANY", SOME.class);
556       fail("The expected exception was not thrown");
557     }
558     catch(HttpClientErrorException e)
559     {
560       log.debug("{}", e.toString());
561     }
562     catch(Exception e)
563     {
564       fail("A wrong exception was thrown: " + e.toString());
565     }
566
567
568     requestFactory.setBody(
569         "{\n" +
570         "  \"error\":\n" +
571         "  {\n" +
572         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
573         "    \"type\": \"Whatever\",\n" +
574         "    \"code\": 9.9\n" +
575         "  }\n" +
576         "}");
577
578     try
579     {
580       clientTemplate.getForObject("ANY", SOME.class);
581       fail("The expected exception was not thrown");
582     }
583     catch(HttpClientErrorException e)
584     {
585       log.debug("{}", e.toString());
586     }
587     catch(Exception e)
588     {
589       fail("A wrong exception was thrown: " + e.toString());
590     }
591
592
593     requestFactory.setBody(
594         "{\n" +
595         "  \"error\":\n" +
596         "  {\n" +
597         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
598         "    \"type\": \"Whatever\",\n" +
599         "    \"code\": null\n" +
600         "  }\n" +
601         "}");
602
603     try
604     {
605       clientTemplate.getForObject("ANY", SOME.class);
606       fail("The expected exception was not thrown");
607     }
608     catch(HttpClientErrorException e)
609     {
610       log.debug("{}", e.toString());
611     }
612     catch(Exception e)
613     {
614       fail("A wrong exception was thrown: " + e.toString());
615     }
616
617
618     requestFactory.setBody(
619         "{\n" +
620         "  \"error\":\n" +
621         "  {\n" +
622         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
623         "    \"type\": \"Whatever\"\n" +
624         "  }\n" +
625         "}");
626
627     try
628     {
629       clientTemplate.getForObject("ANY", SOME.class);
630       fail("The expected exception was not thrown");
631     }
632     catch(HttpClientErrorException e)
633     {
634       log.debug("{}", e.toString());
635     }
636     catch(Exception e)
637     {
638       fail("A wrong exception was thrown: " + e.toString());
639     }
640
641
642     requestFactory.setBody("{\"error\":{\"message\":null}}");
643
644     try
645     {
646       clientTemplate.getForObject("ANY", SOME.class);
647       fail("The expected exception was not thrown");
648     }
649     catch(HttpClientErrorException e)
650     {
651       log.debug("{}", e.toString());
652     }
653     catch(Exception e)
654     {
655       fail("A wrong exception was thrown: " + e.toString());
656     }
657
658
659     requestFactory.setBody("{\"error\":{\"type\":null}}");
660
661     try
662     {
663       clientTemplate.getForObject("ANY", SOME.class);
664       fail("The expected exception was not thrown");
665     }
666     catch(HttpClientErrorException e)
667     {
668       log.debug("{}", e.toString());
669     }
670     catch(Exception e)
671     {
672       fail("A wrong exception was thrown: " + e.toString());
673     }
674
675
676     requestFactory.setBody("{\"error\":{\"code\":null}}");
677
678     try
679     {
680       clientTemplate.getForObject("ANY", SOME.class);
681       fail("The expected exception was not thrown");
682     }
683     catch(HttpClientErrorException e)
684     {
685       log.debug("{}", e.toString());
686     }
687     catch(Exception e)
688     {
689       fail("A wrong exception was thrown: " + e.toString());
690     }
691
692
693     requestFactory.setBody("{\"error\":{}}");
694
695     try
696     {
697       clientTemplate.getForObject("ANY", SOME.class);
698       fail("The expected exception was not thrown");
699     }
700     catch(HttpClientErrorException e)
701     {
702       log.debug("{}", e.toString());
703     }
704     catch(Exception e)
705     {
706       fail("A wrong exception was thrown: " + e.toString());
707     }
708
709
710     requestFactory.setBody("{\"error\":\"some message\"}");
711
712     try
713     {
714       clientTemplate.getForObject("ANY", SOME.class);
715       fail("The expected exception was not thrown");
716     }
717     catch(HttpClientErrorException e)
718     {
719       log.debug("{}", e.toString());
720     }
721     catch(Exception e)
722     {
723       fail("A wrong exception was thrown: " + e.toString());
724     }
725
726
727     requestFactory.setBody("{\"error\":null}");
728
729     try
730     {
731       clientTemplate.getForObject("ANY", SOME.class);
732       fail("The expected exception was not thrown");
733     }
734     catch(HttpClientErrorException e)
735     {
736       log.debug("{}", e.toString());
737     }
738     catch(Exception e)
739     {
740       fail("A wrong exception was thrown: " + e.toString());
741     }
742
743
744     requestFactory.setBody("{\"some filed\":\"some message\"}");
745
746     try
747     {
748       clientTemplate.getForObject("ANY", SOME.class);
749       fail("The expected exception was not thrown");
750     }
751     catch(HttpClientErrorException e)
752     {
753       log.debug("{}", e.toString());
754     }
755     catch(Exception e)
756     {
757       fail("A wrong exception was thrown: " + e.toString());
758     }
759
760
761     requestFactory.setBody("{}");
762
763     try
764     {
765       clientTemplate.getForObject("ANY", SOME.class);
766       fail("The expected exception was not thrown");
767     }
768     catch(HttpClientErrorException e)
769     {
770       log.debug("{}", e.toString());
771     }
772     catch(Exception e)
773     {
774       fail("A wrong exception was thrown: " + e.toString());
775     }
776
777
778     requestFactory.setBody("");
779
780     try
781     {
782       clientTemplate.getForObject("ANY", SOME.class);
783       fail("The expected exception was not thrown");
784     }
785     catch(HttpClientErrorException e)
786     {
787       log.debug("{}", e.toString());
788     }
789     catch(Exception e)
790     {
791       fail("A wrong exception was thrown: " + e.toString());
792     }
793   }
794
795
796   @Before
797   public void setUp()
798   {
799     requestFactory = new MockClientHttpRequestFactory();
800     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
801     requestFactory.addHeader("Content-Type", "application/json");
802
803     clientTemplate = new RestTemplate();
804     clientTemplate.setRequestFactory(requestFactory);
805     clientTemplate.setErrorHandler(
806         new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
807         );
808   }
809
810
811   static class SOME
812   {
813   }
814 }