Search in sources :

Example 6 with MongoDBFixtures

use of org.graylog.testing.mongodb.MongoDBFixtures in project graylog2-server by Graylog2.

the class V20190805115800_RemoveDashboardStateFromViewsTest method removesDashboardStateFromExistingViews.

@Test
@MongoDBFixtures("V20190805115800_RemoveDashboardStateFromViewsTest.json")
public void removesDashboardStateFromExistingViews() {
    final Migration migration = new V20190805115800_RemoveDashboardStateFromViews(clusterConfigService, mongodb.mongoConnection());
    migration.upgrade();
    final ArgumentCaptor<V20190805115800_RemoveDashboardStateFromViews.MigrationCompleted> argumentCaptor = ArgumentCaptor.forClass(V20190805115800_RemoveDashboardStateFromViews.MigrationCompleted.class);
    verify(clusterConfigService, times(1)).write(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue().modifiedViewsCount()).isEqualTo(4);
    MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection("views");
    assertThat(collection.count()).isEqualTo(4);
}
Also used : Migration(org.graylog2.migrations.Migration) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 7 with MongoDBFixtures

use of org.graylog.testing.mongodb.MongoDBFixtures in project graylog2-server by Graylog2.

the class SidecarServiceImplTest method testFindById.

@Test
@MongoDBFixtures("collectorsMultipleDocuments.json")
public void testFindById() throws Exception {
    final String collector1id = "uniqueid1";
    final Sidecar sidecar = this.sidecarService.findByNodeId(collector1id);
    assertNotNull(sidecar);
    assertEquals(collector1id, sidecar.nodeId());
}
Also used : Sidecar(org.graylog.plugins.sidecar.rest.models.Sidecar) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 8 with MongoDBFixtures

use of org.graylog.testing.mongodb.MongoDBFixtures in project graylog2-server by Graylog2.

the class V20190304102700_MigrateMessageListStructureTest method testMigratingViewStructure.

@Test
@MongoDBFixtures("V20190304102700_MigrateMessageListStructureTest.json")
public void testMigratingViewStructure() {
    final BasicDBObject dbQuery1 = new BasicDBObject();
    dbQuery1.put("_id", new ObjectId("58458e442f857c314491344e"));
    final MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection("views");
    migration.upgrade();
    final FindIterable<Document> views = collection.find(dbQuery1);
    final Document view1 = views.first();
    @SuppressWarnings("unchecked") final List<Document> widgets1 = (List) view1.get("state", Document.class).get("a2a804b7-27cf-4cac-8015-58d9a9640d33", Document.class).get("widgets");
    assertThat(widgets1.size()).isEqualTo(2);
    assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isEqualTo(1);
    assertThat(widgets1.stream().filter(widget -> widget.getString("type").equals("messages")).allMatch((widget) -> {
        final Document config = widget.get("config", Document.class);
        @SuppressWarnings("unchecked") final List<String> fields = (List) config.get("fields");
        final boolean startWithTimestamp = fields.get(0).contains("timestamp");
        final boolean showMessageRow = config.getBoolean("show_message_row");
        return startWithTimestamp && showMessageRow;
    })).isTrue();
    final BasicDBObject dbQuery2 = new BasicDBObject();
    dbQuery2.put("_id", new ObjectId("58458e442f857c314491344f"));
    final FindIterable<Document> views2 = collection.find(dbQuery2);
    final Document view2 = views2.first();
    final Document states = view2.get("state", Document.class);
    assertThat(states.values().size()).isEqualTo(13);
    assertThat(states.keySet()).containsExactly("7c042319-530a-41b9-9dbb-9676fb1da1a4", "9e5144be-a445-4289-a4cc-0f55142524bc", "c13b2482-60e7-4b1e-98c9-0df8d6da8230", "5adc9297-dfc8-4fd9-b422-cbb097715a62", "ade8c853-503c-407f-b125-efbe2d368973", "cc2bf983-b398-4295-bf01-1c10ed1a97e1", "64feccae-9447-40ef-a401-79a7972078a2", "7c7e04c6-f9f0-495c-91cc-865f60687f8c", "eeaa8838-616f-40c0-88c0-1059ac64f37e", "91c6f8c9-024c-48ec-a869-90548fad218a", "955a71f2-673a-4e1c-a99f-ef97b1b4ae71", "343ff7b6-4554-49d4-bc0b-1339fdc5dac0", "7a84d053-e40a-48c1-a433-97521f7ce7ef");
    states.values().forEach(state -> {
        @SuppressWarnings("unchecked") final List<Document> widgets2 = (List) ((Document) state).get("widgets");
        assertThat(widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).count()).isGreaterThan(0);
        widgets2.stream().filter(widget -> widget.getString("type").equals("messages")).forEach((widget) -> {
            final Document config = widget.get("config", Document.class);
            @SuppressWarnings("unchecked") final List<String> fields = (List) config.get("fields");
            final boolean startWithTimestamp = fields.get(0).contains("timestamp");
            final boolean showMessageRow = config.getBoolean("show_message_row");
            assertThat(startWithTimestamp).isTrue();
            assertThat(showMessageRow).isTrue();
        });
    });
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test) List(java.util.List) Rule(org.junit.Rule) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) ObjectId(org.bson.types.ObjectId) MockitoJUnit(org.mockito.junit.MockitoJUnit) Migration(org.graylog2.migrations.Migration) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Before(org.junit.Before) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) List(java.util.List) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 9 with MongoDBFixtures

use of org.graylog.testing.mongodb.MongoDBFixtures 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 10 with MongoDBFixtures

use of org.graylog.testing.mongodb.MongoDBFixtures in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method updateWithSchedulingDisabled.

@Test
@MongoDBFixtures("event-processors.json")
public void updateWithSchedulingDisabled() {
    final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
    final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
    final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
    final JobDefinitionDto existingJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElse(null);
    final JobTriggerDto existingTrigger = jobTriggerService.get("54e3deadbeefdeadbeef0002").orElse(null);
    final TestEventProcessorConfig existingConfig = (TestEventProcessorConfig) existingDto.config();
    final TestEventProcessorConfig newConfig = existingConfig.toBuilder().executeEveryMs(550000).searchWithinMs(800000).build();
    assertThat(existingDto).isNotNull();
    assertThat(existingJobDefinition).isNotNull();
    assertThat(existingTrigger).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).config(newConfig).build();
    assertThat(handler.update(updatedDto, false)).isNotEqualTo(existingDto);
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(newTitle);
        assertThat(dto.description()).isEqualTo(newDescription);
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isNotPresent();
    assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isNotPresent();
}
Also used : TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)167 Test (org.junit.Test)167 Entity (org.graylog2.contentpacks.model.entities.Entity)47 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)47 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)42 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)24 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)23 DateTime (org.joda.time.DateTime)17 ObjectId (org.bson.types.ObjectId)15 EntityExcerpt (org.graylog2.contentpacks.model.entities.EntityExcerpt)15 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)15 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)11 AbstractMap (java.util.AbstractMap)10 MigrationCompleted (org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.V20191203120602_MigrateSavedSearchesToViews.MigrationCompleted)10 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)10 LookupDataAdapterEntity (org.graylog2.contentpacks.model.entities.LookupDataAdapterEntity)10 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)10 GRN (org.graylog.grn.GRN)9 PipelineEntity (org.graylog2.contentpacks.model.entities.PipelineEntity)9 Document (org.bson.Document)8