Search in sources :

Example 26 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class NotificationDtoTest method testValidateEmailWithEmptyConfigParameters.

@Test
public void testValidateEmailWithEmptyConfigParameters() {
    final EmailEventNotificationConfig emptyConfig = EmailEventNotificationConfig.Builder.create().sender("").subject("").bodyTemplate("").htmlBodyTemplate("").build();
    final NotificationDto emptyNotification = getEmailNotification().toBuilder().config(emptyConfig).build();
    final ValidationResult validationResult = emptyNotification.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors().size()).isEqualTo(3);
    assertThat(validationResult.getErrors()).containsOnlyKeys("subject", "body", "recipients");
}
Also used : EmailEventNotificationConfig(org.graylog.events.notifications.types.EmailEventNotificationConfig) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Test(org.junit.Test)

Example 27 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class NotificationDtoTest method testValidHttpNotification.

@Test
public void testValidHttpNotification() {
    final NotificationDto validNotification = getHttpNotification();
    final ValidationResult validationResult = validNotification.validate();
    assertThat(validationResult.failed()).isFalse();
    assertThat(validationResult.getErrors().size()).isEqualTo(0);
}
Also used : ValidationResult(org.graylog2.plugin.rest.ValidationResult) Test(org.junit.Test)

Example 28 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult 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.message()).startsWith("Validation failed!");
    assertThat(responseEntity.validationErrors()).containsKeys("foo", "bar");
    assertThat(responseEntity.validationErrors().get("foo")).hasSize(1);
    assertThat(responseEntity.validationErrors().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)

Example 29 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class UserImplTest method testLastNameLengthValidation.

@Test
public void testLastNameLengthValidation() {
    user = new UserImpl(null, null, null);
    ValidationResult result = user.getValidations().get(UserImpl.LAST_NAME).validate(StringUtils.repeat("*", 10));
    assertTrue(result.passed());
    result = user.getValidations().get(UserImpl.LAST_NAME).validate(StringUtils.repeat("*", 210));
    assertFalse(result.passed());
}
Also used : ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) Test(org.junit.Test)

Example 30 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class UserImplTest method testFirstNameLengthValidation.

@Test
public void testFirstNameLengthValidation() {
    user = new UserImpl(null, null, null);
    ValidationResult result = user.getValidations().get(UserImpl.FIRST_NAME).validate(StringUtils.repeat("*", 10));
    assertTrue(result.passed());
    result = user.getValidations().get(UserImpl.FIRST_NAME).validate(StringUtils.repeat("*", 210));
    assertFalse(result.passed());
}
Also used : ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationResult (org.graylog2.plugin.rest.ValidationResult)43 Test (org.junit.Test)24 ApiOperation (io.swagger.annotations.ApiOperation)16 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)16 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)14 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)12 POST (javax.ws.rs.POST)11 List (java.util.List)10 Subject (org.apache.shiro.subject.Subject)8 GRN (org.graylog.grn.GRN)8 User (org.graylog2.plugin.database.users.User)8 ArrayList (java.util.ArrayList)7 Produces (javax.ws.rs.Produces)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 BasicDBObject (com.mongodb.BasicDBObject)5 Collectors (java.util.stream.Collectors)5 NotFoundException (javax.ws.rs.NotFoundException)5