Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST param validation is not covering MethodArgumentNotValidException #855

Merged
merged 5 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +132,23 @@ public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

/** Method for handling exception of type MethodArgumentNotValidException
* which is thrown in case the passed parameters are invalid or missing.
* Called by the Spring-Framework for exception handling.
*
* @param request
* the Http request
* @param ex
* the exception which occurred
* @return the entity to be responded containing the exception information
* as entity.
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionInfo> handleMethodArgumentNotValidException(final HttpServletRequest request,
Nkyn marked this conversation as resolved.
Show resolved Hide resolved
final Exception ex){
return handleHttpMessageNotReadableException(request, ex);
}

/**
* Method for handling exception of type ConstraintViolationException which
* is thrown in case the request is rejected due to a constraint violation.
Expand Down