use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.
the class BaseScheduledJobHandlerTest method mockJob.
private void mockJob() {
jobs = new HashMap<>();
Mockito.when(schedulerService.create(Mockito.isA(CreateScheduledJobParams.class))).thenAnswer(invocation -> {
final CreateScheduledJobParams params = invocation.getArgument(0);
final ScheduledJob job = ScheduledJob.create().name(params.getName()).descriptor(params.getDescriptor()).description(params.getDescription()).calendar(params.getCalendar()).config(params.getConfig()).enabled(params.isEnabled()).user(params.getUser()).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:creator")).createdTime(Instant.parse("2016-11-02T10:36:00Z")).modifiedTime(Instant.parse("2016-11-02T10:36:00Z")).build();
jobs.put(job.getName(), job);
return job;
});
Mockito.when(schedulerService.modify(Mockito.isA(ModifyScheduledJobParams.class))).thenAnswer(invocation -> {
final ModifyScheduledJobParams params = invocation.getArgument(0);
final EditableScheduledJob editableJob = new EditableScheduledJob(jobs.get(params.getName()));
params.getEditor().edit(editableJob);
ScheduledJob modifiedJob = editableJob.build();
modifiedJob = ScheduledJob.create().name(modifiedJob.getName()).description(modifiedJob.getDescription()).calendar(modifiedJob.getCalendar()).enabled(modifiedJob.isEnabled()).descriptor(modifiedJob.getDescriptor()).config(modifiedJob.getConfig()).user(modifiedJob.getUser()).creator(modifiedJob.getCreator()).createdTime(modifiedJob.getCreatedTime()).modifiedTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifier(PrincipalKey.from("user:system:modifier")).build();
jobs.put(params.getName(), modifiedJob);
return modifiedJob;
});
Mockito.when(schedulerService.delete(Mockito.isA(ScheduledJobName.class))).thenAnswer(invocation -> {
final ScheduledJobName name = invocation.getArgument(0);
final ScheduledJob job = jobs.remove(name);
return job != null;
});
Mockito.when(schedulerService.get(Mockito.isA(ScheduledJobName.class))).thenAnswer(invocation -> jobs.get(invocation.getArgument(0)));
Mockito.when(schedulerService.list()).thenAnswer(invocation -> new ArrayList<>(jobs.values()));
}
use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.
the class SchedulerConfigImpl method parseProperties.
private CreateScheduledJobParams parseProperties(final ScheduledJobName name, final Configuration properties) {
final CreateScheduledJobParams.Builder job = CreateScheduledJobParams.create().name(name);
TimeZone timeZone = null;
String cronValue = null;
for (final Map.Entry<String, String> entry : properties.asMap().entrySet()) {
final String value = entry.getValue();
final Matcher matcher = JOB_PROPERTY_PATTERN.matcher(entry.getKey());
if (matcher.matches()) {
final String propertyName = matcher.group("property");
switch(propertyName) {
case ScheduledJobPropertyNames.DESCRIPTION:
job.description(value);
break;
case ScheduledJobPropertyNames.DESCRIPTOR:
job.descriptor(DescriptorKey.from(value));
break;
case ScheduledJobPropertyNames.ENABLED:
job.enabled(Boolean.parseBoolean(value));
break;
case ScheduledJobPropertyNames.USER:
job.user(PrincipalKey.from("user:" + value));
break;
case ScheduledJobPropertyNames.CONFIG:
try {
job.config(treeMarshallerService.marshal(MAPPER.readValue(value, HashMap.class)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
break;
case ScheduledJobPropertyNames.CALENDAR_TIMEZONE:
timeZone = TimeZone.getTimeZone(value);
break;
case "cron":
cronValue = value;
break;
default:
throw new IllegalArgumentException(String.format("[%s] is invalid job property.", propertyName));
}
}
}
final ScheduleCalendar calendar = calendarService.cron(cronValue, timeZone != null ? timeZone : TimeZone.getDefault());
return job.calendar(calendar).build();
}
use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.
the class ScheduleAuditLogSupportImplTest method testCreateJob.
@Test
public void testCreateJob() throws Exception {
final PropertyTree config = new PropertyTree();
config.addString("property", "value");
final CronCalendarImpl calendar = CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getTimeZone("GMT+5:30")).build();
final DescriptorKey descriptor = DescriptorKey.from("appKey:descriptorName");
final String jobDescription = "Job description";
final PrincipalKey userKey = PrincipalKey.from("user:system:user");
final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).descriptor(descriptor).description(jobDescription).calendar(calendar).config(config).user(userKey).enabled(true).build();
final ScheduledJob job = ScheduledJob.create().name(name).calendar(calendar).descriptor(descriptor).description(jobDescription).config(config).enabled(true).user(userKey).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:creator")).createdTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).lastRun(Instant.parse("2021-03-25T10:44:33.170079900Z")).lastTaskId(TaskId.from("task-id")).build();
context.runWith(() -> support.create(params, job));
executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
final ArgumentCaptor<LogAuditLogParams> argumentCaptor = ArgumentCaptor.forClass(LogAuditLogParams.class);
verify(auditLogService, times(1)).log(argumentCaptor.capture());
assertEquals(user.getKey(), argumentCaptor.getValue().getUser());
assertEquals("system.scheduler:master:job-name", argumentCaptor.getValue().getObjectUris().first().getValue());
assertEquals("com.enonic.xp.core-scheduler", argumentCaptor.getValue().getSource());
assertEquals(userKey.toString(), argumentCaptor.getValue().getData().getSet("result").getString("user"));
assertEquals(job.getCreator().toString(), argumentCaptor.getValue().getData().getSet("result").getString("creator"));
assertEquals(job.getModifier().toString(), argumentCaptor.getValue().getData().getSet("result").getString("modifier"));
assertEquals(job.getCreatedTime(), argumentCaptor.getValue().getData().getSet("result").getInstant("createdTime"));
assertEquals(job.getModifiedTime(), argumentCaptor.getValue().getData().getSet("result").getInstant("modifiedTime"));
assertEquals(name.getValue(), argumentCaptor.getValue().getData().getSet("result").getString("name"));
assertEquals(jobDescription, argumentCaptor.getValue().getData().getSet("result").getString("description"));
assertEquals(descriptor.toString(), argumentCaptor.getValue().getData().getSet("result").getString("descriptor"));
assertEquals(calendar.getType().name(), argumentCaptor.getValue().getData().getSet("result").getSet("calendar").getString("type"));
assertEquals(calendar.getCronValue(), argumentCaptor.getValue().getData().getSet("result").getSet("calendar").getString("value"));
assertEquals(calendar.getTimeZone().getID(), argumentCaptor.getValue().getData().getSet("result").getSet("calendar").getString("timezone"));
assertEquals(true, argumentCaptor.getValue().getData().getSet("result").getBoolean("enabled"));
assertEquals(config.getRoot(), argumentCaptor.getValue().getData().getSet("result").getSet("config"));
assertNull(argumentCaptor.getValue().getData().getSet("result").getInstant("lastRun"));
assertNull(argumentCaptor.getValue().getData().getSet("result").getString("lastTaskId"));
}
use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.
the class SchedulerServiceActivatorTest method initWithJob.
@Test
void initWithJob() {
final CreateScheduledJobParams jobParams = CreateScheduledJobParams.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("appKey:descriptorName")).calendar(calendarService.cron("* * * * *", TimeZone.getDefault())).config(new PropertyTree()).build();
mockNode(jobParams);
when(schedulerConfig.jobs()).thenReturn(Set.of(jobParams));
activator.activate(bundleContext);
verify(nodeService, times(1)).create(isA(CreateNodeParams.class));
}
use of com.enonic.xp.scheduler.CreateScheduledJobParams in project xp by enonic.
the class SchedulerServiceActivatorTest method rescheduleFinishedRescheduleTask.
@Test
void rescheduleFinishedRescheduleTask() {
final CreateScheduledJobParams jobParams = CreateScheduledJobParams.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("appKey:descriptorName")).calendar(calendarService.cron("* * * * *", TimeZone.getDefault())).config(new PropertyTree()).build();
mockNode(jobParams);
when(schedulerConfig.jobs()).thenReturn(Set.of(jobParams));
when(schedulerExecutorService.getAllFutures()).thenReturn(Set.of(RescheduleTask.NAME));
final ScheduledFuture<?> scheduledFuture = mock(ScheduledFuture.class);
when(scheduledFuture.isDone()).thenReturn(true);
final Optional<? extends ScheduledFuture<?>> optional = Optional.of(scheduledFuture);
doReturn(optional).when(schedulerExecutorService).get(RescheduleTask.NAME);
activator.activate(bundleContext);
verify(schedulerExecutorService, times(1)).scheduleAtFixedRate(isA(SchedulableTask.class), anyLong(), anyLong(), isA(TimeUnit.class));
}
Aggregations