WIP:it
[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.BeforeEach;
5 import org.junit.jupiter.api.Test;
6 import static org.mockserver.matchers.Times.exactly;
7 import static org.mockserver.model.HttpForward.forward;
8 import static org.mockserver.model.HttpRequest.request;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
14 import org.springframework.http.MediaType;
15 import org.springframework.test.web.servlet.MockMvc;
16 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
20 import org.springframework.web.context.WebApplicationContext;
21 import org.springframework.web.reactive.function.client.WebClient;
22
23 @SpringBootTest
24 @AutoConfigureWebMvc
25 class DemoApplicationIT extends IntegrationTestBase
26 {
27   MockMvc mockMvc;
28   @Autowired
29   WebApplicationContext context;
30
31
32   @BeforeEach
33   void setUp(WebApplicationContext context)
34   {
35     mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
36   }
37
38
39   @Test
40   void test() throws Exception
41   {
42     MOCK_SERVER
43         .when(request().withPath("/test.txt"), exactly(1))
44         .forward(forward()
45             .withHost(NGINX.getContainerIpAddress())
46             .withPort(NGINX.getMappedPort(80)));
47     mockMvc
48         .perform(get(URI.create("http://S.U.T/?path=test.txt")))
49         .andExpect(status().isOk())
50         .andExpect(content().contentType(MediaType.TEXT_PLAIN))
51         .andExpect(content().string("Hello World!\n"));
52   }
53
54   @Configuration
55   static class Application
56   {
57     @Bean
58     RemoteContentService service()
59     {
60       return new RemoteContentService(
61           WebClient
62               .builder()
63               .baseUrl("http://localhost:" +
64                   DemoApplicationIT.MOCK_SERVER.getLocalPort())
65               .build());
66     }
67
68     @Bean
69     HtmlController controller(RemoteContentService service)
70     {
71       return new HtmlController(service);
72     }
73   }
74 }