Search in sources :

Example 6 with ScheduledJob

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

the class EditableScheduledJobTest method testChanged.

@Test
public void testChanged() {
    final ScheduledJob source = ScheduledJob.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("app:key")).calendar(mock(ScheduleCalendar.class)).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();
    final PrincipalKey user = PrincipalKey.ofUser(IdProviderKey.createDefault(), "user");
    final DescriptorKey descriptor = DescriptorKey.from(ApplicationKey.BASE, "descriptor");
    final PropertyTree config = new PropertyTree();
    config.addString("property", "value");
    final String description = "description";
    final EditableScheduledJob editable = new EditableScheduledJob(source);
    editable.config = config;
    editable.user = user;
    editable.descriptor = descriptor;
    editable.description = description;
    editable.calendar = mock(ScheduleCalendar.class);
    editable.enabled = true;
    final ScheduledJob target = editable.build();
    assertEquals(source.getName(), target.getName());
    assertEquals(config, target.getConfig());
    assertEquals(user, target.getUser());
    assertEquals(descriptor, target.getDescriptor());
    assertEquals(description, target.getDescription());
    assertTrue(target.isEnabled());
}
Also used : ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) EditableScheduledJob(com.enonic.xp.scheduler.EditableScheduledJob) PropertyTree(com.enonic.xp.data.PropertyTree) DescriptorKey(com.enonic.xp.page.DescriptorKey) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) PrincipalKey(com.enonic.xp.security.PrincipalKey) EditableScheduledJob(com.enonic.xp.scheduler.EditableScheduledJob) Test(org.junit.jupiter.api.Test)

Example 7 with ScheduledJob

use of com.enonic.xp.scheduler.ScheduledJob 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 8 with ScheduledJob

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

the class ScheduleAuditLogSupportImplTest method testUpdateContent.

@Test
public void testUpdateContent() 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 OneTimeCalendarImpl oneTimeCalendar = OneTimeCalendarImpl.create().value(Instant.parse("2021-04-25T10:44:33.170079900Z")).build();
    final ModifyScheduledJobParams params = ModifyScheduledJobParams.create().name(name).editor(edit -> edit.calendar = oneTimeCalendar).build();
    final ScheduledJob job = ScheduledJob.create().name(name).calendar(oneTimeCalendar).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")).build();
    context.runWith(() -> support.modify(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().toString(), argumentCaptor.getValue().getData().getSet("params").getString("modifier"));
    assertEquals("2021-04-25T10:44:33.170079900Z", argumentCaptor.getValue().getData().getSet("result").getSet("calendar").getString("value"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) IdProviderKey(com.enonic.xp.security.IdProviderKey) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ArgumentCaptor(org.mockito.ArgumentCaptor) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) AuditLogService(com.enonic.xp.audit.AuditLogService) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) User(com.enonic.xp.security.User) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TimeZone(java.util.TimeZone) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.times(org.mockito.Mockito.times) Instant(java.time.Instant) Executors(java.util.concurrent.Executors) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) Context(com.enonic.xp.context.Context) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) 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 9 with ScheduledJob

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

the class ScheduleAuditLogSupportImplTest method testUpdateWithoutCreator.

@Test
public // jobs were produced with an empty `creator` and `createdTime` fields from 7.7.0 to 7.7.2
void testUpdateWithoutCreator() 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 OneTimeCalendarImpl oneTimeCalendar = OneTimeCalendarImpl.create().value(Instant.parse("2021-04-25T10:44:33.170079900Z")).build();
    final ModifyScheduledJobParams params = ModifyScheduledJobParams.create().name(name).editor(edit -> edit.calendar = oneTimeCalendar).build();
    final ScheduledJob job = ScheduledJob.create().name(name).calendar(oneTimeCalendar).descriptor(descriptor).description(jobDescription).config(config).enabled(true).user(userKey).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();
    context.runWith(() -> support.modify(params, job));
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
    final ArgumentCaptor<LogAuditLogParams> argumentCaptor = ArgumentCaptor.forClass(LogAuditLogParams.class);
    verify(auditLogService, times(1)).log(argumentCaptor.capture());
    assertNull(argumentCaptor.getValue().getData().getSet("params").getString("creator"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) IdProviderKey(com.enonic.xp.security.IdProviderKey) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ArgumentCaptor(org.mockito.ArgumentCaptor) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) AuditLogService(com.enonic.xp.audit.AuditLogService) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) User(com.enonic.xp.security.User) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TimeZone(java.util.TimeZone) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.times(org.mockito.Mockito.times) Instant(java.time.Instant) Executors(java.util.concurrent.Executors) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) Context(com.enonic.xp.context.Context) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) 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 10 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)

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