WIP: WebClient
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiExchangeFilterFunctionIntegrationTest.java
index 2e3ae22..227c3e0 100644 (file)
@@ -40,7 +40,7 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .builder()
             .clientConnector(new JettyClientHttpConnector())
             .baseUrl(server.url("/").toString())
-            .filter(GraphApiExchangeFilterFunction.INSTANCE)
+            .filter(new GraphApiExchangeFilterFunction())
             .build();
        }
 
@@ -51,6 +51,208 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
        }
 
 
+  @Test
+  public void testNoError()
+  {
+    //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!"));
+
+    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.TEMPORARY_REDIRECT.value())
+            .setHeader("Content-Type", "text/plain")
+            .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.INTERNAL_SERVER_ERROR.value())
+            .setHeader("Content-Type", "text/plain")
+            .setBody("Hallo Welt!"));
+
+    try
+    {
+      clientTemplate.getForObject(uri, String.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpServerErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+  }
+
+  @Test
+  public void testValidError()
+  {
+    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(uri, String.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(RateLimitExceededException e)
+    {
+      LOG.debug("{}", e.toString());
+      assertEquals(new Integer(613), e.getCode());
+      assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
+      assertEquals(Type.OAuthException, e.getType());
+    }
+  }
+
+  @Test
+  public void testUnmappedError()
+  {
+    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(uri, String.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(GraphApiException e)
+    {
+      LOG.debug("{}", e.toString());
+      assertEquals(new Integer(999999999), e.getCode());
+      assertEquals("This error does not exist.", e.getMessage());
+      try
+      {
+        Type type = e.getType();
+        LOG.error("unknown type: {}", type);
+        fail("unmapped type was resolved by enum: " + type);
+      }
+      catch (IllegalArgumentException ee) {}
+    }
+  }
+
+  @Test
+  public void testInvlalidError()
+  {
+    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(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);
+    }
+
+
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.BAD_REQUEST.value())
+            .setHeader("Content-Type", "text/plain")
+            .setBody("Hallo Welt!"));
+
+    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);
+    }
+  }
+
   @Test
   public void testValidError()
   {
@@ -87,6 +289,6 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
           assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
           assertEquals(Type.OAuthException, e.getType());
         })
-        .verifyLater();
+        .verify(Duration.ofSeconds(3));
   }
 }