X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FDemoApplicationIT.java;fp=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FDemoApplicationIT.java;h=15d3382a45ab0e33f2fc486a512eb0f747d3ea28;hb=07ca86e8fe8bd302acc1a837f9ee2a62991ab6e7;hp=0000000000000000000000000000000000000000;hpb=ddda08de0026c84ed095dc76955f874fa7ef2dc6;p=demos%2Ftesting diff --git a/src/test/java/de/juplo/demo/DemoApplicationIT.java b/src/test/java/de/juplo/demo/DemoApplicationIT.java new file mode 100644 index 0000000..15d3382 --- /dev/null +++ b/src/test/java/de/juplo/demo/DemoApplicationIT.java @@ -0,0 +1,158 @@ +package de.juplo.demo; + +import static de.juplo.demo.IntegrationTestBase.MOCK_SERVER; +import java.util.regex.Pattern; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpForward.forward; +import static org.mockserver.model.HttpRequest.request; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.client.WebClient; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@EnableAutoConfiguration +class DemoApplicationIT extends IntegrationTestBase +{ + static Pattern MISSING_PATTERN = + Pattern.compile("^404\\ Not\\ Found\\ from\\ GET\\ http:\\/\\/localhost:[0-9]*/missing.txt$"); + + + @Autowired + WebTestClient webClient; + + + @Test + @DisplayName("HtmlController - Remote-Response: 200") + void testHtmlControllerResponse200() throws Exception + { + MOCK_SERVER + .when(request().withPath("/test.txt"), exactly(1)) + .forward(forward() + .withHost(NGINX.getContainerIpAddress()) + .withPort(NGINX.getMappedPort(80))); + + webClient + .get() + .uri("/?path=test.txt") + .exchange() + .expectStatus().isOk() + .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML) + .expectBody(String.class).value(rendered -> + { + Document doc = Jsoup.parse(rendered); + assertThat( + doc.select("html > body > main > div > div > div > pre").text()) + .isEqualTo("Hello World!"); + }); + } + + @Test + @DisplayName("HtmlController - Remote-Response: 404") + void testHtmlControllerResponse404() throws Exception + { + MOCK_SERVER + .when(request().withPath("/missing.txt"), exactly(1)) + .forward(forward() + .withHost(NGINX.getContainerIpAddress()) + .withPort(NGINX.getMappedPort(80))); + + webClient + .get() + .uri("/?path=missing.txt") + .exchange() + .expectStatus().isOk() + .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML) + .expectBody(String.class).value(rendered -> + { + Document doc = Jsoup.parse(rendered); + assertThat( + doc.select("html > body > main > div > div > div > pre").text()) + .matches(MISSING_PATTERN); + }); + } + + @Test + @DisplayName("RestController - Remote-Response: 200") + void testRestController200() throws Exception + { + MOCK_SERVER + .when(request().withPath("/test.txt"), exactly(1)) + .forward(forward() + .withHost(NGINX.getContainerIpAddress()) + .withPort(NGINX.getMappedPort(80))); + + webClient + .get() + .uri("/?path=test.txt") + .header("Accept", MediaType.TEXT_PLAIN_VALUE) + .exchange() + .expectStatus().isOk() + .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_PLAIN) + .expectBody(String.class).isEqualTo("Hello World!\n"); + } + + @Test + @DisplayName("RestController - Remote-Response: 404") + void testRestController404() throws Exception + { + MOCK_SERVER + .when(request().withPath("/missing.txt"), exactly(1)) + .forward(forward() + .withHost(NGINX.getContainerIpAddress()) + .withPort(NGINX.getMappedPort(80))); + + webClient + .get() + .uri("/?path=missing.txt") + .header("Accept", MediaType.TEXT_PLAIN_VALUE) + .exchange() + .expectStatus().isNotFound() + .expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) + .expectBody() + .jsonPath("status").isEqualTo(404) + .jsonPath("error").isEqualTo("Not Found") + .jsonPath("message").isEqualTo( + "Cause: 404 Not Found from GET http://localhost:" + + MOCK_SERVER.getLocalPort() + + "/missing.txt") + .jsonPath("timestamp").exists(); + + } + + @Configuration + static class Application + { + @Bean + RemoteContentService service() + { + return new RemoteContentService( + WebClient + .builder() + .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort()) + .build()); + } + + @Bean + HtmlController htmlController(RemoteContentService service) + { + return new HtmlController(service); + } + + @Bean + RestController restController(RemoteContentService service) + { + return new RestController(service); + } + } +}