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