|
| 1 | +package com.getyourguide.openapi.validation.integration; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.never; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 12 | + |
| 13 | +import com.getyourguide.openapi.validation.integration.controller.DefaultRestController; |
| 14 | +import com.getyourguide.openapi.validation.test.TestViolationLogger; |
| 15 | +import java.util.Optional; |
| 16 | +import org.hamcrest.Matchers; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 22 | +import org.springframework.boot.test.context.SpringBootTest; |
| 23 | +import org.springframework.boot.test.mock.mockito.SpyBean; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 26 | +import org.springframework.test.web.servlet.MockMvc; |
| 27 | + |
| 28 | +@SpringBootTest(properties = { |
| 29 | + "openapi.validation.should-fail-on-request-violation=true", |
| 30 | + "openapi.validation.should-fail-on-response-violation=true", |
| 31 | +}) |
| 32 | +@AutoConfigureMockMvc |
| 33 | +@ExtendWith(SpringExtension.class) |
| 34 | +public class FailOnViolationIntegrationTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private MockMvc mockMvc; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private TestViolationLogger openApiViolationLogger; |
| 41 | + |
| 42 | + @SpyBean |
| 43 | + private DefaultRestController defaultRestController; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void setup() { |
| 47 | + openApiViolationLogger.clearViolations(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenValidRequestThenReturnSuccessfully() throws Exception { |
| 52 | + mockMvc.perform(post("/test") |
| 53 | + .content("{ \"value\": \"testing\", \"responseStatusCode\": 200 }").contentType(MediaType.APPLICATION_JSON)) |
| 54 | + .andExpectAll( |
| 55 | + status().isOk(), |
| 56 | + jsonPath("$.value").value("testing") |
| 57 | + ); |
| 58 | + Thread.sleep(100); |
| 59 | + |
| 60 | + assertEquals(0, openApiViolationLogger.getViolations().size()); |
| 61 | + verify(defaultRestController).postTest(any()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void whenInvalidRequestThenReturn400AndNoViolationLogged() throws Exception { |
| 66 | + mockMvc.perform(post("/test").content("{ \"value\": 1 }").contentType(MediaType.APPLICATION_JSON)) |
| 67 | + .andExpectAll( |
| 68 | + status().is4xxClientError(), |
| 69 | + content().string(Matchers.blankOrNullString()) |
| 70 | + ); |
| 71 | + Thread.sleep(100); |
| 72 | + |
| 73 | + assertEquals(0, openApiViolationLogger.getViolations().size()); |
| 74 | + verify(defaultRestController, never()).postTest(any()); |
| 75 | + // TODO check that something else gets logged? |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void whenInvalidResponseThenReturn500AndViolationLogged() throws Exception { |
| 80 | + mockMvc.perform(get("/test").queryParam("value", "invalid-response-value!")) |
| 81 | + .andExpectAll( |
| 82 | + status().is5xxServerError(), |
| 83 | + content().string(Matchers.blankOrNullString()) |
| 84 | + ); |
| 85 | + Thread.sleep(100); |
| 86 | + |
| 87 | + assertEquals(1, openApiViolationLogger.getViolations().size()); |
| 88 | + var violation = openApiViolationLogger.getViolations().get(0); |
| 89 | + assertEquals("validation.response.body.schema.pattern", violation.getRule()); |
| 90 | + assertEquals(Optional.of(200), violation.getResponseStatus()); |
| 91 | + assertEquals(Optional.of("/value"), violation.getInstance()); |
| 92 | + verify(defaultRestController).getTest(any(), any(), any()); |
| 93 | + } |
| 94 | +} |
0 commit comments