Added a html-controller, that renders the fetched data
authorKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 18:25:37 +0000 (19:25 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 18:41:03 +0000 (19:41 +0100)
src/main/java/de/juplo/demo/HtmlController.java [new file with mode: 0644]
src/main/resources/templates/layout.html [new file with mode: 0644]
src/test/java/de/juplo/demo/HtmlControllerTest.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/demo/HtmlController.java b/src/main/java/de/juplo/demo/HtmlController.java
new file mode 100644 (file)
index 0000000..03c8709
--- /dev/null
@@ -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 (file)
index 0000000..c4e636f
--- /dev/null
@@ -0,0 +1,22 @@
+<!doctype html>
+<html xmlns:th="http://www.thymeleaf.org" lang="en">
+  <head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
+    <title>Shows Remote-Content</title>
+    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
+  </head>
+  <body>
+    <nav class="navbar navbar-dark bg-primary navbar-expand navbar-dark flex-column">
+      <h2 class="navbar-brand">Demo: Unit-Test or Integration-Test &mdash; A Practical Guide</h2>
+    </nav>
+    <main class="container mt-5">
+      <div class="card">
+        <div class="card-header" id="content"><h1>Rendered Remote-Content</h1></div>
+        <div class="card-body">
+          <p class="card-text"><pre th:text="${text}">TEXT</pre></p>
+        </div>
+      </div>
+    </main>
+  </body>
+</html>
diff --git a/src/test/java/de/juplo/demo/HtmlControllerTest.java b/src/test/java/de/juplo/demo/HtmlControllerTest.java
new file mode 100644 (file)
index 0000000..9c4fc0d
--- /dev/null
@@ -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<String> 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);
+  }
+}