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 String result = clientTemplate.getForObject("ANY", String.class);
41 assertEquals("{ \"message\": \"Hello World!\" }", result);
45 LOG.debug("{}", e.toString());
46 fail("Unexpected error: " + e);
49 requestFactory.setStatus(HttpStatus.OK);
52 String result = clientTemplate.getForObject("ANY", String.class);
53 assertEquals("{ \"message\": \"Hello World!\" }", result);
57 LOG.debug("{}", e.toString());
58 fail("Unexpected error: " + e);
61 requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
64 String result = clientTemplate.getForObject("ANY", String.class);
65 assertEquals("{ \"message\": \"Hello World!\" }", result);
69 LOG.debug("{}", e.toString());
70 fail("Unexpected error: " + e);
73 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
76 clientTemplate.getForObject("ANY", String.class);
77 fail("The parent handler should have raised an exception!");
79 catch(HttpClientErrorException e)
81 LOG.debug("Expexted error: {}", e.toString());
85 LOG.debug("{}", e.toString());
86 fail("Unexpected error: " + e);
89 requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
92 clientTemplate.getForObject("ANY", String.class);
93 fail("The parent handler should have raised an exception!");
95 catch(HttpServerErrorException e)
97 LOG.debug("Expexted error: {}", e.toString());
101 LOG.debug("{}", e.toString());
102 fail("Unexpected error: " + e);
107 public void testValidError()
109 requestFactory.setBody(
113 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
114 " \"type\": \"OAuthException\",\n" +
121 clientTemplate.getForObject("ANY", String.class);
122 fail("The expected exception was not thrown");
124 catch(RateLimitExceededException e)
126 LOG.debug("{}", e.toString());
127 assertEquals(new Integer(613), e.getCode());
128 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
129 assertEquals(Type.OAuthException, e.getType());
134 public void testUnmappedError()
136 requestFactory.setBody(
140 " \"message\": \"This error does not exist.\",\n" +
141 " \"type\": \"NonexistentTypeException\",\n" +
142 " \"code\": 999999999\n" +
148 clientTemplate.getForObject("ANY", String.class);
149 fail("The expected exception was not thrown");
151 catch(GraphApiException e)
153 LOG.debug("{}", e.toString());
154 assertEquals(new Integer(999999999), e.getCode());
155 assertEquals("This error does not exist.", e.getMessage());
158 Type type = e.getType();
159 LOG.error("unknown type: {}", type);
160 fail("unmapped type was resolved by enum: " + type);
162 catch (IllegalArgumentException ee) {}
167 public void testInvlalidError()
169 requestFactory.setBody(
173 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
174 " \"type\": \"Whatever\",\n" +
175 " \"code\": \"some string\"\n" +
181 clientTemplate.getForObject("ANY", String.class);
182 fail("The expected exception was not thrown");
184 catch(HttpClientErrorException e)
186 LOG.debug("{}", e.toString());
190 fail("A wrong exception was thrown: " + e.toString());
198 requestFactory = new MockClientHttpRequestFactory();
199 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
200 requestFactory.addHeader("Content-Type", "application/json");
202 clientTemplate = new RestTemplate();
203 clientTemplate.setRequestFactory(requestFactory);
204 clientTemplate.setErrorHandler(
205 new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())