Added a rest-controller, that serves the fetched data as text/plain
authorKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 11:43:27 +0000 (12:43 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:18:03 +0000 (11:18 +0100)
src/main/java/de/juplo/demo/HtmlController.java
src/main/java/de/juplo/demo/RestController.java [new file with mode: 0644]
src/test/java/de/juplo/demo/RestControllerTest.java [new file with mode: 0644]

index 92ed7ff..d048ffe 100644 (file)
@@ -1,6 +1,7 @@
 package de.juplo.demo;
 
 
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -24,7 +25,7 @@ public class HtmlController
   }
 
 
-  @GetMapping({ "", "/" })
+  @GetMapping(path = { "", "/" }, produces = MediaType.TEXT_HTML_VALUE)
   public String fetch(Model model, @RequestParam(required = false) String path)
   {
     model.addAttribute("path", path);
diff --git a/src/main/java/de/juplo/demo/RestController.java b/src/main/java/de/juplo/demo/RestController.java
new file mode 100644 (file)
index 0000000..b32f796
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.demo;
+
+
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Fetches and returns data from a remote-webserver.
+ * @author Kai Moritz
+ */
+@org.springframework.web.bind.annotation.RestController
+public class RestController
+{
+  RemoteContentService service;
+
+
+  public RestController(RemoteContentService service)
+  {
+    this.service = service;
+  }
+
+
+  @GetMapping(path = "/", produces = MediaType.TEXT_PLAIN_VALUE)
+  public Mono<String> fetch(@RequestParam String path)
+  {
+    return service.getRemoteText(path);
+  }
+}
diff --git a/src/test/java/de/juplo/demo/RestControllerTest.java b/src/test/java/de/juplo/demo/RestControllerTest.java
new file mode 100644 (file)
index 0000000..8b248ce
--- /dev/null
@@ -0,0 +1,45 @@
+package de.juplo.demo;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+import static org.mockito.Mockito.when;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+
+/**
+ * Unit-Test for class {@link RestController}.
+ * @author Kai Moritz
+ */
+public class RestControllerTest
+{
+  RestController controller;
+  RemoteContentService service;
+
+
+  @BeforeEach
+  void setUp()
+  {
+    service = Mockito.mock(RemoteContentService.class);
+    controller = new RestController(service);
+  }
+
+
+  @Test
+  @DisplayName("Data successfully fetched from remote-server")
+  void testResponseOK()
+  {
+    when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
+
+    Mono<String> result = controller.fetch("foo");
+
+    StepVerifier
+        .create(result)
+        .expectNext("bar")
+        .expectComplete()
+        .verify();
+  }
+}