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 requestFactory.setBody("{ \"message\": \"Hello World!\" }");
37 requestFactory.setStatus(HttpStatus.CONTINUE);
40 clientTemplate.getForObject("ANY", SOME.class);
44 LOG.debug("{}", e.toString());
45 fail("Unexpected error: " + e);
48 requestFactory.setStatus(HttpStatus.OK);
51 clientTemplate.getForObject("ANY", SOME.class);
55 LOG.debug("{}", e.toString());
56 fail("Unexpected error: " + e);
59 requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
62 clientTemplate.getForObject("ANY", SOME.class);
66 LOG.debug("{}", e.toString());
67 fail("Unexpected error: " + e);
70 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
73 clientTemplate.getForObject("ANY", SOME.class);
74 fail("The parent handler should have raised an exception!");
76 catch(HttpClientErrorException e)
78 LOG.debug("Expexted error: {}", e.toString());
82 LOG.debug("{}", e.toString());
83 fail("Unexpected error: " + e);
86 requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
89 clientTemplate.getForObject("ANY", SOME.class);
90 fail("The parent handler should have raised an exception!");
92 catch(HttpServerErrorException e)
94 LOG.debug("Expexted error: {}", e.toString());
98 LOG.debug("{}", e.toString());
99 fail("Unexpected error: " + e);
104 public void testValidError()
106 requestFactory.setBody(
110 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
111 " \"type\": \"OAuthException\",\n" +
118 clientTemplate.getForObject("ANY", SOME.class);
119 fail("The expected exception was not thrown");
121 catch(RateLimitExceededException e)
123 LOG.debug("{}", e.toString());
124 assertEquals(new Integer(613), e.getCode());
125 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
126 assertEquals(Type.OAuthException, e.getType());
131 public void testUnmappedError()
133 requestFactory.setBody(
137 " \"message\": \"This error does not exist.\",\n" +
138 " \"type\": \"NonexistentTypeException\",\n" +
139 " \"code\": 999999999\n" +
145 clientTemplate.getForObject("ANY", SOME.class);
146 fail("The expected exception was not thrown");
148 catch(GraphApiException e)
150 LOG.debug("{}", e.toString());
151 assertEquals(new Integer(999999999), e.getCode());
152 assertEquals("This error does not exist.", e.getMessage());
155 Type type = e.getType();
156 LOG.error("unknown type: {}", type);
157 fail("unmapped type was resolved by enum: " + type);
159 catch (IllegalArgumentException ee) {}
164 public void testInvlalidError()
166 requestFactory.setBody(
170 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
171 " \"type\": \"Whatever\",\n" +
172 " \"code\": \"some string\"\n" +
178 clientTemplate.getForObject("ANY", SOME.class);
179 fail("The expected exception was not thrown");
181 catch(HttpClientErrorException e)
183 LOG.debug("{}", e.toString());
187 fail("A wrong exception was thrown: " + e.toString());
195 requestFactory = new MockClientHttpRequestFactory();
196 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
197 requestFactory.addHeader("Content-Type", "application/json");
199 clientTemplate = new RestTemplate();
200 clientTemplate.setRequestFactory(requestFactory);
201 clientTemplate.setErrorHandler(
202 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())