Search in sources :

Example 31 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition 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 32 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition 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 33 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition 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);
    when(alertService.shouldRepeatNotifications(alertCondition, alert)).thenReturn(true);
    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 34 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition 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 35 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class AlertScannerTest method testNoCheckWhileInGracePeriod.

@Test
public void testNoCheckWhileInGracePeriod() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    final Stream stream = mock(Stream.class);
    when(alertService.inGracePeriod(eq(alertCondition))).thenReturn(true);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isFalse();
    verify(alertCondition, never()).runCheck();
}
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)45 Stream (org.graylog2.plugin.streams.Stream)35 Test (org.junit.Test)32 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)10 DateTime (org.joda.time.DateTime)10 Timed (com.codahale.metrics.annotation.Timed)9 ApiOperation (io.swagger.annotations.ApiOperation)9 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)9 Path (javax.ws.rs.Path)8 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)8 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)8 ApiResponses (io.swagger.annotations.ApiResponses)7 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)7 Date (java.util.Date)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 List (java.util.List)5 POST (javax.ws.rs.POST)5 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)5 EmailConfiguration (org.graylog2.configuration.EmailConfiguration)5 CreateAlarmCallbackRequest (org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest)5