Search in sources :

Example 46 with ValidationResult

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

the class LookupTableResource method validateAdapter.

@POST
@Path("adapters/validate")
@NoAuditEvent("Validation only")
@ApiOperation(value = "Validate the data adapter config")
@RequiresPermissions(RestPermissions.LOOKUP_TABLES_READ)
public ValidationResult validateAdapter(@Valid @ApiParam DataAdapterApi toValidate) {
    final ValidationResult validation = new ValidationResult();
    final Optional<DataAdapterDto> dtoOptional = dbDataAdapterService.get(toValidate.name());
    if (dtoOptional.isPresent()) {
        // an adapter exist with the given name, check that the IDs are the same, this might be an update
        final DataAdapterDto adapterDto = dtoOptional.get();
        // noinspection ConstantConditions
        if (!adapterDto.id().equals(toValidate.id())) {
            // an adapter exists with a different id, so the name is already in use, fail validation
            validation.addError("name", "The data adapter name is already in use.");
        }
    }
    final Optional<Multimap<String, String>> configValidations = toValidate.config().validate(lookupDataAdapterValidationContext);
    configValidations.ifPresent(validation::addAll);
    return validation;
}
Also used : Multimap(com.google.common.collect.Multimap) DataAdapterDto(org.graylog2.lookup.dto.DataAdapterDto) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 47 with ValidationResult

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

the class NotificationDtoTest method testValidateHttpWithEmptyConfigParameters.

@Test
public void testValidateHttpWithEmptyConfigParameters() {
    final HTTPEventNotificationConfig emptyConfig = HTTPEventNotificationConfig.Builder.create().url("").build();
    final NotificationDto emptyNotification = getHttpNotification().toBuilder().config(emptyConfig).build();
    final ValidationResult validationResult = emptyNotification.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors()).containsOnlyKeys("url");
}
Also used : ValidationResult(org.graylog2.plugin.rest.ValidationResult) HTTPEventNotificationConfig(org.graylog.events.notifications.types.HTTPEventNotificationConfig) Test(org.junit.Test)

Example 48 with ValidationResult

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

the class NotificationDtoTest method testValidateWithEmptyTitle.

@Test
public void testValidateWithEmptyTitle() {
    final NotificationDto invalidNotification = getHttpNotification().toBuilder().title("").build();
    final ValidationResult validationResult = invalidNotification.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors()).containsOnlyKeys("title");
}
Also used : ValidationResult(org.graylog2.plugin.rest.ValidationResult) Test(org.junit.Test)

Example 49 with ValidationResult

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

the class NotificationDtoTest method testValidateLegacyWithEmptyConfigParameters.

@Test
public void testValidateLegacyWithEmptyConfigParameters() {
    final LegacyAlarmCallbackEventNotificationConfig emptyConfig = LegacyAlarmCallbackEventNotificationConfig.Builder.create().callbackType("").configuration(new HashMap<>()).build();
    final NotificationDto emptyNotification = getLegacyNotification().toBuilder().config(emptyConfig).build();
    final ValidationResult validationResult = emptyNotification.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors()).containsOnlyKeys("callback_type");
}
Also used : HashMap(java.util.HashMap) LegacyAlarmCallbackEventNotificationConfig(org.graylog.events.legacy.LegacyAlarmCallbackEventNotificationConfig) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Test(org.junit.Test)

Example 50 with ValidationResult

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

the class EventDefinitionDtoTest method testValidateWithEmptyConfigType.

@Test
public void testValidateWithEmptyConfigType() {
    final EventDefinitionDto invalidEventDefinition = testSubject.toBuilder().config(new EventProcessorConfig.FallbackConfig()).build();
    final ValidationResult validationResult = invalidEventDefinition.validate();
    assertThat(validationResult.failed()).isTrue();
    assertThat(validationResult.getErrors()).containsOnlyKeys("config");
}
Also used : ValidationResult(org.graylog2.plugin.rest.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