Search in sources :

Example 1 with CreateScheduledJobParams

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()));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) EditableScheduledJob(com.enonic.xp.scheduler.EditableScheduledJob) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) EditableScheduledJob(com.enonic.xp.scheduler.EditableScheduledJob)

Example 2 with CreateScheduledJobParams

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();
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) TimeZone(java.util.TimeZone) Matcher(java.util.regex.Matcher) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with CreateScheduledJobParams

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"));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 4 with CreateScheduledJobParams

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));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) PropertyTree(com.enonic.xp.data.PropertyTree) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Example 5 with CreateScheduledJobParams

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));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) PropertyTree(com.enonic.xp.data.PropertyTree) SchedulableTask(com.enonic.xp.impl.scheduler.distributed.SchedulableTask) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Aggregations

CreateScheduledJobParams (com.enonic.xp.scheduler.CreateScheduledJobParams)17 Test (org.junit.jupiter.api.Test)13 PropertyTree (com.enonic.xp.data.PropertyTree)11 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)7 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)6 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)5 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 CronCalendar (com.enonic.xp.scheduler.CronCalendar)4 PrincipalKey (com.enonic.xp.security.PrincipalKey)4 HashMap (java.util.HashMap)4 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)3 ScheduleCalendar (com.enonic.xp.scheduler.ScheduleCalendar)3 SchedulableTask (com.enonic.xp.impl.scheduler.distributed.SchedulableTask)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Instant (java.time.Instant)2 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 TimeUnit (java.util.concurrent.TimeUnit)2 Matcher (java.util.regex.Matcher)2 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)1