Search in sources :

Example 36 with AlertCondition

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

the class FormattedEmailAlertSenderTest method buildBodyContainsURLIfWebInterfaceURLIsSet.

@Test
public void buildBodyContainsURLIfWebInterfaceURLIsSet() throws Exception {
    final EmailConfiguration configuration = new EmailConfiguration() {

        @Override
        public URI getWebInterfaceUri() {
            return URI.create("https://localhost");
        }
    };
    this.emailAlertSender = new FormattedEmailAlertSender(configuration, mockNotificationService, mockNodeId, templateEngine, emailFactory);
    Stream stream = mock(Stream.class);
    when(stream.getId()).thenReturn("123456");
    when(stream.getTitle()).thenReturn("Stream Title");
    AlertCondition alertCondition = mock(AlertCondition.class);
    AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
    when(checkResult.getTriggeredAt()).thenReturn(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
    when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);
    String body = emailAlertSender.buildBody(stream, checkResult, Collections.<Message>emptyList());
    assertThat(body).contains("Stream URL: https://localhost/streams/123456/");
}
Also used : EmailConfiguration(org.graylog2.configuration.EmailConfiguration) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 37 with AlertCondition

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

the class FormattedEmailAlertSenderTest method buildBodyContainsInfoMessageIfWebInterfaceURLIsIncomplete.

@Test
public void buildBodyContainsInfoMessageIfWebInterfaceURLIsIncomplete() throws Exception {
    final EmailConfiguration configuration = new EmailConfiguration() {

        @Override
        public URI getWebInterfaceUri() {
            return URI.create("");
        }
    };
    this.emailAlertSender = new FormattedEmailAlertSender(configuration, mockNotificationService, mockNodeId, templateEngine, emailFactory);
    Stream stream = mock(Stream.class);
    when(stream.getId()).thenReturn("123456");
    when(stream.getTitle()).thenReturn("Stream Title");
    AlertCondition alertCondition = mock(AlertCondition.class);
    AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
    when(checkResult.getTriggeredAt()).thenReturn(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
    when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);
    String body = emailAlertSender.buildBody(stream, checkResult, Collections.<Message>emptyList());
    assertThat(body).contains("Stream URL: Please configure 'transport_email_web_interface_url' in your Graylog configuration file.");
}
Also used : EmailConfiguration(org.graylog2.configuration.EmailConfiguration) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 38 with AlertCondition

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

the class FormattedEmailAlertSenderTest method buildBodyContainsInfoMessageIfWebInterfaceURLIsNotSet.

@Test
public void buildBodyContainsInfoMessageIfWebInterfaceURLIsNotSet() throws Exception {
    final EmailConfiguration configuration = new EmailConfiguration() {

        @Override
        public URI getWebInterfaceUri() {
            return null;
        }
    };
    this.emailAlertSender = new FormattedEmailAlertSender(configuration, mockNotificationService, mockNodeId, templateEngine, emailFactory);
    Stream stream = mock(Stream.class);
    when(stream.getId()).thenReturn("123456");
    when(stream.getTitle()).thenReturn("Stream Title");
    AlertCondition alertCondition = mock(AlertCondition.class);
    AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
    when(checkResult.getTriggeredAt()).thenReturn(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
    when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);
    String body = emailAlertSender.buildBody(stream, checkResult, Collections.<Message>emptyList());
    assertThat(body).contains("Stream URL: Please configure 'transport_email_web_interface_url' in your Graylog configuration file.");
}
Also used : EmailConfiguration(org.graylog2.configuration.EmailConfiguration) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 39 with AlertCondition

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

the class V20161125142400_EmailAlarmCallbackMigrationTest method doMigrateMultipleQualifyingStreams.

