Implemented an interceptor, that forbiddes access according to a header
[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.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;
18
19
20 @SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" })
21 @AutoConfigureMockMvc
22 class DemoApplicationTests
23 {
24
25         @Value("${build.version}")
26         String projectVersion;
27
28
29         @Test
30         void contextLoads()
31         {
32         }
33
34
35         @Test
36         void getWelcomeResponseIsOkIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
37         {
38                 mvc
39                                 .perform(get("/"))
40                                 .andExpect(status().isOk());
41         }
42
43         @Test
44         void getWelcomeResponseIsOkIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
45         {
46                 mvc
47                                 .perform(get("/")
48                                                 .header(REQUEST_TYPE, MACHINE))
49                                 .andExpect(status().isOk());
50         }
51
52         @Test
53         void getWelcomeResponseIsOkIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
54         {
55                 mvc
56                                 .perform(get("/")
57                                                 .header(REQUEST_TYPE, HUMAN))
58                                 .andExpect(status().isOk());
59         }
60
61         @Test
62         void expectedResultsForGreetIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
63         {
64                 mvc
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.\"}"));
71         }
72
73         @Test
74         void expectedResultsForGreetIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
75         {
76                 mvc
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.\"}"));
83         }
84
85         @Test
86         void badRequestForGreetIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
87         {
88                 mvc
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());
93         }
94
95         @Test
96         void expectedResultsForAcknowlegeIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
97         {
98                 mvc
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.\"}"));
105         }
106
107         @Test
108         void isForbiddenForAcknowlegeIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
109         {
110                 mvc
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());
116         }
117
118         @Test
119         void badRequestForAcknowlegeIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
120         {
121                 mvc
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());
126         }
127 }