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");
}
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);
});
}
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());
});
}
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);
});
}
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());
});
}
Aggregations