Search in sources :

Example 6 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class EventDefinitionFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
    final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
    final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
    when(jobDefinitionDto.id()).thenReturn("job-123123");
    when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
    assertThat(nativeEntity).isNotNull();
    final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
    assertThat(eventDefinitionDto.title()).isEqualTo("title");
    assertThat(eventDefinitionDto.description()).isEqualTo("description");
    assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
    // verify that ownership was registered for this entity
    verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 7 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class NotificationFacadeTest method createExcerpt.

@Test
@MongoDBFixtures("NotificationFacadeTest.json")
public void createExcerpt() {
    final Optional<NotificationDto> notificationDto = notificationService.get("5d4d33753d27460ad18e0c4d");
    assertThat(notificationDto).isPresent();
    final EntityExcerpt excerpt = facade.createExcerpt(notificationDto.get());
    assertThat(excerpt.title()).isEqualTo("title");
    assertThat(excerpt.id()).isEqualTo(ModelId.of("5d4d33753d27460ad18e0c4d"));
    assertThat(excerpt.type()).isEqualTo(ModelTypes.NOTIFICATION_V1);
}
Also used : EntityExcerpt(org.graylog2.contentpacks.model.entities.EntityExcerpt) NotificationDto(org.graylog.events.notifications.NotificationDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 8 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class NotificationFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<NotificationDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), ImmutableMap.of(), "kmerz");
    assertThat(nativeEntity).isNotNull();
    final NotificationDto notificationDto = nativeEntity.entity();
    assertThat(notificationDto.title()).isEqualTo("title");
    assertThat(notificationDto.description()).isEqualTo("descriptions");
    assertThat(notificationDto.config().type()).isEqualTo("http-notification-v1");
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) Test(org.junit.Test)

Example 9 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class LegacyAlertConditionMigrator method migrateAlarmCallback.

/**
 * Example alarm callback data structure:
 * <pre>{@code
 *     {
 *       "_id": "54e3deadbeefdeadbeef0001",
 *       "stream_id" : "54e3deadbeefdeadbeef0001",
 *       "type" : "org.graylog2.alarmcallbacks.HTTPAlarmCallback",
 *       "title" : "HTTP Callback Test",
 *       "configuration" : {
 *         "url" : "http://localhost:11000/"
 *       },
 *       "created_at": "2019-01-01T00:00:00.000Z",
 *       "creator_user_id" : "admin"
 *     }
 * }</pre>
 */
private NotificationDto migrateAlarmCallback(Document alarmCallback) {
    final String title = alarmCallback.getString("title");
    final String type = alarmCallback.getString("type");
    final Document configDoc = (Document) alarmCallback.get("configuration");
    final Map<String, Object> configuration = configDoc.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    final LegacyAlarmCallbackEventNotificationConfig config = LegacyAlarmCallbackEventNotificationConfig.builder().callbackType(type).configuration(configuration).build();
    final NotificationDto dto = NotificationDto.builder().title(firstNonNull(title, "Untitled")).description("Migrated legacy alarm callback").config(config).build();
    LOG.info("Migrate legacy alarm callback <{}>", dto.title());
    return notificationResourceHandler.create(dto, userService.getRootUser());
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) Document(org.bson.Document) Map(java.util.Map)

Example 10 with NotificationDto

use of org.graylog.events.notifications.NotificationDto in project graylog2-server by Graylog2.

the class NotificationFacade method decode.

private NativeEntity<NotificationDto> decode(EntityV1 entityV1, Map<String, ValueReference> parameters, Map<EntityDescriptor, Object> nativeEntities, User user) {
    final NotificationEntity entity = objectMapper.convertValue(entityV1.data(), NotificationEntity.class);
    final NotificationDto notificationDto = entity.toNativeEntity(parameters, nativeEntities);
    final NotificationDto savedDto = notificationResourceHandler.create(notificationDto, Optional.ofNullable(user));
    return NativeEntity.create(entityV1.id(), savedDto.id(), ModelTypes.NOTIFICATION_V1, savedDto.title(), savedDto);
}
Also used : NotificationDto(org.graylog.events.notifications.NotificationDto) NotificationEntity(org.graylog.events.contentpack.entities.NotificationEntity)

Aggregations

NotificationDto (org.graylog.events.notifications.NotificationDto)12 Test (org.junit.Test)5 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)3 ApiOperation (io.swagger.annotations.ApiOperation)2 NotificationEntity (org.graylog.events.contentpack.entities.NotificationEntity)2 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)2 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)2 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)2 Permissions (org.graylog2.shared.security.Permissions)2 UserImpl (org.graylog2.users.UserImpl)2 Timed (com.codahale.metrics.annotation.Timed)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ApiResponses (io.swagger.annotations.ApiResponses)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 GET (javax.ws.rs.GET)1