use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.
the class AlarmCallbackHistoryServiceImplTest method testSaveForDummySuccess.
@Test
public void testSaveForDummySuccess() throws Exception {
final Date createdAt = DateTime.parse(CFG_CREATED_AT).toDate();
final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(createdAt);
final Alert alert = mockAlert();
final AlertCondition alertCondition = mockAlertCondition();
final AlarmCallbackHistory success = this.alarmCallbackHistoryService.success(alarmCallbackConfiguration, alert, alertCondition);
this.alarmCallbackHistoryService.save(success);
MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection(collectionName);
Document document = collection.find().first();
Document configuration = document.get("alarmcallbackconfiguration", Document.class);
Document result = document.get("result", Document.class);
assertThat(document.get("alert_id")).isEqualTo(ALERT_ID);
assertThat(document.get("alertcondition_id")).isEqualTo(ALERT_CONDITION_ID);
assertThat(configuration.get("id")).isEqualTo(CFG_ID);
assertThat(configuration.get("type")).isEqualTo(CFG_TYPE);
assertThat(configuration.get("stream_id")).isEqualTo(CFG_STREAM_ID);
assertThat(configuration.get("creator_user_id")).isEqualTo(CFG_USER);
assertThat(result.get("type")).isEqualTo("success");
}
use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.
the class AlertNotificationsSenderTest method executeStreamWithNotifications.
@Test
public void executeStreamWithNotifications() throws Exception {
final Stream stream = mock(Stream.class);
final Alert alert = mock(Alert.class);
final AlertCondition alertCondition = mock(AlertCondition.class);
final AlertCondition.CheckResult positiveCheckResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Mocked positive CheckResult", Tools.nowUTC(), Collections.emptyList());
final AlarmCallbackConfiguration alarmCallbackConfiguration = mock(AlarmCallbackConfiguration.class);
when(alarmCallbackConfigurationService.getForStream(eq(stream))).thenReturn(ImmutableList.of(alarmCallbackConfiguration));
final AlarmCallback alarmCallback = mock(AlarmCallback.class);
when(alarmCallbackFactory.create(eq(alarmCallbackConfiguration))).thenReturn(alarmCallback);
alertNotificationsSender.send(positiveCheckResult, stream, alert, alertCondition);
final ArgumentCaptor<Stream> streamCaptor = ArgumentCaptor.forClass(Stream.class);
final ArgumentCaptor<AlertCondition.CheckResult> checkResultCaptor = ArgumentCaptor.forClass(AlertCondition.CheckResult.class);
verify(alarmCallback, times(1)).call(streamCaptor.capture(), checkResultCaptor.capture());
assertThat(streamCaptor.getValue()).isEqualTo(stream);
assertThat(checkResultCaptor.getValue()).isEqualTo(positiveCheckResult);
final ArgumentCaptor<AlarmCallbackConfiguration> alarmCallbackConfigurationCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
final ArgumentCaptor<Alert> alertCaptor = ArgumentCaptor.forClass(Alert.class);
final ArgumentCaptor<AlertCondition> alertConditionCaptor = ArgumentCaptor.forClass(AlertCondition.class);
verify(alarmCallbackHistoryService, times(1)).success(alarmCallbackConfigurationCaptor.capture(), alertCaptor.capture(), alertConditionCaptor.capture());
assertThat(alarmCallbackConfigurationCaptor.getValue()).isEqualTo(alarmCallbackConfiguration);
assertThat(alertCaptor.getValue()).isEqualTo(alert);
assertThat(alertConditionCaptor.getValue()).isEqualTo(alertCondition);
}
use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.
the class AlertServiceImplTest method shouldNotRepeatNotificationsWhenOptionIsDisabled.
@Test
@MongoDBFixtures("unresolved-alert.json")
public void shouldNotRepeatNotificationsWhenOptionIsDisabled() throws Exception {
final AlertCondition alertCondition = mock(AlertCondition.class);
when(alertCondition.shouldRepeatNotifications()).thenReturn(false);
final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isFalse();
}
use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.
the class AlertServiceImplTest method repeatNotificationsOptionShouldComplyWithGracePeriod.
@Test
@MongoDBFixtures("unresolved-alert.json")
public void repeatNotificationsOptionShouldComplyWithGracePeriod() throws Exception {
final AlertCondition alertCondition = mock(AlertCondition.class);
when(alertCondition.getGrace()).thenReturn(15);
when(alertCondition.shouldRepeatNotifications()).thenReturn(true);
final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
// Should repeat notification when there was no previous alert
assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isTrue();
final AlarmCallbackHistory alarmCallbackHistory = mock(AlarmCallbackHistory.class);
when(alarmCallbackHistory.createdAt()).thenReturn(DateTime.now(DateTimeZone.UTC).minusMinutes(14));
when(alarmCallbackHistoryService.getForAlertId(ALERT_ID)).thenReturn(ImmutableList.of(alarmCallbackHistory));
// Should not repeat notification if a notification was sent during grace period
assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isFalse();
when(alarmCallbackHistory.createdAt()).thenReturn(DateTime.now(DateTimeZone.UTC).minusMinutes(15));
when(alarmCallbackHistoryService.getForAlertId(ALERT_ID)).thenReturn(ImmutableList.of(alarmCallbackHistory));
// Should repeat notification after grace period passed
assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isTrue();
}
use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.
the class AlertScannerTest method testCheckTriggersAlertIfPreviousIsResolved.
@Test
public void testCheckTriggersAlertIfPreviousIsResolved() throws Exception {
final Stream stream = mock(Stream.class);
final AlertCondition alertCondition = mock(AlertCondition.class);
when(alertService.inGracePeriod(eq(alertCondition))).thenReturn(false);
final AlertCondition.CheckResult positiveCheckResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Mocked positive CheckResult", Tools.nowUTC(), Collections.emptyList());
when(alertCondition.runCheck()).thenReturn(positiveCheckResult);
final Alert alert = mock(Alert.class);
final Alert previousAlert = mock(Alert.class);
when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(previousAlert));
when(alertService.isResolved(previousAlert)).thenReturn(true);
when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
verify(alertCondition, times(1)).runCheck();
verify(alertNotificationsSender, times(1)).send(positiveCheckResult, stream, alert, alertCondition);
verify(alertService, never()).resolveAlert(alert);
}
Aggregations