infra:完善 job 的单元测试

This commit is contained in:
YunaiV 2023-02-04 08:42:29 +08:00
parent 3070392ecd
commit ebf441ef13
2 changed files with 156 additions and 120 deletions

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.infra.service.job;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogExportReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.log.JobLogPageReqVO;
@ -12,18 +11,20 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static java.util.Collections.singleton;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@Import(JobLogServiceImpl.class)
public class JobLogServiceTest extends BaseDbUnitTest {
public class JobLogServiceImplTest extends BaseDbUnitTest {
@Resource
private JobLogServiceImpl jobLogService;
@ -31,66 +32,103 @@ public class JobLogServiceTest extends BaseDbUnitTest {
private JobLogMapper jobLogMapper;
@Test
public void testCreateJobLog_success() {
public void testCreateJobLog() {
// 准备参数
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
o.setExecuteIndex(1);
});
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
// 调用
Long jobLogId = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(), reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
Long id = jobLogService.createJobLog(reqVO.getJobId(), reqVO.getBeginTime(),
reqVO.getHandlerName(), reqVO.getHandlerParam(), reqVO.getExecuteIndex());
// 断言
assertNotNull(jobLogId);
assertNotNull(id);
// 校验记录的属性是否正确
JobLogDO job = jobLogMapper.selectById(jobLogId);
JobLogDO job = jobLogMapper.selectById(id);
assertEquals(JobLogStatusEnum.RUNNING.getStatus(), job.getStatus());
}
@Test
public void testUpdateJobLogResultAsync_success() {
// 准备参数
JobLogDO reqVO = randomPojo(JobLogDO.class, o -> {
// mock 数据
JobLogDO log = randomPojo(JobLogDO.class, o -> {
o.setExecuteIndex(1);
o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
});
JobLogDO log = JobLogDO.builder().jobId(reqVO.getJobId()).handlerName(reqVO.getHandlerName()).handlerParam(reqVO.getHandlerParam()).executeIndex(reqVO.getExecuteIndex())
.beginTime(reqVO.getBeginTime()).status(JobLogStatusEnum.RUNNING.getStatus()).build();
jobLogMapper.insert(log);
// 调用
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), true,reqVO.getResult());
// 校验记录的属性是否正确
JobLogDO job = jobLogMapper.selectById(log.getId());
assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), job.getStatus());
// 准备参数
Long logId = log.getId();
LocalDateTime endTime = randomLocalDateTime();
Integer duration = randomInteger();
boolean success = true;
String result = randomString();
// 调用
jobLogService.updateJobLogResultAsync(log.getId(), reqVO.getBeginTime(), reqVO.getDuration(), false,reqVO.getResult());
jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
// 校验记录的属性是否正确
JobLogDO job2 = jobLogMapper.selectById(log.getId());
assertEquals(JobLogStatusEnum.FAILURE.getStatus(), job2.getStatus());
JobLogDO dbLog = jobLogMapper.selectById(log.getId());
assertEquals(endTime, dbLog.getEndTime());
assertEquals(duration, dbLog.getDuration());
assertEquals(JobLogStatusEnum.SUCCESS.getStatus(), dbLog.getStatus());
assertEquals(result, dbLog.getResult());
}
@Test
public void testGetJobLogListByIds_success() {
public void testUpdateJobLogResultAsync_failure() {
// mock 数据
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
JobLogDO log = randomPojo(JobLogDO.class, o -> {
o.setExecuteIndex(1);
o.setStatus(randomEle(JobLogStatusEnum.values()).getStatus()); // 保证 status 的范围
o.setStatus(JobLogStatusEnum.RUNNING.getStatus());
});
JobLogDO cloneJobLog = ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()));
jobLogMapper.insert(log);
// 准备参数
Long logId = log.getId();
LocalDateTime endTime = randomLocalDateTime();
Integer duration = randomInteger();
boolean success = false;
String result = randomString();
// 调用
jobLogService.updateJobLogResultAsync(logId, endTime, duration, success, result);
// 校验记录的属性是否正确
JobLogDO dbLog = jobLogMapper.selectById(log.getId());
assertEquals(endTime, dbLog.getEndTime());
assertEquals(duration, dbLog.getDuration());
assertEquals(JobLogStatusEnum.FAILURE.getStatus(), dbLog.getStatus());
assertEquals(result, dbLog.getResult());
}
@Test
public void testGetJobLog() {
// mock 数据
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
jobLogMapper.insert(dbJobLog);
// 准备参数
Long id = dbJobLog.getId();
// 调用
JobLogDO jobLog = jobLogService.getJobLog(id);
// 断言
assertPojoEquals(dbJobLog, jobLog);
}
@Test
public void testGetJobLogList() {
// mock 数据
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> o.setExecuteIndex(1));
jobLogMapper.insert(dbJobLog);
// 测试 handlerName 不匹配
jobLogMapper.insert(cloneJobLog);
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> {}));
// 准备参数
ArrayList ids = new ArrayList<>();
ids.add(dbJobLog.getId());
ids.add(cloneJobLog.getId());
Collection<Long> ids = singleton(dbJobLog.getId());
// 调用
List<JobLogDO> list = jobLogService.getJobLogList(ids);
// 断言
assertEquals(2, list.size());
assertEquals(1, list.size());
assertPojoEquals(dbJobLog, list.get(0));
}
@Test
public void testGetJobPage_success() {
public void testGetJobPage() {
// mock 数据
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
o.setExecuteIndex(1);
@ -101,15 +139,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
});
jobLogMapper.insert(dbJobLog);
// 测试 jobId 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
// 测试 handlerName 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
// 测试 beginTime 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
// 测试 endTime 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
// 测试 status 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
// 准备参数
JobLogPageReqVO reqVo = new JobLogPageReqVO();
reqVo.setJobId(dbJobLog.getJobId());
@ -117,6 +155,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
reqVo.setBeginTime(dbJobLog.getBeginTime());
reqVo.setEndTime(dbJobLog.getEndTime());
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
// 调用
PageResult<JobLogDO> pageResult = jobLogService.getJobLogPage(reqVo);
// 断言
@ -126,7 +165,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
}
@Test
public void testGetJobListForExport_success() {
public void testGetJobList_export() {
// mock 数据
JobLogDO dbJobLog = randomPojo(JobLogDO.class, o -> {
o.setExecuteIndex(1);
@ -137,15 +176,15 @@ public class JobLogServiceTest extends BaseDbUnitTest {
});
jobLogMapper.insert(dbJobLog);
// 测试 jobId 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId())));
// 测试 handlerName 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())));
// 测试 beginTime 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7))));
// 测试 endTime 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9))));
// 测试 status 不匹配
jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
jobLogMapper.insert(cloneIgnoreId(dbJobLog, o -> o.setStatus(JobLogStatusEnum.FAILURE.getStatus())));
// 准备参数
JobLogExportReqVO reqVo = new JobLogExportReqVO();
reqVo.setJobId(dbJobLog.getJobId());
@ -153,6 +192,7 @@ public class JobLogServiceTest extends BaseDbUnitTest {
reqVo.setBeginTime(dbJobLog.getBeginTime());
reqVo.setEndTime(dbJobLog.getEndTime());
reqVo.setStatus(JobLogStatusEnum.SUCCESS.getStatus());
// 调用
List<JobLogDO> list = jobLogService.getJobLogList(reqVo);
// 断言

View File

@ -1,14 +1,12 @@
package cn.iocoder.yudao.module.infra.service.job;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.quartz.core.scheduler.SchedulerManager;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobCreateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobExportReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.job.vo.job.JobUpdateReqVO;
import cn.iocoder.yudao.module.infra.convert.job.JobConvert;
import cn.iocoder.yudao.module.infra.dal.dataobject.job.JobDO;
import cn.iocoder.yudao.module.infra.dal.mysql.job.JobMapper;
import cn.iocoder.yudao.module.infra.enums.job.JobStatusEnum;
@ -18,22 +16,23 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@Import(JobServiceImpl.class)
public class JobServiceTest extends BaseDbUnitTest {
public class JobServiceImplTest extends BaseDbUnitTest {
@Resource
private JobServiceImpl jobService;
@ -46,6 +45,7 @@ public class JobServiceTest extends BaseDbUnitTest {
public void testCreateJob_cronExpressionValid() {
// 准备参数Cron 表达式为 String 类型默认随机字符串
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class);
// 调用并断言异常
assertServiceException(() -> jobService.createJob(reqVO), JOB_CRON_EXPRESSION_VALID);
}
@ -54,6 +54,7 @@ public class JobServiceTest extends BaseDbUnitTest {
public void testCreateJob_jobHandlerExists() throws SchedulerException {
// 准备参数 指定 Cron 表达式
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
// 调用
jobService.createJob(reqVO);
// 调用并断言异常
@ -64,6 +65,7 @@ public class JobServiceTest extends BaseDbUnitTest {
public void testCreateJob_success() throws SchedulerException {
// 准备参数 指定 Cron 表达式
JobCreateReqVO reqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
// 调用
Long jobId = jobService.createJob(reqVO);
// 断言
@ -73,14 +75,15 @@ public class JobServiceTest extends BaseDbUnitTest {
assertPojoEquals(reqVO, job);
assertEquals(JobStatusEnum.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()));
verify(schedulerManager).addJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()),
eq(job.getCronExpression()), eq(reqVO.getRetryCount()), eq(reqVO.getRetryInterval()));
}
@Test
public void testUpdateJob_jobNotExists(){
// 准备参数
JobUpdateReqVO reqVO = randomPojo(JobUpdateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
// 调用并断言异常
assertServiceException(() -> jobService.updateJob(reqVO), JOB_NOT_EXISTS);
}
@ -88,150 +91,136 @@ public class JobServiceTest extends BaseDbUnitTest {
@Test
public void testUpdateJob_onlyNormalStatus(){
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.INIT.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.INIT.getStatus()));
jobMapper.insert(job);
// 准备参数
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
o.setId(job.getId());
o.setName(createReqVO.getName());
o.setCronExpression(createReqVO.getCronExpression());
o.setCronExpression("0 0/1 * * * ? *");
});
// 调用并断言异常
assertServiceException(() -> jobService.updateJob(updateReqVO), JOB_UPDATE_ONLY_NORMAL_STATUS);
assertServiceException(() -> jobService.updateJob(updateReqVO),
JOB_UPDATE_ONLY_NORMAL_STATUS);
}
@Test
public void testUpdateJob_success() throws SchedulerException {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
jobMapper.insert(job);
// 准备参数
JobUpdateReqVO updateReqVO = randomPojo(JobUpdateReqVO.class, o -> {
o.setId(job.getId());
o.setName(createReqVO.getName());
o.setCronExpression(createReqVO.getCronExpression());
o.setCronExpression("0 0/1 * * * ? *");
});
// 调用
jobService.updateJob(updateReqVO);
// 校验记录的属性是否正确
JobDO updateJob = jobMapper.selectById(updateReqVO.getId());
assertPojoEquals(updateReqVO, updateJob);
// 校验调用
verify(schedulerManager, times(1)).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()), eq(updateReqVO.getCronExpression()),
eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
verify(schedulerManager).updateJob(eq(job.getHandlerName()), eq(updateReqVO.getHandlerParam()),
eq(updateReqVO.getCronExpression()), eq(updateReqVO.getRetryCount()), eq(updateReqVO.getRetryInterval()));
}
@Test
public void testUpdateJobStatus_changeStatusInvalid() {
// 调用并断言异常
assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()), JOB_CHANGE_STATUS_INVALID);
assertServiceException(() -> jobService.updateJobStatus(1L, JobStatusEnum.INIT.getStatus()),
JOB_CHANGE_STATUS_INVALID);
}
@Test
public void testUpdateJobStatus_changeStatusEquals() {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
jobMapper.insert(job);
// 调用并断言异常
assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()), JOB_CHANGE_STATUS_EQUALS);
assertServiceException(() -> jobService.updateJobStatus(job.getId(), job.getStatus()),
JOB_CHANGE_STATUS_EQUALS);
}
@Test
public void testUpdateJobStatus_NormalToStop_success() throws SchedulerException {
public void testUpdateJobStatus_stopSuccess() throws SchedulerException {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus()));
jobMapper.insert(job);
// 调用
jobService.updateJobStatus(job.getId(), JobStatusEnum.STOP.getStatus());
// 校验记录的属性是否正确
JobDO updateJob = jobMapper.selectById(job.getId());
assertEquals(JobStatusEnum.STOP.getStatus(), updateJob.getStatus());
JobDO dbJob = jobMapper.selectById(job.getId());
assertEquals(JobStatusEnum.STOP.getStatus(), dbJob.getStatus());
// 校验调用
verify(schedulerManager, times(1)).pauseJob(eq(job.getHandlerName()));
verify(schedulerManager).pauseJob(eq(job.getHandlerName()));
}
@Test
public void testUpdateJobStatus_StopToNormal_success() throws SchedulerException {
public void testUpdateJobStatus_normalSuccess() throws SchedulerException {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.STOP.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class, o -> o.setStatus(JobStatusEnum.STOP.getStatus()));
jobMapper.insert(job);
// 调用
jobService.updateJobStatus(job.getId(), JobStatusEnum.NORMAL.getStatus());
// 校验记录的属性是否正确
JobDO updateJob = jobMapper.selectById(job.getId());
assertEquals(JobStatusEnum.NORMAL.getStatus(), updateJob.getStatus());
JobDO dbJob = jobMapper.selectById(job.getId());
assertEquals(JobStatusEnum.NORMAL.getStatus(), dbJob.getStatus());
// 校验调用
verify(schedulerManager, times(1)).resumeJob(eq(job.getHandlerName()));
verify(schedulerManager).resumeJob(eq(job.getHandlerName()));
}
@Test
public void testTriggerJob_success() throws SchedulerException {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class);
jobMapper.insert(job);
// 调用
jobService.triggerJob(job.getId());
// 校验调用
verify(schedulerManager, times(1)).triggerJob(eq(job.getId()), eq(job.getHandlerName()), eq(job.getHandlerParam()));
verify(schedulerManager).triggerJob(eq(job.getId()),
eq(job.getHandlerName()), eq(job.getHandlerParam()));
}
@Test
public void testDeleteJob_success() throws SchedulerException {
// mock 数据
JobCreateReqVO createReqVO = randomPojo(JobCreateReqVO.class, o -> o.setCronExpression("0 0/1 * * * ? *"));
JobDO job = JobConvert.INSTANCE.convert(createReqVO);
job.setStatus(JobStatusEnum.NORMAL.getStatus());
fillJobMonitorTimeoutEmpty(job);
JobDO job = randomPojo(JobDO.class);
jobMapper.insert(job);
// 调用 UPDATE inf_job SET deleted=1 WHERE id=? AND deleted=0
// 调用
jobService.deleteJob(job.getId());
// 校验数据不存在 WHERE id=? AND deleted=0 查询为空正常
// 校验不存在
assertNull(jobMapper.selectById(job.getId()));
// 校验调用
verify(schedulerManager, times(1)).deleteJob(eq(job.getHandlerName()));
verify(schedulerManager).deleteJob(eq(job.getHandlerName()));
}
@Test
public void testGetJobListByIds_success() {
public void testGetJobList() {
// mock 数据
JobDO dbJob = randomPojo(JobDO.class, o -> {
o.setStatus(randomEle(JobStatusEnum.values()).getStatus()); // 保证 status 的范围
});
JobDO cloneJob = ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()));
jobMapper.insert(dbJob);
// 测试 handlerName 不匹配
jobMapper.insert(cloneJob);
// 测试 id 不匹配
jobMapper.insert(cloneIgnoreId(dbJob, o -> {}));
// 准备参数
ArrayList<Long> ids = new ArrayList<>();
ids.add(dbJob.getId());
ids.add(cloneJob.getId());
Collection<Long> ids = singletonList(dbJob.getId());
// 调用
List<JobDO> list = jobService.getJobList(ids);
// 断言
assertEquals(2, list.size());
assertEquals(1, list.size());
assertPojoEquals(dbJob, list.get(0));
}
@Test
public void testGetJobPage_success() {
public void testGetJobPage() {
// mock 数据
JobDO dbJob = randomPojo(JobDO.class, o -> {
o.setName("定时任务测试");
@ -240,16 +229,17 @@ public class JobServiceTest extends BaseDbUnitTest {
});
jobMapper.insert(dbJob);
// 测试 name 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
// 测试 status 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
// 测试 handlerName 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
// 准备参数
JobPageReqVO reqVo = new JobPageReqVO();
reqVo.setName("定时");
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
reqVo.setHandlerName("单元");
// 调用
PageResult<JobDO> pageResult = jobService.getJobPage(reqVo);
// 断言
@ -259,7 +249,7 @@ public class JobServiceTest extends BaseDbUnitTest {
}
@Test
public void testGetJobListForExport_success() {
public void testGetJobList_export() {
// mock 数据
JobDO dbJob = randomPojo(JobDO.class, o -> {
o.setName("定时任务测试");
@ -268,16 +258,17 @@ public class JobServiceTest extends BaseDbUnitTest {
});
jobMapper.insert(dbJob);
// 测试 name 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆")));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setName("土豆")));
// 测试 status 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setStatus(JobStatusEnum.NORMAL.getStatus())));
// 测试 handlerName 不匹配
jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
jobMapper.insert(cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())));
// 准备参数
JobExportReqVO reqVo = new JobExportReqVO();
reqVo.setName("定时");
reqVo.setStatus(JobStatusEnum.INIT.getStatus());
reqVo.setHandlerName("单元");
// 调用
List<JobDO> list = jobService.getJobList(reqVo);
// 断言
@ -285,10 +276,15 @@ public class JobServiceTest extends BaseDbUnitTest {
assertPojoEquals(dbJob, list.get(0));
}
private static void fillJobMonitorTimeoutEmpty(JobDO job) {
if (job.getMonitorTimeout() == null) {
job.setMonitorTimeout(0);
}
@Test
public void testGetJob() {
// mock 数据
JobDO dbJob = randomPojo(JobDO.class);
jobMapper.insert(dbJob);
// 调用
JobDO job = jobService.getJob(dbJob.getId());
// 断言
assertPojoEquals(dbJob, job);
}
}