Search in sources :

Example 1 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class EventNotificationHandlerConfigEntity method toNativeEntity.

@Override
public EventNotificationHandler.Config toNativeEntity(Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> natvieEntities) {
    String notificationId = notificationId().asString(parameters);
    final EntityDescriptor notificationDescriptor = EntityDescriptor.create(notificationId, ModelTypes.NOTIFICATION_V1);
    final Object notification = natvieEntities.get(notificationDescriptor);
    final EventNotificationHandler.Config.Builder configBuilder = EventNotificationHandler.Config.builder();
    if (notification == null) {
        throw new ContentPackException("Missing notification (" + notificationId + ") for event definition");
    } else if (notification instanceof NotificationDto) {
        NotificationDto notificationDto = (NotificationDto) notification;
        configBuilder.notificationId(notificationDto.id());
    } else {
        throw new ContentPackException("Invalid type for notification (" + notificationId + ") of event definition: " + notification.getClass());
    }
    return configBuilder.notificationParameters(notificationParameters().orElse(null)).build();
}
Also used : ContentPackException(org.graylog2.contentpacks.exceptions.ContentPackException) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NotificationDto(org.graylog.events.notifications.NotificationDto)

Example 2 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class NotificationFacade method exportEntity.

@Override
public Optional<Entity> exportEntity(EntityDescriptor entityDescriptor, EntityDescriptorIds entityDescriptorIds) {
    final ModelId modelId = entityDescriptor.id();
    final Optional<NotificationDto> notificationDto = notificationService.get(modelId.id());
    if (!notificationDto.isPresent()) {
        LOG.debug("Couldn't find notification {}", entityDescriptor);
        return Optional.empty();
    }
    final NotificationEntity entity = (NotificationEntity) notificationDto.get().toContentPackEntity(entityDescriptorIds);
    final JsonNode data = objectMapper.convertValue(entity, JsonNode.class);
    return Optional.of(EntityV1.builder().id(ModelId.of(entityDescriptorIds.getOrThrow(notificationDto.get().id(), ModelTypes.NOTIFICATION_V1))).type(ModelTypes.NOTIFICATION_V1).data(data).build());
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) NotificationEntity(org.graylog.events.contentpack.entities.NotificationEntity) ModelId(org.graylog2.contentpacks.model.ModelId)

Example 3 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class V20191129134600_CreateInitialUrlWhitelist method createWhitelist.

private UrlWhitelist createWhitelist() {
    final Set<WhitelistEntry> entries = new HashSet<>();
    dataAdapterService.findAll().stream().map(this::extractFromDataAdapter).forEach(e -> e.ifPresent(entries::add));
    try (final Stream<NotificationDto> notificationsStream = notificationService.streamAll()) {
        notificationsStream.map(this::extractFromNotification).forEach(e -> e.ifPresent(entries::add));
    }
    log.info("Created {} whitelist entries from URLs configured in data adapters and event notifications.", entries.size());
    final UrlWhitelist whitelist = UrlWhitelist.createEnabled(new ArrayList<>(entries));
    log.debug("Resulting whitelist: {}.", whitelist);
    return whitelist;
}
Also used : RegexWhitelistEntry(org.graylog2.system.urlwhitelist.RegexWhitelistEntry) LiteralWhitelistEntry(org.graylog2.system.urlwhitelist.LiteralWhitelistEntry) WhitelistEntry(org.graylog2.system.urlwhitelist.WhitelistEntry) NotificationDto(org.graylog.events.notifications.NotificationDto) UrlWhitelist(org.graylog2.system.urlwhitelist.UrlWhitelist) HashSet(java.util.HashSet)

Example 4 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class LegacyAlertConditionMigratorTest method run.

