1 package de.juplo.facebook.client;
3 import de.juplo.facebook.exceptions.AccessTokenRequiredException;
4 import de.juplo.facebook.exceptions.UnsupportedGetRequestException;
5 import de.juplo.facebook.exceptions.UnexpectedErrorException;
6 import de.juplo.facebook.exceptions.RateExceededException;
7 import de.juplo.facebook.exceptions.GraphApiException;
8 import de.juplo.facebook.exceptions.UnknownErrorException;
9 import de.juplo.facebook.exceptions.PageMigratedException;
10 import de.juplo.facebook.exceptions.UnmappedErrorException;
11 import java.util.Date;
14 import javax.annotation.Resource;
15 import static org.junit.Assert.*;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.http.HttpStatus;
22 import org.springframework.security.access.AccessDeniedException;
23 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
24 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
25 import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
26 import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
27 import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
28 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
29 import org.springframework.security.oauth2.client.token.AccessTokenRequest;
30 import org.springframework.security.oauth2.common.OAuth2AccessToken;
31 import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE;
32 import org.springframework.security.oauth2.common.OAuth2RefreshToken;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35 import org.springframework.web.client.HttpClientErrorException;
43 @RunWith(SpringJUnit4ClassRunner.class)
44 @ContextConfiguration(
46 "classpath:/spring/test-facebook-error-handler.xml"
48 public class GraphApiErrorHandlerTest
50 private static final Logger log =
51 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
54 private OAuth2RestTemplate clientTemplate;
56 private MockClientHttpRequestFactory requestFactory;
60 public void testError1()
62 log.info("testError1");
65 requestFactory.setBody(
69 " \"message\": \"An unknown error has occurred.\",\n" +
70 " \"type\": \"OAuthException\",\n" +
77 clientTemplate.getForObject("ANY", SOME.class);
78 fail("The expected exception was not thrown");
80 catch(UnknownErrorException e)
82 log.debug("{}", e.toString());
83 assertEquals("invalid_request", e.getOAuth2ErrorCode());
84 assertEquals(new Integer(1), e.getCode());
85 assertEquals("An unknown error has occurred.", e.getMessage());
86 assertEquals("OAuthException", e.getType());
91 public void testError2()
93 log.info("testError2");
96 requestFactory.setBody(
100 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
101 " \"type\": \"OAuthException\",\n" +
108 clientTemplate.getForObject("ANY", SOME.class);
109 fail("The expected exception was not thrown");
111 catch(UnexpectedErrorException e)
113 log.debug("{}", e.toString());
114 assertEquals("invalid_request", e.getOAuth2ErrorCode());
115 assertEquals(new Integer(2), e.getCode());
116 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
117 assertEquals("OAuthException", e.getType());
122 public void testError21()
124 log.info("testError21");
127 requestFactory.setBody(
131 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
132 " \"type\": \"OAuthException\",\n" +
139 clientTemplate.getForObject("ANY", SOME.class);
140 fail("The expected exception was not thrown");
142 catch(PageMigratedException e)
144 log.debug("{}", e.toString());
145 assertEquals("invalid_request", e.getOAuth2ErrorCode());
146 assertEquals(new Integer(21), e.getCode());
147 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
148 assertEquals("OAuthException", e.getType());
153 public void testError100()
155 log.info("testError100");
158 requestFactory.setBody(
162 " \"message\": \"Unsupported get request.\",\n" +
163 " \"type\": \"GraphMethodException\",\n" +
170 clientTemplate.getForObject("ANY", SOME.class);
171 fail("The expected exception was not thrown");
173 catch(UnsupportedGetRequestException e)
175 log.debug("{}", e.toString());
176 assertEquals("invalid_request", e.getOAuth2ErrorCode());
177 assertEquals(new Integer(100), e.getCode());
178 assertEquals("Unsupported get request.", e.getMessage());
179 assertEquals("GraphMethodException", e.getType());
184 public void testError104()
186 log.info("testError104");
188 requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
192 clientTemplate.getForObject("ANY", SOME.class);
193 fail("The expected exception was not thrown");
195 catch(AccessTokenRequiredException e)
197 log.debug("{}", e.toString());
198 assertEquals("invalid_request", e.getOAuth2ErrorCode());
199 assertEquals(new Integer(104), e.getCode());
200 assertEquals("An access token is required to request this resource.", e.getMessage());
201 assertEquals("OAuthException", e.getType());
202 assertEquals("E2Jjkj5++LL", e.getTraceId());
207 public void testError613()
209 log.info("testError613");
212 requestFactory.setBody(
216 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
217 " \"type\": \"OAuthException\",\n" +
224 clientTemplate.getForObject("ANY", SOME.class);
225 fail("The expected exception was not thrown");
227 catch(RateExceededException e)
229 log.debug("{}", e.toString());
230 assertEquals("invalid_request", e.getOAuth2ErrorCode());
231 assertEquals(new Integer(613), e.getCode());
232 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
233 assertEquals("OAuthException", e.getType());
238 public void testUnmappedError()
240 log.info("testUnmappedError");
243 requestFactory.setBody(
247 " \"message\": \"This error does not exist.\",\n" +
248 " \"type\": \"NonexistentException\",\n" +
249 " \"code\": 999999999\n" +
255 clientTemplate.getForObject("ANY", SOME.class);
256 fail("The expected exception was not thrown");
258 catch(GraphApiException e)
260 log.debug("{}", e.toString());
261 assertEquals("invalid_request", e.getOAuth2ErrorCode());
262 assertEquals(new Integer(999999999), e.getCode());
263 assertEquals("This error does not exist.", e.getMessage());
264 assertEquals("NonexistentException", e.getType());
269 public void testUnmappedErrors()
271 log.info("testUnmappedErrors");
274 requestFactory.setBody(
278 " \"message\": null,\n" +
279 " \"type\": \"Whatever\",\n" +
280 " \"code\": 999999999\n" +
286 clientTemplate.getForObject("ANY", SOME.class);
287 fail("The expected exception was not thrown");
289 catch(UnmappedErrorException e)
291 log.debug("{}", e.toString());
292 assertNull(e.getMessage());
293 assertEquals("Whatever", e.getType());
294 assertEquals(new Integer(999999999), e.getCode());
295 assertNull(e.getSubCode());
296 assertNull(e.getUserTitle());
297 assertNull(e.getUserMessage());
298 assertNull(e.getTraceId());
302 fail("A wrong exception was thrown: " + e.toString());
306 requestFactory.setBody(
310 " \"type\": \"Whatever\",\n" +
311 " \"code\": 999999999\n" +
317 clientTemplate.getForObject("ANY", SOME.class);
318 fail("The expected exception was not thrown");
320 catch(UnmappedErrorException e)
322 log.debug("{}", e.toString());
323 assertNull(e.getMessage());
324 assertEquals("Whatever", e.getType());
325 assertEquals(new Integer(999999999), e.getCode());
326 assertNull(e.getSubCode());
327 assertNull(e.getUserTitle());
328 assertNull(e.getUserMessage());
329 assertNull(e.getTraceId());
333 fail("A wrong exception was thrown: " + e.toString());
337 requestFactory.setBody(
341 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
342 " \"type\": null,\n" +
343 " \"code\": 999999999\n" +
349 clientTemplate.getForObject("ANY", SOME.class);
350 fail("The expected exception was not thrown");
352 catch(UnmappedErrorException e)
354 log.debug("{}", e.toString());
355 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
356 assertNull(e.getType());
357 assertEquals(new Integer(999999999), e.getCode());
358 assertNull(e.getSubCode());
359 assertNull(e.getUserTitle());
360 assertNull(e.getUserMessage());
361 assertNull(e.getTraceId());
365 fail("A wrong exception was thrown: " + e.toString());
369 requestFactory.setBody(
373 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
374 " \"code\": 999999999\n" +
380 clientTemplate.getForObject("ANY", SOME.class);
381 fail("The expected exception was not thrown");
383 catch(UnmappedErrorException e)
385 log.debug("{}", e.toString());
386 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
387 assertNull(e.getType());
388 assertEquals(new Integer(999999999), e.getCode());
389 assertNull(e.getSubCode());
390 assertNull(e.getUserTitle());
391 assertNull(e.getUserMessage());
392 assertNull(e.getTraceId());
396 fail("A wrong exception was thrown: " + e.toString());
401 public void testInvlalidErrors()
403 log.info("testInvalidErrors");
406 requestFactory.setBody(
410 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
411 " \"type\": \"Whatever\",\n" +
412 " \"code\": \"some string\"\n" +
418 clientTemplate.getForObject("ANY", SOME.class);
419 fail("The expected exception was not thrown");
421 catch(HttpClientErrorException e)
423 log.debug("{}", e.toString());
427 fail("A wrong exception was thrown: " + e.toString());
431 requestFactory.setBody(
435 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
436 " \"type\": \"Whatever\",\n" +
443 clientTemplate.getForObject("ANY", SOME.class);
444 fail("The expected exception was not thrown");
446 catch(HttpClientErrorException e)
448 log.debug("{}", e.toString());
452 fail("A wrong exception was thrown: " + e.toString());
456 requestFactory.setBody(
460 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
461 " \"type\": \"Whatever\",\n" +
462 " \"code\": null\n" +
468 clientTemplate.getForObject("ANY", SOME.class);
469 fail("The expected exception was not thrown");
471 catch(HttpClientErrorException e)
473 log.debug("{}", e.toString());
477 fail("A wrong exception was thrown: " + e.toString());
481 requestFactory.setBody(
485 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
486 " \"type\": \"Whatever\"\n" +
492 clientTemplate.getForObject("ANY", SOME.class);
493 fail("The expected exception was not thrown");
495 catch(HttpClientErrorException e)
497 log.debug("{}", e.toString());
501 fail("A wrong exception was thrown: " + e.toString());
505 requestFactory.setBody("{\"error\":{\"message\":null}}");
509 clientTemplate.getForObject("ANY", SOME.class);
510 fail("The expected exception was not thrown");
512 catch(HttpClientErrorException e)
514 log.debug("{}", e.toString());
518 fail("A wrong exception was thrown: " + e.toString());
522 requestFactory.setBody("{\"error\":{\"type\":null}}");
526 clientTemplate.getForObject("ANY", SOME.class);
527 fail("The expected exception was not thrown");
529 catch(HttpClientErrorException e)
531 log.debug("{}", e.toString());
535 fail("A wrong exception was thrown: " + e.toString());
539 requestFactory.setBody("{\"error\":{\"code\":null}}");
543 clientTemplate.getForObject("ANY", SOME.class);
544 fail("The expected exception was not thrown");
546 catch(HttpClientErrorException e)
548 log.debug("{}", e.toString());
552 fail("A wrong exception was thrown: " + e.toString());
556 requestFactory.setBody("{\"error\":{}}");
560 clientTemplate.getForObject("ANY", SOME.class);
561 fail("The expected exception was not thrown");
563 catch(HttpClientErrorException e)
565 log.debug("{}", e.toString());
569 fail("A wrong exception was thrown: " + e.toString());
573 requestFactory.setBody("{\"error\":\"some message\"}");
577 clientTemplate.getForObject("ANY", SOME.class);
578 fail("The expected exception was not thrown");
580 catch(HttpClientErrorException e)
582 log.debug("{}", e.toString());
586 fail("A wrong exception was thrown: " + e.toString());
590 requestFactory.setBody("{\"error\":null}");
594 clientTemplate.getForObject("ANY", SOME.class);
595 fail("The expected exception was not thrown");
597 catch(HttpClientErrorException e)
599 log.debug("{}", e.toString());
603 fail("A wrong exception was thrown: " + e.toString());
607 requestFactory.setBody("{\"some filed\":\"some message\"}");
611 clientTemplate.getForObject("ANY", SOME.class);
612 fail("The expected exception was not thrown");
614 catch(HttpClientErrorException e)
616 log.debug("{}", e.toString());
620 fail("A wrong exception was thrown: " + e.toString());
624 requestFactory.setBody("{}");
628 clientTemplate.getForObject("ANY", SOME.class);
629 fail("The expected exception was not thrown");
631 catch(HttpClientErrorException e)
633 log.debug("{}", e.toString());
637 fail("A wrong exception was thrown: " + e.toString());
641 requestFactory.setBody("");
645 clientTemplate.getForObject("ANY", SOME.class);
646 fail("The expected exception was not thrown");
648 catch(HttpClientErrorException e)
650 log.debug("{}", e.toString());
654 fail("A wrong exception was thrown: " + e.toString());
662 requestFactory = new MockClientHttpRequestFactory();
663 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
664 requestFactory.addHeader("Content-Type", "application/json");
665 clientTemplate.setRequestFactory(requestFactory);
667 clientTemplate.setErrorHandler(
668 new GraphApiErrorHandler(
669 (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
673 clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
676 public OAuth2AccessToken obtainAccessToken(
677 OAuth2ProtectedResourceDetails details,
678 AccessTokenRequest parameters
681 UserRedirectRequiredException,
682 UserApprovalRequiredException,
683 AccessDeniedException
685 return new OAuth2AccessToken() {
688 public Map<String, Object> getAdditionalInformation()
690 throw new UnsupportedOperationException("Not supported yet.");
694 public Set<String> getScope()
696 throw new UnsupportedOperationException("Not supported yet.");
700 public OAuth2RefreshToken getRefreshToken()
702 throw new UnsupportedOperationException("Not supported yet.");
706 public String getTokenType()
712 public boolean isExpired()
718 public Date getExpiration()
720 throw new UnsupportedOperationException("Not supported yet.");
724 public int getExpiresIn()
726 throw new UnsupportedOperationException("Not supported yet.");
730 public String getValue()
738 public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
744 public OAuth2AccessToken refreshAccessToken(
745 OAuth2ProtectedResourceDetails resource,
746 OAuth2RefreshToken refreshToken,
747 AccessTokenRequest request
750 UserRedirectRequiredException
752 throw new UnsupportedOperationException("Not supported yet.");
756 public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)