Search in sources :

Example 11 with AlarmCallbackConfiguration

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

the class V20161125161400_AlertReceiversMigrationTest method doMigrateSingleQualifyingStream.

@Test
public void doMigrateSingleQualifyingStream() throws Exception {
    final String matchingStreamId = new ObjectId().toHexString();
    final Stream stream1 = mock(Stream.class);
    when(stream1.getAlertReceivers()).thenReturn(Collections.emptyMap());
    final Stream stream2 = mock(Stream.class);
    when(stream2.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser"), "emails", ImmutableList.of("foo@bar.com")));
    when(stream2.getId()).thenReturn(matchingStreamId);
    when(this.streamService.loadAll()).thenReturn(ImmutableList.of(stream1, stream2));
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(this.streamService.getAlertConditions(eq(stream2))).thenReturn(ImmutableList.of(alertCondition));
    final String alarmCallbackId = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback = AlarmCallbackConfigurationImpl.create(alarmCallbackId, matchingStreamId, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    when(alarmCallbackConfigurationService.getForStream(eq(stream2))).thenReturn(ImmutableList.of(alarmCallback));
    when(alarmCallbackConfigurationService.save(eq(alarmCallback))).thenReturn(alarmCallbackId);
    when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId));
    this.alertReceiversMigration.upgrade();
    final ArgumentCaptor<AlarmCallbackConfiguration> configurationArgumentCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    verify(this.alarmCallbackConfigurationService, times(1)).save(configurationArgumentCaptor.capture());
    final AlarmCallbackConfiguration updatedConfiguration = configurationArgumentCaptor.getValue();
    assertThat(updatedConfiguration).isEqualTo(alarmCallback);
    assertThat(updatedConfiguration.getType()).isEqualTo(EmailAlarmCallback.class.getCanonicalName());
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).size()).isEqualTo(1);
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).get(0)).isEqualTo("foo@bar.com");
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser");
    final ArgumentCaptor<BasicDBObject> queryCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    final ArgumentCaptor<BasicDBObject> updateCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    verify(this.dbCollection, times(1)).update(queryCaptor.capture(), updateCaptor.capture());
    assertThat(queryCaptor.getValue().toJson()).isEqualTo("{\"_id\": {\"$oid\": \"" + matchingStreamId + "\"}}");
    assertThat(updateCaptor.getValue().toJson()).isEqualTo("{\"$unset\": {\"" + StreamImpl.FIELD_ALERT_RECEIVERS + "\": \"\"}}");
    verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId, Optional.of(alarmCallbackId)));
}
Also used : ObjectId(org.bson.types.ObjectId) Date(java.util.Date) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Test(org.junit.Test)

Example 12 with AlarmCallbackConfiguration

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

the class AlarmCallbackConfigurationServiceImplTest method testCreate.

@Test
public void testCreate() throws Exception {
    final CreateAlarmCallbackRequest request = CreateAlarmCallbackRequest.create("", "", Collections.emptyMap());
    final String streamId = "54e3deadbeefdeadbeefaffe";
    final String userId = "someuser";
    final AlarmCallbackConfiguration alarmCallbackConfiguration = this.alarmCallbackConfigurationService.create(streamId, request, userId);
    assertNotNull(alarmCallbackConfiguration);
    assertEquals(streamId, alarmCallbackConfiguration.getStreamId());
    assertEquals("Create should not save the object", 0, alarmCallbackConfigurationService.count());
}
Also used : CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 13 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration 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 14 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration 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 15 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration 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)

Aggregations

AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)18 Stream (org.graylog2.plugin.streams.Stream)15 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)13 Test (org.junit.Test)12 Timed (com.codahale.metrics.annotation.Timed)8 ApiOperation (io.swagger.annotations.ApiOperation)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (org.graylog2.plugin.database.ValidationException)7 Date (java.util.Date)6 List (java.util.List)6 Path (javax.ws.rs.Path)6 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)6 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)6 BasicDBObject (com.mongodb.BasicDBObject)5 ApiResponses (io.swagger.annotations.ApiResponses)5 POST (javax.ws.rs.POST)5 Produces (javax.ws.rs.Produces)5 Alert (org.graylog2.alerts.Alert)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)5