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)));
}
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());
}
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);
}
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);
}
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");
}
Aggregations