From 172c06b6381f0b7ec4de4ac6ef95b26b7d2d3050 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 11 Apr 2021 19:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=9F=AD=E4=BF=A1=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/sms/SysSmsLogServiceTest.java | 111 ++++++++++++++++-- 1 file changed, 103 insertions(+), 8 deletions(-) diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogServiceTest.java index 8b337b013..eaf45f5ec 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogServiceTest.java @@ -1,11 +1,14 @@ package cn.iocoder.dashboard.modules.system.service.sms; +import cn.hutool.core.map.MapUtil; import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.common.enums.UserTypeEnum; +import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.system.controller.sms.vo.log.SysSmsLogExportReqVO; import cn.iocoder.dashboard.modules.system.controller.sms.vo.log.SysSmsLogPageReqVO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsLogDO; +import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsLogMapper; import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; @@ -17,16 +20,17 @@ import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; import javax.annotation.Resource; -import java.util.HashMap; +import java.util.Date; import java.util.List; +import java.util.Map; import java.util.function.Consumer; +import static cn.hutool.core.util.RandomUtil.randomBoolean; import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; -import static cn.iocoder.dashboard.util.RandomUtils.randomString; +import static cn.iocoder.dashboard.util.RandomUtils.*; import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; /** * {@link SysSmsLogServiceImpl} 的单元测试类 @@ -42,7 +46,93 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest { @Resource private SysSmsLogMapper smsLogMapper; - @Test // TODO 请修改 null 为需要的值 + @Test + public void testCreateSmsLog() { + // 准备参数 + String mobile = randomString(); + Long userId = randomLongId(); + Integer userType = randomEle(UserTypeEnum.values()).getValue(); + Boolean isSend = randomBoolean(); + SysSmsTemplateDO templateDO = randomPojo(SysSmsTemplateDO.class, + o -> o.setType(randomEle(SysSmsTemplateTypeEnum.values()).getType())); + String templateContent = randomString(); + Map templateParams = randomTemplateParams(); + // mock 方法 + + // 调用 + Long logId = smsLogService.createSmsLog(mobile, userId, userType, isSend, + templateDO, templateContent, templateParams); + // 断言 + SysSmsLogDO logDO = smsLogMapper.selectById(logId); + assertEquals(isSend ? SysSmsSendStatusEnum.INIT.getStatus() : SysSmsSendStatusEnum.IGNORE.getStatus(), + logDO.getSendStatus()); + assertEquals(mobile, logDO.getMobile()); + assertEquals(userType, logDO.getUserType()); + assertEquals(userId, logDO.getUserId()); + assertEquals(templateDO.getId(), logDO.getTemplateId()); + assertEquals(templateDO.getCode(), logDO.getTemplateCode()); + assertEquals(templateDO.getType(), logDO.getTemplateType()); + assertEquals(templateDO.getChannelId(), logDO.getChannelId()); + assertEquals(templateDO.getChannelCode(), logDO.getChannelCode()); + assertEquals(templateContent, logDO.getTemplateContent()); + assertEquals(templateParams, logDO.getTemplateParams()); + assertEquals(SysSmsReceiveStatusEnum.INIT.getStatus(), logDO.getReceiveStatus()); + } + + @Test + public void testUpdateSmsSendResult() { + // mock 数据 + SysSmsLogDO dbSmsLog = randomSmsLogDO( + o -> o.setSendStatus(SysSmsSendStatusEnum.IGNORE.getStatus())); + smsLogMapper.insert(dbSmsLog); + // 准备参数 + Long id = dbSmsLog.getId(); + Integer sendCode = randomInteger(); + String sendMsg = randomString(); + String apiSendCode = randomString(); + String apiSendMsg = randomString(); + String apiRequestId = randomString(); + String apiSerialNo = randomString(); + + // 调用 + smsLogService.updateSmsSendResult(id, sendCode, sendMsg, + apiSendCode, apiSendMsg, apiRequestId, apiSerialNo); + // 断言 + dbSmsLog = smsLogMapper.selectById(id); + assertEquals(CommonResult.isSuccess(sendCode) ? SysSmsSendStatusEnum.SUCCESS.getStatus() + : SysSmsSendStatusEnum.FAILURE.getStatus(), dbSmsLog.getSendStatus()); + assertNotNull(dbSmsLog.getSendTime()); + assertEquals(sendMsg, dbSmsLog.getSendMsg()); + assertEquals(apiSendCode, dbSmsLog.getApiSendCode()); + assertEquals(apiSendMsg, dbSmsLog.getApiSendMsg()); + assertEquals(apiRequestId, dbSmsLog.getApiRequestId()); + assertEquals(apiSerialNo, dbSmsLog.getApiSerialNo()); + } + + public void testUpdateSmsReceiveResult() { + // mock 数据 + SysSmsLogDO dbSmsLog = randomSmsLogDO( + o -> o.setReceiveStatus(SysSmsReceiveStatusEnum.INIT.getStatus())); + smsLogMapper.insert(dbSmsLog); + // 准备参数 + Long id = dbSmsLog.getId(); + Boolean success = randomBoolean(); + Date receiveTime = randomDate(); + String apiReceiveCode = randomString(); + String apiReceiveMsg = randomString(); + + // 调用 + smsLogService.updateSmsReceiveResult(id, success, receiveTime, apiReceiveCode, apiReceiveMsg); + // 断言 + dbSmsLog = smsLogMapper.selectById(id); + assertEquals(success ? SysSmsReceiveStatusEnum.SUCCESS.getStatus() + : SysSmsReceiveStatusEnum.FAILURE.getStatus(), dbSmsLog.getReceiveStatus()); + assertEquals(receiveTime, dbSmsLog.getReceiveTime()); + assertEquals(apiReceiveCode, dbSmsLog.getApiSendCode()); + assertEquals(apiReceiveMsg, dbSmsLog.getApiReceiveMsg()); + } + + @Test public void testGetSmsLogPage() { // mock 数据 SysSmsLogDO dbSmsLog = randomSmsLogDO(o -> { // 等会查询到 @@ -140,12 +230,17 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest { @SafeVarargs private static SysSmsLogDO randomSmsLogDO(Consumer... consumers) { Consumer consumer = (o) -> { - o.setTemplateParams(new HashMap<>()); - o.getTemplateParams().put(randomString(), randomString()); - o.getTemplateParams().put(randomString(), randomString()); + o.setTemplateParams(randomTemplateParams()); o.setTemplateType(randomEle(SysSmsTemplateTypeEnum.values()).getType()); // 保证 templateType 的范围 o.setUserType(randomEle(UserTypeEnum.values()).getValue()); // 保证 userType 的范围 + o.setReceiveStatus(randomEle(SysSmsReceiveStatusEnum.values()).getStatus()); // 保证 receiveStatus 的范围 }; return randomPojo(SysSmsLogDO.class, ArrayUtils.append(consumer, consumers)); } + + + private static Map randomTemplateParams() { + return MapUtil.builder().put(randomString(), randomString()) + .put(randomString(), randomString()).build(); + } }