Implemented a narrow integration-test: DemoApplicationIT
[demos/testing] / src / test / java / de / juplo / demo / DemoApplicationIT.java
1 package de.juplo.demo;
2
3 import java.net.URI;
4 import org.junit.jupiter.api.Test;
5 import static org.mockserver.matchers.Times.exactly;
6 import static org.mockserver.model.HttpForward.forward;
7 import static org.mockserver.model.HttpRequest.request;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10 import org.springframework.boot.test.context.SpringBootTest;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.context.annotation.Configuration;
13 import org.springframework.http.MediaType;
14 import org.springframework.test.web.servlet.MockMvc;
15 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18 import org.springframework.web.reactive.function.client.WebClient;
19
20 @SpringBootTest
21 @AutoConfigureMockMvc
22 class DemoApplicationIT extends IntegrationTestBase
23 {
24   @Autowired
25   MockMvc mockMvc;
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     mockMvc
37         .perform(get(URI.create("http://S.U.T/?path=test.txt")))
38         .andExpect(status().isOk())
39         .andExpect(content().contentType(MediaType.TEXT_PLAIN))
40         .andExpect(content().string("Hello World!\n"));
41   }
42
43   @Configuration
44   static class Application
45   {
46     @Bean
47     RemoteContentService service()
48     {
49       return new RemoteContentService(
50           WebClient
51               .builder()
52               .baseUrl("http://localhost:" +
53                   DemoApplicationIT.MOCK_SERVER.getLocalPort())
54               .build());
55     }
56
57     @Bean
58     HtmlController controller(RemoteContentService service)
59     {
60       return new HtmlController(service);
61     }
62   }
63 }