use of org.graylog2.plugin.rest.ValidationApiError in project graylog2-server by Graylog2.
the class ValidationExceptionMapperTest method testToResponse.
@Test
public void testToResponse() throws Exception {
final ExceptionMapper<ValidationException> mapper = new ValidationExceptionMapper();
final Map<String, List<ValidationResult>> validationErrors = ImmutableMap.of("foo", ImmutableList.of(new ValidationResult.ValidationFailed("foo failed")), "bar", ImmutableList.of(new ValidationResult.ValidationFailed("bar failed"), new ValidationResult.ValidationFailed("baz failed")));
@SuppressWarnings("ThrowableInstanceNeverThrown") final ValidationException exception = new ValidationException(validationErrors);
final Response response = mapper.toResponse(exception);
assertThat(response.getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
assertThat(response.getMediaType()).isEqualTo(MediaType.APPLICATION_JSON_TYPE);
assertThat(response.hasEntity()).isTrue();
assertThat(response.getEntity()).isInstanceOf(ValidationApiError.class);
final ValidationApiError responseEntity = (ValidationApiError) response.getEntity();
assertThat(responseEntity.getMessage()).startsWith("Validation failed!");
assertThat(responseEntity.getValidationErrors()).containsKeys("foo", "bar");
assertThat(responseEntity.getValidationErrors().get("foo")).hasSize(1);
assertThat(responseEntity.getValidationErrors().get("bar")).hasSize(2);
}
Aggregations