Implemented an interceptor, that forbiddes access according to a header
[demos/spring-boot] / src / test / java / de / juplo / demo / DemoApplicationTests.java
index 576544f..0c07e93 100644 (file)
@@ -7,7 +7,9 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.web.servlet.MockMvc;
 
-import static de.juplo.demo.BackendVersionInterceptor.BACKEND_VERSION;
+import static de.juplo.demo.RequestTypeInterceptor.REQUEST_TYPE;
+import static de.juplo.demo.RequestTypeInterceptor.RequestType.HUMAN;
+import static de.juplo.demo.RequestTypeInterceptor.RequestType.MACHINE;
 import static org.springframework.http.HttpHeaders.ACCEPT;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@@ -31,60 +33,95 @@ class DemoApplicationTests
 
 
        @Test
-       void getWelcomeResponseIsOkIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
+       void getWelcomeResponseIsOkIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
        {
-               mvc.perform(get("/")).andExpect(status().isOk());
+               mvc
+                               .perform(get("/"))
+                               .andExpect(status().isOk());
        }
 
        @Test
-       void getWelcomeResponseIsOkIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
+       void getWelcomeResponseIsOkIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
        {
                mvc
                                .perform(get("/")
-                                               .header(BACKEND_VERSION, projectVersion))
+                                               .header(REQUEST_TYPE, MACHINE))
                                .andExpect(status().isOk());
        }
 
        @Test
-       void getWelcomeResponseIsGoneIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
+       void getWelcomeResponseIsOkIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
        {
                mvc
                                .perform(get("/")
-                                               .header(BACKEND_VERSION, "FOO"))
-                               .andExpect(status().isGone());
+                                               .header(REQUEST_TYPE, HUMAN))
+                               .andExpect(status().isOk());
        }
 
        @Test
-       void getGreetingIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
+       void expectedResultsForGreetIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
        {
                mvc
-                               .perform(post("/peter")
+                               .perform(post("/greet/peter")
                                                .header(ACCEPT, "application/json")
-                                               .header(BACKEND_VERSION, projectVersion)
+                                               .header(REQUEST_TYPE, MACHINE)
                                                .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
                                .andExpect(status().isOk())
-                               .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
+                               .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
        }
 
        @Test
-       void getGreetingIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
+       void expectedResultsForGreetIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
        {
                mvc
-                               .perform(post("/peter")
+                               .perform(post("/greet/peter")
                                                .header(ACCEPT, "application/json")
-                                               .header(BACKEND_VERSION, "FOO")
+                                               .header(REQUEST_TYPE, HUMAN)
                                                .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
-                               .andExpect(status().isGone());
+                               .andExpect(status().isOk())
+                               .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
        }
 
        @Test
-       void getGreetingIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
+       void badRequestForGreetIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
        {
                mvc
-                               .perform(post("/peter")
+                               .perform(post("/greet/peter")
                                                .header(ACCEPT, "application/json")
                                                .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isBadRequest());
+       }
+
+       @Test
+       void expectedResultsForAcknowlegeIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/acknowledge/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .header(REQUEST_TYPE, MACHINE)
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
                                .andExpect(status().isOk())
-                               .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
+                               .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
+       }
+
+       @Test
+       void isForbiddenForAcknowlegeIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/acknowledge/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .header(REQUEST_TYPE, HUMAN)
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isForbidden());
+       }
+
+       @Test
+       void badRequestForAcknowlegeIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/acknowledge/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isBadRequest());
        }
 }