Added a `@RestController`
[demos/spring-boot] / src / test / java / de / juplo / demo / DemoApplicationTests.java
1 package de.juplo.demo;
2
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;
9
10 import static de.juplo.demo.BackendVersionInterceptor.BACKEND_VERSION;
11 import static org.springframework.http.HttpHeaders.ACCEPT;
12 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
13 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
14 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16
17
18 @SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" })
19 @AutoConfigureMockMvc
20 class DemoApplicationTests
21 {
22
23         @Value("${build.version}")
24         String projectVersion;
25
26
27         @Test
28         void contextLoads()
29         {
30         }
31
32
33         @Test
34         void getWelcomeResponseIsOkIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
35         {
36                 mvc.perform(get("/")).andExpect(status().isOk());
37         }
38
39         @Test
40         void getWelcomeResponseIsOkIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
41         {
42                 mvc
43                                 .perform(get("/")
44                                                 .header(BACKEND_VERSION, projectVersion))
45                                 .andExpect(status().isOk());
46         }
47
48         @Test
49         void getWelcomeResponseIsGoneIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
50         {
51                 mvc
52                                 .perform(get("/")
53                                                 .header(BACKEND_VERSION, "FOO"))
54                                 .andExpect(status().isGone());
55         }
56
57         @Test
58         void getGreetingIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
59         {
60                 mvc
61                                 .perform(post("/peter")
62                                                 .header(ACCEPT, "application/json")
63                                                 .header(BACKEND_VERSION, projectVersion)
64                                                 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
65                                 .andExpect(status().isOk())
66                                 .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
67         }
68
69         @Test
70         void getGreetingIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
71         {
72                 mvc
73                                 .perform(post("/peter")
74                                                 .header(ACCEPT, "application/json")
75                                                 .header(BACKEND_VERSION, "FOO")
76                                                 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
77                                 .andExpect(status().isGone());
78         }
79
80         @Test
81         void getGreetingIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
82         {
83                 mvc
84                                 .perform(post("/peter")
85                                                 .header(ACCEPT, "application/json")
86                                                 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
87                                 .andExpect(status().isOk())
88                                 .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
89         }
90 }