Search in sources :

Example 1 with AlarmCallbackHistory

use of org.graylog2.alarmcallbacks.AlarmCallbackHistory in project graylog2-server by Graylog2.

the class AlarmCallbackHistoryServiceImplTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(new Date());
    final Alert alert = mockAlert();
    final AlertCondition alertCondition = mockAlertCondition();
    final AlarmCallbackHistory alarmCallbackHistory = this.alarmCallbackHistoryService.success(alarmCallbackConfiguration, alert, alertCondition);
    verifyAlarmCallbackHistory(alarmCallbackHistory, alert, alertCondition);
    assertThat(alarmCallbackHistory.result()).isNotNull().isInstanceOf(AlarmCallbackSuccess.class);
    assertThat(alarmCallbackHistory.result().type()).isEqualTo("success");
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 2 with AlarmCallbackHistory

use of org.graylog2.alarmcallbacks.AlarmCallbackHistory in project graylog2-server by Graylog2.

the class AlarmCallbackHistoryServiceImplTest method testError.

@Test
public void testError() throws Exception {
    final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(new Date());
    final Alert alert = mockAlert();
    final AlertCondition alertCondition = mockAlertCondition();
    final String errorMessage = "Dummy Error Message";
    final AlarmCallbackHistory alarmCallbackHistory = this.alarmCallbackHistoryService.error(alarmCallbackConfiguration, alert, alertCondition, errorMessage);
    verifyAlarmCallbackHistory(alarmCallbackHistory, alert, alertCondition);
    assertThat(alarmCallbackHistory.result()).isNotNull().isInstanceOf(AlarmCallbackError.class);
    assertThat(alarmCallbackHistory.result().type()).isEqualTo("error");
    final AlarmCallbackError result = (AlarmCallbackError) alarmCallbackHistory.result();
    assertThat(result.error()).isEqualTo(errorMessage);
}
Also used : AlarmCallbackError(org.graylog2.rest.models.alarmcallbacks.AlarmCallbackError) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 3 with AlarmCallbackHistory

use of org.graylog2.alarmcallbacks.AlarmCallbackHistory in project graylog2-server by Graylog2.

the class AlarmCallbackHistoryServiceImplTest method testSaveForDummyError.

@Test
public void testSaveForDummyError() 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 String errorMessage = "Dummy Error Message";
    final AlarmCallbackHistory error = this.alarmCallbackHistoryService.error(alarmCallbackConfiguration, alert, alertCondition, errorMessage);
    this.alarmCallbackHistoryService.save(error);
    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("error");
    assertThat(result.get("error")).isEqualTo(errorMessage);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Document(org.bson.Document) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 4 with AlarmCallbackHistory

use of org.graylog2.alarmcallbacks.AlarmCallbackHistory 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");
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Document(org.bson.Document) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 5 with AlarmCallbackHistory

use of org.graylog2.alarmcallbacks.AlarmCallbackHistory 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();
}
Also used : AlarmCallbackHistory(org.graylog2.alarmcallbacks.AlarmCallbackHistory) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Aggregations

MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)5 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)5 Test (org.junit.Test)5 Date (java.util.Date)4 AlarmCallbackHistory (org.graylog2.alarmcallbacks.AlarmCallbackHistory)4 Alert (org.graylog2.alerts.Alert)4 Document (org.bson.Document)2 Timed (com.codahale.metrics.annotation.Timed)1 ApiOperation (io.swagger.annotations.ApiOperation)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)1 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)1 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)1 AlarmCallbackError (org.graylog2.rest.models.alarmcallbacks.AlarmCallbackError)1 AlarmCallbackHistorySummary (org.graylog2.rest.models.alarmcallbacks.AlarmCallbackHistorySummary)1