完成短信日志的单元测试

pull/2/head
YunaiV 2021-04-11 19:19:26 +08:00
parent 7760e163b3
commit 172c06b638
1 changed files with 103 additions and 8 deletions

View File

@ -1,11 +1,14 @@
package cn.iocoder.dashboard.modules.system.service.sms; package cn.iocoder.dashboard.modules.system.service.sms;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.BaseDbUnitTest;
import cn.iocoder.dashboard.common.enums.UserTypeEnum; 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.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.log.SysSmsLogExportReqVO; 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.controller.sms.vo.log.SysSmsLogPageReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsLogDO; 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.dal.mysql.sms.SysSmsLogMapper;
import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum;
import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; 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 org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomBoolean;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; import static cn.iocoder.dashboard.util.RandomUtils.*;
import static cn.iocoder.dashboard.util.RandomUtils.randomString;
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; 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} * {@link SysSmsLogServiceImpl}
@ -42,7 +46,93 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest {
@Resource @Resource
private SysSmsLogMapper smsLogMapper; 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<String, Object> 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() { public void testGetSmsLogPage() {
// mock 数据 // mock 数据
SysSmsLogDO dbSmsLog = randomSmsLogDO(o -> { // 等会查询到 SysSmsLogDO dbSmsLog = randomSmsLogDO(o -> { // 等会查询到
@ -140,12 +230,17 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest {
@SafeVarargs @SafeVarargs
private static SysSmsLogDO randomSmsLogDO(Consumer<SysSmsLogDO>... consumers) { private static SysSmsLogDO randomSmsLogDO(Consumer<SysSmsLogDO>... consumers) {
Consumer<SysSmsLogDO> consumer = (o) -> { Consumer<SysSmsLogDO> consumer = (o) -> {
o.setTemplateParams(new HashMap<>()); o.setTemplateParams(randomTemplateParams());
o.getTemplateParams().put(randomString(), randomString());
o.getTemplateParams().put(randomString(), randomString());
o.setTemplateType(randomEle(SysSmsTemplateTypeEnum.values()).getType()); // 保证 templateType 的范围 o.setTemplateType(randomEle(SysSmsTemplateTypeEnum.values()).getType()); // 保证 templateType 的范围
o.setUserType(randomEle(UserTypeEnum.values()).getValue()); // 保证 userType 的范围 o.setUserType(randomEle(UserTypeEnum.values()).getValue()); // 保证 userType 的范围
o.setReceiveStatus(randomEle(SysSmsReceiveStatusEnum.values()).getStatus()); // 保证 receiveStatus 的范围
}; };
return randomPojo(SysSmsLogDO.class, ArrayUtils.append(consumer, consumers)); return randomPojo(SysSmsLogDO.class, ArrayUtils.append(consumer, consumers));
} }
private static Map<String, Object> randomTemplateParams() {
return MapUtil.<String, Object>builder().put(randomString(), randomString())
.put(randomString(), randomString()).build();
}
} }