Search in sources :

Example 16 with JobDefinitionDto

use of org.graylog.scheduler.JobDefinitionDto 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 17 with JobDefinitionDto

use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method create.

@Test
public void create() {
    final EventDefinitionDto newDto = EventDefinitionDto.builder().title("Test").description("A test event definition").config(TestEventProcessorConfig.builder().message("This is a test event processor").searchWithinMs(300000).executeEveryMs(60001).build()).priority(3).alert(false).notificationSettings(EventNotificationSettings.withGracePeriod(60000)).keySpec(ImmutableList.of("a", "b")).notifications(ImmutableList.of()).build();
    final EventDefinitionDto dto = handler.create(newDto, Optional.empty());
    // Handler should create the event definition
    assertThat(eventDefinitionService.get(dto.id())).isPresent();
    final Optional<JobDefinitionDto> jobDefinition = jobDefinitionService.getByConfigField("event_definition_id", dto.id());
    // Handler also should create the job definition for the event definition/processor
    assertThat(jobDefinition).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo("Test");
        assertThat(definition.description()).isEqualTo("A test event definition");
        assertThat(definition.config()).isInstanceOf(EventProcessorExecutionJob.Config.class);
        final EventProcessorExecutionJob.Config config = (EventProcessorExecutionJob.Config) definition.config();
        assertThat(config.processingWindowSize()).isEqualTo(300000);
        assertThat(config.processingHopSize()).isEqualTo(60001);
    });
    // And the handler should also create a job trigger for the created job definition
    final Optional<JobTriggerDto> jobTrigger = jobTriggerService.nextRunnableTrigger();
    assertThat(jobTrigger).isPresent().get().satisfies(trigger -> {
        assertThat(trigger.jobDefinitionId()).isEqualTo(jobDefinition.get().id());
        assertThat(trigger.schedule()).isInstanceOf(IntervalJobSchedule.class);
        final IntervalJobSchedule schedule = (IntervalJobSchedule) trigger.schedule();
        assertThat(schedule.interval()).isEqualTo(60001);
        assertThat(schedule.unit()).isEqualTo(TimeUnit.MILLISECONDS);
    });
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 18 with JobDefinitionDto

use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method update.

@Test
@MongoDBFixtures("event-processors.json")
public void update() {
    final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
    final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
    final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
    final JobDefinitionDto existingJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElse(null);
    final JobTriggerDto existingTrigger = jobTriggerService.get("54e3deadbeefdeadbeef0002").orElse(null);
    final TestEventProcessorConfig existingConfig = (TestEventProcessorConfig) existingDto.config();
    final TestEventProcessorConfig newConfig = existingConfig.toBuilder().executeEveryMs(550000).searchWithinMs(800000).build();
    final EventProcessorExecutionJob.Data existingTriggerData = (EventProcessorExecutionJob.Data) existingTrigger.data().orElseThrow(AssertionError::new);
    assertThat(existingDto).isNotNull();
    assertThat(existingJobDefinition).isNotNull();
    assertThat(existingTrigger).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).config(newConfig).build();
    assertThat(handler.update(updatedDto, true)).isNotEqualTo(existingDto);
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(newTitle);
        assertThat(dto.description()).isEqualTo(newDescription);
    });
    // Test that the schedule is updated to the new config
    final JobDefinitionDto newJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElseThrow(AssertionError::new);
    assertThat(newJobDefinition.title()).isEqualTo(newTitle);
    assertThat(newJobDefinition.description()).isEqualTo(newDescription);
    assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingHopSize()).isEqualTo(550000);
    assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingWindowSize()).isEqualTo(800000);
    // Test if the EventDefinition update removed the old trigger data
    // and reset the job definition timerange to the new parameters
    final EventProcessorExecutionJob.Config newJobConfig = (EventProcessorExecutionJob.Config) newJobDefinition.config();
    final TimeRange newTimeRange = newJobConfig.parameters().timerange();
    assertThat(newTimeRange.getFrom()).isEqualTo(clock.nowUTC().minus(newConfig.searchWithinMs()));
    assertThat(newTimeRange.getTo()).isEqualTo(clock.nowUTC());
    assertThat(jobTriggerService.get("54e3deadbeefdeadbeef0002")).isPresent().get().satisfies(trigger -> {
        assertThat(trigger.data()).isEmpty();
        assertThat(trigger.nextTime()).isEqualTo(clock.nowUTC());
    });
}
Also used : TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 19 with JobDefinitionDto

use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method updateWithSchedulingReEnabled.

