3 import org.junit.jupiter.api.Test;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.test.web.servlet.MockMvc;
10 import static de.juplo.demo.RequestTypeInterceptor.REQUEST_TYPE;
11 import static de.juplo.demo.RequestTypeInterceptor.RequestType.HUMAN;
12 import static de.juplo.demo.RequestTypeInterceptor.RequestType.MACHINE;
13 import static org.springframework.http.HttpHeaders.ACCEPT;
14 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
15 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20 @SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" })
22 class DemoApplicationTests
25 @Value("${build.version}")
26 String projectVersion;
36 void getWelcomeResponseIsOkIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
40 .andExpect(status().isOk());
44 void getWelcomeResponseIsOkIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
48 .header(REQUEST_TYPE, MACHINE))
49 .andExpect(status().isOk());
53 void getWelcomeResponseIsOkIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
57 .header(REQUEST_TYPE, HUMAN))
58 .andExpect(status().isOk());
62 void expectedResultsForGreetIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
65 .perform(post("/greet/peter")
66 .header(ACCEPT, "application/json")
67 .header(REQUEST_TYPE, MACHINE)
68 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
69 .andExpect(status().isOk())
70 .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
74 void expectedResultsForGreetIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
77 .perform(post("/greet/peter")
78 .header(ACCEPT, "application/json")
79 .header(REQUEST_TYPE, HUMAN)
80 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
81 .andExpect(status().isOk())
82 .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
86 void badRequestForGreetIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
89 .perform(post("/greet/peter")
90 .header(ACCEPT, "application/json")
91 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
92 .andExpect(status().isBadRequest());
96 void expectedResultsForAcknowlegeIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
99 .perform(post("/acknowledge/peter")
100 .header(ACCEPT, "application/json")
101 .header(REQUEST_TYPE, MACHINE)
102 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
103 .andExpect(status().isOk())
104 .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
108 void isForbiddenForAcknowlegeIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
111 .perform(post("/acknowledge/peter")
112 .header(ACCEPT, "application/json")
113 .header(REQUEST_TYPE, HUMAN)
114 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
115 .andExpect(status().isForbidden());
119 void badRequestForAcknowlegeIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
122 .perform(post("/acknowledge/peter")
123 .header(ACCEPT, "application/json")
124 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
125 .andExpect(status().isBadRequest());