WIP: WebClient
authorKai Moritz <kai@jupl.de>
Fri, 22 Nov 2019 05:34:56 +0000 (06:34 +0100)
committerKai Moritz <kai@jupl.de>
Fri, 22 Nov 2019 05:34:56 +0000 (06:34 +0100)
src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java

index b9636ce..227c3e0 100644 (file)
@@ -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()
   {
@@ -89,42 +291,4 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
         })
         .verify(Duration.ofSeconds(3));
   }
-
-  @Test
-  public void testTest()
-  {
-    server
-        .enqueue(new MockResponse()
-            .setResponseCode(400)
-            .setHeader("Content-Type", "text/plain")
-            .setBody("Hello Spring!"));
-
-               Mono<String> result;
-
-    result = webClient.get()
-                               .uri("/greeting?name=Spring")
-                               .retrieve()
-                               .bodyToMono(String.class);
-
-               StepVerifier.create(result)
-                               .expectError(Exception.class)
-                               .verify(Duration.ofSeconds(3));
-
-    server
-        .enqueue(new MockResponse()
-            .setResponseCode(200)
-            .setHeader("Content-Type", "text/plain")
-            .setHeader("Foo", "Bar")
-            .setBody("Hello Spring!"));
-
-               result = webClient.get()
-                               .uri("/greeting?name=Spring")
-                               .retrieve()
-                               .bodyToMono(String.class);
-
-               StepVerifier.create(result)
-                               .expectNext("Hello Spring!")
-        .expectComplete()
-                               .verify(Duration.ofSeconds(3));
-  }
 }