f8cb142f6cd7a6edfb70e989c943036cee305b9e
[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 testError102()
153   {
154     log.info("testError102");
155
156     requestFactory.setBody("{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}");
157
158     try
159     {
160       clientTemplate.getForObject("ANY", SOME.class);
161       fail("The expected exception was not thrown");
162     }
163     catch(UserAccessTokenRequiredException e)
164     {
165       log.debug("{}", e.toString());
166       assertEquals(new Integer(102), e.getCode());
167       assertEquals("A user access token is required to request this resource.", e.getMessage());
168       assertEquals(Type.OAuthException, e.getType());
169       assertEquals("DhdMyf23Ki7", e.getTraceId());
170     }
171   }
172
173   @Test
174   public void testError104()
175   {
176     log.info("testError104");
177
178     requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
179
180     try
181     {
182       clientTemplate.getForObject("ANY", SOME.class);
183       fail("The expected exception was not thrown");
184     }
185     catch(AccessTokenRequiredException e)
186     {
187       log.debug("{}", e.toString());
188       assertEquals(new Integer(104), e.getCode());
189       assertEquals("An access token is required to request this resource.", e.getMessage());
190       assertEquals(Type.OAuthException, e.getType());
191       assertEquals("E2Jjkj5++LL", e.getTraceId());
192     }
193   }
194
195   @Test
196   public void testError613()
197   {
198     log.info("testError613");
199
200
201     requestFactory.setBody(
202         "{\n" +
203         "  \"error\":\n" +
204         "  {\n" +
205         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
206         "    \"type\": \"OAuthException\",\n" +
207         "    \"code\": 613\n" +
208         "  }\n" +
209         "}");
210
211     try
212     {
213       clientTemplate.getForObject("ANY", SOME.class);
214       fail("The expected exception was not thrown");
215     }
216     catch(RateExceededException e)
217     {
218       log.debug("{}", e.toString());
219       assertEquals(new Integer(613), e.getCode());
220       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
221       assertEquals(Type.OAuthException, e.getType());
222     }
223   }
224
225   @Test
226   public void testError2200()
227   {
228     requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
229     
230     try
231     {
232       clientTemplate.getForObject("ANY", SOME.class);
233       fail("The expected exception was not thrown");
234     }
235     catch(CallbackVerificationFailedException e)
236     {
237       log.debug("{}", e.toString());
238       assertEquals(new Integer(2200), e.getCode());
239       assertEquals("(#2200) callback verification failed: ", e.getMessage());
240       assertEquals(Type.OAuthException, e.getType());
241       assertEquals("ESLjoZKvPXg", e.getTraceId());
242     }
243   }
244
245   @Test
246   public void testUnmappedError()
247   {
248     log.info("testUnmappedError");
249
250
251     requestFactory.setBody(
252         "{\n" +
253         "  \"error\":\n" +
254         "  {\n" +
255         "    \"message\": \"This error does not exist.\",\n" +
256         "    \"type\": \"NonexistentTypeException\",\n" +
257         "    \"code\": 999999999\n" +
258         "  }\n" +
259         "}");
260
261     try
262     {
263       clientTemplate.getForObject("ANY", SOME.class);
264       fail("The expected exception was not thrown");
265     }
266     catch(GraphApiException e)
267     {
268       log.debug("{}", e.toString());
269       assertEquals(new Integer(999999999), e.getCode());
270       assertEquals("This error does not exist.", e.getMessage());
271       try
272       {
273         Type type = e.getType();
274         log.error("unknown type: {}", type);
275         fail("unmapped type was resolved by enum: " + type);
276       }
277       catch (IllegalArgumentException ee) {}
278     }
279   }
280
281   @Test
282   public void testUnmappedErrors()
283   {
284     log.info("testUnmappedErrors");
285
286
287     requestFactory.setBody(
288         "{\n" +
289         "  \"error\":\n" +
290         "  {\n" +
291         "    \"message\": null,\n" +
292         "    \"type\": \"WhateverTypeException\",\n" +
293         "    \"code\": 999999999\n" +
294         "  }\n" +
295         "}");
296
297     try
298     {
299       clientTemplate.getForObject("ANY", SOME.class);
300       fail("The expected exception was not thrown");
301     }
302     catch(UnmappedErrorException e)
303     {
304       log.debug("{}", e.toString());
305       assertNull(e.getMessage());
306       try
307       {
308         Type type = e.getType();
309         log.error("unknown type: {}", type);
310         fail("unmapped type was resolved by enum: " + type);
311       }
312       catch (IllegalArgumentException ee) {}
313       assertEquals(new Integer(999999999), e.getCode());
314       assertNull(e.getSubCode());
315       assertNull(e.getUserTitle());
316       assertNull(e.getUserMessage());
317       assertNull(e.getTraceId());
318     }
319     catch(Exception e)
320     {
321       fail("A wrong exception was thrown: " + e.toString());
322     }
323
324
325     requestFactory.setBody(
326         "{\n" +
327         "  \"error\":\n" +
328         "  {\n" +
329         "    \"type\": \"WhateverTypeException\",\n" +
330         "    \"code\": 999999999\n" +
331         "  }\n" +
332         "}");
333
334     try
335     {
336       clientTemplate.getForObject("ANY", SOME.class);
337       fail("The expected exception was not thrown");
338     }
339     catch(UnmappedErrorException e)
340     {
341       log.debug("{}", e.toString());
342       assertNull(e.getMessage());
343       try
344       {
345         Type type = e.getType();
346         log.error("unknown type: {}", type);
347         fail("unmapped type was resolved by enum: " + type);
348       }
349       catch (IllegalArgumentException ee) {}
350       assertEquals(new Integer(999999999), e.getCode());
351       assertNull(e.getSubCode());
352       assertNull(e.getUserTitle());
353       assertNull(e.getUserMessage());
354       assertNull(e.getTraceId());
355     }
356     catch(Exception e)
357     {
358       fail("A wrong exception was thrown: " + e.toString());
359     }
360
361
362     requestFactory.setBody(
363         "{\n" +
364         "  \"error\":\n" +
365         "  {\n" +
366         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
367         "    \"type\": null,\n" +
368         "    \"code\": 999999999\n" +
369         "  }\n" +
370         "}");
371
372     try
373     {
374       clientTemplate.getForObject("ANY", SOME.class);
375       fail("The expected exception was not thrown");
376     }
377     catch(UnmappedErrorException e)
378     {
379       log.debug("{}", e.toString());
380       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
381       assertNull(e.getType());
382       assertEquals(new Integer(999999999), e.getCode());
383       assertNull(e.getSubCode());
384       assertNull(e.getUserTitle());
385       assertNull(e.getUserMessage());
386       assertNull(e.getTraceId());
387     }
388     catch(Exception e)
389     {
390       fail("A wrong exception was thrown: " + e.toString());
391     }
392
393
394     requestFactory.setBody(
395         "{\n" +
396         "  \"error\":\n" +
397         "  {\n" +
398         "    \"message\": \"An unmapped Graph-API-Exception.\",\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   @Test
426   public void testInvlalidErrors()
427   {
428     log.info("testInvalidErrors");
429
430
431     requestFactory.setBody(
432         "{\n" +
433         "  \"error\":\n" +
434         "  {\n" +
435         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
436         "    \"type\": \"Whatever\",\n" +
437         "    \"code\": \"some string\"\n" +
438         "  }\n" +
439         "}");
440
441     try
442     {
443       clientTemplate.getForObject("ANY", SOME.class);
444       fail("The expected exception was not thrown");
445     }
446     catch(HttpClientErrorException e)
447     {
448       log.debug("{}", e.toString());
449     }
450     catch(Exception e)
451     {
452       fail("A wrong exception was thrown: " + e.toString());
453     }
454
455
456     requestFactory.setBody(
457         "{\n" +
458         "  \"error\":\n" +
459         "  {\n" +
460         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
461         "    \"type\": \"Whatever\",\n" +
462         "    \"code\": 9.9\n" +
463         "  }\n" +
464         "}");
465
466     try
467     {
468       clientTemplate.getForObject("ANY", SOME.class);
469       fail("The expected exception was not thrown");
470     }
471     catch(HttpClientErrorException e)
472     {
473       log.debug("{}", e.toString());
474     }
475     catch(Exception e)
476     {
477       fail("A wrong exception was thrown: " + e.toString());
478     }
479
480
481     requestFactory.setBody(
482         "{\n" +
483         "  \"error\":\n" +
484         "  {\n" +
485         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
486         "    \"type\": \"Whatever\",\n" +
487         "    \"code\": null\n" +
488         "  }\n" +
489         "}");
490
491     try
492     {
493       clientTemplate.getForObject("ANY", SOME.class);
494       fail("The expected exception was not thrown");
495     }
496     catch(HttpClientErrorException e)
497     {
498       log.debug("{}", e.toString());
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\": \"Not a Graph-Api-Exception.\",\n" +
511         "    \"type\": \"Whatever\"\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(HttpClientErrorException e)
521     {
522       log.debug("{}", e.toString());
523     }
524     catch(Exception e)
525     {
526       fail("A wrong exception was thrown: " + e.toString());
527     }
528
529
530     requestFactory.setBody("{\"error\":{\"message\":null}}");
531
532     try
533     {
534       clientTemplate.getForObject("ANY", SOME.class);
535       fail("The expected exception was not thrown");
536     }
537     catch(HttpClientErrorException e)
538     {
539       log.debug("{}", e.toString());
540     }
541     catch(Exception e)
542     {
543       fail("A wrong exception was thrown: " + e.toString());
544     }
545
546
547     requestFactory.setBody("{\"error\":{\"type\":null}}");
548
549     try
550     {
551       clientTemplate.getForObject("ANY", SOME.class);
552       fail("The expected exception was not thrown");
553     }
554     catch(HttpClientErrorException e)
555     {
556       log.debug("{}", e.toString());
557     }
558     catch(Exception e)
559     {
560       fail("A wrong exception was thrown: " + e.toString());
561     }
562
563
564     requestFactory.setBody("{\"error\":{\"code\":null}}");
565
566     try
567     {
568       clientTemplate.getForObject("ANY", SOME.class);
569       fail("The expected exception was not thrown");
570     }
571     catch(HttpClientErrorException e)
572     {
573       log.debug("{}", e.toString());
574     }
575     catch(Exception e)
576     {
577       fail("A wrong exception was thrown: " + e.toString());
578     }
579
580
581     requestFactory.setBody("{\"error\":{}}");
582
583     try
584     {
585       clientTemplate.getForObject("ANY", SOME.class);
586       fail("The expected exception was not thrown");
587     }
588     catch(HttpClientErrorException e)
589     {
590       log.debug("{}", e.toString());
591     }
592     catch(Exception e)
593     {
594       fail("A wrong exception was thrown: " + e.toString());
595     }
596
597
598     requestFactory.setBody("{\"error\":\"some message\"}");
599
600     try
601     {
602       clientTemplate.getForObject("ANY", SOME.class);
603       fail("The expected exception was not thrown");
604     }
605     catch(HttpClientErrorException e)
606     {
607       log.debug("{}", e.toString());
608     }
609     catch(Exception e)
610     {
611       fail("A wrong exception was thrown: " + e.toString());
612     }
613
614
615     requestFactory.setBody("{\"error\":null}");
616
617     try
618     {
619       clientTemplate.getForObject("ANY", SOME.class);
620       fail("The expected exception was not thrown");
621     }
622     catch(HttpClientErrorException e)
623     {
624       log.debug("{}", e.toString());
625     }
626     catch(Exception e)
627     {
628       fail("A wrong exception was thrown: " + e.toString());
629     }
630
631
632     requestFactory.setBody("{\"some filed\":\"some message\"}");
633
634     try
635     {
636       clientTemplate.getForObject("ANY", SOME.class);
637       fail("The expected exception was not thrown");
638     }
639     catch(HttpClientErrorException e)
640     {
641       log.debug("{}", e.toString());
642     }
643     catch(Exception e)
644     {
645       fail("A wrong exception was thrown: " + e.toString());
646     }
647
648
649     requestFactory.setBody("{}");
650
651     try
652     {
653       clientTemplate.getForObject("ANY", SOME.class);
654       fail("The expected exception was not thrown");
655     }
656     catch(HttpClientErrorException e)
657     {
658       log.debug("{}", e.toString());
659     }
660     catch(Exception e)
661     {
662       fail("A wrong exception was thrown: " + e.toString());
663     }
664
665
666     requestFactory.setBody("");
667
668     try
669     {
670       clientTemplate.getForObject("ANY", SOME.class);
671       fail("The expected exception was not thrown");
672     }
673     catch(HttpClientErrorException e)
674     {
675       log.debug("{}", e.toString());
676     }
677     catch(Exception e)
678     {
679       fail("A wrong exception was thrown: " + e.toString());
680     }
681   }
682
683
684   @Before
685   public void setUp()
686   {
687     requestFactory = new MockClientHttpRequestFactory();
688     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
689     requestFactory.addHeader("Content-Type", "application/json");
690
691     clientTemplate = new RestTemplate();
692     clientTemplate.setRequestFactory(requestFactory);
693     clientTemplate.setErrorHandler(
694         new GraphApiErrorHandler(clientTemplate.getErrorHandler())
695         );
696   }
697
698
699   static class SOME
700   {
701   }
702 }