Search in sources :

Example 11 with CreateScheduledJobParams

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

the class SchedulerServiceImplTest method createWithoutUser.

@Test
void createWithoutUser() {
    final ScheduledJobName name = ScheduledJobName.from("test");
    final CronCalendar calendar = calendarService.cron("* * * * *", TimeZone.getDefault());
    final CreateScheduledJobParams params = CreateScheduledJobParams.create().name(name).calendar(calendar).descriptor(DescriptorKey.from(ApplicationKey.from("com.enonic.app.features"), "landing")).config(new PropertyTree()).build();
    Context context = ContextAccessor.current();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.copyOf(AuthenticationInfo.unAuthenticated()).user(null).principals(context.getAuthInfo().getPrincipals()).principals(RoleKeys.ADMIN).build();
    context = ContextBuilder.from(context).authInfo(authenticationInfo).build();
    final ScheduledJob scheduledJob = context.callWith(() -> schedulerService.create(params));
    assertEquals(User.ANONYMOUS.getKey(), scheduledJob.getCreator());
    assertEquals(User.ANONYMOUS.getKey(), scheduledJob.getModifier());
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) Context(com.enonic.xp.context.Context) CronCalendar(com.enonic.xp.scheduler.CronCalendar) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 12 with CreateScheduledJobParams

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

the class SchedulerServiceActivatorTest method initWithExistJob.

@Test
void initWithExistJob() {
    final CreateScheduledJobParams jobParams = CreateScheduledJobParams.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("appKey:descriptorName")).calendar(calendarService.cron("* * * * *", TimeZone.getDefault())).config(new PropertyTree()).build();
    when(schedulerConfig.jobs()).thenReturn(Set.of(jobParams));
    when(nodeService.create(isA(CreateNodeParams.class))).thenThrow(new NodeAlreadyExistAtPathException(NodePath.create(NodePath.ROOT, jobParams.getName().getValue()).build(), RepositoryId.from("repo"), Branch.from("branch")));
    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) NodeAlreadyExistAtPathException(com.enonic.xp.node.NodeAlreadyExistAtPathException) CreateNodeParams(com.enonic.xp.node.CreateNodeParams) Test(org.junit.jupiter.api.Test)

Example 13 with CreateScheduledJobParams

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

the class SchedulerServiceActivatorTest method initWithInvalidJob.

@Test
void initWithInvalidJob() {
    final CreateScheduledJobParams jobParams = CreateScheduledJobParams.create().name(ScheduledJobName.from("name")).descriptor(DescriptorKey.from("appKey:descriptorName")).calendar(calendarService.cron("* * * * *", TimeZone.getDefault())).config(new PropertyTree()).build();
    when(schedulerConfig.jobs()).thenReturn(Set.of(jobParams));
    when(nodeService.create(isA(CreateNodeParams.class))).thenThrow(new RuntimeException());
    assertThrows(RuntimeException.class, () -> activator.activate(bundleContext));
}
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 14 with CreateScheduledJobParams

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

the class SchedulerServiceActivatorTest method initWithAlreadyScheduledRescheduleTask.

@Test
void initWithAlreadyScheduledRescheduleTask() {
    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(false);
    final Optional<? extends ScheduledFuture<?>> optional = Optional.of(scheduledFuture);
    doReturn(optional).when(schedulerExecutorService).get(RescheduleTask.NAME);
    activator.activate(bundleContext);
    verify(schedulerExecutorService, never()).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)

Example 15 with CreateScheduledJobParams

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

the class SchedulerConfigImplTest method cronJob.

@Test
void cronJob() {
    Map<String, String> properties = new HashMap<>();
    properties.put("init-job.landing1.enabled", "true");
    properties.put("init-job.landing1.description", "landing1 description");
    properties.put("init-job.landing1.descriptor", "com.enonic.app.features:landing");
    properties.put("init-job.landing1.user", "user:system:user");
    properties.put("init-job.landing1.config", "{\"a\":\"valueA\"}");
    properties.put("init-job.landing1.cron", "* * * * *");
    properties.put("init-job.landing1.timezone", "GMT+5:30");
    schedulerConfig = new SchedulerConfigImpl(properties, propertyTreeMarshallerService, calendarService);
    final Set<CreateScheduledJobParams> jobs = schedulerConfig.jobs();
    assertEquals(2, jobs.size());
    final CreateScheduledJobParams job = jobs.stream().filter(params -> params.getName().getValue().equals("landing1")).findAny().orElseThrow(RuntimeException::new);
    assertTrue(job.isEnabled());
    assertEquals(PrincipalKey.from("user:system:user"), job.getUser());
    assertEquals(DescriptorKey.from("com.enonic.app.features:landing"), job.getDescriptor());
    assertEquals("landing1 description", job.getDescription());
    assertEquals("valueA", job.getConfig().getString("a"));
    assertEquals(ScheduleCalendarType.CRON, job.getCalendar().getType());
    assertEquals("* * * * *", ((CronCalendar) job.getCalendar()).getCronValue());
    assertEquals(TimeZone.getTimeZone("GMT+5:30"), ((CronCalendar) job.getCalendar()).getTimeZone());
}
Also used : CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) HashMap(java.util.HashMap) 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