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