1 package de.juplo.facebook.client;
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;
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;
41 @RunWith(SpringJUnit4ClassRunner.class)
42 @ContextConfiguration(
44 "classpath:/spring/test-facebook-error-handler.xml"
46 public class GraphApiErrorHandlerTest
48 private static final Logger log =
49 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
52 private OAuth2RestTemplate clientTemplate;
54 private MockClientHttpRequestFactory requestFactory;
58 public void testError1()
60 log.info("testError1");
63 requestFactory.setBody(
67 " \"message\": \"An unknown error has occurred.\",\n" +
68 " \"type\": \"OAuthException\",\n" +
75 clientTemplate.getForObject("ANY", SOME.class);
76 fail("The expected exception was not thrown");
78 catch(UnknownErrorException e)
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());
89 public void testError2()
91 log.info("testError2");
94 requestFactory.setBody(
98 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
99 " \"type\": \"OAuthException\",\n" +
106 clientTemplate.getForObject("ANY", SOME.class);
107 fail("The expected exception was not thrown");
109 catch(UnexpectedErrorException e)
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());
120 public void testError21()
122 log.info("testError21");
125 requestFactory.setBody(
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" +
137 clientTemplate.getForObject("ANY", SOME.class);
138 fail("The expected exception was not thrown");
140 catch(PageMigratedException e)
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());
151 public void testError100()
153 log.info("testError100");
156 requestFactory.setBody(
160 " \"message\": \"Unsupported get request.\",\n" +
161 " \"type\": \"GraphMethodException\",\n" +
168 clientTemplate.getForObject("ANY", SOME.class);
169 fail("The expected exception was not thrown");
171 catch(UnsupportedGetRequestException e)
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());
182 public void testError613()
184 log.info("testError613");
187 requestFactory.setBody(
191 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
192 " \"type\": \"OAuthException\",\n" +
199 clientTemplate.getForObject("ANY", SOME.class);
200 fail("The expected exception was not thrown");
202 catch(RateExceededException e)
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());
213 public void testUnmappedError()
215 log.info("testUnmappedError");
218 requestFactory.setBody(
222 " \"message\": \"This error does not exist.\",\n" +
223 " \"type\": \"NonexistentException\",\n" +
224 " \"code\": 999999999\n" +
230 clientTemplate.getForObject("ANY", SOME.class);
231 fail("The expected exception was not thrown");
233 catch(GraphApiException e)
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());
244 public void testInvlalidErrors()
246 log.info("testInvalidErrors");
249 requestFactory.setBody(
253 " \"message\": null,\n" +
254 " \"type\": \"Whatever\",\n" +
255 " \"code\": 999999999\n" +
261 clientTemplate.getForObject("ANY", SOME.class);
262 fail("The expected exception was not thrown");
264 catch(HttpClientErrorException e)
266 log.debug("{}", e.toString());
270 fail("A wrong exception was thrown: " + e.toString());
274 requestFactory.setBody(
278 " \"type\": \"Whatever\",\n" +
279 " \"code\": 999999999\n" +
285 clientTemplate.getForObject("ANY", SOME.class);
286 fail("The expected exception was not thrown");
288 catch(HttpClientErrorException e)
290 log.debug("{}", e.toString());
294 fail("A wrong exception was thrown: " + e.toString());
298 requestFactory.setBody(
302 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
303 " \"type\": null,\n" +
304 " \"code\": 999999999\n" +
310 clientTemplate.getForObject("ANY", SOME.class);
311 fail("The expected exception was not thrown");
313 catch(HttpClientErrorException e)
315 log.debug("{}", e.toString());
319 fail("A wrong exception was thrown: " + e.toString());
323 requestFactory.setBody(
327 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
328 " \"code\": 999999999\n" +
334 clientTemplate.getForObject("ANY", SOME.class);
335 fail("The expected exception was not thrown");
337 catch(HttpClientErrorException e)
339 log.debug("{}", e.toString());
343 fail("A wrong exception was thrown: " + e.toString());
347 requestFactory.setBody(
351 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
352 " \"type\": \"Whatever\",\n" +
353 " \"code\": \"some string\"\n" +
359 clientTemplate.getForObject("ANY", SOME.class);
360 fail("The expected exception was not thrown");
362 catch(HttpClientErrorException e)
364 log.debug("{}", e.toString());
368 fail("A wrong exception was thrown: " + e.toString());
372 requestFactory.setBody(
376 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
377 " \"type\": \"Whatever\",\n" +
384 clientTemplate.getForObject("ANY", SOME.class);
385 fail("The expected exception was not thrown");
387 catch(HttpClientErrorException e)
389 log.debug("{}", e.toString());
393 fail("A wrong exception was thrown: " + e.toString());
397 requestFactory.setBody(
401 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
402 " \"type\": \"Whatever\",\n" +
403 " \"code\": null\n" +
409 clientTemplate.getForObject("ANY", SOME.class);
410 fail("The expected exception was not thrown");
412 catch(HttpClientErrorException e)
414 log.debug("{}", e.toString());
418 fail("A wrong exception was thrown: " + e.toString());
422 requestFactory.setBody(
426 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
427 " \"type\": \"Whatever\"\n" +
433 clientTemplate.getForObject("ANY", SOME.class);
434 fail("The expected exception was not thrown");
436 catch(HttpClientErrorException e)
438 log.debug("{}", e.toString());
442 fail("A wrong exception was thrown: " + e.toString());
446 requestFactory.setBody("{\"error\":{\"message\":null}}");
450 clientTemplate.getForObject("ANY", SOME.class);
451 fail("The expected exception was not thrown");
453 catch(HttpClientErrorException e)
455 log.debug("{}", e.toString());
459 fail("A wrong exception was thrown: " + e.toString());
463 requestFactory.setBody("{\"error\":{\"type\":null}}");
467 clientTemplate.getForObject("ANY", SOME.class);
468 fail("The expected exception was not thrown");
470 catch(HttpClientErrorException e)
472 log.debug("{}", e.toString());
476 fail("A wrong exception was thrown: " + e.toString());
480 requestFactory.setBody("{\"error\":{\"code\":null}}");
484 clientTemplate.getForObject("ANY", SOME.class);
485 fail("The expected exception was not thrown");
487 catch(HttpClientErrorException e)
489 log.debug("{}", e.toString());
493 fail("A wrong exception was thrown: " + e.toString());
497 requestFactory.setBody("{\"error\":{}}");
501 clientTemplate.getForObject("ANY", SOME.class);
502 fail("The expected exception was not thrown");
504 catch(HttpClientErrorException e)
506 log.debug("{}", e.toString());
510 fail("A wrong exception was thrown: " + e.toString());
514 requestFactory.setBody("{\"error\":\"some message\"}");
518 clientTemplate.getForObject("ANY", SOME.class);
519 fail("The expected exception was not thrown");
521 catch(HttpClientErrorException e)
523 log.debug("{}", e.toString());
527 fail("A wrong exception was thrown: " + e.toString());
531 requestFactory.setBody("{\"error\":null}");
535 clientTemplate.getForObject("ANY", SOME.class);
536 fail("The expected exception was not thrown");
538 catch(HttpClientErrorException e)
540 log.debug("{}", e.toString());
544 fail("A wrong exception was thrown: " + e.toString());
548 requestFactory.setBody("{\"some filed\":\"some message\"}");
552 clientTemplate.getForObject("ANY", SOME.class);
553 fail("The expected exception was not thrown");
555 catch(HttpClientErrorException e)
557 log.debug("{}", e.toString());
561 fail("A wrong exception was thrown: " + e.toString());
565 requestFactory.setBody("{}");
569 clientTemplate.getForObject("ANY", SOME.class);
570 fail("The expected exception was not thrown");
572 catch(HttpClientErrorException e)
574 log.debug("{}", e.toString());
578 fail("A wrong exception was thrown: " + e.toString());
582 requestFactory.setBody("");
586 clientTemplate.getForObject("ANY", SOME.class);
587 fail("The expected exception was not thrown");
589 catch(HttpClientErrorException e)
591 log.debug("{}", e.toString());
595 fail("A wrong exception was thrown: " + e.toString());
603 requestFactory = new MockClientHttpRequestFactory();
604 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
605 requestFactory.addHeader("Content-Type", "application/json");
606 clientTemplate.setRequestFactory(requestFactory);
608 clientTemplate.setErrorHandler(
609 new GraphApiErrorHandler(
610 (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
614 clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
617 public OAuth2AccessToken obtainAccessToken(
618 OAuth2ProtectedResourceDetails details,
619 AccessTokenRequest parameters
622 UserRedirectRequiredException,
623 UserApprovalRequiredException,
624 AccessDeniedException
626 return new OAuth2AccessToken() {
629 public Map<String, Object> getAdditionalInformation()
631 throw new UnsupportedOperationException("Not supported yet.");
635 public Set<String> getScope()
637 throw new UnsupportedOperationException("Not supported yet.");
641 public OAuth2RefreshToken getRefreshToken()
643 throw new UnsupportedOperationException("Not supported yet.");
647 public String getTokenType()
653 public boolean isExpired()
659 public Date getExpiration()
661 throw new UnsupportedOperationException("Not supported yet.");
665 public int getExpiresIn()
667 throw new UnsupportedOperationException("Not supported yet.");
671 public String getValue()
679 public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
685 public OAuth2AccessToken refreshAccessToken(
686 OAuth2ProtectedResourceDetails resource,
687 OAuth2RefreshToken refreshToken,
688 AccessTokenRequest request
691 UserRedirectRequiredException
693 throw new UnsupportedOperationException("Not supported yet.");
697 public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)