@Test
@MongoDBFixtures("event-processors-without-schedule.json")
public void updateWithSchedulingReEnabled() {
    final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
    final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
    final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
    final TestEventProcessorConfig existingConfig = (TestEventProcessorConfig) existingDto.config();
    final TestEventProcessorConfig newConfig = existingConfig.toBuilder().executeEveryMs(550000).searchWithinMs(800000).build();
    assertThat(existingDto).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).config(newConfig).build();
    assertThat(handler.update(updatedDto, true)).isNotEqualTo(existingDto);
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(newTitle);
        assertThat(dto.description()).isEqualTo(newDescription);
    });
    final JobDefinitionDto newJobDefinition = jobDefinitionService.getByConfigField("event_definition_id", existingDto.id()).orElseThrow(AssertionError::new);
    assertThat(newJobDefinition.title()).isEqualTo(newTitle);
    assertThat(newJobDefinition.description()).isEqualTo(newDescription);
    assertThat(((EventProcessorExecutionJob.Config) newJobDefinition.config()).processingHopSize()).isEqualTo(550000);
    assertThat(jobTriggerService.getForJob(newJobDefinition.id()).get(0)).satisfies(trigger -> {
        final IntervalJobSchedule schedule = (IntervalJobSchedule) trigger.schedule();
        assertThat(schedule.interval()).isEqualTo(550000);
    });
}
Also used : TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) TestEventProcessorConfig(org.graylog.events.TestEventProcessorConfig) IntervalJobSchedule(org.graylog.scheduler.schedule.IntervalJobSchedule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 20 with JobDefinitionDto

use of org.graylog.scheduler.JobDefinitionDto in project graylog2-server by Graylog2.

the class EventDefinitionHandlerTest method updateWithErrors.

@Test
@MongoDBFixtures("event-processors.json")
public void updateWithErrors() {
    final String newTitle = "A NEW TITLE " + DateTime.now(DateTimeZone.UTC).toString();
    final String newDescription = "A NEW DESCRIPTION " + DateTime.now(DateTimeZone.UTC).toString();
    final EventDefinitionDto existingDto = eventDefinitionService.get("54e3deadbeefdeadbeef0000").orElse(null);
    final JobDefinitionDto existingJobDefinition = jobDefinitionService.get("54e3deadbeefdeadbeef0001").orElse(null);
    final JobTriggerDto existingTrigger = jobTriggerService.get("54e3deadbeefdeadbeef0002").orElse(null);
    assertThat(existingDto).isNotNull();
    assertThat(existingJobDefinition).isNotNull();
    assertThat(existingTrigger).isNotNull();
    final EventDefinitionDto updatedDto = existingDto.toBuilder().title(newTitle).description(newDescription).build();
    doThrow(new NullPointerException("yolo1")).when(eventDefinitionService).save(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo1");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
    // Reset all before doing new stubs
    reset(eventDefinitionService);
    reset(jobDefinitionService);
    reset(jobTriggerService);
    doThrow(new NullPointerException("yolo2")).when(jobDefinitionService).save(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo2");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
    // Reset all before doing new stubs
    reset(eventDefinitionService);
    reset(jobDefinitionService);
    reset(jobTriggerService);
    doThrow(new NullPointerException("yolo3")).when(jobTriggerService).update(any());
    assertThatCode(() -> handler.update(updatedDto, true)).isInstanceOf(NullPointerException.class).hasMessageContaining("yolo3");
    assertThat(eventDefinitionService.get(existingDto.id())).isPresent().get().satisfies(dto -> {
        assertThat(dto.id()).isEqualTo(existingDto.id());
        assertThat(dto.title()).isEqualTo(existingDto.title());
        assertThat(dto.description()).isEqualTo(existingDto.description());
    });
    assertThat(jobDefinitionService.get("54e3deadbeefdeadbeef0001")).isPresent().get().satisfies(definition -> {
        assertThat(definition.title()).isEqualTo(existingJobDefinition.title());
        assertThat(definition.description()).isEqualTo(existingJobDefinition.description());
    });
}
Also used : JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)23 JobTriggerDto (org.graylog.scheduler.JobTriggerDto)15 Test (org.junit.Test)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 TestEventProcessorParameters (org.graylog.events.TestEventProcessorParameters)8 EventProcessorExecutionJob (org.graylog.events.processor.EventProcessorExecutionJob)8 JobExecutionContext (org.graylog.scheduler.JobExecutionContext)8 JobTriggerUpdates (org.graylog.scheduler.JobTriggerUpdates)8 DateTime (org.joda.time.DateTime)8 JobTriggerUpdate (org.graylog.scheduler.JobTriggerUpdate)7 TestEventProcessorConfig (org.graylog.events.TestEventProcessorConfig)4 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)4 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 NotificationDto (org.graylog.events.notifications.NotificationDto)2 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)2 IntervalJobSchedule (org.graylog.scheduler.schedule.IntervalJobSchedule)2 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)2 PasswordAlgorithmFactory (org.graylog2.security.PasswordAlgorithmFactory)2 Permissions (org.graylog2.shared.security.Permissions)2