Search in sources :

Example 11 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob in project xp by enonic.

the class RescheduleTaskTest method mockJobs.

private void mockJobs() {
    final ScheduledJob job1 = ScheduledJob.create().name(ScheduledJobName.from("task1")).calendar(CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getDefault()).build()).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task1")).config(new PropertyTree()).enabled(false).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")).build();
    final ScheduledJob job2 = ScheduledJob.create().name(ScheduledJobName.from("task2")).calendar(CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getDefault()).build()).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task2")).config(new PropertyTree()).enabled(true).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:modifier")).createdTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-25T10:44:53.170079900Z")).build();
    final ScheduledJob job3 = ScheduledJob.create().name(ScheduledJobName.from("task3")).calendar(OneTimeCalendarImpl.create().value(Instant.now().minus(Duration.of(1, ChronoUnit.SECONDS))).build()).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task3")).config(new PropertyTree()).enabled(true).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:creator")).createdTime(Instant.parse("2021-02-26T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-26T10:44:33.170079900Z")).build();
    final ScheduledJob job4 = ScheduledJob.create().name(ScheduledJobName.from("task4")).calendar(CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getDefault()).build()).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task4")).config(new PropertyTree()).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:modifier")).createdTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-25T11:44:33.170079900Z")).enabled(true).build();
    when(schedulerService.list()).thenReturn(List.of(job1, job2, job3, job4));
}
Also used : ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) PropertyTree(com.enonic.xp.data.PropertyTree)

Example 12 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob in project xp by enonic.

the class SchedulerServiceImplTest method get.

@Test
void get() {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final PropertyTree config = new PropertyTree();
    config.addString("string", "value");
    adminContext().callWith(() -> schedulerService.create(CreateScheduledJobParams.create().name(name).descriptor(descriptor).calendar(calendar).config(config).description("description").build()));
    final ScheduledJob scheduledJob = adminContext().callWith(() -> schedulerService.get(name));
    assertEquals(name, scheduledJob.getName());
    assertEquals(descriptor, scheduledJob.getDescriptor());
    assertEquals(calendar.getCronValue(), ((CronCalendar) scheduledJob.getCalendar()).getCronValue());
    assertEquals(calendar.getTimeZone(), ((CronCalendar) scheduledJob.getCalendar()).getTimeZone());
    assertEquals(config, scheduledJob.getConfig());
    assertEquals("description", scheduledJob.getDescription());
}
Also used : CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 13 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob in project xp by enonic.

the class SchedulerServiceImplTest method modify.

@Test
void modify() throws Exception {
    final ScheduledJobName name = ScheduledJobName.from("test");
    adminContext().callWith(() -> schedulerService.create(CreateScheduledJobParams.create().name(name).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task1")).calendar(calendarService.cron("* * * * *", TimeZone.getTimeZone("GMT+5:30"))).config(new PropertyTree()).build()));
    final Instant now = Instant.now();
    Thread.sleep(100);
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("User 1").email("user1@enonic.com").login("user1").build();
    final Context adminContext = adminContext();
    final Context userAdminContext = ContextBuilder.from(adminContext).authInfo(AuthenticationInfo.copyOf(adminContext.getAuthInfo()).user(user).build()).build();
    final ScheduledJob modifiedJob = userAdminContext.callWith(() -> schedulerService.modify(ModifyScheduledJobParams.create().name(name).editor(edit -> {
        edit.enabled = true;
        edit.description = "new description";
        edit.descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task2");
        edit.config.addString("string", "value");
        edit.user = PrincipalKey.from("user:provider:user");
        edit.calendar = calendarService.oneTime(Instant.parse("2021-02-25T10:44:33.170079900Z"));
    }).build()));
    assertEquals(name, modifiedJob.getName());
    assertEquals(DescriptorKey.from(ApplicationKey.from("com.enonic.app.test"), "task2"), modifiedJob.getDescriptor());
    assertEquals("new description", modifiedJob.getDescription());
    assertEquals("2021-02-25T10:44:33.170079900Z", ((OneTimeCalendar) modifiedJob.getCalendar()).getValue().toString());
    assertEquals(ScheduleCalendarType.ONE_TIME, modifiedJob.getCalendar().getType());
    assertEquals("value", modifiedJob.getConfig().getString("string"));
    assertEquals(PrincipalKey.from("user:provider:user"), modifiedJob.getUser());
    assertEquals(PrincipalKey.from("user:system:repo-test-user"), modifiedJob.getCreator());
    assertEquals(PrincipalKey.from("user:system:user1"), modifiedJob.getModifier());
    assertEquals(user.getKey(), modifiedJob.getModifier());
    assertTrue(now.isBefore(modifiedJob.getModifiedTime()));
    assertTrue(Instant.now().isAfter(modifiedJob.getModifiedTime()));
    assertTrue(Instant.now().isAfter(modifiedJob.getCreatedTime()));
}
Also used : Context(com.enonic.xp.context.Context) SchedulerExecutorService(com.enonic.xp.impl.scheduler.SchedulerExecutorService) BeforeEach(org.junit.jupiter.api.BeforeEach) IdProviderKey(com.enonic.xp.security.IdProviderKey) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) OneTimeCalendar(com.enonic.xp.scheduler.OneTimeCalendar) SchedulerConstants(com.enonic.xp.scheduler.SchedulerConstants) UpdateLastRunCommand(com.enonic.xp.impl.scheduler.UpdateLastRunCommand) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ContextAccessor(com.enonic.xp.context.ContextAccessor) SchedulerServiceImpl(com.enonic.xp.impl.scheduler.SchedulerServiceImpl) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) ContextBuilder(com.enonic.xp.context.ContextBuilder) User(com.enonic.xp.security.User) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TimeZone(java.util.TimeZone) Set(java.util.Set) SchedulerConfig(com.enonic.xp.impl.scheduler.SchedulerConfig) Instant(java.time.Instant) ScheduleAuditLogSupportImpl(com.enonic.xp.impl.scheduler.ScheduleAuditLogSupportImpl) TaskId(com.enonic.xp.task.TaskId) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest) CronCalendar(com.enonic.xp.scheduler.CronCalendar) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) Mockito.mock(org.mockito.Mockito.mock) Strictness(org.mockito.quality.Strictness) ScheduleAuditLogExecutorImpl(com.enonic.xp.impl.scheduler.ScheduleAuditLogExecutorImpl) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) ScheduleCalendarType(com.enonic.xp.scheduler.ScheduleCalendarType) AuditLogService(com.enonic.xp.audit.AuditLogService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PropertyTree(com.enonic.xp.data.PropertyTree) NodeAlreadyExistAtPathException(com.enonic.xp.node.NodeAlreadyExistAtPathException) CalendarServiceImpl(com.enonic.xp.impl.scheduler.CalendarServiceImpl) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ApplicationKey(com.enonic.xp.app.ApplicationKey) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) NodeAccessException(com.enonic.xp.node.NodeAccessException) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) User(com.enonic.xp.security.User) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Instant(java.time.Instant) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) OneTimeCalendar(com.enonic.xp.scheduler.OneTimeCalendar) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 14 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob in project xp by enonic.

