Added a rest-controller, that serves the fetched data
authorKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 11:43:27 +0000 (12:43 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 13 Jan 2020 18:51:06 +0000 (19:51 +0100)
src/main/java/de/juplo/demo/RemoteContentController.java [new file with mode: 0644]
src/test/java/de/juplo/demo/RemoteContentControllerTest.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/demo/RemoteContentController.java b/src/main/java/de/juplo/demo/RemoteContentController.java
new file mode 100644 (file)
index 0000000..422f5a6
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.demo;
+
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Fetches and returns data from remote-webserver.
+ * @author Kai Moritz
+ */
+@RestController
+public class RemoteContentController
+{
+  RemoteContentService service;
+
+
+  public RemoteContentController(RemoteContentService service)
+  {
+    this.service = service;
+  }
+
+
+  @GetMapping("/")
+  public Mono<String> fetch(@RequestParam String path)
+  {
+    return service.getRemoteText(path);
+  }
+}
diff --git a/src/test/java/de/juplo/demo/RemoteContentControllerTest.java b/src/test/java/de/juplo/demo/RemoteContentControllerTest.java
new file mode 100644 (file)
index 0000000..fdba099
--- /dev/null
@@ -0,0 +1,44 @@
+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.when;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Unit-Test for class {@link RemoteContentController}.
+ * @author Kai Moritz
+ */
+@ExtendWith(SpringExtension.class)
+public class RemoteContentControllerTest
+{
+  RemoteContentController controller;
+  RemoteContentService service;
+
+
+  @BeforeEach
+  void setUp()
+  {
+    service = Mockito.mock(RemoteContentService.class);
+    controller = new RemoteContentController(service);
+  }
+
+
+  @Test
+  void test()
+  {
+    Mono<String> mono = Mono.empty();
+    when(service.getRemoteText("foo")).thenReturn(mono);
+
+    // when
+    Mono<String> result = controller.fetch("foo");
+
+    assertThat(result).isSameAs(mono);
+  }
+}