WIP: Clearified intend...
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiErrorResponseErrorHandlerIntegrationTest.java
index 6593375..9f70b35 100644 (file)
@@ -2,6 +2,9 @@ package de.juplo.facebook.errors;
 
 
 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;
@@ -10,6 +13,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpServerErrorException;
 import org.springframework.web.client.RestTemplate;
 
 
@@ -23,28 +27,58 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   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()
   {
-    LOG.info("testNoError");
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.CONTINUE.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody("{ \"message\": \"Hello World!\" }"));
+
+    try
+    {
+      SimpleMessage result = clientTemplate.getForObject(uri, SimpleMessage.class);
+      assertEquals("Hello World!", result.message);
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
 
-    requestFactory.setBody("{\n" +
-        "  \"error\": {\n" +
-        "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
-        "    \"type\": \"OAuthException\",\n" +
-        "    \"code\": 200\n" +
-        "  }\n" +
-        "}");
 
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.OK.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody("{ \"message\": \"Hello World!\" }"));
 
-    requestFactory.setStatus(HttpStatus.CONTINUE);
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      SimpleMessage result = clientTemplate.getForObject(uri, SimpleMessage.class);
+      assertEquals("Hello World!", result.message);
     }
     catch(Exception e)
     {
@@ -52,10 +86,17 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
       fail("Unexpected error: " + e);
     }
 
-    requestFactory.setStatus(HttpStatus.OK);
+
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.TEMPORARY_REDIRECT.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody("{ \"message\": \"Hello World!\" }"));
+
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      SimpleMessage result = clientTemplate.getForObject(uri, SimpleMessage.class);
+      assertEquals("Hello World!", result.message);
     }
     catch(Exception e)
     {
@@ -63,10 +104,21 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
       fail("Unexpected error: " + e);
     }
 
-    requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
+
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody("{ \"message\": \"Hello World!\" }"));
+
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      clientTemplate.getForObject(uri, SimpleMessage.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpClientErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
     }
     catch(Exception e)
     {
@@ -74,10 +126,21 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
       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", SOME.class);
+      clientTemplate.getForObject(uri, SimpleMessage.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpServerErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
     }
     catch(Exception e)
     {
@@ -89,22 +152,23 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @Test
   public void testValidError()
   {
-    LOG.info("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", SOME.class);
+      clientTemplate.getForObject(uri, SimpleMessage.class);
       fail("The expected exception was not thrown");
     }
     catch(RateLimitExceededException e)
@@ -119,10 +183,11 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @Test
   public void testUnmappedError()
   {
-    LOG.info("testUnmappedError");
-
-
-    requestFactory.setBody(
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody(
         "{\n" +
         "  \"error\":\n" +
         "  {\n" +
@@ -130,11 +195,11 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
         "    \"type\": \"NonexistentTypeException\",\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
-        "}");
+        "}"));
 
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      clientTemplate.getForObject(uri, SimpleMessage.class);
       fail("The expected exception was not thrown");
     }
     catch(GraphApiException e)
@@ -155,10 +220,11 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @Test
   public void testInvlalidError()
   {
-    LOG.info("testInvalidError");
-
-
-    requestFactory.setBody(
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody(
         "{\n" +
         "  \"error\":\n" +
         "  {\n" +
@@ -166,11 +232,11 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
         "    \"type\": \"Whatever\",\n" +
         "    \"code\": \"some string\"\n" +
         "  }\n" +
-        "}");
+        "}"));
 
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      clientTemplate.getForObject(uri, SimpleMessage.class);
       fail("The expected exception was not thrown");
     }
     catch(HttpClientErrorException e)
@@ -184,22 +250,8 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   }
 
 
-  @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())
-        );
-  }
-
-
-  static class SOME
+  static class SimpleMessage
   {
+    String message;
   }
 }