WIP:itflux
[demos/testing] / src / test / java / de / juplo / demo / DemoApplicationIT.java
1 package de.juplo.demo;
2
3 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4 import org.jsoup.Jsoup;
5 import org.jsoup.nodes.Document;
6 import org.junit.jupiter.api.Test;
7 import static org.mockserver.matchers.Times.exactly;
8 import static org.mockserver.model.HttpForward.forward;
9 import static org.mockserver.model.HttpRequest.request;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Configuration;
16 import org.springframework.http.MediaType;
17 import org.springframework.test.web.reactive.server.WebTestClient;
18 import org.springframework.web.reactive.function.client.WebClient;
19
20 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
21 @EnableAutoConfiguration
22 class DemoApplicationIT extends IntegrationTestBase
23 {
24   @Autowired
25   WebTestClient webClient;
26
27
28   @Test
29   void test() throws Exception
30   {
31     MOCK_SERVER
32         .when(request().withPath("/test.txt"), exactly(1))
33         .forward(forward()
34             .withHost(NGINX.getContainerIpAddress())
35             .withPort(NGINX.getMappedPort(80)));
36     webClient
37         .get()
38         .uri("/?path=test.txt")
39         .exchange()
40         .expectStatus().isOk()
41         .expectHeader().contentType(MediaType.TEXT_HTML)
42         .expectBody(String.class).value(rendered ->
43         {
44           Document doc = Jsoup.parse(rendered);
45           assertThat(
46               doc.select("html > body > main > div > div > pre").text())
47               .isEqualTo("Hello World!");
48         });
49   }
50
51   @Configuration
52   static class Application
53   {
54     @Bean
55     RemoteContentService service()
56     {
57       return new RemoteContentService(
58           WebClient
59               .builder()
60               .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort())
61               .build());
62     }
63
64     @Bean
65     HtmlController controller(RemoteContentService service)
66     {
67       return new HtmlController(service);
68     }
69   }
70 }