import de.juplo.facebook.errors.GraphApiException.Type;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import org.junit.After;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Before;
private static final Logger LOG =
LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class);
+ private MockWebServer server;
+ private String uri;
private RestTemplate clientTemplate;
- private MockClientHttpRequestFactory requestFactory;
+
+
+ @Before
+ public void setup()
+ {
+ server = new MockWebServer();
+ uri = server.url("/egal").toString();
+ clientTemplate = new RestTemplate();
+ clientTemplate.setErrorHandler(new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler()));
+ }
+
+ @After
+ public void shutdown() throws Exception
+ {
+ this.server.shutdown();
+ }
@Test
public void testNoError()
{
- requestFactory.setBody("{ \"message\": \"Hello World!\" }");
+// server
+// .enqueue(new MockResponse()
+// .setResponseCode(HttpStatus.CONTINUE.value())
+// .setHeader("Content-Type", "application/json")
+// .setBody("{ \"message\": \"Hello World!\" }"));
+//
+// try
+// {
+// String result = clientTemplate.getForObject(uri, String.class);
+// assertEquals("{ \"message\": \"Hello World!\" }", result);
+// }
+// catch(Exception e)
+// {
+// LOG.debug("{}", e.toString());
+// fail("Unexpected error: " + e);
+// }
+
+
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.OK.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody("{ \"message\": \"Hello World!\" }"));
-
- requestFactory.setStatus(HttpStatus.CONTINUE);
try
{
- String result = clientTemplate.getForObject("ANY", String.class);
+ String result = clientTemplate.getForObject(uri, String.class);
assertEquals("{ \"message\": \"Hello World!\" }", result);
}
catch(Exception e)
fail("Unexpected error: " + e);
}
- requestFactory.setStatus(HttpStatus.OK);
- try
- {
- String result = clientTemplate.getForObject("ANY", String.class);
- assertEquals("{ \"message\": \"Hello World!\" }", result);
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
- requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.TEMPORARY_REDIRECT.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody("{ \"message\": \"Hello World!\" }"));
+
try
{
- String result = clientTemplate.getForObject("ANY", String.class);
+ String result = clientTemplate.getForObject(uri, String.class);
assertEquals("{ \"message\": \"Hello World!\" }", result);
}
catch(Exception e)
fail("Unexpected error: " + e);
}
- requestFactory.setStatus(HttpStatus.BAD_REQUEST);
+
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.BAD_REQUEST.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody("{ \"message\": \"Hello World!\" }"));
+
try
{
- clientTemplate.getForObject("ANY", String.class);
+ clientTemplate.getForObject(uri, String.class);
fail("The parent handler should have raised an exception!");
}
catch(HttpClientErrorException e)
fail("Unexpected error: " + e);
}
- requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
+
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody("{ \"message\": \"Hello World!\" }"));
+
try
{
- clientTemplate.getForObject("ANY", String.class);
+ clientTemplate.getForObject(uri, String.class);
fail("The parent handler should have raised an exception!");
}
catch(HttpServerErrorException e)
@Test
public void 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" +
- "}");
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.BAD_REQUEST.value())
+ .setHeader("Content-Type", "application/json")
+ .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", String.class);
+ clientTemplate.getForObject(uri, String.class);
fail("The expected exception was not thrown");
}
catch(RateLimitExceededException e)
@Test
public void testUnmappedError()
{
- requestFactory.setBody(
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.BAD_REQUEST.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody(
"{\n" +
" \"error\":\n" +
" {\n" +
" \"type\": \"NonexistentTypeException\",\n" +
" \"code\": 999999999\n" +
" }\n" +
- "}");
+ "}"));
try
{
- clientTemplate.getForObject("ANY", String.class);
+ clientTemplate.getForObject(uri, String.class);
fail("The expected exception was not thrown");
}
catch(GraphApiException e)
@Test
public void testInvlalidError()
{
- requestFactory.setBody(
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.BAD_REQUEST.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody(
"{\n" +
" \"error\":\n" +
" {\n" +
" \"type\": \"Whatever\",\n" +
" \"code\": \"some string\"\n" +
" }\n" +
- "}");
+ "}"));
try
{
- clientTemplate.getForObject("ANY", String.class);
+ clientTemplate.getForObject(uri, String.class);
fail("The expected exception was not thrown");
}
catch(HttpClientErrorException 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())
- );
- }
}