Search in sources :

Example 11 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class HTTPAlarmCallbackTest method getRequestedConfigurationContainsURLField.

@Test
public void getRequestedConfigurationContainsURLField() throws Exception {
    final ConfigurationRequest requestedConfiguration = alarmCallback.getRequestedConfiguration();
    final ConfigurationField field = requestedConfiguration.getField("url");
    assertThat(field).isNotNull();
    assertThat(field.getHumanName()).isEqualTo("URL");
    assertThat(field.isOptional()).isEqualTo(ConfigurationField.Optional.NOT_OPTIONAL);
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) Test(org.junit.Test)

Example 12 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class ConfigurationMapConverterTest method convertValuesThrowsIllegalArgumentExceptionOnEmptyFieldDescription.

@Test
public void convertValuesThrowsIllegalArgumentExceptionOnEmptyFieldDescription() throws Exception {
    thrown.expect(ValidationException.class);
    thrown.expectMessage("Unknown configuration field description for field \"string\"");
    final ConfigurationRequest cr = new ConfigurationRequest();
    final Map<String, Object> data = new HashMap<>();
    data.put("string", "foo");
    ConfigurationMapConverter.convertValues(data, cr);
}
Also used : HashMap(java.util.HashMap) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) Test(org.junit.Test)

Example 13 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest 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 14 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.

the class V20161125142400_EmailAlarmCallbackMigrationTest method extractDefaultValuesFromEmailAlarmCallbackConfiguration.

@Test
public void extractDefaultValuesFromEmailAlarmCallbackConfiguration() throws Exception {
    final ConfigurationRequest configurationRequest = mock(ConfigurationRequest.class);
    when(emailAlarmCallback.getRequestedConfiguration()).thenReturn(configurationRequest);
    final ConfigurationField configurationField1 = mock(ConfigurationField.class);
    when(configurationField1.getDefaultValue()).thenReturn(42);
    final ConfigurationField configurationField2 = mock(ConfigurationField.class);
    when(configurationField2.getDefaultValue()).thenReturn("foobar");
    final ConfigurationField configurationField3 = mock(ConfigurationField.class);
    when(configurationField3.getDefaultValue()).thenReturn(true);
    final Map<String, ConfigurationField> configurationFields = ImmutableMap.of("field1", configurationField1, "field2", configurationField2, "field3", configurationField3);
    when(configurationRequest.getFields()).thenReturn(configurationFields);
    final Map<String, Object> defaultConfig = this.emailAlarmCallbackMigrationPeriodical.getDefaultEmailAlarmCallbackConfig();
    assertThat(defaultConfig).isNotNull().isNotEmpty().hasSize(3).isEqualTo(ImmutableMap.of("field1", 42, "field2", "foobar", "field3", true));
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) ConfigurationField(org.graylog2.plugin.configuration.fields.ConfigurationField) Test(org.junit.Test)

Example 15 with ConfigurationRequest

use of org.graylog2.plugin.configuration.ConfigurationRequest 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

ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)18 Test (org.junit.Test)11 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)4 HashMap (java.util.HashMap)3 TextField (org.graylog2.plugin.configuration.fields.TextField)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 BadRequestException (javax.ws.rs.BadRequestException)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 ConfigurationField (org.graylog2.plugin.configuration.fields.ConfigurationField)2 Stream (org.graylog2.plugin.streams.Stream)2 CreateAlarmCallbackRequest (org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest)2 Maps (com.google.common.collect.Maps)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)1 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)1 Configuration (org.graylog2.plugin.configuration.Configuration)1