From: Kai Moritz Date: Mon, 13 Jan 2020 18:25:37 +0000 (+0100) Subject: Added a html-controller, that renders the fetched data X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=a59fd998f3909a11dcf0d5fe000174496628cb99;p=demos%2Ftesting Added a html-controller, that renders the fetched data --- diff --git a/pom.xml b/pom.xml index d44e1ef..8da5bd0 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,11 @@ + + io.projectreactor + reactor-test + test + org.springframework.boot diff --git a/src/main/java/de/juplo/demo/HtmlController.java b/src/main/java/de/juplo/demo/HtmlController.java new file mode 100644 index 0000000..eaa2fcc --- /dev/null +++ b/src/main/java/de/juplo/demo/HtmlController.java @@ -0,0 +1,32 @@ +package de.juplo.demo; + + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + + +/** + * Fetches data from a remote-webserver and renders it as HTML. + * @author Kai Moritz + */ +@Controller +public class HtmlController +{ + RemoteContentService service; + + + public HtmlController(RemoteContentService service) + { + this.service = service; + } + + + @GetMapping("/") + public String fetch(Model model, @RequestParam String path) + { + model.addAttribute("text", service.getRemoteText(path)); + return "layout"; + } +} diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html new file mode 100644 index 0000000..bc5799a --- /dev/null +++ b/src/main/resources/templates/layout.html @@ -0,0 +1,22 @@ + + + + + + Shows Remote-Content + + + + +
+
+

Information About Fetched Data

+
+

TEXT

+
+
+
+ + diff --git a/src/test/java/de/juplo/demo/HtmlControllerTest.java b/src/test/java/de/juplo/demo/HtmlControllerTest.java new file mode 100644 index 0000000..ceb14e4 --- /dev/null +++ b/src/test/java/de/juplo/demo/HtmlControllerTest.java @@ -0,0 +1,59 @@ +package de.juplo.demo; + + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +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 reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + + +/** + * Unit-Test for class {@link RemoteContentHtmlController}. + * @author Kai Moritz + */ +@ExtendWith(SpringExtension.class) +public class HtmlControllerTest +{ + HtmlController controller; + + @MockBean + RemoteContentService service; + @MockBean + Model model; + + + @BeforeEach + void setUp() + { + controller = new HtmlController(service); + } + + + @Test + @DisplayName("Data successfully fetched from remote-server") + void testOK() + { + when(service.getRemoteText("foo")).thenReturn(Mono.just("bar")); + + String result = controller.fetch(model, "foo"); + + assertThat(result).isEqualTo("layout"); + ArgumentCaptor> captor = ArgumentCaptor.forClass(Mono.class); + verify(model).addAttribute(eq("text"), captor.capture()); + StepVerifier + .create(captor.getValue()) + .expectNext("bar") + .expectComplete() + .verify(); + } +}