Implemented a narrow integration-test: DemoApplicationIT
[demos/testing] / src / test / java / de / juplo / demo / DemoApplicationIT.java
1 package de.juplo.demo;
2
3 import static de.juplo.demo.IntegrationTestBase.MOCK_SERVER;
4 import java.util.regex.Pattern;
5 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
6 import org.jsoup.Jsoup;
7 import org.jsoup.nodes.Document;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import static org.mockserver.matchers.Times.exactly;
11 import static org.mockserver.model.HttpForward.forward;
12 import static org.mockserver.model.HttpRequest.request;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.context.annotation.Configuration;
19 import org.springframework.http.MediaType;
20 import org.springframework.test.web.reactive.server.WebTestClient;
21 import org.springframework.web.reactive.function.client.WebClient;
22
23 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
24 @EnableAutoConfiguration
25 class DemoApplicationIT extends IntegrationTestBase
26 {
27   static Pattern MISSING_PATTERN =
28       Pattern.compile("^404\\ Not\\ Found\\ from\\ GET\\ http:\\/\\/localhost:[0-9]*/missing.txt$");
29
30
31   @Autowired
32   WebTestClient webClient;
33
34
35   @Test
36   @DisplayName("HtmlController - Remote-Response: 200")
37   void testHtmlControllerResponse200() throws Exception
38   {
39     MOCK_SERVER
40         .when(request().withPath("/test.txt"), exactly(1))
41         .forward(forward()
42             .withHost(NGINX.getContainerIpAddress())
43             .withPort(NGINX.getMappedPort(80)));
44
45     webClient
46         .get()
47         .uri("/?path=test.txt")
48         .exchange()
49         .expectStatus().isOk()
50         .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
51         .expectBody(String.class).value(rendered ->
52         {
53           Document doc = Jsoup.parse(rendered);
54           assertThat(
55               doc.select("html > body > main > div > div > div > pre").text())
56               .isEqualTo("Hello World!");
57         });
58   }
59
60   @Test
61   @DisplayName("HtmlController - Remote-Response: 404")
62   void testHtmlControllerResponse404() throws Exception
63   {
64     MOCK_SERVER
65         .when(request().withPath("/missing.txt"), exactly(1))
66         .forward(forward()
67             .withHost(NGINX.getContainerIpAddress())
68             .withPort(NGINX.getMappedPort(80)));
69
70     webClient
71         .get()
72         .uri("/?path=missing.txt")
73         .exchange()
74         .expectStatus().isOk()
75         .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_HTML)
76         .expectBody(String.class).value(rendered ->
77         {
78           Document doc = Jsoup.parse(rendered);
79           assertThat(
80               doc.select("html > body > main > div > div > div > pre").text())
81               .matches(MISSING_PATTERN);
82         });
83   }
84
85   @Test
86   @DisplayName("RestController - Remote-Response: 200")
87   void testRestController200() throws Exception
88   {
89     MOCK_SERVER
90         .when(request().withPath("/test.txt"), exactly(1))
91         .forward(forward()
92             .withHost(NGINX.getContainerIpAddress())
93             .withPort(NGINX.getMappedPort(80)));
94
95     webClient
96         .get()
97         .uri("/?path=test.txt")
98         .header("Accept", MediaType.TEXT_PLAIN_VALUE)
99         .exchange()
100         .expectStatus().isOk()
101         .expectHeader().contentTypeCompatibleWith(MediaType.TEXT_PLAIN)
102         .expectBody(String.class).isEqualTo("Hello World!\n");
103   }
104
105   @Test
106   @DisplayName("RestController - Remote-Response: 404")
107   void testRestController404() throws Exception
108   {
109     MOCK_SERVER
110         .when(request().withPath("/missing.txt"), exactly(1))
111         .forward(forward()
112             .withHost(NGINX.getContainerIpAddress())
113             .withPort(NGINX.getMappedPort(80)));
114
115     webClient
116         .get()
117         .uri("/?path=missing.txt")
118         .header("Accept", MediaType.TEXT_PLAIN_VALUE)
119         .exchange()
120         .expectStatus().isNotFound()
121         .expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
122         .expectBody()
123         .jsonPath("status").isEqualTo(404)
124         .jsonPath("error").isEqualTo("Not Found")
125         .jsonPath("message").isEqualTo(
126             "Cause: 404 Not Found from GET http://localhost:" +
127             MOCK_SERVER.getLocalPort() +
128             "/missing.txt")
129         .jsonPath("timestamp").exists();
130
131   }
132
133   @Configuration
134   static class Application
135   {
136     @Bean
137     RemoteContentService service()
138     {
139       return new RemoteContentService(
140           WebClient
141               .builder()
142               .baseUrl("http://localhost:" + MOCK_SERVER.getLocalPort())
143               .build());
144     }
145
146     @Bean
147     HtmlController htmlController(RemoteContentService service)
148     {
149       return new HtmlController(service);
150     }
151
152     @Bean
153     RestController restController(RemoteContentService service)
154     {
155       return new RestController(service);
156     }
157   }
158 }