From c8dedd6cee345a8d609a3c143362f7dc46121f9c Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 2 Oct 2024 10:34:21 +0200 Subject: [PATCH] Satisfy Sonarqube Issues --- requests/updateProject.http | 14 +++++++++++++- .../exceptionHandling/GlobalExceptionHandler.java | 8 ++++++++ .../project/UpdateProjectActionTest.java | 2 -- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/requests/updateProject.http b/requests/updateProject.http index d1ae70c..dfa5f0b 100644 --- a/requests/updateProject.http +++ b/requests/updateProject.http @@ -1,3 +1,15 @@ ### GET request to example server PUT http://localhost:8080/projects/1 -Authorization: Bearer {{auth_token}} \ No newline at end of file +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{ + "name": "newName", + "leading_employee": 2, + "employees": [], + "contractor": 9, + "contractor_name": "New Contractor name", + "comment": "new goal of project", + "start_date": "01.01.2010", + "planned_end_date": "01.01.2021" +} \ No newline at end of file diff --git a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java index 8ee2aaf..4c93a21 100644 --- a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java +++ b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java @@ -6,6 +6,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.ConstraintViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -40,6 +41,13 @@ public class GlobalExceptionHandler { return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); } + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException ex, WebRequest request) { + ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); + + return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(ConstraintViolationException.class) public ResponseEntity handleConstraintViolationException(ConstraintViolationException ex, WebRequest request) { String errorMessage = ex.getConstraintViolations().stream().findFirst().get().getMessage(); diff --git a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java index 5fb4c02..594745e 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/UpdateProjectActionTest.java @@ -16,8 +16,6 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.is; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;