Search in sources :

Example 1 with AlertService

use of org.graylog2.alerts.AlertService in project graylog2-server by Graylog2.

the class AlertScannerTest method testCheckStatefulAlertNotifications.

@Test
public void testCheckStatefulAlertNotifications() throws Exception {
    final Stream stream = mock(Stream.class);
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(false);
    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);
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.empty());
    when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(alert));
    when(alertService.isResolved(alert)).thenReturn(false);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    verify(alertCondition, times(2)).runCheck();
    verify(alertNotificationsSender, times(1)).send(positiveCheckResult, stream, alert, alertCondition);
    verify(alertService, never()).resolveAlert(alert);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 2 with AlertService

use of org.graylog2.alerts.AlertService in project graylog2-server by Graylog2.

the class AlertScannerTest method testCheckTriggersFirstAlert.

@Test
public void testCheckTriggersFirstAlert() 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);
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.empty());
    when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(alert));
    when(alertService.isResolved(alert)).thenReturn(false);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    verify(alertCondition, times(2)).runCheck();
    verify(alertNotificationsSender, times(1)).send(positiveCheckResult, stream, alert, alertCondition);
    verify(alertService, never()).resolveAlert(alert);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 3 with AlertService

use of org.graylog2.alerts.AlertService in project graylog2-server by Graylog2.

the class AlertScannerTest method testAlertIsResolved.

@Test
public void testAlertIsResolved() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    final Stream stream = mock(Stream.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);
    when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    verify(alertCondition, times(1)).runCheck();
    verify(alertService, never()).resolveAlert(alert);
    when(alertCondition.runCheck()).thenReturn(new AbstractAlertCondition.NegativeCheckResult());
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(alert));
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isFalse();
    verify(alertCondition, times(2)).runCheck();
    verify(alertService, times(1)).resolveAlert(alert);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 4 with AlertService

use of org.graylog2.alerts.AlertService in project graylog2-server by Graylog2.

the class AlertScannerTest method testCheckRepeatedAlertNotifications.

@Test
public void testCheckRepeatedAlertNotifications() throws Exception {
    final Stream stream = mock(Stream.class);
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(true);
    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);
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.empty());
    when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(alert));
    when(alertService.isResolved(alert)).thenReturn(false);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    verify(alertCondition, times(2)).runCheck();
    verify(alertNotificationsSender, times(2)).send(positiveCheckResult, stream, alert, alertCondition);
    verify(alertService, never()).resolveAlert(alert);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 5 with AlertService

use of org.graylog2.alerts.AlertService 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);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Aggregations

AlertCondition (org.graylog2.plugin.alarms.AlertCondition)5 Stream (org.graylog2.plugin.streams.Stream)5 Test (org.junit.Test)5