Added an exception for error 12: X field is deprecated vor version vY.Z and higher
[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 testError12()
93   {
94     log.info("testError12");
95
96
97     requestFactory.setBody(
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
108     try
109     {
110       clientTemplate.getForObject("ANY", SOME.class);
111       fail("The expected exception was not thrown");
112     }
113     catch(DeprecatedException e)
114     {
115       log.debug("{}", e.toString());
116       assertEquals(new Integer(12), e.getCode());
117       assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
118       assertEquals(Type.OAuthException, e.getType());
119     }
120   }
121
122   @Test
123   public void testError21()
124   {
125     log.info("testError21");
126
127
128     requestFactory.setBody(
129         "{\n" +
130         "  \"error\":\n" +
131         "  {\n" +
132         "    \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID\",\n" +
133         "    \"type\": \"OAuthException\",\n" +
134         "    \"code\": 21\n" +
135         "  }\n" +
136         "}");
137
138     try
139     {
140       clientTemplate.getForObject("ANY", SOME.class);
141       fail("The expected exception was not thrown");
142     }
143     catch(PageMigratedException e)
144     {
145       log.debug("{}", e.toString());
146       assertEquals(new Integer(21), e.getCode());
147       assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID", e.getMessage());
148       assertEquals(Type.OAuthException, e.getType());
149     }
150   }
151
152   @Test
153   public void testError100()
154   {
155     log.info("testError100");
156
157
158     requestFactory.setBody(
159         "{\n" +
160         "  \"error\":\n" +
161         "  {\n" +
162         "    \"message\": \"Unsupported get request.\",\n" +
163         "    \"type\": \"GraphMethodException\",\n" +
164         "    \"code\": 100\n" +
165         "  }\n" +
166         "}");
167
168     try
169     {
170       clientTemplate.getForObject("ANY", SOME.class);
171       fail("The expected exception was not thrown");
172     }
173     catch(UnsupportedGetRequestException e)
174     {
175       log.debug("{}", e.toString());
176       assertEquals(new Integer(100), e.getCode());
177       assertEquals("Unsupported get request.", e.getMessage());
178       assertEquals(Type.GraphMethodException, e.getType());
179     }
180   }
181
182   @Test
183   public void testError102()
184   {
185     log.info("testError102");
186
187     requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
188
189     try
190     {
191       clientTemplate.getForObject("ANY", SOME.class);
192       fail("The expected exception was not thrown");
193     }
194     catch(UserAccessTokenRequiredException e)
195     {
196       log.debug("{}", e.toString());
197       assertEquals(new Integer(102), e.getCode());
198       assertEquals("A user access token is required to request this resource.", e.getMessage());
199       assertEquals(Type.OAuthException, e.getType());
200       assertEquals("DhdMyf23Ki7", e.getTraceId());
201     }
202   }
203
204   @Test
205   public void testError104()
206   {
207     log.info("testError104");
208
209     requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
210
211     try
212     {
213       clientTemplate.getForObject("ANY", SOME.class);
214       fail("The expected exception was not thrown");
215     }
216     catch(AccessTokenRequiredException e)
217     {
218       log.debug("{}", e.toString());
219       assertEquals(new Integer(104), e.getCode());
220       assertEquals("An access token is required to request this resource.", e.getMessage());
221       assertEquals(Type.OAuthException, e.getType());
222       assertEquals("E2Jjkj5++LL", e.getTraceId());
223     }
224   }
225
226   @Test
227   public void testError613()
228   {
229     log.info("testError613");
230
231
232     requestFactory.setBody(
233         "{\n" +
234         "  \"error\":\n" +
235         "  {\n" +
236         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
237         "    \"type\": \"OAuthException\",\n" +
238         "    \"code\": 613\n" +
239         "  }\n" +
240         "}");
241
242     try
243     {
244       clientTemplate.getForObject("ANY", SOME.class);
245       fail("The expected exception was not thrown");
246     }
247     catch(RateLimitExceededException e)
248     {
249       log.debug("{}", e.toString());
250       assertEquals(new Integer(613), e.getCode());
251       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
252       assertEquals(Type.OAuthException, e.getType());
253     }
254   }
255
256   @Test
257   public void testError2200()
258   {
259     requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
260     
261     try
262     {
263       clientTemplate.getForObject("ANY", SOME.class);
264       fail("The expected exception was not thrown");
265     }
266     catch(CallbackVerificationFailedException e)
267     {
268       log.debug("{}", e.toString());
269       assertEquals(new Integer(2200), e.getCode());
270       assertEquals("(#2200) callback verification failed: ", e.getMessage());
271       assertEquals(Type.OAuthException, e.getType());
272       assertEquals("ESLjoZKvPXg", e.getTraceId());
273     }
274   }
275
276   @Test
277   public void testUnmappedError()
278   {
279     log.info("testUnmappedError");
280
281
282     requestFactory.setBody(
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
292     try
293     {
294       clientTemplate.getForObject("ANY", SOME.class);
295       fail("The expected exception was not thrown");
296     }
297     catch(GraphApiException e)
298     {
299       log.debug("{}", e.toString());
300       assertEquals(new Integer(999999999), e.getCode());
301       assertEquals("This error does not exist.", e.getMessage());
302       try
303       {
304         Type type = e.getType();
305         log.error("unknown type: {}", type);
306         fail("unmapped type was resolved by enum: " + type);
307       }
308       catch (IllegalArgumentException ee) {}
309     }
310   }
311
312   @Test
313   public void testUnmappedErrors()
314   {
315     log.info("testUnmappedErrors");
316
317
318     requestFactory.setBody(
319         "{\n" +
320         "  \"error\":\n" +
321         "  {\n" +
322         "    \"message\": null,\n" +
323         "    \"type\": \"WhateverTypeException\",\n" +
324         "    \"code\": 999999999\n" +
325         "  }\n" +
326         "}");
327
328     try
329     {
330       clientTemplate.getForObject("ANY", SOME.class);
331       fail("The expected exception was not thrown");
332     }
333     catch(UnmappedErrorException e)
334     {
335       log.debug("{}", e.toString());
336       assertNull(e.getMessage());
337       try
338       {
339         Type type = e.getType();
340         log.error("unknown type: {}", type);
341         fail("unmapped type was resolved by enum: " + type);
342       }
343       catch (IllegalArgumentException ee) {}
344       assertEquals(new Integer(999999999), e.getCode());
345       assertNull(e.getSubCode());
346       assertNull(e.getUserTitle());
347       assertNull(e.getUserMessage());
348       assertNull(e.getTraceId());
349     }
350     catch(Exception e)
351     {
352       fail("A wrong exception was thrown: " + e.toString());
353     }
354
355
356     requestFactory.setBody(
357         "{\n" +
358         "  \"error\":\n" +
359         "  {\n" +
360         "    \"type\": \"WhateverTypeException\",\n" +
361         "    \"code\": 999999999\n" +
362         "  }\n" +
363         "}");
364
365     try
366     {
367       clientTemplate.getForObject("ANY", SOME.class);
368       fail("The expected exception was not thrown");
369     }
370     catch(UnmappedErrorException e)
371     {
372       log.debug("{}", e.toString());
373       assertNull(e.getMessage());
374       try
375       {
376         Type type = e.getType();
377         log.error("unknown type: {}", type);
378         fail("unmapped type was resolved by enum: " + type);
379       }
380       catch (IllegalArgumentException ee) {}
381       assertEquals(new Integer(999999999), e.getCode());
382       assertNull(e.getSubCode());
383       assertNull(e.getUserTitle());
384       assertNull(e.getUserMessage());
385       assertNull(e.getTraceId());
386     }
387     catch(Exception e)
388     {
389       fail("A wrong exception was thrown: " + e.toString());
390     }
391
392
393     requestFactory.setBody(
394         "{\n" +
395         "  \"error\":\n" +
396         "  {\n" +
397         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
398         "    \"type\": null,\n" +
399         "    \"code\": 999999999\n" +
400         "  }\n" +
401         "}");
402
403     try
404     {
405       clientTemplate.getForObject("ANY", SOME.class);
406       fail("The expected exception was not thrown");
407     }
408     catch(UnmappedErrorException e)
409     {
410       log.debug("{}", e.toString());
411       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
412       assertNull(e.getType());
413       assertEquals(new Integer(999999999), e.getCode());
414       assertNull(e.getSubCode());
415       assertNull(e.getUserTitle());
416       assertNull(e.getUserMessage());
417       assertNull(e.getTraceId());
418     }
419     catch(Exception e)
420     {
421       fail("A wrong exception was thrown: " + e.toString());
422     }
423
424
425     requestFactory.setBody(
426         "{\n" +
427         "  \"error\":\n" +
428         "  {\n" +
429         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
430         "    \"code\": 999999999\n" +
431         "  }\n" +
432         "}");
433
434     try
435     {
436       clientTemplate.getForObject("ANY", SOME.class);
437       fail("The expected exception was not thrown");
438     }
439     catch(UnmappedErrorException e)
440     {
441       log.debug("{}", e.toString());
442       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
443       assertNull(e.getType());
444       assertEquals(new Integer(999999999), e.getCode());
445       assertNull(e.getSubCode());
446       assertNull(e.getUserTitle());
447       assertNull(e.getUserMessage());
448       assertNull(e.getTraceId());
449     }
450     catch(Exception e)
451     {
452       fail("A wrong exception was thrown: " + e.toString());
453     }
454   }
455
456   @Test
457   public void testInvlalidErrors()
458   {
459     log.info("testInvalidErrors");
460
461
462     requestFactory.setBody(
463         "{\n" +
464         "  \"error\":\n" +
465         "  {\n" +
466         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
467         "    \"type\": \"Whatever\",\n" +
468         "    \"code\": \"some string\"\n" +
469         "  }\n" +
470         "}");
471
472     try
473     {
474       clientTemplate.getForObject("ANY", SOME.class);
475       fail("The expected exception was not thrown");
476     }
477     catch(HttpClientErrorException e)
478     {
479       log.debug("{}", e.toString());
480     }
481     catch(Exception e)
482     {
483       fail("A wrong exception was thrown: " + e.toString());
484     }
485
486
487     requestFactory.setBody(
488         "{\n" +
489         "  \"error\":\n" +
490         "  {\n" +
491         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
492         "    \"type\": \"Whatever\",\n" +
493         "    \"code\": 9.9\n" +
494         "  }\n" +
495         "}");
496
497     try
498     {
499       clientTemplate.getForObject("ANY", SOME.class);
500       fail("The expected exception was not thrown");
501     }
502     catch(HttpClientErrorException e)
503     {
504       log.debug("{}", e.toString());
505     }
506     catch(Exception e)
507     {
508       fail("A wrong exception was thrown: " + e.toString());
509     }
510
511
512     requestFactory.setBody(
513         "{\n" +
514         "  \"error\":\n" +
515         "  {\n" +
516         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
517         "    \"type\": \"Whatever\",\n" +
518         "    \"code\": null\n" +
519         "  }\n" +
520         "}");
521
522     try
523     {
524       clientTemplate.getForObject("ANY", SOME.class);
525       fail("The expected exception was not thrown");
526     }
527     catch(HttpClientErrorException e)
528     {
529       log.debug("{}", e.toString());
530     }
531     catch(Exception e)
532     {
533       fail("A wrong exception was thrown: " + e.toString());
534     }
535
536
537     requestFactory.setBody(
538         "{\n" +
539         "  \"error\":\n" +
540         "  {\n" +
541         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
542         "    \"type\": \"Whatever\"\n" +
543         "  }\n" +
544         "}");
545
546     try
547     {
548       clientTemplate.getForObject("ANY", SOME.class);
549       fail("The expected exception was not thrown");
550     }
551     catch(HttpClientErrorException e)
552     {
553       log.debug("{}", e.toString());
554     }
555     catch(Exception e)
556     {
557       fail("A wrong exception was thrown: " + e.toString());
558     }
559
560
561     requestFactory.setBody("{\"error\":{\"message\":null}}");
562
563     try
564     {
565       clientTemplate.getForObject("ANY", SOME.class);
566       fail("The expected exception was not thrown");
567     }
568     catch(HttpClientErrorException e)
569     {
570       log.debug("{}", e.toString());
571     }
572     catch(Exception e)
573     {
574       fail("A wrong exception was thrown: " + e.toString());
575     }
576
577
578     requestFactory.setBody("{\"error\":{\"type\":null}}");
579
580     try
581     {
582       clientTemplate.getForObject("ANY", SOME.class);
583       fail("The expected exception was not thrown");
584     }
585     catch(HttpClientErrorException e)
586     {
587       log.debug("{}", e.toString());
588     }
589     catch(Exception e)
590     {
591       fail("A wrong exception was thrown: " + e.toString());
592     }
593
594
595     requestFactory.setBody("{\"error\":{\"code\":null}}");
596
597     try
598     {
599       clientTemplate.getForObject("ANY", SOME.class);
600       fail("The expected exception was not thrown");
601     }
602     catch(HttpClientErrorException e)
603     {
604       log.debug("{}", e.toString());
605     }
606     catch(Exception e)
607     {
608       fail("A wrong exception was thrown: " + e.toString());
609     }
610
611
612     requestFactory.setBody("{\"error\":{}}");
613
614     try
615     {
616       clientTemplate.getForObject("ANY", SOME.class);
617       fail("The expected exception was not thrown");
618     }
619     catch(HttpClientErrorException e)
620     {
621       log.debug("{}", e.toString());
622     }
623     catch(Exception e)
624     {
625       fail("A wrong exception was thrown: " + e.toString());
626     }
627
628
629     requestFactory.setBody("{\"error\":\"some message\"}");
630
631     try
632     {
633       clientTemplate.getForObject("ANY", SOME.class);
634       fail("The expected exception was not thrown");
635     }
636     catch(HttpClientErrorException e)
637     {
638       log.debug("{}", e.toString());
639     }
640     catch(Exception e)
641     {
642       fail("A wrong exception was thrown: " + e.toString());
643     }
644
645
646     requestFactory.setBody("{\"error\":null}");
647
648     try
649     {
650       clientTemplate.getForObject("ANY", SOME.class);
651       fail("The expected exception was not thrown");
652     }
653     catch(HttpClientErrorException e)
654     {
655       log.debug("{}", e.toString());
656     }
657     catch(Exception e)
658     {
659       fail("A wrong exception was thrown: " + e.toString());
660     }
661
662
663     requestFactory.setBody("{\"some filed\":\"some message\"}");
664
665     try
666     {
667       clientTemplate.getForObject("ANY", SOME.class);
668       fail("The expected exception was not thrown");
669     }
670     catch(HttpClientErrorException e)
671     {
672       log.debug("{}", e.toString());
673     }
674     catch(Exception e)
675     {
676       fail("A wrong exception was thrown: " + e.toString());
677     }
678
679
680     requestFactory.setBody("{}");
681
682     try
683     {
684       clientTemplate.getForObject("ANY", SOME.class);
685       fail("The expected exception was not thrown");
686     }
687     catch(HttpClientErrorException e)
688     {
689       log.debug("{}", e.toString());
690     }
691     catch(Exception e)
692     {
693       fail("A wrong exception was thrown: " + e.toString());
694     }
695
696
697     requestFactory.setBody("");
698
699     try
700     {
701       clientTemplate.getForObject("ANY", SOME.class);
702       fail("The expected exception was not thrown");
703     }
704     catch(HttpClientErrorException e)
705     {
706       log.debug("{}", e.toString());
707     }
708     catch(Exception e)
709     {
710       fail("A wrong exception was thrown: " + e.toString());
711     }
712   }
713
714
715   @Before
716   public void setUp()
717   {
718     requestFactory = new MockClientHttpRequestFactory();
719     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
720     requestFactory.addHeader("Content-Type", "application/json");
721
722     clientTemplate = new RestTemplate();
723     clientTemplate.setRequestFactory(requestFactory);
724     clientTemplate.setErrorHandler(
725         new GraphApiErrorHandler(clientTemplate.getErrorHandler())
726         );
727   }
728
729
730   static class SOME
731   {
732   }
733 }