The error-message is reanderd instead of the text, in case of a 404
authorKai Moritz <kai@juplo.de>
Tue, 14 Jan 2020 16:08:19 +0000 (17:08 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:03:10 +0000 (11:03 +0100)
* Added a unit-test, that defines the expected behavior
* Fixed the behavior in HtmlController accordingly

src/main/java/de/juplo/demo/HtmlController.java
src/test/java/de/juplo/demo/HtmlControllerTest.java

index 5696718..d26c8c5 100644 (file)
@@ -5,6 +5,7 @@ import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
+import reactor.core.publisher.Mono;
 
 
 /**
@@ -26,7 +27,11 @@ public class HtmlController
   @GetMapping("/")
   public String fetch(Model model, @RequestParam String path)
   {
-    model.addAttribute("text", service.getRemoteText(path));
+    model.addAttribute(
+        "text",
+        service
+            .getRemoteText(path)
+            .onErrorResume(t -> Mono.just(t.getMessage())));
     return "home";
   }
 }
index 1cf7274..aede077 100644 (file)
@@ -13,6 +13,7 @@ import static org.mockito.Mockito.when;
 import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.ui.Model;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
@@ -56,4 +57,23 @@ public class HtmlControllerTest
         .expectComplete()
         .verify();
   }
+
+  @Test
+  @DisplayName("Data not found on remote-server")
+  void testNotFoud()
+  {
+    Mono<String> mono = Mono.error(WebClientResponseException.create(404, "", null, null, null));
+    when(service.getRemoteText("foo")).thenReturn(mono);
+
+    String result = controller.fetch(model, "foo");
+
+    assertThat(result).isEqualTo("home");
+    ArgumentCaptor<Mono<String>> captor = ArgumentCaptor.forClass(Mono.class);
+    verify(model).addAttribute(eq("text"), captor.capture());
+    StepVerifier
+        .create(captor.getValue())
+        .expectNext("404 ")
+        .expectComplete()
+        .verify();
+  }
 }