job 单元测试增加校验调用

This commit is contained in:
neilz 2021-03-15 07:53:29 +08:00
parent d5fb438f59
commit 15391dd394
2 changed files with 41 additions and 20 deletions

View File

@ -34,5 +34,4 @@ public class InfJobBaseVO {
@ApiModelProperty(value = "监控超时时间", example = "1000") @ApiModelProperty(value = "监控超时时间", example = "1000")
private Integer monitorTimeout; private Integer monitorTimeout;
} }

View File

@ -14,6 +14,9 @@ import static cn.iocoder.dashboard.util.RandomUtils.randomString;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -84,6 +87,9 @@ public class InfJobServiceTest extends BaseDbUnitTest {
InfJobDO job = jobMapper.selectById(jobId); InfJobDO job = jobMapper.selectById(jobId);
assertPojoEquals(reqVO, job); assertPojoEquals(reqVO, job);
assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus()); assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus());
// 校验调用
verify(schedulerManager, times(1)).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()), eq(job.getCronExpression()),
eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
} }
@Test @Test
@ -120,8 +126,6 @@ public class InfJobServiceTest extends BaseDbUnitTest {
job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); job.setStatus(InfJobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job); fillJobMonitorTimeoutEmpty(job);
jobMapper.insert(job); jobMapper.insert(job);
schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(),
createReqVO.getRetryCount(), createReqVO.getRetryInterval());
// 准备参数 // 准备参数
InfJobUpdateReqVO updateReqVO = randomPojo(InfJobUpdateReqVO.class, o -> { InfJobUpdateReqVO updateReqVO = randomPojo(InfJobUpdateReqVO.class, o -> {
o.setId(job.getId()); o.setId(job.getId());
@ -133,6 +137,9 @@ public class InfJobServiceTest extends BaseDbUnitTest {
// 校验记录的属性是否正确 // 校验记录的属性是否正确
InfJobDO updateJob = jobMapper.selectById(updateReqVO.getId()); InfJobDO updateJob = jobMapper.selectById(updateReqVO.getId());
assertPojoEquals(updateReqVO, updateJob); assertPojoEquals(updateReqVO, updateJob);
// 校验调用
verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()),
eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
} }
@Test @Test
@ -154,36 +161,51 @@ public class InfJobServiceTest extends BaseDbUnitTest {
} }
@Test @Test
public void testUpdateJobStatus_success() throws SchedulerException { public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException {
// mock 数据 // mock 数据
InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *")); InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO); InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO);
job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); job.setStatus(InfJobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job); fillJobMonitorTimeoutEmpty(job);
jobMapper.insert(job); jobMapper.insert(job);
schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(),
createReqVO.getRetryCount(), createReqVO.getRetryInterval());
// 调用 // 调用
jobService.updateJobStatus(job.getId(), InfJobStatusEnum.STOP.getStatus()); jobService.updateJobStatus(job.getId(), InfJobStatusEnum.STOP.getStatus());
// 校验记录的属性是否正确 // 校验记录的属性是否正确
InfJobDO updateJob = jobMapper.selectById(job.getId()); InfJobDO updateJob = jobMapper.selectById(job.getId());
assertEquals(InfJobStatusEnum.STOP.getStatus(), updateJob.getStatus()); assertEquals(InfJobStatusEnum.STOP.getStatus(), updateJob.getStatus());
// 校验调用
verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName()));
}
@Test
public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException {
// mock 数据
InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO);
job.setStatus(InfJobStatusEnum.STOP.getStatus());
fillJobMonitorTimeoutEmpty(job);
jobMapper.insert(job);
// 调用
jobService.updateJobStatus(job.getId(), InfJobStatusEnum.NORMAL.getStatus());
// 校验记录的属性是否正确
InfJobDO updateJob = jobMapper.selectById(job.getId());
assertEquals(InfJobStatusEnum.NORMAL.getStatus(), updateJob.getStatus());
// 校验调用
verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName()));
} }
/**
* 页面"执行一次"按钮功能集成测试发现问题
* inf_job 表初始化任务 sysUserSessionTimeoutJob 点击报错是因为 Job 并没有添加到 Quartz
* 没有走 createJob scheduler.scheduleJob() 这一步报错任务找不到
* // FINISHED Quartz 相关表新增初始化任务 sysUserSessionTimeoutJob sql
*/
@Test @Test
public void testTriggerJob_success() throws SchedulerException { public void testTriggerJob_success() throws SchedulerException {
/** // mock 数据
* TODO 不知道是否要将 Quartz 相关 SQL 引入来做单元测试 InfJobCreateReqVO createReqVO = randomPojo(InfJobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
* 1schedulerManager.addJob sysUserSessionTimeoutJob InfJobDO job = InfJobConvert.INSTANCE.convert(createReqVO);
* 2schedulerManager.triggerJob job.setStatus(InfJobStatusEnum.NORMAL.getStatus());
* 3check inf_job_log fillJobMonitorTimeoutEmpty(job);
*/ jobMapper.insert(job);
// 调用
jobService.triggerJob(job.getId());
// 校验调用
verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()));
} }
@Test @Test
@ -194,12 +216,12 @@ public class InfJobServiceTest extends BaseDbUnitTest {
job.setStatus(InfJobStatusEnum.NORMAL.getStatus()); job.setStatus(InfJobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job); fillJobMonitorTimeoutEmpty(job);
jobMapper.insert(job); jobMapper.insert(job);
schedulerManager.addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(),
createReqVO.getRetryCount(), createReqVO.getRetryInterval());
// 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0 // 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0
jobService.deleteJob(job.getId()); jobService.deleteJob(job.getId());
// 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常 // 校验数据不存在了 WHERE id=? AND deleted=0 查询为空正常
assertNull(jobMapper.selectById(job.getId())); assertNull(jobMapper.selectById(job.getId()));
// 校验调用
verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName()));
} }
@Test @Test