Upgraded all dependencies
[facebook-utils] / src / test / java / de / juplo / facebook / GraphApiErrorHandlerTest.java
1 package de.juplo.facebook;
2
3 import java.util.Date;
4 import java.util.Map;
5 import java.util.Set;
6 import javax.annotation.Resource;
7 import static org.junit.Assert.*;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.security.access.AccessDeniedException;
15 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
16 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
17 import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
18 import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
19 import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
20 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
21 import org.springframework.security.oauth2.client.token.AccessTokenRequest;
22 import org.springframework.security.oauth2.common.OAuth2AccessToken;
23 import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE;
24 import org.springframework.security.oauth2.common.OAuth2RefreshToken;
25 import org.springframework.test.context.ContextConfiguration;
26 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27 import org.springframework.web.client.HttpClientErrorException;
28
29
30
31 /**
32  *
33  * @author kai
34  */
35 @RunWith(SpringJUnit4ClassRunner.class)
36 @ContextConfiguration(
37   locations = {
38     "classpath:/spring/test-facebook-error-handler.xml"
39     })
40 public class GraphApiErrorHandlerTest
41 {
42   private static final Logger log =
43       LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
44
45   @Resource
46   private OAuth2RestTemplate clientTemplate;
47
48   private MockClientHttpRequestFactory requestFactory;
49
50
51   @Test
52   public void testError1()
53   {
54     log.info("testError1");
55
56
57     requestFactory.setBody(
58         "{\n" +
59         "  \"error\":\n" +
60         "  {\n" +
61         "    \"message\": \"An unknown error has occurred.\",\n" +
62         "    \"type\": \"OAuthException\",\n" +
63         "    \"code\": 1\n" +
64         "  }\n" +
65         "}");
66
67     try
68     {
69       clientTemplate.getForObject("ANY", SOME.class);
70       fail("The expected exception was not thrown");
71     }
72     catch(UnknownErrorException e)
73     {
74       log.debug("{}", e.toString());
75       assertEquals("invalid_request", e.getOAuth2ErrorCode());
76       assertEquals(1, e.getCode());
77       assertEquals("An unknown error has occurred.", e.getMessage());
78       assertEquals("OAuthException", e.getType());
79     }
80   }
81
82   @Test
83   public void testError2()
84   {
85     log.info("testError2");
86
87
88     requestFactory.setBody(
89         "{\n" +
90         "  \"error\":\n" +
91         "  {\n" +
92         "    \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
93         "    \"type\": \"OAuthException\",\n" +
94         "    \"code\": 2\n" +
95         "  }\n" +
96         "}");
97
98     try
99     {
100       clientTemplate.getForObject("ANY", SOME.class);
101       fail("The expected exception was not thrown");
102     }
103     catch(UnexpectedErrorException e)
104     {
105       log.debug("{}", e.toString());
106       assertEquals("invalid_request", e.getOAuth2ErrorCode());
107       assertEquals(2, e.getCode());
108       assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
109       assertEquals("OAuthException", e.getType());
110     }
111   }
112
113   @Test
114   public void testError21()
115   {
116     log.info("testError21");
117
118
119     requestFactory.setBody(
120         "{\n" +
121         "  \"error\":\n" +
122         "  {\n" +
123         "    \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID\",\n" +
124         "    \"type\": \"OAuthException\",\n" +
125         "    \"code\": 21\n" +
126         "  }\n" +
127         "}");
128
129     try
130     {
131       clientTemplate.getForObject("ANY", SOME.class);
132       fail("The expected exception was not thrown");
133     }
134     catch(PageMigratedException e)
135     {
136       log.debug("{}", e.toString());
137       assertEquals("invalid_request", e.getOAuth2ErrorCode());
138       assertEquals(21, e.getCode());
139       assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID", e.getMessage());
140       assertEquals("OAuthException", e.getType());
141     }
142   }
143
144   @Test
145   public void testError100()
146   {
147     log.info("testError100");
148
149
150     requestFactory.setBody(
151         "{\n" +
152         "  \"error\":\n" +
153         "  {\n" +
154         "    \"message\": \"Unsupported get request.\",\n" +
155         "    \"type\": \"GraphMethodException\",\n" +
156         "    \"code\": 100\n" +
157         "  }\n" +
158         "}");
159
160     try
161     {
162       clientTemplate.getForObject("ANY", SOME.class);
163       fail("The expected exception was not thrown");
164     }
165     catch(UnsupportedGetRequestException e)
166     {
167       log.debug("{}", e.toString());
168       assertEquals("invalid_request", e.getOAuth2ErrorCode());
169       assertEquals(100, e.getCode());
170       assertEquals("Unsupported get request.", e.getMessage());
171       assertEquals("GraphMethodException", e.getType());
172     }
173   }
174
175   @Test
176   public void testError613()
177   {
178     log.info("testError613");
179
180
181     requestFactory.setBody(
182         "{\n" +
183         "  \"error\":\n" +
184         "  {\n" +
185         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
186         "    \"type\": \"OAuthException\",\n" +
187         "    \"code\": 613\n" +
188         "  }\n" +
189         "}");
190
191     try
192     {
193       clientTemplate.getForObject("ANY", SOME.class);
194       fail("The expected exception was not thrown");
195     }
196     catch(RateExceededException e)
197     {
198       log.debug("{}", e.toString());
199       assertEquals("invalid_request", e.getOAuth2ErrorCode());
200       assertEquals(613, e.getCode());
201       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
202       assertEquals("OAuthException", e.getType());
203     }
204   }
205
206   @Test
207   public void testUnmappedError()
208   {
209     log.info("testUnmappedError");
210
211
212     requestFactory.setBody(
213         "{\n" +
214         "  \"error\":\n" +
215         "  {\n" +
216         "    \"message\": \"This error does not exist.\",\n" +
217         "    \"type\": \"NonexistentException\",\n" +
218         "    \"code\": 999999999\n" +
219         "  }\n" +
220         "}");
221
222     try
223     {
224       clientTemplate.getForObject("ANY", SOME.class);
225       fail("The expected exception was not thrown");
226     }
227     catch(GraphApiException e)
228     {
229       log.debug("{}", e.toString());
230       assertEquals("invalid_request", e.getOAuth2ErrorCode());
231       assertEquals(999999999, e.getCode());
232       assertEquals("This error does not exist.", e.getMessage());
233       assertEquals("NonexistentException", e.getType());
234     }
235   }
236
237   @Test
238   public void testInvlalidErrors()
239   {
240     log.info("testInvalidErrors");
241
242
243     requestFactory.setBody(
244         "{\n" +
245         "  \"error\":\n" +
246         "  {\n" +
247         "    \"message\": null,\n" +
248         "    \"type\": \"Whatever\",\n" +
249         "    \"code\": 999999999\n" +
250         "  }\n" +
251         "}");
252
253     try
254     {
255       clientTemplate.getForObject("ANY", SOME.class);
256       fail("The expected exception was not thrown");
257     }
258     catch(HttpClientErrorException e)
259     {
260       log.debug("{}", e.toString());
261     }
262     catch(Exception e)
263     {
264       fail("A wrong exception was thrown: " + e.toString());
265     }
266
267
268     requestFactory.setBody(
269         "{\n" +
270         "  \"error\":\n" +
271         "  {\n" +
272         "    \"type\": \"Whatever\",\n" +
273         "    \"code\": 999999999\n" +
274         "  }\n" +
275         "}");
276
277     try
278     {
279       clientTemplate.getForObject("ANY", SOME.class);
280       fail("The expected exception was not thrown");
281     }
282     catch(HttpClientErrorException e)
283     {
284       log.debug("{}", e.toString());
285     }
286     catch(Exception e)
287     {
288       fail("A wrong exception was thrown: " + e.toString());
289     }
290
291
292     requestFactory.setBody(
293         "{\n" +
294         "  \"error\":\n" +
295         "  {\n" +
296         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
297         "    \"type\": null,\n" +
298         "    \"code\": 999999999\n" +
299         "  }\n" +
300         "}");
301
302     try
303     {
304       clientTemplate.getForObject("ANY", SOME.class);
305       fail("The expected exception was not thrown");
306     }
307     catch(HttpClientErrorException e)
308     {
309       log.debug("{}", e.toString());
310     }
311     catch(Exception e)
312     {
313       fail("A wrong exception was thrown: " + e.toString());
314     }
315
316
317     requestFactory.setBody(
318         "{\n" +
319         "  \"error\":\n" +
320         "  {\n" +
321         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
322         "    \"code\": 999999999\n" +
323         "  }\n" +
324         "}");
325
326     try
327     {
328       clientTemplate.getForObject("ANY", SOME.class);
329       fail("The expected exception was not thrown");
330     }
331     catch(HttpClientErrorException e)
332     {
333       log.debug("{}", e.toString());
334     }
335     catch(Exception e)
336     {
337       fail("A wrong exception was thrown: " + e.toString());
338     }
339
340
341     requestFactory.setBody(
342         "{\n" +
343         "  \"error\":\n" +
344         "  {\n" +
345         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
346         "    \"type\": \"Whatever\",\n" +
347         "    \"code\": \"some string\"\n" +
348         "  }\n" +
349         "}");
350
351     try
352     {
353       clientTemplate.getForObject("ANY", SOME.class);
354       fail("The expected exception was not thrown");
355     }
356     catch(HttpClientErrorException e)
357     {
358       log.debug("{}", e.toString());
359     }
360     catch(Exception e)
361     {
362       fail("A wrong exception was thrown: " + e.toString());
363     }
364
365
366     requestFactory.setBody(
367         "{\n" +
368         "  \"error\":\n" +
369         "  {\n" +
370         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
371         "    \"type\": \"Whatever\",\n" +
372         "    \"code\": 9.9\n" +
373         "  }\n" +
374         "}");
375
376     try
377     {
378       clientTemplate.getForObject("ANY", SOME.class);
379       fail("The expected exception was not thrown");
380     }
381     catch(HttpClientErrorException e)
382     {
383       log.debug("{}", e.toString());
384     }
385     catch(Exception e)
386     {
387       fail("A wrong exception was thrown: " + e.toString());
388     }
389
390
391     requestFactory.setBody(
392         "{\n" +
393         "  \"error\":\n" +
394         "  {\n" +
395         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
396         "    \"type\": \"Whatever\",\n" +
397         "    \"code\": null\n" +
398         "  }\n" +
399         "}");
400
401     try
402     {
403       clientTemplate.getForObject("ANY", SOME.class);
404       fail("The expected exception was not thrown");
405     }
406     catch(HttpClientErrorException e)
407     {
408       log.debug("{}", e.toString());
409     }
410     catch(Exception e)
411     {
412       fail("A wrong exception was thrown: " + e.toString());
413     }
414
415
416     requestFactory.setBody(
417         "{\n" +
418         "  \"error\":\n" +
419         "  {\n" +
420         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
421         "    \"type\": \"Whatever\"\n" +
422         "  }\n" +
423         "}");
424
425     try
426     {
427       clientTemplate.getForObject("ANY", SOME.class);
428       fail("The expected exception was not thrown");
429     }
430     catch(HttpClientErrorException e)
431     {
432       log.debug("{}", e.toString());
433     }
434     catch(Exception e)
435     {
436       fail("A wrong exception was thrown: " + e.toString());
437     }
438
439
440     requestFactory.setBody("{\"error\":{\"message\":null}}");
441
442     try
443     {
444       clientTemplate.getForObject("ANY", SOME.class);
445       fail("The expected exception was not thrown");
446     }
447     catch(HttpClientErrorException e)
448     {
449       log.debug("{}", e.toString());
450     }
451     catch(Exception e)
452     {
453       fail("A wrong exception was thrown: " + e.toString());
454     }
455
456
457     requestFactory.setBody("{\"error\":{\"type\":null}}");
458
459     try
460     {
461       clientTemplate.getForObject("ANY", SOME.class);
462       fail("The expected exception was not thrown");
463     }
464     catch(HttpClientErrorException e)
465     {
466       log.debug("{}", e.toString());
467     }
468     catch(Exception e)
469     {
470       fail("A wrong exception was thrown: " + e.toString());
471     }
472
473
474     requestFactory.setBody("{\"error\":{\"code\":null}}");
475
476     try
477     {
478       clientTemplate.getForObject("ANY", SOME.class);
479       fail("The expected exception was not thrown");
480     }
481     catch(HttpClientErrorException e)
482     {
483       log.debug("{}", e.toString());
484     }
485     catch(Exception e)
486     {
487       fail("A wrong exception was thrown: " + e.toString());
488     }
489
490
491     requestFactory.setBody("{\"error\":{}}");
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\":\"some message\"}");
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\":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("{\"some filed\":\"some message\"}");
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("{}");
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("");
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
594   @Before
595   public void setUp()
596   {
597     requestFactory = new MockClientHttpRequestFactory();
598     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
599     requestFactory.addHeader("Content-Type", "application/json");
600     clientTemplate.setRequestFactory(requestFactory);
601
602     clientTemplate.setErrorHandler(
603         new GraphApiErrorHandler(
604             (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
605             )
606         );
607
608     clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
609     {
610       @Override
611       public OAuth2AccessToken obtainAccessToken(
612           OAuth2ProtectedResourceDetails details,
613           AccessTokenRequest parameters
614           )
615           throws
616             UserRedirectRequiredException,
617             UserApprovalRequiredException,
618             AccessDeniedException
619       {
620         return new OAuth2AccessToken() {
621
622           @Override
623           public Map<String, Object> getAdditionalInformation()
624           {
625             throw new UnsupportedOperationException("Not supported yet.");
626           }
627
628           @Override
629           public Set<String> getScope()
630           {
631             throw new UnsupportedOperationException("Not supported yet.");
632           }
633
634           @Override
635           public OAuth2RefreshToken getRefreshToken()
636           {
637             throw new UnsupportedOperationException("Not supported yet.");
638           }
639
640           @Override
641           public String getTokenType()
642           {
643             return OAUTH2_TYPE;
644           }
645
646           @Override
647           public boolean isExpired()
648           {
649             return false;
650           }
651
652           @Override
653           public Date getExpiration()
654           {
655             throw new UnsupportedOperationException("Not supported yet.");
656           }
657
658           @Override
659           public int getExpiresIn()
660           {
661             throw new UnsupportedOperationException("Not supported yet.");
662           }
663
664           @Override
665           public String getValue()
666           {
667             return "ANY";
668           }
669         };
670       }
671
672       @Override
673       public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
674       {
675         return true;
676       }
677
678       @Override
679       public OAuth2AccessToken refreshAccessToken(
680           OAuth2ProtectedResourceDetails resource,
681           OAuth2RefreshToken refreshToken,
682           AccessTokenRequest request
683           )
684           throws
685             UserRedirectRequiredException
686       {
687         throw new UnsupportedOperationException("Not supported yet.");
688       }
689
690       @Override
691       public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)
692       {
693         return false;
694       }
695     });
696   }
697
698
699   static class SOME
700   {
701   }
702 }