mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-24 13:44:33 +00:00
renamed template method to not rely on overloading
added test for scheduler service bean
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
package dev.sheldan.abstracto.scheduling.factory;
|
||||
|
||||
import dev.sheldan.abstracto.scheduling.model.database.SchedulerJob;
|
||||
import dev.sheldan.abstracto.scheduling.service.SchedulerServiceBean;
|
||||
import dev.sheldan.abstracto.scheduling.service.management.SchedulerJobManagementServiceBean;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.quartz.*;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
|
||||
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
|
||||
import static org.quartz.TriggerBuilder.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SchedulerServiceBeanTest {
|
||||
|
||||
public static final String JOB_CLASS = "dev.sheldan.abstracto.scheduling.factory.TestJob";
|
||||
public static final String JOB_NAME = "jobName";
|
||||
public static final String GROUP_NAME = "groupName";
|
||||
|
||||
@InjectMocks
|
||||
private SchedulerServiceBean classToTest;
|
||||
|
||||
@Mock
|
||||
private SchedulerFactoryBean schedulerFactoryBean;
|
||||
|
||||
@Mock
|
||||
private ApplicationContext context;
|
||||
|
||||
@Mock
|
||||
private QuartzConfigFactory scheduleCreator;
|
||||
|
||||
@Mock
|
||||
private SchedulerJobManagementServiceBean schedulerJobManagementServiceBean;
|
||||
|
||||
@Mock
|
||||
private Scheduler scheduler;
|
||||
|
||||
@Before
|
||||
public void setup() throws ClassNotFoundException {
|
||||
when(schedulerFactoryBean.getScheduler()).thenReturn(scheduler);
|
||||
when(scheduleCreator.createJob(eq(TestJob.class), anyBoolean(), any(), anyString(), anyString(), anyBoolean())).thenReturn(getJobDetail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartingAllJobs() throws SchedulerException {
|
||||
when(schedulerJobManagementServiceBean.findAll()).thenReturn(allCronJobsActive());
|
||||
classToTest.startScheduledJobs();
|
||||
verify(scheduler, times(2)).checkExists(eq(new JobKey("jobName", "groupName")));
|
||||
verify(scheduler, times(2)).addJob(any(JobDetail.class), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartSomeJobs() throws SchedulerException {
|
||||
when(schedulerJobManagementServiceBean.findAll()).thenReturn(someCronJobsActive());
|
||||
classToTest.startScheduledJobs();
|
||||
verify(scheduler, times(1)).checkExists(eq(new JobKey("jobName", "groupName")));
|
||||
verify(scheduler, times(1)).addJob(any(JobDetail.class), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidClass() throws SchedulerException {
|
||||
when(schedulerJobManagementServiceBean.findAll()).thenReturn(Arrays.asList(SchedulerJob.builder().active(true).cronExpression("*").clazz("invalidJob").groupName(GROUP_NAME).name(JOB_NAME).build()));
|
||||
classToTest.startScheduledJobs();
|
||||
verify(scheduler, times(0)).checkExists(eq(new JobKey("jobName", "groupName")));
|
||||
verify(scheduler, times(0)).addJob(any(JobDetail.class), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scheduleSingleJob() throws SchedulerException {
|
||||
classToTest.scheduleJob(activeJobCronJob());
|
||||
verify(scheduler, times(1)).checkExists(eq(new JobKey("jobName", "groupName")));
|
||||
verify(scheduler, times(1)).addJob(any(JobDetail.class), eq(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unScheduleSingleJob() throws SchedulerException {
|
||||
classToTest.unScheduleJob(JOB_NAME);
|
||||
verify(scheduler, times(1)).unscheduleJob(any(TriggerKey.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeJobOnce() throws SchedulerException {
|
||||
when(scheduleCreator.createOnceOnlyTriggerForJob(eq(JOB_NAME), eq(GROUP_NAME), any(Date.class), any(JobDataMap.class))).thenReturn(Mockito.mock(Trigger.class));
|
||||
classToTest.executeJobWithParametersOnce(JOB_NAME, GROUP_NAME, new JobDataMap(), new Date());
|
||||
verify(scheduler, times(1)).scheduleJob(any(Trigger.class));
|
||||
}
|
||||
|
||||
private List<SchedulerJob> allCronJobsActive() {
|
||||
List<SchedulerJob> jobs = new ArrayList<>();
|
||||
jobs.add(activeJobCronJob());
|
||||
jobs.add(activeJobCronJob());
|
||||
return jobs;
|
||||
}
|
||||
|
||||
private List<SchedulerJob> someCronJobsActive() {
|
||||
List<SchedulerJob> jobs = new ArrayList<>();
|
||||
jobs.add(activeJobCronJob());
|
||||
jobs.add(inactiveCronJob());
|
||||
return jobs;
|
||||
}
|
||||
|
||||
private SchedulerJob activeJobCronJob() {
|
||||
return SchedulerJob.builder().active(true).cronExpression("*").clazz(JOB_CLASS).groupName(GROUP_NAME).name(JOB_NAME).build();
|
||||
}
|
||||
|
||||
private SchedulerJob inactiveCronJob() {
|
||||
return SchedulerJob.builder().active(false).cronExpression("*").clazz(JOB_CLASS).groupName(GROUP_NAME).name(JOB_NAME).build();
|
||||
}
|
||||
|
||||
private JobDetail getJobDetail() throws ClassNotFoundException {
|
||||
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
|
||||
Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(JOB_CLASS);
|
||||
factoryBean.setJobClass(jobClass);
|
||||
factoryBean.setDurability(true);
|
||||
factoryBean.setApplicationContext(context);
|
||||
factoryBean.setRequestsRecovery(false);
|
||||
factoryBean.setName(JOB_NAME);
|
||||
factoryBean.setGroup(GROUP_NAME);
|
||||
|
||||
JobDataMap jobDataMap = new JobDataMap();
|
||||
jobDataMap.put(JOB_NAME + GROUP_NAME, jobClass.getName());
|
||||
factoryBean.setJobDataMap(jobDataMap);
|
||||
|
||||
factoryBean.afterPropertiesSet();
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package dev.sheldan.abstracto.scheduling.factory;
|
||||
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
public class TestJob extends QuartzJobBean {
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user