@Test
public void doMigrateMultipleQualifyingStreams() throws Exception {
    final String matchingStreamId1 = "matchingStreamId1";
    final String matchingStreamId2 = "matchingStreamId2";
    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(matchingStreamId1);
    final Stream stream3 = mock(Stream.class);
    when(stream3.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser2")));
    when(stream3.getId()).thenReturn(matchingStreamId2);
    when(this.streamService.loadAll()).thenReturn(ImmutableList.of(stream1, stream2, stream3));
    final AlertCondition alertCondition1 = mock(AlertCondition.class);
    final AlertCondition alertCondition2 = mock(AlertCondition.class);
    when(this.streamService.getAlertConditions(eq(stream2))).thenReturn(ImmutableList.of(alertCondition1));
    when(this.streamService.getAlertConditions(eq(stream3))).thenReturn(ImmutableList.of(alertCondition2));
    final ConfigurationRequest configurationRequest = mock(ConfigurationRequest.class);
    when(emailAlarmCallback.getRequestedConfiguration()).thenReturn(configurationRequest);
    when(configurationRequest.getFields()).thenReturn(Collections.emptyMap());
    final AlarmCallbackConfiguration newAlarmCallback1 = mock(AlarmCallbackConfiguration.class);
    final String newAlarmCallbackId1 = "newAlarmCallbackId1";
    final AlarmCallbackConfiguration newAlarmCallback2 = mock(AlarmCallbackConfiguration.class);
    final String newAlarmCallbackId2 = "newAlarmCallbackId2";
    when(alarmCallbackConfigurationService.create(eq(matchingStreamId1), any(CreateAlarmCallbackRequest.class), eq(localAdminId))).thenReturn(newAlarmCallback1);
    when(alarmCallbackConfigurationService.create(eq(matchingStreamId2), any(CreateAlarmCallbackRequest.class), eq(localAdminId))).thenReturn(newAlarmCallback2);
    when(alarmCallbackConfigurationService.save(eq(newAlarmCallback1))).thenReturn(newAlarmCallbackId1);
    when(alarmCallbackConfigurationService.save(eq(newAlarmCallback2))).thenReturn(newAlarmCallbackId2);
    this.emailAlarmCallbackMigrationPeriodical.upgrade();
    final ArgumentCaptor<String> streamIdCaptor = ArgumentCaptor.forClass(String.class);
    final ArgumentCaptor<CreateAlarmCallbackRequest> createAlarmCallbackRequestCaptor = ArgumentCaptor.forClass(CreateAlarmCallbackRequest.class);
    final ArgumentCaptor<String> userIdCaptor = ArgumentCaptor.forClass(String.class);
    verify(this.alarmCallbackConfigurationService, times(2)).create(streamIdCaptor.capture(), createAlarmCallbackRequestCaptor.capture(), userIdCaptor.capture());
    assertThat(streamIdCaptor.getAllValues()).isNotNull().isNotEmpty().contains(matchingStreamId1).contains(matchingStreamId2);
    createAlarmCallbackRequestCaptor.getAllValues().forEach(createAlarmCallbackRequest -> assertThat(createAlarmCallbackRequest.type()).isEqualTo(EmailAlarmCallback.class.getCanonicalName()));
    final ArgumentCaptor<AlarmCallbackConfiguration> alarmCallbackConfigurationCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    verify(this.alarmCallbackConfigurationService, times(2)).save(alarmCallbackConfigurationCaptor.capture());
    assertThat(alarmCallbackConfigurationCaptor.getAllValues()).isNotNull().isNotEmpty().hasSize(2).contains(newAlarmCallback1).contains(newAlarmCallback2);
    verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId1, Optional.of(newAlarmCallbackId1), matchingStreamId2, Optional.of(newAlarmCallbackId2)));
}
Also used : CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Test(org.junit.Test)

Example 40 with AlertCondition

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

the class V20161125142400_EmailAlarmCallbackMigrationTest method doMigrateSingleQualifyingStream.

@Test
public void doMigrateSingleQualifyingStream() throws Exception {
    final String matchingStreamId = "matchingStreamId";
    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 ConfigurationRequest configurationRequest = mock(ConfigurationRequest.class);
    when(emailAlarmCallback.getRequestedConfiguration()).thenReturn(configurationRequest);
    when(configurationRequest.getFields()).thenReturn(Collections.emptyMap());
    final AlarmCallbackConfiguration newAlarmCallback = mock(AlarmCallbackConfiguration.class);
    final String newAlarmCallbackId = "newAlarmCallbackId";
    when(alarmCallbackConfigurationService.create(eq(matchingStreamId), any(CreateAlarmCallbackRequest.class), eq(localAdminId))).thenReturn(newAlarmCallback);
    when(alarmCallbackConfigurationService.save(eq(newAlarmCallback))).thenReturn(newAlarmCallbackId);
    this.emailAlarmCallbackMigrationPeriodical.upgrade();
    final ArgumentCaptor<String> streamIdCaptor = ArgumentCaptor.forClass(String.class);
    final ArgumentCaptor<CreateAlarmCallbackRequest> createAlarmCallbackRequestCaptor = ArgumentCaptor.forClass(CreateAlarmCallbackRequest.class);
    final ArgumentCaptor<String> userIdCaptor = ArgumentCaptor.forClass(String.class);
    verify(this.alarmCallbackConfigurationService, times(1)).create(streamIdCaptor.capture(), createAlarmCallbackRequestCaptor.capture(), userIdCaptor.capture());
    assertThat(streamIdCaptor.getValue()).isNotNull().isNotEmpty().isEqualTo(matchingStreamId);
    final CreateAlarmCallbackRequest createAlarmCallbackRequest = createAlarmCallbackRequestCaptor.getValue();
    assertThat(createAlarmCallbackRequest.type()).isEqualTo(EmailAlarmCallback.class.getCanonicalName());
    final ArgumentCaptor<AlarmCallbackConfiguration> alarmCallbackConfigurationCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    verify(this.alarmCallbackConfigurationService, times(1)).save(alarmCallbackConfigurationCaptor.capture());
    assertThat(alarmCallbackConfigurationCaptor.getValue()).isEqualTo(newAlarmCallback);
    verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId, Optional.of(newAlarmCallbackId)));
}
Also used : CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) 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