@Test
@MongoDBFixtures("legacy-alert-conditions.json")
public void run() {
    final int migratedConditions = 10;
    final int migratedCallbacks = 4;
    assertThat(migrator.run(Collections.emptySet(), Collections.emptySet())).satisfies(result -> {
        assertThat(result.completedAlertConditions()).containsOnly("00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002", "00000000-0000-0000-0000-000000000003", "00000000-0000-0000-0000-000000000004", "00000000-0000-0000-0000-000000000005", "00000000-0000-0000-0000-000000000006", "00000000-0000-0000-0000-000000000007", "00000000-0000-0000-0000-000000000008", "00000000-0000-0000-0000-000000000009", "00000000-0000-0000-0000-000000000010");
        assertThat(result.completedAlarmCallbacks()).containsOnly("54e3deadbeefdeadbeef0001", "54e3deadbeefdeadbeef0002", "54e3deadbeefdeadbeef0003", "54e3deadbeefdeadbeef0004");
    });
    // Make sure we use the EventDefinitionHandler to create the event definitions
    verify(eventDefinitionHandler, times(migratedConditions)).create(any(EventDefinitionDto.class), any(Optional.class));
    // Make sure we use the NotificationResourceHandler to create the notifications
    verify(notificationResourceHandler, times(migratedCallbacks)).create(any(NotificationDto.class), any(Optional.class));
    assertThat(eventDefinitionService.streamAll().count()).isEqualTo(migratedConditions);
    assertThat(notificationService.streamAll().count()).isEqualTo(migratedCallbacks);
    final NotificationDto httpNotification = notificationService.streamAll().filter(n -> n.title().equals("HTTP Callback Test")).findFirst().orElse(null);
    assertThat(httpNotification).isNotNull();
    assertThat(httpNotification.title()).isEqualTo("HTTP Callback Test");
    assertThat(httpNotification.description()).isEqualTo("Migrated legacy alarm callback");
    assertThat(httpNotification.config()).isInstanceOf(LegacyAlarmCallbackEventNotificationConfig.class);
    assertThat((LegacyAlarmCallbackEventNotificationConfig) httpNotification.config()).satisfies(config -> {
        assertThat(config.callbackType()).isEqualTo("org.graylog2.alarmcallbacks.HTTPAlarmCallback");
        assertThat(config.configuration().get("url")).isEqualTo("http://localhost:11000/");
    });
    final NotificationDto httpNotificationWithoutTitle = notificationService.streamAll().filter(n -> n.title().equals("Untitled")).findFirst().orElse(null);
    assertThat(httpNotificationWithoutTitle).isNotNull();
    assertThat(httpNotificationWithoutTitle.title()).isEqualTo("Untitled");
    assertThat(httpNotificationWithoutTitle.description()).isEqualTo("Migrated legacy alarm callback");
    assertThat(httpNotificationWithoutTitle.config()).isInstanceOf(LegacyAlarmCallbackEventNotificationConfig.class);
    assertThat((LegacyAlarmCallbackEventNotificationConfig) httpNotificationWithoutTitle.config()).satisfies(config -> {
        assertThat(config.callbackType()).isEqualTo("org.graylog2.alarmcallbacks.HTTPAlarmCallback");
        assertThat(config.configuration().get("url")).isEqualTo("http://localhost:11000/");
    });
    final NotificationDto emailNotification = notificationService.streamAll().filter(n -> n.title().equals("Email Callback Test")).findFirst().orElse(null);
    assertThat(emailNotification).isNotNull();
    assertThat(emailNotification.title()).isEqualTo("Email Callback Test");
    assertThat(emailNotification.description()).isEqualTo("Migrated legacy alarm callback");
    assertThat(emailNotification.config()).isInstanceOf(LegacyAlarmCallbackEventNotificationConfig.class);
    assertThat((LegacyAlarmCallbackEventNotificationConfig) emailNotification.config()).satisfies(config -> {
        assertThat(config.callbackType()).isEqualTo("org.graylog2.alarmcallbacks.EmailAlarmCallback");
        assertThat(config.configuration().get("sender")).isEqualTo("graylog@example.org");
        assertThat(config.configuration().get("subject")).isEqualTo("Graylog alert for stream: ${stream.title}: ${check_result.resultDescription}");
        assertThat((String) config.configuration().get("body")).contains("Alert Description: ${check_result.resultDescription}\nDate: ");
        assertThat(config.configuration().get("user_receivers")).isEqualTo(Collections.emptyList());
        assertThat(config.configuration().get("email_receivers")).isEqualTo(Collections.singletonList("jane@example.org"));
    });
    final NotificationDto slackNotification = notificationService.streamAll().filter(n -> n.title().equals("Slack Callback Test")).findFirst().orElse(null);
    assertThat(slackNotification).isNotNull();
    assertThat(slackNotification.title()).isEqualTo("Slack Callback Test");
    assertThat(slackNotification.description()).isEqualTo("Migrated legacy alarm callback");
    assertThat(slackNotification.config()).isInstanceOf(LegacyAlarmCallbackEventNotificationConfig.class);
    assertThat((LegacyAlarmCallbackEventNotificationConfig) slackNotification.config()).satisfies(config -> {
        assertThat(config.callbackType()).isEqualTo("org.graylog2.plugins.slack.callback.SlackAlarmCallback");
        assertThat(config.configuration().get("icon_url")).isEqualTo("");
        assertThat(config.configuration().get("graylog2_url")).isEqualTo("");
        assertThat(config.configuration().get("link_names")).isEqualTo(true);
        assertThat(config.configuration().get("webhook_url")).isEqualTo("http://example.com/slack-hook");
        assertThat(config.configuration().get("color")).isEqualTo("#FF0000");
        assertThat(config.configuration().get("icon_emoji")).isEqualTo("");
        assertThat(config.configuration().get("user_name")).isEqualTo("Graylog");
        assertThat(config.configuration().get("backlog_items")).isEqualTo(5);
        assertThat(config.configuration().get("custom_fields")).isEqualTo("");
        assertThat(config.configuration().get("proxy_address")).isEqualTo("");
        assertThat(config.configuration().get("channel")).isEqualTo("#channel");
        assertThat(config.configuration().get("notify_channel")).isEqualTo(false);
        assertThat(config.configuration().get("add_attachment")).isEqualTo(true);
        assertThat(config.configuration().get("short_mode")).isEqualTo(false);
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Message Count - MORE")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(10);
        assertThat(eventDefinition.notifications()).hasSize(2);
        assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())).containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id());
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001");
            assertThat(config.query()).isEqualTo("hello:world");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(10 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.COUNT);
            assertThat(config.series().get(0).field()).isNotPresent();
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(1));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Message Count - LESS")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0);
        assertThat(eventDefinition.notifications()).hasSize(2);
        assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toList())).containsOnly(httpNotification.id(), httpNotificationWithoutTitle.id());
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0001");
            assertThat(config.query()).isEmpty();
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(4 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.COUNT);
            assertThat(config.series().get(0).field()).isNotPresent();
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Lesser.class);
                    final Expr.Lesser lesser = (Expr.Lesser) expression;
                    assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(42));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - HIGHER - MEAN")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15);
        assertThat(eventDefinition.notifications()).isEmpty();
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002");
            assertThat(config.query()).isEqualTo("*");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.AVG);
            assertThat(config.series().get(0).field()).get().isEqualTo("test_field_1");
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - SUM")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15);
        assertThat(eventDefinition.notifications()).isEmpty();
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002");
            assertThat(config.query()).isEqualTo("*");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.SUM);
            assertThat(config.series().get(0).field()).get().isEqualTo("test_field_1");
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Lesser.class);
                    final Expr.Lesser lesser = (Expr.Lesser) expression;
                    assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - MIN")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15);
        assertThat(eventDefinition.notifications()).isEmpty();
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002");
            assertThat(config.query()).isEqualTo("*");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.MIN);
            assertThat(config.series().get(0).field()).get().isEqualTo("test_field_1");
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Lesser.class);
                    final Expr.Lesser lesser = (Expr.Lesser) expression;
                    assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - MAX")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15);
        assertThat(eventDefinition.notifications()).isEmpty();
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002");
            assertThat(config.query()).isEqualTo("*");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.MAX);
            assertThat(config.series().get(0).field()).get().isEqualTo("test_field_1");
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Lesser.class);
                    final Expr.Lesser lesser = (Expr.Lesser) expression;
                    assertThat(lesser.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(lesser.right()).isEqualTo(Expr.NumberValue.create(23));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Value - LOWER - STDDEV")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(60000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(15);
        assertThat(eventDefinition.notifications()).isEmpty();
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0002");
            assertThat(config.query()).isEqualTo("*");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(5 * 60 * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.STDDEV);
            assertThat(config.series().get(0).field()).get().isEqualTo("test_field_1");
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(23));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Content - WITHOUT QUERY")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(120000);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(100);
        assertThat(eventDefinition.notifications()).hasSize(2);
        assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())).containsOnly(emailNotification.id(), slackNotification.id());
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003");
            assertThat(config.query()).isEqualTo("test_field_2:\"hello\"");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.COUNT);
            assertThat(config.series().get(0).field()).isNotPresent();
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Field Content - WITH QUERY")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0);
        assertThat(eventDefinition.notifications()).hasSize(2);
        assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())).containsOnly(emailNotification.id(), slackNotification.id());
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003");
            assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.COUNT);
            assertThat(config.series().get(0).field()).isNotPresent();
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0));
                });
            });
        });
    });
    assertThat(eventDefinitionService.streamAll().filter(ed -> ed.title().equals("Untitled")).findFirst()).get().satisfies(eventDefinition -> {
        assertThat(eventDefinition.alert()).isTrue();
        assertThat(eventDefinition.priority()).isEqualTo(2);
        assertThat(eventDefinition.keySpec()).isEmpty();
        assertThat(eventDefinition.notificationSettings().gracePeriodMs()).isEqualTo(0);
        assertThat(eventDefinition.notificationSettings().backlogSize()).isEqualTo(0);
        assertThat(eventDefinition.notifications()).hasSize(2);
        assertThat(eventDefinition.notifications().stream().map(EventNotificationHandler.Config::notificationId).collect(Collectors.toSet())).containsOnly(emailNotification.id(), slackNotification.id());
        assertThat((AggregationEventProcessorConfig) eventDefinition.config()).satisfies(config -> {
            assertThat(config.streams()).containsExactly("54e3deadbeefdeadbeef0003");
            assertThat(config.query()).isEqualTo("test_field_3:\"foo\" AND foo:bar");
            assertThat(config.groupBy()).isEmpty();
            assertThat(config.searchWithinMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.executeEveryMs()).isEqualTo(CHECK_INTERVAL * 1000);
            assertThat(config.series()).hasSize(1);
            assertThat(config.series().get(0).id()).isNotBlank();
            assertThat(config.series().get(0).function()).isEqualTo(AggregationFunction.COUNT);
            assertThat(config.series().get(0).field()).isNotPresent();
            assertThat(config.conditions()).get().satisfies(conditions -> {
                assertThat(conditions.expression()).get().satisfies(expression -> {
                    assertThat(expression).isInstanceOf(Expr.Greater.class);
                    final Expr.Greater greater = (Expr.Greater) expression;
                    assertThat(greater.left()).isEqualTo(Expr.NumberReference.create(config.series().get(0).id()));
                    assertThat(greater.right()).isEqualTo(Expr.NumberValue.create(0));
                });
            });
        });
    });
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) Optional(java.util.Optional) EventNotificationHandler(org.graylog.events.notifications.EventNotificationHandler) AggregationEventProcessorConfig(org.graylog.events.processor.aggregation.AggregationEventProcessorConfig) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) Expr(org.graylog.events.conditions.Expr) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 5 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class NotificationFacadeTest method loadNativeEntity.

