From 5f24099ec4ffd2fcff39a479fd258aef8576b014 Mon Sep 17 00:00:00 2001 From: Patrick Boos Date: Wed, 29 Nov 2023 15:20:21 +0100 Subject: [PATCH] Add more integration tests --- .../OpenApiValidationIntegrationTest.java | 12 ++++++++++++ .../controller/DefaultRestController.java | 5 +++++ .../OpenApiValidationIntegrationTest.java | 17 +++++++++++++++++ .../controller/DefaultRestController.java | 5 +++++ test/openapi.yaml | 13 +++++++++++++ 5 files changed, 52 insertions(+) diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java index 2c6ec3b..bc0a647 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java @@ -123,6 +123,18 @@ public void whenTestOptionsCallThenShouldNotValidate() throws Exception { assertEquals(0, openApiViolationLogger.getViolations().size()); } + @Test + public void whenTestNoBodyThenShouldReturnSuccessAndNoViolation() throws Exception { + mockMvc.perform(post("/test/no-body")) + .andExpectAll( + status().isNoContent(), + content().string(Matchers.blankOrNullString()) + ); + Thread.sleep(100); + + assertEquals(0, openApiViolationLogger.getViolations().size()); + } + @Nullable private OpenApiViolation getViolationByRule(List violations, String rule) { return violations.stream() diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java index 7baaea7..7b1a367 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java @@ -34,4 +34,9 @@ public ResponseEntity postTest(PostTestRequest postTestRequest) { } return ResponseEntity.noContent().build(); } + + @Override + public ResponseEntity postTestNoBody() { + return ResponseEntity.noContent().build(); + } } diff --git a/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java b/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java index 3a7afe4..c479ddf 100644 --- a/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java +++ b/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/OpenApiValidationIntegrationTest.java @@ -2,6 +2,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +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; import com.getyourguide.openapi.validation.api.model.OpenApiViolation; import com.getyourguide.openapi.validation.test.TestViolationLogger; @@ -9,6 +12,7 @@ import java.util.List; import java.util.Optional; import javax.annotation.Nullable; +import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -134,6 +138,19 @@ public void whenTestOptionsCallThenShouldNotValidate() throws Exception { assertEquals(0, openApiViolationLogger.getViolations().size()); } + @Test + public void whenTestNoBodyThenShouldReturnSuccessAndNoViolation() throws Exception { + webTestClient + .post().uri("/test/no-body") + .accept(MediaType.APPLICATION_JSON) + .exchange() + .expectStatus().isNoContent() + .expectBody().isEmpty(); + Thread.sleep(100); + + assertEquals(0, openApiViolationLogger.getViolations().size()); + } + @Nullable private OpenApiViolation getViolationByRule(List violations, String rule) { return violations.stream() diff --git a/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java b/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java index e1362bc..43c78ad 100644 --- a/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java +++ b/spring-boot-starter/spring-boot-starter-webflux/src/test/java/com/getyourguide/openapi/validation/integration/controller/DefaultRestController.java @@ -43,4 +43,9 @@ public Mono> postTest( return Mono.just(ResponseEntity.noContent().build()); }); } + + @Override + public Mono> postTestNoBody(ServerWebExchange exchange) { + return Mono.just(ResponseEntity.noContent().build()); + } } diff --git a/test/openapi.yaml b/test/openapi.yaml index 41833e8..dd433c2 100644 --- a/test/openapi.yaml +++ b/test/openapi.yaml @@ -78,6 +78,19 @@ paths: "$ref": "#/components/schemas/TestResponse" '204': description: Successful response without content + "/test/no-body": + post: + description: Post test without request/response body + operationId: postTestNoBody + responses: + '200': + description: Successful response + content: + application/json: + schema: + "$ref": "#/components/schemas/TestResponse" + '204': + description: Successful response without content components: schemas: TestResponse: