Upgraded Spring Boot from 1.3.7.RELEASE to 2.2.0.RELEASE
[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 testError613()
259   {
260     log.info("testError613");
261
262
263     requestFactory.setBody(
264         "{\n" +
265         "  \"error\":\n" +
266         "  {\n" +
267         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
268         "    \"type\": \"OAuthException\",\n" +
269         "    \"code\": 613\n" +
270         "  }\n" +
271         "}");
272
273     try
274     {
275       clientTemplate.getForObject("ANY", SOME.class);
276       fail("The expected exception was not thrown");
277     }
278     catch(RateLimitExceededException e)
279     {
280       log.debug("{}", e.toString());
281       assertEquals(new Integer(613), e.getCode());
282       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
283       assertEquals(Type.OAuthException, e.getType());
284     }
285   }
286
287   @Test
288   public void testError2200()
289   {
290     requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
291     
292     try
293     {
294       clientTemplate.getForObject("ANY", SOME.class);
295       fail("The expected exception was not thrown");
296     }
297     catch(CallbackVerificationFailedException e)
298     {
299       log.debug("{}", e.toString());
300       assertEquals(new Integer(2200), e.getCode());
301       assertEquals("(#2200) callback verification failed: ", e.getMessage());
302       assertEquals(Type.OAuthException, e.getType());
303       assertEquals("ESLjoZKvPXg", e.getTraceId());
304     }
305   }
306
307   @Test
308   public void testUnmappedError()
309   {
310     log.info("testUnmappedError");
311
312
313     requestFactory.setBody(
314         "{\n" +
315         "  \"error\":\n" +
316         "  {\n" +
317         "    \"message\": \"This error does not exist.\",\n" +
318         "    \"type\": \"NonexistentTypeException\",\n" +
319         "    \"code\": 999999999\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(GraphApiException e)
329     {
330       log.debug("{}", e.toString());
331       assertEquals(new Integer(999999999), e.getCode());
332       assertEquals("This error does not exist.", e.getMessage());
333       try
334       {
335         Type type = e.getType();
336         log.error("unknown type: {}", type);
337         fail("unmapped type was resolved by enum: " + type);
338       }
339       catch (IllegalArgumentException ee) {}
340     }
341   }
342
343   @Test
344   public void testUnmappedErrors()
345   {
346     log.info("testUnmappedErrors");
347
348
349     requestFactory.setBody(
350         "{\n" +
351         "  \"error\":\n" +
352         "  {\n" +
353         "    \"message\": null,\n" +
354         "    \"type\": \"WhateverTypeException\",\n" +
355         "    \"code\": 999999999\n" +
356         "  }\n" +
357         "}");
358
359     try
360     {
361       clientTemplate.getForObject("ANY", SOME.class);
362       fail("The expected exception was not thrown");
363     }
364     catch(UnmappedErrorException e)
365     {
366       log.debug("{}", e.toString());
367       assertNull(e.getMessage());
368       try
369       {
370         Type type = e.getType();
371         log.error("unknown type: {}", type);
372         fail("unmapped type was resolved by enum: " + type);
373       }
374       catch (IllegalArgumentException ee) {}
375       assertEquals(new Integer(999999999), e.getCode());
376       assertNull(e.getSubCode());
377       assertNull(e.getUserTitle());
378       assertNull(e.getUserMessage());
379       assertNull(e.getTraceId());
380     }
381     catch(Exception e)
382     {
383       fail("A wrong exception was thrown: " + e.toString());
384     }
385
386
387     requestFactory.setBody(
388         "{\n" +
389         "  \"error\":\n" +
390         "  {\n" +
391         "    \"type\": \"WhateverTypeException\",\n" +
392         "    \"code\": 999999999\n" +
393         "  }\n" +
394         "}");
395
396     try
397     {
398       clientTemplate.getForObject("ANY", SOME.class);
399       fail("The expected exception was not thrown");
400     }
401     catch(UnmappedErrorException e)
402     {
403       log.debug("{}", e.toString());
404       assertNull(e.getMessage());
405       try
406       {
407         Type type = e.getType();
408         log.error("unknown type: {}", type);
409         fail("unmapped type was resolved by enum: " + type);
410       }
411       catch (IllegalArgumentException ee) {}
412       assertEquals(new Integer(999999999), e.getCode());
413       assertNull(e.getSubCode());
414       assertNull(e.getUserTitle());
415       assertNull(e.getUserMessage());
416       assertNull(e.getTraceId());
417     }
418     catch(Exception e)
419     {
420       fail("A wrong exception was thrown: " + e.toString());
421     }
422
423
424     requestFactory.setBody(
425         "{\n" +
426         "  \"error\":\n" +
427         "  {\n" +
428         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
429         "    \"type\": null,\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     requestFactory.setBody(
457         "{\n" +
458         "  \"error\":\n" +
459         "  {\n" +
460         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
461         "    \"code\": 999999999\n" +
462         "  }\n" +
463         "}");
464
465     try
466     {
467       clientTemplate.getForObject("ANY", SOME.class);
468       fail("The expected exception was not thrown");
469     }
470     catch(UnmappedErrorException e)
471     {
472       log.debug("{}", e.toString());
473       assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
474       assertNull(e.getType());
475       assertEquals(new Integer(999999999), e.getCode());
476       assertNull(e.getSubCode());
477       assertNull(e.getUserTitle());
478       assertNull(e.getUserMessage());
479       assertNull(e.getTraceId());
480     }
481     catch(Exception e)
482     {
483       fail("A wrong exception was thrown: " + e.toString());
484     }
485   }
486
487   @Test
488   public void testInvlalidErrors()
489   {
490     log.info("testInvalidErrors");
491
492
493     requestFactory.setBody(
494         "{\n" +
495         "  \"error\":\n" +
496         "  {\n" +
497         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
498         "    \"type\": \"Whatever\",\n" +
499         "    \"code\": \"some string\"\n" +
500         "  }\n" +
501         "}");
502
503     try
504     {
505       clientTemplate.getForObject("ANY", SOME.class);
506       fail("The expected exception was not thrown");
507     }
508     catch(HttpClientErrorException e)
509     {
510       log.debug("{}", e.toString());
511     }
512     catch(Exception e)
513     {
514       fail("A wrong exception was thrown: " + e.toString());
515     }
516
517
518     requestFactory.setBody(
519         "{\n" +
520         "  \"error\":\n" +
521         "  {\n" +
522         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
523         "    \"type\": \"Whatever\",\n" +
524         "    \"code\": 9.9\n" +
525         "  }\n" +
526         "}");
527
528     try
529     {
530       clientTemplate.getForObject("ANY", SOME.class);
531       fail("The expected exception was not thrown");
532     }
533     catch(HttpClientErrorException e)
534     {
535       log.debug("{}", e.toString());
536     }
537     catch(Exception e)
538     {
539       fail("A wrong exception was thrown: " + e.toString());
540     }
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\": null\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         "  }\n" +
575         "}");
576
577     try
578     {
579       clientTemplate.getForObject("ANY", SOME.class);
580       fail("The expected exception was not thrown");
581     }
582     catch(HttpClientErrorException e)
583     {
584       log.debug("{}", e.toString());
585     }
586     catch(Exception e)
587     {
588       fail("A wrong exception was thrown: " + e.toString());
589     }
590
591
592     requestFactory.setBody("{\"error\":{\"message\":null}}");
593
594     try
595     {
596       clientTemplate.getForObject("ANY", SOME.class);
597       fail("The expected exception was not thrown");
598     }
599     catch(HttpClientErrorException e)
600     {
601       log.debug("{}", e.toString());
602     }
603     catch(Exception e)
604     {
605       fail("A wrong exception was thrown: " + e.toString());
606     }
607
608
609     requestFactory.setBody("{\"error\":{\"type\":null}}");
610
611     try
612     {
613       clientTemplate.getForObject("ANY", SOME.class);
614       fail("The expected exception was not thrown");
615     }
616     catch(HttpClientErrorException e)
617     {
618       log.debug("{}", e.toString());
619     }
620     catch(Exception e)
621     {
622       fail("A wrong exception was thrown: " + e.toString());
623     }
624
625
626     requestFactory.setBody("{\"error\":{\"code\":null}}");
627
628     try
629     {
630       clientTemplate.getForObject("ANY", SOME.class);
631       fail("The expected exception was not thrown");
632     }
633     catch(HttpClientErrorException e)
634     {
635       log.debug("{}", e.toString());
636     }
637     catch(Exception e)
638     {
639       fail("A wrong exception was thrown: " + e.toString());
640     }
641
642
643     requestFactory.setBody("{\"error\":{}}");
644
645     try
646     {
647       clientTemplate.getForObject("ANY", SOME.class);
648       fail("The expected exception was not thrown");
649     }
650     catch(HttpClientErrorException e)
651     {
652       log.debug("{}", e.toString());
653     }
654     catch(Exception e)
655     {
656       fail("A wrong exception was thrown: " + e.toString());
657     }
658
659
660     requestFactory.setBody("{\"error\":\"some message\"}");
661
662     try
663     {
664       clientTemplate.getForObject("ANY", SOME.class);
665       fail("The expected exception was not thrown");
666     }
667     catch(HttpClientErrorException e)
668     {
669       log.debug("{}", e.toString());
670     }
671     catch(Exception e)
672     {
673       fail("A wrong exception was thrown: " + e.toString());
674     }
675
676
677     requestFactory.setBody("{\"error\":null}");
678
679     try
680     {
681       clientTemplate.getForObject("ANY", SOME.class);
682       fail("The expected exception was not thrown");
683     }
684     catch(HttpClientErrorException e)
685     {
686       log.debug("{}", e.toString());
687     }
688     catch(Exception e)
689     {
690       fail("A wrong exception was thrown: " + e.toString());
691     }
692
693
694     requestFactory.setBody("{\"some filed\":\"some message\"}");
695
696     try
697     {
698       clientTemplate.getForObject("ANY", SOME.class);
699       fail("The expected exception was not thrown");
700     }
701     catch(HttpClientErrorException e)
702     {
703       log.debug("{}", e.toString());
704     }
705     catch(Exception e)
706     {
707       fail("A wrong exception was thrown: " + e.toString());
708     }
709
710
711     requestFactory.setBody("{}");
712
713     try
714     {
715       clientTemplate.getForObject("ANY", SOME.class);
716       fail("The expected exception was not thrown");
717     }
718     catch(HttpClientErrorException e)
719     {
720       log.debug("{}", e.toString());
721     }
722     catch(Exception e)
723     {
724       fail("A wrong exception was thrown: " + e.toString());
725     }
726
727
728     requestFactory.setBody("");
729
730     try
731     {
732       clientTemplate.getForObject("ANY", SOME.class);
733       fail("The expected exception was not thrown");
734     }
735     catch(HttpClientErrorException e)
736     {
737       log.debug("{}", e.toString());
738     }
739     catch(Exception e)
740     {
741       fail("A wrong exception was thrown: " + e.toString());
742     }
743   }
744
745
746   @Before
747   public void setUp()
748   {
749     requestFactory = new MockClientHttpRequestFactory();
750     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
751     requestFactory.addHeader("Content-Type", "application/json");
752
753     clientTemplate = new RestTemplate();
754     clientTemplate.setRequestFactory(requestFactory);
755     clientTemplate.setErrorHandler(
756         new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
757         );
758   }
759
760
761   static class SOME
762   {
763   }
764 }