Search in sources :

Example 1 with ValidationApiError

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);
}
Also used : Response(javax.ws.rs.core.Response) ValidationException(org.graylog2.plugin.database.ValidationException) ValidationApiError(org.graylog2.plugin.rest.ValidationApiError) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Response (javax.ws.rs.core.Response)1 ValidationException (org.graylog2.plugin.database.ValidationException)1 ValidationApiError (org.graylog2.plugin.rest.ValidationApiError)1 Test (org.junit.Test)1