X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FDemoApplicationTests.java;h=0c07e930944806b490f4fca0de1151fd670c3608;hb=280599c77a6ae267a544236bdc1e8eb5172b14e7;hp=791a60bb58a7cd23760d03e3e4cf1ca09cdc333e;hpb=14bb7bd1253dbc5d210515f7512636f7bc1d3eb3;p=demos%2Fspring-boot diff --git a/src/test/java/de/juplo/demo/DemoApplicationTests.java b/src/test/java/de/juplo/demo/DemoApplicationTests.java index 791a60b..0c07e93 100644 --- a/src/test/java/de/juplo/demo/DemoApplicationTests.java +++ b/src/test/java/de/juplo/demo/DemoApplicationTests.java @@ -1,13 +1,127 @@ package de.juplo.demo; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +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; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" }) +@AutoConfigureMockMvc +class DemoApplicationTests +{ + + @Value("${build.version}") + String projectVersion; + + + @Test + void contextLoads() + { + } -@SpringBootTest -class DemoApplicationTests { @Test - void contextLoads() { + void getWelcomeResponseIsOkIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/")) + .andExpect(status().isOk()); } + @Test + void getWelcomeResponseIsOkIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(REQUEST_TYPE, MACHINE)) + .andExpect(status().isOk()); + } + + @Test + void getWelcomeResponseIsOkIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(REQUEST_TYPE, HUMAN)) + .andExpect(status().isOk()); + } + + @Test + void expectedResultsForGreetIfRequestIsOfTypeMachine(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(post("/greet/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("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}")); + } + + @Test + void expectedResultsForGreetIfRequestIsOfTypeHuman(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(post("/greet/peter") + .header(ACCEPT, "application/json") + .header(REQUEST_TYPE, HUMAN) + .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME.")) + .andExpect(status().isOk()) + .andExpect(content().json("{\"message\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}")); + } + + @Test + void badRequestForGreetIfRequestTypeHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception + { + mvc + .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("{\"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()); + } }