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