diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerITCase.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerITCase.java index aa4c22a72e4c6e2d81e7b65b98b978653ddd4ea9..ed27fe464a0b60b2f9e2e191c933f15b3e221c97 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerITCase.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerITCase.java @@ -1,13 +1,16 @@ package de.ozgcloud.admin.common.errorhandling; +import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.stream.Stream; import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -15,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @@ -69,4 +73,16 @@ public class AdminExceptionHandlerITCase { } } + @Nested + class TestConstraintViolationException { + @Test + @SneakyThrows + void shouldHaveInfoInBody() { + var result = mockMvc + .perform(post("/api/test-error").content("{ \"string\" : \"\" }").contentType(MediaType.APPLICATION_JSON).with(csrf())); + + assertThat(result.andReturn().getResponse().getContentAsString()).contains("validatedClass.string"); + } + } + } diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerTest.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerTest.java index 918d2e0244fdcc6974336373b6edd46f5f0af89f..2cd825c630f0812143c7b4f3ae4747d48dd91d5c 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerTest.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/AdminExceptionHandlerTest.java @@ -80,7 +80,7 @@ class AdminExceptionHandlerTest { @SneakyThrows private ResultActions doPerformWithError(Class<? extends Exception> exceptionClass) { - return mockMvc.perform(get("/test-error").param("errorClassName", exceptionClass.getName())); + return mockMvc.perform(get("/api/test-error").param("errorClassName", exceptionClass.getName())); } } diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/HelperService.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/HelperService.java new file mode 100644 index 0000000000000000000000000000000000000000..43847acb229ee1b9d7fb29200fef38da6d170796 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/HelperService.java @@ -0,0 +1,15 @@ +package de.ozgcloud.admin.common.errorhandling; + +import jakarta.validation.Valid; + +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +@Service +@Validated +class HelperService { + + ValidatedClass returnValidObject(@Valid ValidatedClass validatedClass) { + return validatedClass; + } +} diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java index 858db6b852454a2270b76b5d31cc6823ee2cc834..b2a08ef2997799c98db184bb5e8446de0f5c1230 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java @@ -26,9 +26,12 @@ import java.util.Map; import jakarta.validation.ConstraintViolationException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -37,9 +40,11 @@ import de.ozgcloud.common.errorhandling.TechnicalException; import io.micrometer.common.lang.NonNullApi; @RestController -@RequestMapping("/test-error") +@RequestMapping("/api/test-error") @NonNullApi class TestErrorController { + @Autowired + private HelperService service; @FunctionalInterface interface ExceptionProducer { @@ -61,4 +66,10 @@ class TestErrorController { throw EXCEPTION_PRODUCER.get( Class.forName(errorClassName)).produceException(); } + + @PostMapping + public ValidatedClass postMethodName(@RequestBody ValidatedClass object) { + return service.returnValidObject(object); + } + } diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/ValidatedClass.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/ValidatedClass.java new file mode 100644 index 0000000000000000000000000000000000000000..7262bf8076b7becb9c1e6515db6f87b17ba08249 --- /dev/null +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/ValidatedClass.java @@ -0,0 +1,15 @@ +package de.ozgcloud.admin.common.errorhandling; + +import jakarta.validation.constraints.NotEmpty; + +import lombok.Builder; +import lombok.Getter; +import lombok.extern.jackson.Jacksonized; + +@Getter +@Builder +@Jacksonized +public class ValidatedClass { + @NotEmpty(message = "Empty field") + private String string; +}