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;
}
- @GetMapping({ "", "/" })
+ @GetMapping(path = { "", "/" }, produces = MediaType.TEXT_HTML_VALUE)
public String fetch(Model model, @RequestParam(required = false) String path)
{
model.addAttribute("path", path);
--- /dev/null
+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);
+ }
+}
--- /dev/null
+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();
+ }
+}