X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fdemo%2FDemoApplicationTests.java;h=576544fc9c176f2a98200f3a5e1c3468583f83fb;hb=2c62443fe15477f75b6d2409a6b1d7eac7950732;hp=850debd12ef49e160b380fee6c15364f3c8a7a0a;hpb=457b4d27ce2f689218fdebe45761487ddacf2a73;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 850debd..576544f 100644 --- a/src/test/java/de/juplo/demo/DemoApplicationTests.java +++ b/src/test/java/de/juplo/demo/DemoApplicationTests.java @@ -2,26 +2,89 @@ 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.BackendVersionInterceptor.BACKEND_VERSION; +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 +@SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" }) @AutoConfigureMockMvc -class DemoApplicationTests { +class DemoApplicationTests +{ + + @Value("${build.version}") + String projectVersion; + @Test - void contextLoads() { + void contextLoads() + { } @Test - void getWelcomeResponseIsOk(@Autowired MockMvc mvc) throws Exception + void getWelcomeResponseIsOkIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception { mvc.perform(get("/")).andExpect(status().isOk()); } + + @Test + void getWelcomeResponseIsOkIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(BACKEND_VERSION, projectVersion)) + .andExpect(status().isOk()); + } + + @Test + void getWelcomeResponseIsGoneIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(get("/") + .header(BACKEND_VERSION, "FOO")) + .andExpect(status().isGone()); + } + + @Test + void getGreetingIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(post("/peter") + .header(ACCEPT, "application/json") + .header(BACKEND_VERSION, projectVersion) + .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.\"}")); + } + + @Test + void getGreetingIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(post("/peter") + .header(ACCEPT, "application/json") + .header(BACKEND_VERSION, "FOO") + .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME.")) + .andExpect(status().isGone()); + } + + @Test + void getGreetingIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception + { + mvc + .perform(post("/peter") + .header(ACCEPT, "application/json") + .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.\"}")); + } }