1 package de.juplo.facebook;
6 import javax.annotation.Resource;
7 import static org.junit.Assert.*;
8 import org.junit.Before;
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;
35 @RunWith(SpringJUnit4ClassRunner.class)
36 @ContextConfiguration(
38 "classpath:/spring/test-facebook-error-handler.xml"
40 public class GraphApiErrorHandlerTest
42 private static final Logger log =
43 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
46 private OAuth2RestTemplate clientTemplate;
48 private MockClientHttpRequestFactory requestFactory;
52 public void testError1()
54 log.info("testError1");
57 requestFactory.setBody(
61 " \"message\": \"An unknown error has occurred.\",\n" +
62 " \"type\": \"OAuthException\",\n" +
69 clientTemplate.getForObject("ANY", SOME.class);
70 fail("The expected exception was not thrown");
72 catch(UnknownErrorException e)
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());
83 public void testError2()
85 log.info("testError2");
88 requestFactory.setBody(
92 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
93 " \"type\": \"OAuthException\",\n" +
100 clientTemplate.getForObject("ANY", SOME.class);
101 fail("The expected exception was not thrown");
103 catch(UnexpectedErrorException e)
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());
114 public void testError21()
116 log.info("testError21");
119 requestFactory.setBody(
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" +
131 clientTemplate.getForObject("ANY", SOME.class);
132 fail("The expected exception was not thrown");
134 catch(PageMigratedException e)
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());
145 public void testError100()
147 log.info("testError100");
150 requestFactory.setBody(
154 " \"message\": \"Unsupported get request.\",\n" +
155 " \"type\": \"GraphMethodException\",\n" +
162 clientTemplate.getForObject("ANY", SOME.class);
163 fail("The expected exception was not thrown");
165 catch(UnsupportedGetRequestException e)
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());
176 public void testError613()
178 log.info("testError613");
181 requestFactory.setBody(
185 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
186 " \"type\": \"OAuthException\",\n" +
193 clientTemplate.getForObject("ANY", SOME.class);
194 fail("The expected exception was not thrown");
196 catch(RateExceededException e)
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());
207 public void testUnmappedError()
209 log.info("testUnmappedError");
212 requestFactory.setBody(
216 " \"message\": \"This error does not exist.\",\n" +
217 " \"type\": \"NonexistentException\",\n" +
218 " \"code\": 999999999\n" +
224 clientTemplate.getForObject("ANY", SOME.class);
225 fail("The expected exception was not thrown");
227 catch(GraphApiException e)
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());
238 public void testInvlalidErrors()
240 log.info("testInvalidErrors");
243 requestFactory.setBody(
247 " \"message\": null,\n" +
248 " \"type\": \"Whatever\",\n" +
249 " \"code\": 999999999\n" +
255 clientTemplate.getForObject("ANY", SOME.class);
256 fail("The expected exception was not thrown");
258 catch(HttpClientErrorException e)
260 log.debug("{}", e.toString());
264 fail("A wrong exception was thrown: " + e.toString());
268 requestFactory.setBody(
272 " \"type\": \"Whatever\",\n" +
273 " \"code\": 999999999\n" +
279 clientTemplate.getForObject("ANY", SOME.class);
280 fail("The expected exception was not thrown");
282 catch(HttpClientErrorException e)
284 log.debug("{}", e.toString());
288 fail("A wrong exception was thrown: " + e.toString());
292 requestFactory.setBody(
296 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
297 " \"type\": null,\n" +
298 " \"code\": 999999999\n" +
304 clientTemplate.getForObject("ANY", SOME.class);
305 fail("The expected exception was not thrown");
307 catch(HttpClientErrorException e)
309 log.debug("{}", e.toString());
313 fail("A wrong exception was thrown: " + e.toString());
317 requestFactory.setBody(
321 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
322 " \"code\": 999999999\n" +
328 clientTemplate.getForObject("ANY", SOME.class);
329 fail("The expected exception was not thrown");
331 catch(HttpClientErrorException e)
333 log.debug("{}", e.toString());
337 fail("A wrong exception was thrown: " + e.toString());
341 requestFactory.setBody(
345 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
346 " \"type\": \"Whatever\",\n" +
347 " \"code\": \"some string\"\n" +
353 clientTemplate.getForObject("ANY", SOME.class);
354 fail("The expected exception was not thrown");
356 catch(HttpClientErrorException e)
358 log.debug("{}", e.toString());
362 fail("A wrong exception was thrown: " + e.toString());
366 requestFactory.setBody(
370 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
371 " \"type\": \"Whatever\",\n" +
378 clientTemplate.getForObject("ANY", SOME.class);
379 fail("The expected exception was not thrown");
381 catch(HttpClientErrorException e)
383 log.debug("{}", e.toString());
387 fail("A wrong exception was thrown: " + e.toString());
391 requestFactory.setBody(
395 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
396 " \"type\": \"Whatever\",\n" +
397 " \"code\": null\n" +
403 clientTemplate.getForObject("ANY", SOME.class);
404 fail("The expected exception was not thrown");
406 catch(HttpClientErrorException e)
408 log.debug("{}", e.toString());
412 fail("A wrong exception was thrown: " + e.toString());
416 requestFactory.setBody(
420 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
421 " \"type\": \"Whatever\"\n" +
427 clientTemplate.getForObject("ANY", SOME.class);
428 fail("The expected exception was not thrown");
430 catch(HttpClientErrorException e)
432 log.debug("{}", e.toString());
436 fail("A wrong exception was thrown: " + e.toString());
440 requestFactory.setBody("{\"error\":{\"message\":null}}");
444 clientTemplate.getForObject("ANY", SOME.class);
445 fail("The expected exception was not thrown");
447 catch(HttpClientErrorException e)
449 log.debug("{}", e.toString());
453 fail("A wrong exception was thrown: " + e.toString());
457 requestFactory.setBody("{\"error\":{\"type\":null}}");
461 clientTemplate.getForObject("ANY", SOME.class);
462 fail("The expected exception was not thrown");
464 catch(HttpClientErrorException e)
466 log.debug("{}", e.toString());
470 fail("A wrong exception was thrown: " + e.toString());
474 requestFactory.setBody("{\"error\":{\"code\":null}}");
478 clientTemplate.getForObject("ANY", SOME.class);
479 fail("The expected exception was not thrown");
481 catch(HttpClientErrorException e)
483 log.debug("{}", e.toString());
487 fail("A wrong exception was thrown: " + e.toString());
491 requestFactory.setBody("{\"error\":{}}");
495 clientTemplate.getForObject("ANY", SOME.class);
496 fail("The expected exception was not thrown");
498 catch(HttpClientErrorException e)
500 log.debug("{}", e.toString());
504 fail("A wrong exception was thrown: " + e.toString());
508 requestFactory.setBody("{\"error\":\"some message\"}");
512 clientTemplate.getForObject("ANY", SOME.class);
513 fail("The expected exception was not thrown");
515 catch(HttpClientErrorException e)
517 log.debug("{}", e.toString());
521 fail("A wrong exception was thrown: " + e.toString());
525 requestFactory.setBody("{\"error\":null}");
529 clientTemplate.getForObject("ANY", SOME.class);
530 fail("The expected exception was not thrown");
532 catch(HttpClientErrorException e)
534 log.debug("{}", e.toString());
538 fail("A wrong exception was thrown: " + e.toString());
542 requestFactory.setBody("{\"some filed\":\"some message\"}");
546 clientTemplate.getForObject("ANY", SOME.class);
547 fail("The expected exception was not thrown");
549 catch(HttpClientErrorException e)
551 log.debug("{}", e.toString());
555 fail("A wrong exception was thrown: " + e.toString());
559 requestFactory.setBody("{}");
563 clientTemplate.getForObject("ANY", SOME.class);
564 fail("The expected exception was not thrown");
566 catch(HttpClientErrorException e)
568 log.debug("{}", e.toString());
572 fail("A wrong exception was thrown: " + e.toString());
576 requestFactory.setBody("");
580 clientTemplate.getForObject("ANY", SOME.class);
581 fail("The expected exception was not thrown");
583 catch(HttpClientErrorException e)
585 log.debug("{}", e.toString());
589 fail("A wrong exception was thrown: " + e.toString());
597 requestFactory = new MockClientHttpRequestFactory();
598 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
599 requestFactory.addHeader("Content-Type", "application/json");
600 clientTemplate.setRequestFactory(requestFactory);
602 clientTemplate.setErrorHandler(
603 new GraphApiErrorHandler(
604 (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
608 clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
611 public OAuth2AccessToken obtainAccessToken(
612 OAuth2ProtectedResourceDetails details,
613 AccessTokenRequest parameters
616 UserRedirectRequiredException,
617 UserApprovalRequiredException,
618 AccessDeniedException
620 return new OAuth2AccessToken() {
623 public Map<String, Object> getAdditionalInformation()
625 throw new UnsupportedOperationException("Not supported yet.");
629 public Set<String> getScope()
631 throw new UnsupportedOperationException("Not supported yet.");
635 public OAuth2RefreshToken getRefreshToken()
637 throw new UnsupportedOperationException("Not supported yet.");
641 public String getTokenType()
647 public boolean isExpired()
653 public Date getExpiration()
655 throw new UnsupportedOperationException("Not supported yet.");
659 public int getExpiresIn()
661 throw new UnsupportedOperationException("Not supported yet.");
665 public String getValue()
673 public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
679 public OAuth2AccessToken refreshAccessToken(
680 OAuth2ProtectedResourceDetails resource,
681 OAuth2RefreshToken refreshToken,
682 AccessTokenRequest request
685 UserRedirectRequiredException
687 throw new UnsupportedOperationException("Not supported yet.");
691 public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)