1 package de.juplo.facebook.errors;
4 import de.juplo.facebook.errors.GraphApiException.Type;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.fail;
7 import org.junit.Before;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.web.client.HttpClientErrorException;
13 import org.springframework.web.client.HttpServerErrorException;
14 import org.springframework.web.client.RestTemplate;
22 public class GraphApiErrorResponseErrorHandlerIntegrationTest
24 private static final Logger LOG =
25 LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class);
27 private RestTemplate clientTemplate;
28 private MockClientHttpRequestFactory requestFactory;
32 public void testNoError()
34 LOG.info("testNoError");
36 requestFactory.setBody("{ \"message\": \"Hello World!\" }");
39 requestFactory.setStatus(HttpStatus.CONTINUE);
42 clientTemplate.getForObject("ANY", SOME.class);
46 LOG.debug("{}", e.toString());
47 fail("Unexpected error: " + e);
50 requestFactory.setStatus(HttpStatus.OK);
53 clientTemplate.getForObject("ANY", SOME.class);
57 LOG.debug("{}", e.toString());
58 fail("Unexpected error: " + e);
61 requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
64 clientTemplate.getForObject("ANY", SOME.class);
68 LOG.debug("{}", e.toString());
69 fail("Unexpected error: " + e);
72 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
75 clientTemplate.getForObject("ANY", SOME.class);
76 fail("The parent handler should have raised an exception!");
78 catch(HttpClientErrorException e)
80 LOG.debug("Expexted error: {}", e.toString());
84 LOG.debug("{}", e.toString());
85 fail("Unexpected error: " + e);
88 requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
91 clientTemplate.getForObject("ANY", SOME.class);
92 fail("The parent handler should have raised an exception!");
94 catch(HttpServerErrorException e)
96 LOG.debug("Expexted error: {}", e.toString());
100 LOG.debug("{}", e.toString());
101 fail("Unexpected error: " + e);
106 public void testValidError()
108 LOG.info("testValidError");
111 requestFactory.setBody(
115 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
116 " \"type\": \"OAuthException\",\n" +
123 clientTemplate.getForObject("ANY", SOME.class);
124 fail("The expected exception was not thrown");
126 catch(RateLimitExceededException e)
128 LOG.debug("{}", e.toString());
129 assertEquals(new Integer(613), e.getCode());
130 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
131 assertEquals(Type.OAuthException, e.getType());
136 public void testUnmappedError()
138 LOG.info("testUnmappedError");
141 requestFactory.setBody(
145 " \"message\": \"This error does not exist.\",\n" +
146 " \"type\": \"NonexistentTypeException\",\n" +
147 " \"code\": 999999999\n" +
153 clientTemplate.getForObject("ANY", SOME.class);
154 fail("The expected exception was not thrown");
156 catch(GraphApiException e)
158 LOG.debug("{}", e.toString());
159 assertEquals(new Integer(999999999), e.getCode());
160 assertEquals("This error does not exist.", e.getMessage());
163 Type type = e.getType();
164 LOG.error("unknown type: {}", type);
165 fail("unmapped type was resolved by enum: " + type);
167 catch (IllegalArgumentException ee) {}
172 public void testInvlalidError()
174 LOG.info("testInvalidError");
177 requestFactory.setBody(
181 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
182 " \"type\": \"Whatever\",\n" +
183 " \"code\": \"some string\"\n" +
189 clientTemplate.getForObject("ANY", SOME.class);
190 fail("The expected exception was not thrown");
192 catch(HttpClientErrorException e)
194 LOG.debug("{}", e.toString());
198 fail("A wrong exception was thrown: " + e.toString());
206 requestFactory = new MockClientHttpRequestFactory();
207 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
208 requestFactory.addHeader("Content-Type", "application/json");
210 clientTemplate = new RestTemplate();
211 clientTemplate.setRequestFactory(requestFactory);
212 clientTemplate.setErrorHandler(
213 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())