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.RestTemplate;
21 public class GraphApiErrorResponseErrorHandlerIntegrationTest
23 private static final Logger LOG =
24 LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class);
26 private RestTemplate clientTemplate;
27 private MockClientHttpRequestFactory requestFactory;
31 public void testNoError()
33 LOG.info("testNoError");
35 requestFactory.setBody("{\n" +
37 " \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
38 " \"type\": \"OAuthException\",\n" +
44 requestFactory.setStatus(HttpStatus.CONTINUE);
47 clientTemplate.getForObject("ANY", SOME.class);
51 LOG.debug("{}", e.toString());
52 fail("Unexpected error: " + e);
55 requestFactory.setStatus(HttpStatus.OK);
58 clientTemplate.getForObject("ANY", SOME.class);
62 LOG.debug("{}", e.toString());
63 fail("Unexpected error: " + e);
66 requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
69 clientTemplate.getForObject("ANY", SOME.class);
73 LOG.debug("{}", e.toString());
74 fail("Unexpected error: " + e);
77 requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
80 clientTemplate.getForObject("ANY", SOME.class);
84 LOG.debug("{}", e.toString());
85 fail("Unexpected error: " + e);
90 public void testValidError()
92 LOG.info("testValidError");
95 requestFactory.setBody(
99 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
100 " \"type\": \"OAuthException\",\n" +
107 clientTemplate.getForObject("ANY", SOME.class);
108 fail("The expected exception was not thrown");
110 catch(RateLimitExceededException e)
112 LOG.debug("{}", e.toString());
113 assertEquals(new Integer(613), e.getCode());
114 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
115 assertEquals(Type.OAuthException, e.getType());
120 public void testUnmappedError()
122 LOG.info("testUnmappedError");
125 requestFactory.setBody(
129 " \"message\": \"This error does not exist.\",\n" +
130 " \"type\": \"NonexistentTypeException\",\n" +
131 " \"code\": 999999999\n" +
137 clientTemplate.getForObject("ANY", SOME.class);
138 fail("The expected exception was not thrown");
140 catch(GraphApiException e)
142 LOG.debug("{}", e.toString());
143 assertEquals(new Integer(999999999), e.getCode());
144 assertEquals("This error does not exist.", e.getMessage());
147 Type type = e.getType();
148 LOG.error("unknown type: {}", type);
149 fail("unmapped type was resolved by enum: " + type);
151 catch (IllegalArgumentException ee) {}
156 public void testInvlalidError()
158 LOG.info("testInvalidError");
161 requestFactory.setBody(
165 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
166 " \"type\": \"Whatever\",\n" +
167 " \"code\": \"some string\"\n" +
173 clientTemplate.getForObject("ANY", SOME.class);
174 fail("The expected exception was not thrown");
176 catch(HttpClientErrorException e)
178 LOG.debug("{}", e.toString());
182 fail("A wrong exception was thrown: " + e.toString());
190 requestFactory = new MockClientHttpRequestFactory();
191 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
192 requestFactory.addHeader("Content-Type", "application/json");
194 clientTemplate = new RestTemplate();
195 clientTemplate.setRequestFactory(requestFactory);
196 clientTemplate.setErrorHandler(
197 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())