the class SchedulerServiceImplTest method createOneTimeJob.

@Test
void createOneTimeJob() throws Exception {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing");
    final ScheduleCalendar calendar = calendarService.oneTime(Instant.parse("2021-02-25T10:44:33.170079900Z"));
    final PropertyTree config = new PropertyTree();
    config.addString("string", "value");
    final PrincipalKey user = PrincipalKey.from("user:system:user");
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).descriptor(descriptor).calendar(calendar).config(config).description("description").enabled(true).user(user).build();
    final Instant now = Instant.now();
    Thread.sleep(100);
    final ScheduledJob scheduledJob = adminContext().callWith(() -> schedulerService.create(params));
    assertEquals("2021-02-25T10:44:33.170079900Z", ((OneTimeCalendar) scheduledJob.getCalendar()).getValue().toString());
    assertEquals(ScheduleCalendarType.ONE_TIME, scheduledJob.getCalendar().getType());
    assertEquals("user:system:repo-test-user", scheduledJob.getCreator().toString());
    assertEquals("user:system:repo-test-user", scheduledJob.getModifier().toString());
    assertTrue(now.isBefore(scheduledJob.getModifiedTime()));
    assertTrue(now.isBefore(scheduledJob.getCreatedTime()));
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Instant(java.time.Instant) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) OneTimeCalendar(com.enonic.xp.scheduler.OneTimeCalendar) DescriptorKey(com.enonic.xp.page.DescriptorKey) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 15 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob in project xp by enonic.

the class SchedulerServiceImplTest method create.

@Test
void create() throws Exception {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final PropertyTree config = new PropertyTree();
    config.addString("string", "value");
    final PrincipalKey user = PrincipalKey.from("user:system:user");
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).descriptor(descriptor).calendar(calendar).config(config).description("description").enabled(true).user(user).build();
    final Instant now = Instant.now();
    Thread.sleep(100);
    final ScheduledJob scheduledJob = adminContext().callWith(() -> schedulerService.create(params));
    assertEquals(name, scheduledJob.getName());
    assertEquals(descriptor, scheduledJob.getDescriptor());
    assertEquals(calendar.getCronValue(), ((CronCalendar) scheduledJob.getCalendar()).getCronValue());
    assertEquals(calendar.getTimeZone(), ((CronCalendar) scheduledJob.getCalendar()).getTimeZone());
    assertEquals(config, scheduledJob.getConfig());
    assertEquals("description", scheduledJob.getDescription());
    assertEquals(user, scheduledJob.getUser());
    assertEquals("user:system:repo-test-user", scheduledJob.getModifier().toString());
    assertEquals("user:system:repo-test-user", scheduledJob.getCreator().toString());
    assertTrue(now.isBefore(scheduledJob.getCreatedTime()));
    assertTrue(now.isBefore(scheduledJob.getModifiedTime()));
    assertTrue(scheduledJob.isEnabled());
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) Instant(java.time.Instant) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)23 PropertyTree (com.enonic.xp.data.PropertyTree)14 Test (org.junit.jupiter.api.Test)13 DescriptorKey (com.enonic.xp.page.DescriptorKey)11 CreateScheduledJobParams (com.enonic.xp.scheduler.CreateScheduledJobParams)10 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)10 PrincipalKey (com.enonic.xp.security.PrincipalKey)9 EditableScheduledJob (com.enonic.xp.scheduler.EditableScheduledJob)7 Instant (java.time.Instant)7 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)6 CronCalendar (com.enonic.xp.scheduler.CronCalendar)6 ModifyScheduledJobParams (com.enonic.xp.scheduler.ModifyScheduledJobParams)6 ScheduleCalendar (com.enonic.xp.scheduler.ScheduleCalendar)6 Context (com.enonic.xp.context.Context)5 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)5 AuditLogService (com.enonic.xp.audit.AuditLogService)4 ContextBuilder (com.enonic.xp.context.ContextBuilder)4 OneTimeCalendar (com.enonic.xp.scheduler.OneTimeCalendar)4 IdProviderKey (com.enonic.xp.security.IdProviderKey)4 User (com.enonic.xp.security.User)4