@Test
@MongoDBFixtures("NotificationFacadeTest.json")
public void loadNativeEntity() {
    final NativeEntityDescriptor nativeEntityDescriptor = NativeEntityDescriptor.create(ModelId.of("content-pack-id"), ModelId.of("5d4d33753d27460ad18e0c4d"), ModelTypes.NOTIFICATION_V1, "title");
    final Optional<NativeEntity<NotificationDto>> optionalNativeEntity = facade.loadNativeEntity(nativeEntityDescriptor);
    assertThat(optionalNativeEntity).isPresent();
    final NativeEntity<NotificationDto> nativeEntity = optionalNativeEntity.get();
    assertThat(nativeEntity.entity()).isNotNull();
    final NotificationDto notificationDto = nativeEntity.entity();
    assertThat(notificationDto.id()).isEqualTo("5d4d33753d27460ad18e0c4d");
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

NotificationDto (org.graylog.events.notifications.NotificationDto)12 Test (org.junit.Test)5 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 ApiOperation (io.swagger.annotations.ApiOperation)2 NotificationEntity (org.graylog.events.contentpack.entities.NotificationEntity)2 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)2 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)2 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)2 Permissions (org.graylog2.shared.security.Permissions)2 UserImpl (org.graylog2.users.UserImpl)2 Timed (com.codahale.metrics.annotation.Timed)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ApiResponses (io.swagger.annotations.ApiResponses)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 GET (javax.ws.rs.GET)1