From: Kai Moritz Date: Mon, 13 Jan 2020 11:43:27 +0000 (+0100) Subject: Added a rest-controller, that serves the fetched data as text/plain X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=1d6b6b053ce2ef80739039c8abfa21bd7212a55e;p=demos%2Ftesting Added a rest-controller, that serves the fetched data as text/plain --- diff --git a/src/main/java/de/juplo/demo/HtmlController.java b/src/main/java/de/juplo/demo/HtmlController.java index 92ed7ff..d048ffe 100644 --- a/src/main/java/de/juplo/demo/HtmlController.java +++ b/src/main/java/de/juplo/demo/HtmlController.java @@ -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 index 0000000..b32f796 --- /dev/null +++ b/src/main/java/de/juplo/demo/RestController.java @@ -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 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 index 0000000..8b248ce --- /dev/null +++ b/src/test/java/de/juplo/demo/RestControllerTest.java @@ -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 result = controller.fetch("foo"); + + StepVerifier + .create(result) + .expectNext("bar") + .expectComplete() + .verify(); + } +}