From 3b6359caa04dd7a9483306d0539fc055dadf829b Mon Sep 17 00:00:00 2001 From: neilz Date: Sat, 13 Mar 2021 16:38:07 +0800 Subject: [PATCH] =?UTF-8?q?job=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=20De?= =?UTF-8?q?mo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/job/vo/job/InfJobBaseVO.java | 3 + .../service/job/impl/InfJobServiceImpl.java | 2 + .../infra/service/job/InfJobServiceTest.java | 78 +++++++++++++++++++ src/test/resources/sql/clean.sql | 1 + src/test/resources/sql/create_tables.sql | 18 +++++ 5 files changed, 102 insertions(+) create mode 100644 src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java index 2df3ad823..352c490fc 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/job/vo/job/InfJobBaseVO.java @@ -34,4 +34,7 @@ public class InfJobBaseVO { @ApiModelProperty(value = "监控超时时间", example = "1000") private Integer monitorTimeout; + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + } } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/job/impl/InfJobServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/job/impl/InfJobServiceImpl.java index d82207de1..eab60051c 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/job/impl/InfJobServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/job/impl/InfJobServiceImpl.java @@ -13,6 +13,7 @@ import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; import cn.iocoder.dashboard.modules.infra.service.job.InfJobService; import org.quartz.SchedulerException; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; @@ -37,6 +38,7 @@ public class InfJobServiceImpl implements InfJobService { @Resource private InfJobMapper jobMapper; + @MockBean @Resource private SchedulerManager schedulerManager; diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java new file mode 100644 index 000000000..e8ed0dfc7 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/job/InfJobServiceTest.java @@ -0,0 +1,78 @@ +package cn.iocoder.dashboard.modules.infra.service.job; + +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import javax.annotation.Resource; + +import org.junit.jupiter.api.Test; +import org.quartz.SchedulerException; +import org.springframework.context.annotation.Import; +import cn.iocoder.dashboard.BaseDbUnitTest; +import cn.iocoder.dashboard.framework.quartz.core.scheduler.SchedulerManager; +import cn.iocoder.dashboard.modules.infra.controller.job.vo.job.InfJobCreateReqVO; +import cn.iocoder.dashboard.modules.infra.convert.job.InfJobConvert; +import cn.iocoder.dashboard.modules.infra.dal.dataobject.job.InfJobDO; +import cn.iocoder.dashboard.modules.infra.dal.mysql.job.InfJobMapper; +import cn.iocoder.dashboard.modules.infra.enums.job.InfJobStatusEnum; +import cn.iocoder.dashboard.modules.infra.service.job.impl.InfJobServiceImpl; + +/** + * {@link InfJobServiceImpl} 的单元测试 + * + * @author neilz + */ +@Import(InfJobServiceImpl.class) +public class InfJobServiceTest extends BaseDbUnitTest { + @Resource + private InfJobServiceImpl jobService; + + @Resource + private InfJobMapper jobMapper; + @Resource + private SchedulerManager schedulerManager; + + @Test + public void testCreateJob_success() throws SchedulerException { + // 准备参数 + InfJobCreateReqVO reqVO = randomPojo(InfJobCreateReqVO.class); + reqVO.setCronExpression("0 0/1 * * * ? *"); + + // 调用 + Long jobId = jobService.createJob(reqVO); + + // 断言 + assertNotNull(jobId); + + // 校验记录的属性是否正确 + InfJobDO job = jobMapper.selectById(jobId); + assertPojoEquals(reqVO, job); + assertEquals(InfJobStatusEnum.NORMAL.getStatus(), job.getStatus()); + + // 校验调用 + verify(jobMapper, times(1)).selectByHandlerName(reqVO.getHandlerName()); + + InfJobDO insertJob = InfJobConvert.INSTANCE.convert(reqVO); + insertJob.setStatus(InfJobStatusEnum.INIT.getStatus()); + fillJobMonitorTimeoutEmpty(insertJob); + verify(jobMapper, times(1)).insert(insertJob); + + verify(schedulerManager, times(1)).addJob(job.getId(), job.getHandlerName(), job.getHandlerParam(), job.getCronExpression(), + job.getRetryCount(), job.getRetryInterval()); + + InfJobDO updateObj = InfJobDO.builder().id(insertJob.getId()).status(InfJobStatusEnum.NORMAL.getStatus()).build(); + verify(jobMapper, times(1)).updateById(updateObj); + + } + + private static void fillJobMonitorTimeoutEmpty(InfJobDO job) { + if (job.getMonitorTimeout() == null) { + job.setMonitorTimeout(0); + } + } + +} diff --git a/src/test/resources/sql/clean.sql b/src/test/resources/sql/clean.sql index 75372fd1a..329a94064 100644 --- a/src/test/resources/sql/clean.sql +++ b/src/test/resources/sql/clean.sql @@ -1,6 +1,7 @@ -- inf 开头的 DB DELETE FROM "inf_config"; DELETE FROM "inf_file"; +DELETE FROM "inf_job"; -- sys 开头的 DB DELETE FROM "sys_dept"; diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 36c573dd9..f4908a710 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -29,6 +29,24 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( PRIMARY KEY ("id") ) COMMENT '文件表'; +CREATE TABLE "inf_job" ( + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '任务编号', + "name" varchar(32) NOT NULL COMMENT '任务名称', + "status" tinyint(4) NOT NULL COMMENT '任务状态', + "handler_name" varchar(64) NOT NULL COMMENT '处理器的名字', + "handler_param" varchar(255) DEFAULT NULL COMMENT '处理器的参数', + "cron_expression" varchar(32) NOT NULL COMMENT 'CRON 表达式', + "retry_count" int(11) NOT NULL DEFAULT '0' COMMENT '重试次数', + "retry_interval" int(11) NOT NULL DEFAULT '0' COMMENT '重试间隔', + "monitor_timeout" int(11) NOT NULL DEFAULT '0' COMMENT '监控超时时间', + "creator" varchar(64) DEFAULT '' COMMENT '创建者', + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + "updater" varchar(64) DEFAULT '' COMMENT '更新者', + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + "deleted" bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', + PRIMARY KEY ("id") +) COMMENT='定时任务表'; + -- sys 开头的 DB CREATE TABLE IF NOT EXISTS "sys_dept" (