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

index 9bff65d..6ab18b6 100644 (file)
@@ -27,10 +27,11 @@ public class GraphApiExchangeFilterFunction implements ExchangeFilterFunction
                   HttpStatus.Series.CLIENT_ERROR.equals(response.statusCode().series())
                       ? response
                           .bodyToMono(String.class)
-                          .flatMap(errorBody -> Mono.error(GraphApiException.create(
+                          .map(errorBody -> GraphApiException.create(
                               response.statusCode(),
                               response.headers().asHttpHeaders(),
-                              errorBody.getBytes())))
+                              errorBody.getBytes()))
+                          .flatMap(e -> Mono.error(e))
                       : Mono.just(response);
             });
   }
index 227c3e0..9faae0f 100644 (file)
@@ -2,11 +2,11 @@ package de.juplo.facebook.errors;
 
 
 import de.juplo.facebook.errors.GraphApiException.Type;
-import java.time.Duration;
 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;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.client.reactive.JettyClientHttpConnector;
 import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest;
+import org.springframework.web.reactive.function.client.WebClientResponseException.InternalServerError;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
@@ -54,22 +56,27 @@ 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);
-    //}
+    Mono<String> result;
+
+
+    server
+        .enqueue(new MockResponse()
+            .setResponseCode(HttpStatus.CONTINUE.value())
+            .setHeader("Content-Type", "application/json")
+            .setBody("Hallo Welt!"));
+    
+    result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier
+        .create(result)
+        .expectNext("Hallo Welt!")
+        .expectComplete()
+        .verify();
 
 
     server
@@ -78,16 +85,18 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .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);
-    }
+    result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier
+        .create(result)
+        .expectNext("Hallo Welt!")
+        .expectComplete()
+        .verify();
 
 
     server
@@ -96,16 +105,18 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .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);
-    }
+    result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier
+        .create(result)
+        .expectNext("Hallo Welt!")
+        .expectComplete()
+        .verify();
 
 
     server
@@ -114,20 +125,17 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .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);
-    }
+    result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier
+        .create(result)
+        .expectError(InternalServerError.class)
+        .verify();
   }
 
   @Test
@@ -147,18 +155,23 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
                 "  }\n" +
                 "}"));
 
-    try
-    {
-      clientTemplate.getForObject(uri, String.class);
-      fail("The expected exception was not thrown");
-    }
-    catch(RateLimitExceededException e)
+    Mono<String> result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier.create(result).expectErrorSatisfies(throwable ->
     {
+      assertEquals(RateLimitExceededException.class, throwable.getClass());
+      RateLimitExceededException e = (RateLimitExceededException)throwable;
       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());
-    }
+    })
+    .verify();
   }
 
   @Test
@@ -178,13 +191,18 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
                 "  }\n" +
                 "}"));
 
-    try
-    {
-      clientTemplate.getForObject(uri, String.class);
-      fail("The expected exception was not thrown");
-    }
-    catch(GraphApiException e)
+
+    Mono<String> result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier.create(result).expectErrorSatisfies(throwable ->
     {
+      assertEquals(UnmappedErrorException.class, throwable.getClass());
+      UnmappedErrorException e = (UnmappedErrorException)throwable;
       LOG.debug("{}", e.toString());
       assertEquals(new Integer(999999999), e.getCode());
       assertEquals("This error does not exist.", e.getMessage());
@@ -195,12 +213,16 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
         fail("unmapped type was resolved by enum: " + type);
       }
       catch (IllegalArgumentException ee) {}
-    }
+    })
+    .verify();
   }
 
   @Test
   public void testInvlalidError()
   {
+    Mono<String> result;
+
+
     server
         .enqueue(new MockResponse()
             .setResponseCode(HttpStatus.BAD_REQUEST.value())
@@ -215,20 +237,14 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
                 "  }\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);
-    }
+    result =
+        webClient
+            .get()
+            .uri("/egal")
+            .retrieve()
+            .bodyToMono(String.class);
+
+    StepVerifier.create(result).expectError(BadRequest.class).verify();
 
 
     server
@@ -237,58 +253,13 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .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()
-  {
-    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" +
-                "}"));
-
-
-    Mono<String> result =
+    result =
         webClient
             .get()
             .uri("/egal")
             .retrieve()
             .bodyToMono(String.class);
 
-    StepVerifier
-        .create(result)
-        .expectErrorSatisfies(throwable ->
-        {
-          assertEquals(RateLimitExceededException.class, throwable.getClass());
-          RateLimitExceededException e = (RateLimitExceededException)throwable;
-          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());
-        })
-        .verify(Duration.ofSeconds(3));
+    StepVerifier.create(result).expectError(BadRequest.class).verify();
   }
 }