Clearified intend in GraphApiErrorResponseErrorHandlerIntegrationTest
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 20:21:13 +0000 (21:21 +0100)
committerKai Moritz <kai@jupl.de>
Fri, 22 Nov 2019 06:43:03 +0000 (07:43 +0100)
pom.xml
src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java

diff --git a/pom.xml b/pom.xml
index c223f2a..b1fc524 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>spring-boot-configuration-processor</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.squareup.okhttp3</groupId>
+      <artifactId>mockwebserver</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
index 254b51b..752a13f 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;
@@ -24,20 +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()
   {
-    requestFactory.setBody("{ \"message\": \"Hello World!\" }");
-
+    //server
+    //    .enqueue(new MockResponse()
+    //        .setResponseCode(HttpStatus.CONTINUE.value())
+    //        .setHeader("Content-Type", "application/json")
+    //        .setBody("Hallo Welt!"));
+    //
+    //try
+    //{
+    //  String result = clientTemplate.getForObject(uri, String.class);
+    //  assertEquals("Hallo Welt!", result);
+    //}
+    //catch(Exception e)
+    //{
+    //  LOG.debug("{}", e.toString());
+    //  fail("Unexpected error: " + e);
+    //}
+
+
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.OK.value())
+            .setHeader("Content-Type", "text/plain")
+            .setBody("Hallo Welt!"));
 
-    requestFactory.setStatus(HttpStatus.CONTINUE);
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      String result = clientTemplate.getForObject(uri, String.class);
+      assertEquals("Hallo Welt!", result);
     }
     catch(Exception e)
     {
@@ -45,37 +86,17 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
       fail("Unexpected error: " + e);
     }
 
-    requestFactory.setStatus(HttpStatus.OK);
-    try
-    {
-      clientTemplate.getForObject("ANY", SOME.class);
-    }
-    catch(Exception e)
-    {
-      LOG.debug("{}", e.toString());
-      fail("Unexpected error: " + e);
-    }
 
-    requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
-    try
-    {
-      clientTemplate.getForObject("ANY", SOME.class);
-    }
-    catch(Exception e)
-    {
-      LOG.debug("{}", e.toString());
-      fail("Unexpected error: " + e);
-    }
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.TEMPORARY_REDIRECT.value())
+            .setHeader("Content-Type", "text/plain")
+            .setBody("Hallo Welt!"));
 
-    requestFactory.setStatus(HttpStatus.BAD_REQUEST);
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
-      fail("The parent handler should have raised an exception!");
-    }
-    catch(HttpClientErrorException e)
-    {
-      LOG.debug("Expexted error: {}", e.toString());
+      String result = clientTemplate.getForObject(uri, String.class);
+      assertEquals("Hallo Welt!", result);
     }
     catch(Exception e)
     {
@@ -83,10 +104,16 @@ 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", "text/plain")
+            .setBody("Hallo Welt!"));
+
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      clientTemplate.getForObject(uri, String.class);
       fail("The parent handler should have raised an exception!");
     }
     catch(HttpServerErrorException e)
@@ -103,19 +130,23 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @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", SOME.class);
+      clientTemplate.getForObject(uri, String.class);
       fail("The expected exception was not thrown");
     }
     catch(RateLimitExceededException e)
@@ -130,19 +161,23 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @Test
   public void testUnmappedError()
   {
-    requestFactory.setBody(
-        "{\n" +
-        "  \"error\":\n" +
-        "  {\n" +
-        "    \"message\": \"This error does not exist.\",\n" +
-        "    \"type\": \"NonexistentTypeException\",\n" +
-        "    \"code\": 999999999\n" +
-        "  }\n" +
-        "}");
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody(
+                "{\n" +
+                "  \"error\":\n" +
+                "  {\n" +
+                "    \"message\": \"This error does not exist.\",\n" +
+                "    \"type\": \"NonexistentTypeException\",\n" +
+                "    \"code\": 999999999\n" +
+                "  }\n" +
+                "}"));
 
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
+      clientTemplate.getForObject(uri, String.class);
       fail("The expected exception was not thrown");
     }
     catch(GraphApiException e)
@@ -163,48 +198,55 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   @Test
   public void testInvlalidError()
   {
-    requestFactory.setBody(
-        "{\n" +
-        "  \"error\":\n" +
-        "  {\n" +
-        "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
-        "    \"type\": \"Whatever\",\n" +
-        "    \"code\": \"some string\"\n" +
-        "  }\n" +
-        "}");
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody(
+                "{\n" +
+                "  \"error\":\n" +
+                "  {\n" +
+                "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
+                "    \"type\": \"Whatever\",\n" +
+                "    \"code\": \"some string\"\n" +
+                "  }\n" +
+                "}"));
 
     try
     {
-      clientTemplate.getForObject("ANY", SOME.class);
-      fail("The expected exception was not thrown");
+      clientTemplate.getForObject(uri, String.class);
+      fail("The parent handler should have raised an exception!");
     }
     catch(HttpClientErrorException e)
     {
-      LOG.debug("{}", e.toString());
+      LOG.debug("Expexted error: {}", e.toString());
     }
     catch(Exception e)
     {
-      fail("A wrong exception was thrown: " + e.toString());
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
     }
-  }
 
 
-  @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())
-        );
-  }
-
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "text/plain")
+            .setBody("Hallo Welt!"));
 
-  static class SOME
-  {
+    try
+    {
+      clientTemplate.getForObject(uri, String.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpClientErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
   }
 }