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;
9 import de.juplo.facebook.exceptions.UnmappedErrorException;
10 import java.util.Date;
13 import javax.annotation.Resource;
14 import static org.junit.Assert.*;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.http.HttpStatus;
21 import org.springframework.security.access.AccessDeniedException;
22 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
23 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
24 import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
25 import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
26 import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
27 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
28 import org.springframework.security.oauth2.client.token.AccessTokenRequest;
29 import org.springframework.security.oauth2.common.OAuth2AccessToken;
30 import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE;
31 import org.springframework.security.oauth2.common.OAuth2RefreshToken;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34 import org.springframework.web.client.HttpClientErrorException;
42 @RunWith(SpringJUnit4ClassRunner.class)
43 @ContextConfiguration(
45 "classpath:/spring/test-facebook-error-handler.xml"
47 public class GraphApiErrorHandlerTest
49 private static final Logger log =
50 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
53 private OAuth2RestTemplate clientTemplate;
55 private MockClientHttpRequestFactory requestFactory;
59 public void testError1()
61 log.info("testError1");
64 requestFactory.setBody(
68 " \"message\": \"An unknown error has occurred.\",\n" +
69 " \"type\": \"OAuthException\",\n" +
76 clientTemplate.getForObject("ANY", SOME.class);
77 fail("The expected exception was not thrown");
79 catch(UnknownErrorException e)
81 log.debug("{}", e.toString());
82 assertEquals("invalid_request", e.getOAuth2ErrorCode());
83 assertEquals(new Integer(1), e.getCode());
84 assertEquals("An unknown error has occurred.", e.getMessage());
85 assertEquals("OAuthException", e.getType());
90 public void testError2()
92 log.info("testError2");
95 requestFactory.setBody(
99 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
100 " \"type\": \"OAuthException\",\n" +
107 clientTemplate.getForObject("ANY", SOME.class);
108 fail("The expected exception was not thrown");
110 catch(UnexpectedErrorException e)
112 log.debug("{}", e.toString());
113 assertEquals("invalid_request", e.getOAuth2ErrorCode());
114 assertEquals(new Integer(2), e.getCode());
115 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
116 assertEquals("OAuthException", e.getType());
121 public void testError21()
123 log.info("testError21");
126 requestFactory.setBody(
130 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
131 " \"type\": \"OAuthException\",\n" +
138 clientTemplate.getForObject("ANY", SOME.class);
139 fail("The expected exception was not thrown");
141 catch(PageMigratedException e)
143 log.debug("{}", e.toString());
144 assertEquals("invalid_request", e.getOAuth2ErrorCode());
145 assertEquals(new Integer(21), e.getCode());
146 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
147 assertEquals("OAuthException", e.getType());
152 public void testError100()
154 log.info("testError100");
157 requestFactory.setBody(
161 " \"message\": \"Unsupported get request.\",\n" +
162 " \"type\": \"GraphMethodException\",\n" +
169 clientTemplate.getForObject("ANY", SOME.class);
170 fail("The expected exception was not thrown");
172 catch(UnsupportedGetRequestException e)
174 log.debug("{}", e.toString());
175 assertEquals("invalid_request", e.getOAuth2ErrorCode());
176 assertEquals(new Integer(100), e.getCode());
177 assertEquals("Unsupported get request.", e.getMessage());
178 assertEquals("GraphMethodException", e.getType());
183 public void testError613()
185 log.info("testError613");
188 requestFactory.setBody(
192 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
193 " \"type\": \"OAuthException\",\n" +
200 clientTemplate.getForObject("ANY", SOME.class);
201 fail("The expected exception was not thrown");
203 catch(RateExceededException e)
205 log.debug("{}", e.toString());
206 assertEquals("invalid_request", e.getOAuth2ErrorCode());
207 assertEquals(new Integer(613), e.getCode());
208 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
209 assertEquals("OAuthException", e.getType());
214 public void testUnmappedError()
216 log.info("testUnmappedError");
219 requestFactory.setBody(
223 " \"message\": \"This error does not exist.\",\n" +
224 " \"type\": \"NonexistentException\",\n" +
225 " \"code\": 999999999\n" +
231 clientTemplate.getForObject("ANY", SOME.class);
232 fail("The expected exception was not thrown");
234 catch(GraphApiException e)
236 log.debug("{}", e.toString());
237 assertEquals("invalid_request", e.getOAuth2ErrorCode());
238 assertEquals(new Integer(999999999), e.getCode());
239 assertEquals("This error does not exist.", e.getMessage());
240 assertEquals("NonexistentException", e.getType());
245 public void testUnmappedErrors()
247 log.info("testUnmappedErrors");
250 requestFactory.setBody(
254 " \"message\": null,\n" +
255 " \"type\": \"Whatever\",\n" +
256 " \"code\": 999999999\n" +
262 clientTemplate.getForObject("ANY", SOME.class);
263 fail("The expected exception was not thrown");
265 catch(UnmappedErrorException e)
267 log.debug("{}", e.toString());
268 assertNull(e.getMessage());
269 assertEquals("Whatever", e.getType());
270 assertEquals(new Integer(999999999), e.getCode());
271 assertNull(e.getSubCode());
272 assertNull(e.getUserTitle());
273 assertNull(e.getUserMessage());
274 assertNull(e.getTraceId());
278 fail("A wrong exception was thrown: " + e.toString());
282 requestFactory.setBody(
286 " \"type\": \"Whatever\",\n" +
287 " \"code\": 999999999\n" +
293 clientTemplate.getForObject("ANY", SOME.class);
294 fail("The expected exception was not thrown");
296 catch(UnmappedErrorException e)
298 log.debug("{}", e.toString());
299 assertNull(e.getMessage());
300 assertEquals("Whatever", e.getType());
301 assertEquals(new Integer(999999999), e.getCode());
302 assertNull(e.getSubCode());
303 assertNull(e.getUserTitle());
304 assertNull(e.getUserMessage());
305 assertNull(e.getTraceId());
309 fail("A wrong exception was thrown: " + e.toString());
313 requestFactory.setBody(
317 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
318 " \"type\": null,\n" +
319 " \"code\": 999999999\n" +
325 clientTemplate.getForObject("ANY", SOME.class);
326 fail("The expected exception was not thrown");
328 catch(UnmappedErrorException e)
330 log.debug("{}", e.toString());
331 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
332 assertNull(e.getType());
333 assertEquals(new Integer(999999999), e.getCode());
334 assertNull(e.getSubCode());
335 assertNull(e.getUserTitle());
336 assertNull(e.getUserMessage());
337 assertNull(e.getTraceId());
341 fail("A wrong exception was thrown: " + e.toString());
345 requestFactory.setBody(
349 " \"message\": \"An unmapped Graph-API-Exception.\",\n" +
350 " \"code\": 999999999\n" +
356 clientTemplate.getForObject("ANY", SOME.class);
357 fail("The expected exception was not thrown");
359 catch(UnmappedErrorException e)
361 log.debug("{}", e.toString());
362 assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
363 assertNull(e.getType());
364 assertEquals(new Integer(999999999), e.getCode());
365 assertNull(e.getSubCode());
366 assertNull(e.getUserTitle());
367 assertNull(e.getUserMessage());
368 assertNull(e.getTraceId());
372 fail("A wrong exception was thrown: " + e.toString());
377 public void testInvlalidErrors()
379 log.info("testInvalidErrors");
382 requestFactory.setBody(
386 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
387 " \"type\": \"Whatever\",\n" +
388 " \"code\": \"some string\"\n" +
394 clientTemplate.getForObject("ANY", SOME.class);
395 fail("The expected exception was not thrown");
397 catch(HttpClientErrorException e)
399 log.debug("{}", e.toString());
403 fail("A wrong exception was thrown: " + e.toString());
407 requestFactory.setBody(
411 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
412 " \"type\": \"Whatever\",\n" +
419 clientTemplate.getForObject("ANY", SOME.class);
420 fail("The expected exception was not thrown");
422 catch(HttpClientErrorException e)
424 log.debug("{}", e.toString());
428 fail("A wrong exception was thrown: " + e.toString());
432 requestFactory.setBody(
436 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
437 " \"type\": \"Whatever\",\n" +
438 " \"code\": null\n" +
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(
461 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
462 " \"type\": \"Whatever\"\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("{\"error\":{\"message\":null}}");
485 clientTemplate.getForObject("ANY", SOME.class);
486 fail("The expected exception was not thrown");
488 catch(HttpClientErrorException e)
490 log.debug("{}", e.toString());
494 fail("A wrong exception was thrown: " + e.toString());
498 requestFactory.setBody("{\"error\":{\"type\":null}}");
502 clientTemplate.getForObject("ANY", SOME.class);
503 fail("The expected exception was not thrown");
505 catch(HttpClientErrorException e)
507 log.debug("{}", e.toString());
511 fail("A wrong exception was thrown: " + e.toString());
515 requestFactory.setBody("{\"error\":{\"code\":null}}");
519 clientTemplate.getForObject("ANY", SOME.class);
520 fail("The expected exception was not thrown");
522 catch(HttpClientErrorException e)
524 log.debug("{}", e.toString());
528 fail("A wrong exception was thrown: " + e.toString());
532 requestFactory.setBody("{\"error\":{}}");
536 clientTemplate.getForObject("ANY", SOME.class);
537 fail("The expected exception was not thrown");
539 catch(HttpClientErrorException e)
541 log.debug("{}", e.toString());
545 fail("A wrong exception was thrown: " + e.toString());
549 requestFactory.setBody("{\"error\":\"some message\"}");
553 clientTemplate.getForObject("ANY", SOME.class);
554 fail("The expected exception was not thrown");
556 catch(HttpClientErrorException e)
558 log.debug("{}", e.toString());
562 fail("A wrong exception was thrown: " + e.toString());
566 requestFactory.setBody("{\"error\":null}");
570 clientTemplate.getForObject("ANY", SOME.class);
571 fail("The expected exception was not thrown");
573 catch(HttpClientErrorException e)
575 log.debug("{}", e.toString());
579 fail("A wrong exception was thrown: " + e.toString());
583 requestFactory.setBody("{\"some filed\":\"some message\"}");
587 clientTemplate.getForObject("ANY", SOME.class);
588 fail("The expected exception was not thrown");
590 catch(HttpClientErrorException e)
592 log.debug("{}", e.toString());
596 fail("A wrong exception was thrown: " + e.toString());
600 requestFactory.setBody("{}");
604 clientTemplate.getForObject("ANY", SOME.class);
605 fail("The expected exception was not thrown");
607 catch(HttpClientErrorException e)
609 log.debug("{}", e.toString());
613 fail("A wrong exception was thrown: " + e.toString());
617 requestFactory.setBody("");
621 clientTemplate.getForObject("ANY", SOME.class);
622 fail("The expected exception was not thrown");
624 catch(HttpClientErrorException e)
626 log.debug("{}", e.toString());
630 fail("A wrong exception was thrown: " + e.toString());
638 requestFactory = new MockClientHttpRequestFactory();
639 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
640 requestFactory.addHeader("Content-Type", "application/json");
641 clientTemplate.setRequestFactory(requestFactory);
643 clientTemplate.setErrorHandler(
644 new GraphApiErrorHandler(
645 (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
649 clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
652 public OAuth2AccessToken obtainAccessToken(
653 OAuth2ProtectedResourceDetails details,
654 AccessTokenRequest parameters
657 UserRedirectRequiredException,
658 UserApprovalRequiredException,
659 AccessDeniedException
661 return new OAuth2AccessToken() {
664 public Map<String, Object> getAdditionalInformation()
666 throw new UnsupportedOperationException("Not supported yet.");
670 public Set<String> getScope()
672 throw new UnsupportedOperationException("Not supported yet.");
676 public OAuth2RefreshToken getRefreshToken()
678 throw new UnsupportedOperationException("Not supported yet.");
682 public String getTokenType()
688 public boolean isExpired()
694 public Date getExpiration()
696 throw new UnsupportedOperationException("Not supported yet.");
700 public int getExpiresIn()
702 throw new UnsupportedOperationException("Not supported yet.");
706 public String getValue()
714 public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
720 public OAuth2AccessToken refreshAccessToken(
721 OAuth2ProtectedResourceDetails resource,
722 OAuth2RefreshToken refreshToken,
723 AccessTokenRequest request
726 UserRedirectRequiredException
728 throw new UnsupportedOperationException("Not supported yet.");
732 public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)