Search in sources :

Example 6 with CreateAlarmCallbackRequest

use of org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest 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 7 with CreateAlarmCallbackRequest

use of org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest 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

AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)5 CreateAlarmCallbackRequest (org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest)5 Timed (com.codahale.metrics.annotation.Timed)3 ApiOperation (io.swagger.annotations.ApiOperation)3 BadRequestException (javax.ws.rs.BadRequestException)3 Consumes (javax.ws.rs.Consumes)3 Produces (javax.ws.rs.Produces)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)3 ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 Stream (org.graylog2.plugin.streams.Stream)3 Test (org.junit.Test)3 URI (java.net.URI)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 AlarmCallbackConfigurationException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException)2 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)1 ApiResponses (io.swagger.annotations.ApiResponses)1