Search in sources :

Example 11 with AlertCondition

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

the class HTTPAlarmCallbackTest method callSucceedsIfRemoteRequestSucceeds.

@Test
public void callSucceedsIfRemoteRequestSucceeds() throws Exception {
    when(whitelistService.isWhitelisted(anyString())).thenReturn(true);
    server.enqueue(new MockResponse().setResponseCode(200));
    server.start();
    final Configuration configuration = new Configuration(ImmutableMap.of("url", server.url("/").toString()));
    alarmCallback.initialize(configuration);
    alarmCallback.checkConfiguration();
    final Stream stream = new StreamMock(ImmutableMap.of("_id", "stream-id", "title", "Stream Title", "description", "Stream Description"), ImmutableList.of());
    final AlertCondition alertCondition = new DummyAlertCondition(stream, "condition-id", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), "user", ImmutableMap.of(), "Alert Condition Title");
    final List<MessageSummary> messageSummaries = ImmutableList.of(new MessageSummary("graylog_1", new Message("Test message 1", "source1", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))), new MessageSummary("graylog_2", new Message("Test message 2", "source2", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))));
    final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), messageSummaries);
    alarmCallback.call(stream, checkResult);
    final RecordedRequest request = server.takeRequest();
    assertThat(request.getPath()).isEqualTo("/");
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/json");
    assertThat(request.getBodySize()).isPositive();
    final String requestBody = request.getBody().readUtf8();
    final JsonNode jsonNode = objectMapper.readTree(requestBody);
    assertThat(jsonNode.get("check_result").get("matching_messages").size()).isEqualTo(2);
    assertThat(jsonNode.get("check_result").get("triggered").asBoolean()).isTrue();
    assertThat(jsonNode.get("check_result").get("triggered_at").asText()).isEqualTo("2016-09-06T17:00:00.000Z");
    assertThat(jsonNode.get("stream").get("id").asText()).isEqualTo("stream-id");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Configuration(org.graylog2.plugin.configuration.Configuration) Message(org.graylog2.plugin.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DateTime(org.joda.time.DateTime) StreamMock(org.graylog2.streams.StreamMock) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) MessageSummary(org.graylog2.plugin.MessageSummary) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) Test(org.junit.Test)

Example 12 with AlertCondition

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

the class HTTPAlarmCallbackTest method callThrowsAlarmCallbackExceptionIfRequestBodyCanNotBeBuilt.

@Test
public void callThrowsAlarmCallbackExceptionIfRequestBodyCanNotBeBuilt() throws Exception {
    final Configuration configuration = new Configuration(ImmutableMap.of("url", "http://example.org"));
    alarmCallback.initialize(configuration);
    final Stream stream = mock(Stream.class);
    final AlertCondition alertCondition = mock(AlertCondition.class);
    final List<MessageSummary> messageSummaries = ImmutableList.of();
    final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), messageSummaries) {

        @Override
        public String getResultDescription() {
            throw new RuntimeException("Boom");
        }
    };
    expectedException.expect(AlarmCallbackException.class);
    expectedException.expectMessage("Unable to serialize alarm");
    alarmCallback.call(stream, checkResult);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) MessageSummary(org.graylog2.plugin.MessageSummary) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 13 with AlertCondition

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

the class AbstractAlertConditionTest method testDifferingTypesForNumericalParameters.

@Test
public void testDifferingTypesForNumericalParameters() throws Exception {
    final AlertCondition alertConditionWithDouble = getDummyAlertCondition(ImmutableMap.of("grace", 3.0));
    assertEquals(3, alertConditionWithDouble.getGrace());
    final AlertCondition alertConditionWithInteger = getDummyAlertCondition(ImmutableMap.of("grace", 3));
    assertEquals(3, alertConditionWithInteger.getGrace());
    final AlertCondition alertConditionWithStringDouble = getDummyAlertCondition(ImmutableMap.of("grace", "3.0"));
    assertEquals(3, alertConditionWithStringDouble.getGrace());
    final AlertCondition alertConditionWithStringInteger = getDummyAlertCondition(ImmutableMap.of("grace", "3"));
    assertEquals(3, alertConditionWithStringInteger.getGrace());
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Test(org.junit.Test)

Example 14 with AlertCondition

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

the class AlertServiceImplTest method nonIntervalAlertShouldNotRepeatNotifications.

@Test
@MongoDBFixtures("non-interval-alert.json")
public void nonIntervalAlertShouldNotRepeatNotifications() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(true);
    final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isFalse();
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 15 with AlertCondition

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

the class AlertServiceImplTest method shouldRepeatNotificationsWhenOptionIsEnabled.

@Test
@MongoDBFixtures("unresolved-alert.json")
public void shouldRepeatNotificationsWhenOptionIsEnabled() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(true);
    final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isTrue();
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) 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