X-Git-Url: https://juplo.de/gitweb/?p=facebook-errors;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FGraphApiErrorResponseErrorHandlerIntegrationTest.java;fp=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FGraphApiErrorResponseErrorHandlerIntegrationTest.java;h=659337585754328be4f3e6fd75287759a23bd4bd;hp=0000000000000000000000000000000000000000;hb=29d960306f0bea890cc1f02a4d66943d76013708;hpb=e3b130e04e4e89a08e203185ba9ae7e6c1f350d8 diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java new file mode 100644 index 0000000..6593375 --- /dev/null +++ b/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java @@ -0,0 +1,205 @@ +package de.juplo.facebook.errors; + + +import de.juplo.facebook.errors.GraphApiException.Type; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + + + +/** + * + * @author Kai Moritz + */ +public class GraphApiErrorResponseErrorHandlerIntegrationTest +{ + private static final Logger LOG = + LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class); + + private RestTemplate clientTemplate; + private MockClientHttpRequestFactory requestFactory; + + + @Test + public void testNoError() + { + LOG.info("testNoError"); + + requestFactory.setBody("{\n" + + " \"error\": {\n" + + " \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" + + " \"type\": \"OAuthException\",\n" + + " \"code\": 200\n" + + " }\n" + + "}"); + + + requestFactory.setStatus(HttpStatus.CONTINUE); + try + { + clientTemplate.getForObject("ANY", SOME.class); + } + catch(Exception e) + { + LOG.debug("{}", e.toString()); + fail("Unexpected error: " + e); + } + + requestFactory.setStatus(HttpStatus.OK); + try + { + clientTemplate.getForObject("ANY", SOME.class); + } + catch(Exception e) + { + LOG.debug("{}", e.toString()); + fail("Unexpected error: " + e); + } + + requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT); + try + { + clientTemplate.getForObject("ANY", SOME.class); + } + catch(Exception e) + { + LOG.debug("{}", e.toString()); + fail("Unexpected error: " + e); + } + + requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); + try + { + clientTemplate.getForObject("ANY", SOME.class); + } + catch(Exception e) + { + LOG.debug("{}", e.toString()); + fail("Unexpected error: " + e); + } + } + + @Test + public void testValidError() + { + LOG.info("testValidError"); + + + requestFactory.setBody( + "{\n" + + " \"error\":\n" + + " {\n" + + " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" + + " \"type\": \"OAuthException\",\n" + + " \"code\": 613\n" + + " }\n" + + "}"); + + try + { + clientTemplate.getForObject("ANY", SOME.class); + fail("The expected exception was not thrown"); + } + catch(RateLimitExceededException e) + { + LOG.debug("{}", e.toString()); + assertEquals(new Integer(613), e.getCode()); + assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage()); + assertEquals(Type.OAuthException, e.getType()); + } + } + + @Test + public void testUnmappedError() + { + LOG.info("testUnmappedError"); + + + requestFactory.setBody( + "{\n" + + " \"error\":\n" + + " {\n" + + " \"message\": \"This error does not exist.\",\n" + + " \"type\": \"NonexistentTypeException\",\n" + + " \"code\": 999999999\n" + + " }\n" + + "}"); + + try + { + clientTemplate.getForObject("ANY", SOME.class); + fail("The expected exception was not thrown"); + } + catch(GraphApiException e) + { + LOG.debug("{}", e.toString()); + assertEquals(new Integer(999999999), e.getCode()); + assertEquals("This error does not exist.", e.getMessage()); + try + { + Type type = e.getType(); + LOG.error("unknown type: {}", type); + fail("unmapped type was resolved by enum: " + type); + } + catch (IllegalArgumentException ee) {} + } + } + + @Test + public void testInvlalidError() + { + LOG.info("testInvalidError"); + + + requestFactory.setBody( + "{\n" + + " \"error\":\n" + + " {\n" + + " \"message\": \"Not a Graph-Api-Exception.\",\n" + + " \"type\": \"Whatever\",\n" + + " \"code\": \"some string\"\n" + + " }\n" + + "}"); + + try + { + clientTemplate.getForObject("ANY", SOME.class); + fail("The expected exception was not thrown"); + } + catch(HttpClientErrorException e) + { + LOG.debug("{}", e.toString()); + } + catch(Exception e) + { + fail("A wrong exception was thrown: " + e.toString()); + } + } + + + @Before + public void setUp() + { + requestFactory = new MockClientHttpRequestFactory(); + requestFactory.setStatus(HttpStatus.BAD_REQUEST); + requestFactory.addHeader("Content-Type", "application/json"); + + clientTemplate = new RestTemplate(); + clientTemplate.setRequestFactory(requestFactory); + clientTemplate.setErrorHandler( + new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler()) + ); + } + + + static class SOME + { + } +}