Handle response of MethodArgumentNotValidException (#855)

* Handle response of MethodArgumentNotValidException
* Readapt DdiDeploymentBaseTest after merge conflicts
* Handle Exceptions HttpMessageNotReadable and MethodArgumentNotValid in one ExceptionHandler method

Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch-si.com>
This commit is contained in:
Natalia Kislicyn
2019-06-25 13:31:31 +02:00
committed by Stefan Behl
parent 379726a697
commit 0812937f3f
2 changed files with 18 additions and 11 deletions

View File

@@ -46,6 +46,7 @@ import org.eclipse.hawkbit.repository.model.Target;
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 class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
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 class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
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 @@ public class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
.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);
}

View File

@@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
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 class ResponseExceptionHandler {
}
/**
* 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 class ResponseExceptionHandler {
* @return the entity to be responded containing the exception information
* as entity.
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final HttpServletRequest request,
@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
public ResponseEntity<ExceptionInfo> handleExceptionCausedByIncorrectRequestBody(final HttpServletRequest request,
final Exception ex) {
logRequest(request, ex);
final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());