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-Tag: wip-it~24 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=03f61935d1887f7f90cf46416a8b583bece2fa43;p=demos%2Ftesting Added a html-controller, that renders the fetched data --- 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..03c8709 --- /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 and 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..c4e636f --- /dev/null +++ b/src/main/resources/templates/layout.html @@ -0,0 +1,22 @@ + + + + + + Shows Remote-Content + + + + +
+
+

Rendered Remote-Content

+
+

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..9c4fc0d --- /dev/null +++ b/src/test/java/de/juplo/demo/HtmlControllerTest.java @@ -0,0 +1,52 @@ +package de.juplo.demo; + + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +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; + + +/** + * 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() + { + service = Mockito.mock(RemoteContentService.class); + controller = new HtmlController(service); + } + + + @Test + void test() + { + Mono mono = Mono.empty(); + when(service.getRemoteText("foo")).thenReturn(mono); + + // when + String result = controller.fetch(model, "foo"); + + assertThat(result).isEqualTo("layout"); + verify(model).addAttribute("text", mono); + } +}