diff --git a/hawkbit-rest/hawkbit-ddi-resource/src/test/java/org/eclipse/hawkbit/ddi/rest/resource/DdiDeploymentBaseTest.java b/hawkbit-rest/hawkbit-ddi-resource/src/test/java/org/eclipse/hawkbit/ddi/rest/resource/DdiDeploymentBaseTest.java index beff1b0a7..c276d759d 100644 --- a/hawkbit-rest/hawkbit-ddi-resource/src/test/java/org/eclipse/hawkbit/ddi/rest/resource/DdiDeploymentBaseTest.java +++ b/hawkbit-rest/hawkbit-ddi-resource/src/test/java/org/eclipse/hawkbit/ddi/rest/resource/DdiDeploymentBaseTest.java @@ -46,6 +46,7 @@ import org.eclipse.hawkbit.repository.model.TargetUpdateStatus; import org.eclipse.hawkbit.repository.test.matcher.Expect; import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents; +import org.eclipse.hawkbit.rest.exception.MessageNotReadableException; import org.eclipse.hawkbit.rest.util.JsonBuilder; import org.eclipse.hawkbit.rest.util.MockMvcResultPrinter; import org.junit.Test; @@ -773,7 +774,9 @@ public void missingResultAttributeInFeedbackReturnsBadRequest() throws Exception final String missingResultInFeedback = JsonBuilder.missingResultInFeedback(action.getId().toString(), "closed", "test"); postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingResultInFeedback, - status().isBadRequest()); + status().isBadRequest()) + .andExpect(jsonPath("$.*", hasSize(3))) + .andExpect(jsonPath("$.exceptionClass", equalTo(MessageNotReadableException.class.getCanonicalName()))); } @Test @@ -794,7 +797,9 @@ public void missingFinishedAttributeInFeedbackReturnsBadRequest() throws Excepti final String missingFinishedResultInFeedback = JsonBuilder .missingFinishedResultInFeedback(action.getId().toString(), "closed", "test"); postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingFinishedResultInFeedback, - status().isBadRequest()); + status().isBadRequest()) + .andExpect(jsonPath("$.*", hasSize(3))) + .andExpect(jsonPath("$.exceptionClass", equalTo(MessageNotReadableException.class.getCanonicalName()))); } private void assertActionStatusCount(final int actionStatusCount, final int minActionStatusCountInPage) { @@ -829,14 +834,14 @@ private ResultActions performGet(final String url, final MediaType mediaType, fi .andExpect(content().contentTypeCompatibleWith(mediaType)); } - private void postFeedback(final MediaType mediaType, final String controllerId, final Long id, final String content, + private ResultActions postFeedback(final MediaType mediaType, final String controllerId, final Long id, final String content, final ResultMatcher statusMatcher) throws Exception { - postFeedback(mediaType, controllerId, id, content.getBytes(), statusMatcher); + return postFeedback(mediaType, controllerId, id, content.getBytes(), statusMatcher); } - private void postFeedback(final MediaType mediaType, final String controllerId, final Long id, final byte[] content, + private ResultActions postFeedback(final MediaType mediaType, final String controllerId, final Long id, final byte[] content, final ResultMatcher statusMatcher) throws Exception { - mvc.perform(post(DEPLOYMENT_BASE + id + "/feedback", tenantAware.getCurrentTenant(), controllerId) + return mvc.perform(post(DEPLOYMENT_BASE + id + "/feedback", tenantAware.getCurrentTenant(), controllerId) .content(content).contentType(mediaType).accept(mediaType)).andDo(MockMvcResultPrinter.print()) .andExpect(statusMatcher); } diff --git a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java index a72e80920..7321fe33e 100644 --- a/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java +++ b/hawkbit-rest/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java @@ -26,6 +26,7 @@ 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; import org.springframework.web.multipart.MultipartException; @@ -112,9 +113,10 @@ public ResponseEntity handleSpServerRtExceptions(final HttpServle } /** - * Method for handling exception of type HttpMessageNotReadableException - * which is thrown in case the request body is not well formed and cannot be - * deserialized. Called by the Spring-Framework for exception handling. + * Method for handling exception of type HttpMessageNotReadableException and + * MethodArgumentNotValidException which are thrown in case the request body is + * not well formed (e.g. syntax failures, missing/invalid parameters) and cannot + * be deserialized. Called by the Spring-Framework for exception handling. * * @param request * the Http request @@ -123,8 +125,8 @@ public ResponseEntity handleSpServerRtExceptions(final HttpServle * @return the entity to be responded containing the exception information * as entity. */ - @ExceptionHandler(HttpMessageNotReadableException.class) - public ResponseEntity handleHttpMessageNotReadableException(final HttpServletRequest request, + @ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentNotValidException.class}) + public ResponseEntity handleExceptionCausedByIncorrectRequestBody(final HttpServletRequest request, final Exception ex) { logRequest(request, ex); final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());