houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.sms;
H 2
3 import cn.hutool.core.map.MapUtil;
4 import com.iailab.framework.common.enums.UserTypeEnum;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.common.util.collection.ArrayUtils;
7 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
8 import com.iailab.module.system.controller.admin.sms.vo.log.SmsLogPageReqVO;
9 import com.iailab.module.system.dal.dataobject.sms.SmsLogDO;
10 import com.iailab.module.system.dal.dataobject.sms.SmsTemplateDO;
11 import com.iailab.module.system.dal.mysql.sms.SmsLogMapper;
12 import com.iailab.module.system.enums.sms.SmsReceiveStatusEnum;
13 import com.iailab.module.system.enums.sms.SmsSendStatusEnum;
14 import com.iailab.module.system.enums.sms.SmsTemplateTypeEnum;
15 import org.junit.jupiter.api.Test;
16 import org.springframework.context.annotation.Import;
17
18 import javax.annotation.Resource;
19 import java.time.LocalDateTime;
20 import java.util.Map;
21 import java.util.function.Consumer;
22
23 import static cn.hutool.core.util.RandomUtil.randomBoolean;
24 import static cn.hutool.core.util.RandomUtil.randomEle;
25 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
26 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime;
27 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
28 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
29 import static com.iailab.framework.test.core.util.RandomUtils.*;
30 import static org.junit.jupiter.api.Assertions.assertEquals;
31 import static org.junit.jupiter.api.Assertions.assertNotNull;
32
33 @Import(SmsLogServiceImpl.class)
34 public class SmsLogServiceImplTest extends BaseDbUnitTest {
35
36     @Resource
37     private SmsLogServiceImpl smsLogService;
38
39     @Resource
40     private SmsLogMapper smsLogMapper;
41
42     @Test
43     public void testGetSmsLogPage() {
44        // mock 数据
45        SmsLogDO dbSmsLog = randomSmsLogDO(o -> { // 等会查询到
46            o.setChannelId(1L);
47            o.setTemplateId(10L);
48            o.setMobile("15601691300");
49            o.setSendStatus(SmsSendStatusEnum.INIT.getStatus());
50            o.setSendTime(buildTime(2020, 11, 11));
51            o.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus());
52            o.setReceiveTime(buildTime(2021, 11, 11));
53        });
54        smsLogMapper.insert(dbSmsLog);
55        // 测试 channelId 不匹配
56        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L)));
57        // 测试 templateId 不匹配
58        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L)));
59        // 测试 mobile 不匹配
60        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999")));
61        // 测试 sendStatus 不匹配
62        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus())));
63        // 测试 sendTime 不匹配
64        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12))));
65        // 测试 receiveStatus 不匹配
66        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SmsReceiveStatusEnum.SUCCESS.getStatus())));
67        // 测试 receiveTime 不匹配
68        smsLogMapper.insert(cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12))));
69        // 准备参数
70        SmsLogPageReqVO reqVO = new SmsLogPageReqVO();
71        reqVO.setChannelId(1L);
72        reqVO.setTemplateId(10L);
73        reqVO.setMobile("156");
74        reqVO.setSendStatus(SmsSendStatusEnum.INIT.getStatus());
75        reqVO.setSendTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30));
76        reqVO.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus());
77        reqVO.setReceiveTime(buildBetweenTime(2021, 11, 1, 2021, 11, 30));
78
79        // 调用
80        PageResult<SmsLogDO> pageResult = smsLogService.getSmsLogPage(reqVO);
81        // 断言
82        assertEquals(1, pageResult.getTotal());
83        assertEquals(1, pageResult.getList().size());
84        assertPojoEquals(dbSmsLog, pageResult.getList().get(0));
85     }
86
87     @Test
88     public void testCreateSmsLog() {
89         // 准备参数
90         String mobile = randomString();
91         Long userId = randomLongId();
92         Integer userType = randomEle(UserTypeEnum.values()).getValue();
93         Boolean isSend = randomBoolean();
94         SmsTemplateDO templateDO = randomPojo(SmsTemplateDO.class,
95                 o -> o.setType(randomEle(SmsTemplateTypeEnum.values()).getType()));
96         String templateContent = randomString();
97         Map<String, Object> templateParams = randomTemplateParams();
98         // mock 方法
99
100         // 调用
101         Long logId = smsLogService.createSmsLog(mobile, userId, userType, isSend,
102                 templateDO, templateContent, templateParams);
103         // 断言
104         SmsLogDO logDO = smsLogMapper.selectById(logId);
105         assertEquals(isSend ? SmsSendStatusEnum.INIT.getStatus() : SmsSendStatusEnum.IGNORE.getStatus(),
106                 logDO.getSendStatus());
107         assertEquals(mobile, logDO.getMobile());
108         assertEquals(userType, logDO.getUserType());
109         assertEquals(userId, logDO.getUserId());
110         assertEquals(templateDO.getId(), logDO.getTemplateId());
111         assertEquals(templateDO.getCode(), logDO.getTemplateCode());
112         assertEquals(templateDO.getType(), logDO.getTemplateType());
113         assertEquals(templateDO.getChannelId(), logDO.getChannelId());
114         assertEquals(templateDO.getChannelCode(), logDO.getChannelCode());
115         assertEquals(templateContent, logDO.getTemplateContent());
116         assertEquals(templateParams, logDO.getTemplateParams());
117         assertEquals(SmsReceiveStatusEnum.INIT.getStatus(), logDO.getReceiveStatus());
118     }
119
120     @Test
121     public void testUpdateSmsSendResult() {
122         // mock 数据
123         SmsLogDO dbSmsLog = randomSmsLogDO(
124                 o -> o.setSendStatus(SmsSendStatusEnum.IGNORE.getStatus()));
125         smsLogMapper.insert(dbSmsLog);
126         // 准备参数
127         Long id = dbSmsLog.getId();
128         Boolean success = randomBoolean();
129         String apiSendCode = randomString();
130         String apiSendMsg = randomString();
131         String apiRequestId = randomString();
132         String apiSerialNo = randomString();
133
134         // 调用
135         smsLogService.updateSmsSendResult(id, success,
136                 apiSendCode, apiSendMsg, apiRequestId, apiSerialNo);
137         // 断言
138         dbSmsLog = smsLogMapper.selectById(id);
139         assertEquals(success ? SmsSendStatusEnum.SUCCESS.getStatus() : SmsSendStatusEnum.FAILURE.getStatus(),
140                 dbSmsLog.getSendStatus());
141         assertNotNull(dbSmsLog.getSendTime());
142         assertEquals(apiSendCode, dbSmsLog.getApiSendCode());
143         assertEquals(apiSendMsg, dbSmsLog.getApiSendMsg());
144         assertEquals(apiRequestId, dbSmsLog.getApiRequestId());
145         assertEquals(apiSerialNo, dbSmsLog.getApiSerialNo());
146     }
147
148     @Test
149     public void testUpdateSmsReceiveResult() {
150         // mock 数据
151         SmsLogDO dbSmsLog = randomSmsLogDO(
152                 o -> o.setReceiveStatus(SmsReceiveStatusEnum.INIT.getStatus()));
153         smsLogMapper.insert(dbSmsLog);
154         // 准备参数
155         Long id = dbSmsLog.getId();
156         Boolean success = randomBoolean();
157         LocalDateTime receiveTime = randomLocalDateTime();
158         String apiReceiveCode = randomString();
159         String apiReceiveMsg = randomString();
160
161         // 调用
162         smsLogService.updateSmsReceiveResult(id, success, receiveTime, apiReceiveCode, apiReceiveMsg);
163         // 断言
164         dbSmsLog = smsLogMapper.selectById(id);
165         assertEquals(success ? SmsReceiveStatusEnum.SUCCESS.getStatus()
166                 : SmsReceiveStatusEnum.FAILURE.getStatus(), dbSmsLog.getReceiveStatus());
167         assertEquals(receiveTime, dbSmsLog.getReceiveTime());
168         assertEquals(apiReceiveCode, dbSmsLog.getApiReceiveCode());
169         assertEquals(apiReceiveMsg, dbSmsLog.getApiReceiveMsg());
170     }
171
172     // ========== 随机对象 ==========
173
174     @SafeVarargs
175     private static SmsLogDO randomSmsLogDO(Consumer<SmsLogDO>... consumers) {
176         Consumer<SmsLogDO> consumer = (o) -> {
177             o.setTemplateParams(randomTemplateParams());
178             o.setTemplateType(randomEle(SmsTemplateTypeEnum.values()).getType()); // 保证 templateType 的范围
179             o.setUserType(randomEle(UserTypeEnum.values()).getValue()); // 保证 userType 的范围
180             o.setSendStatus(randomEle(SmsSendStatusEnum.values()).getStatus()); // 保证 sendStatus 的范围
181             o.setReceiveStatus(randomEle(SmsReceiveStatusEnum.values()).getStatus()); // 保证 receiveStatus 的范围
182         };
183         return randomPojo(SmsLogDO.class, ArrayUtils.append(consumer, consumers));
184     }
185
186     private static Map<String, Object> randomTemplateParams() {
187         return MapUtil.<String, Object>builder().put(randomString(), randomString())
188                 .put(randomString(), randomString()).build();
189     }
190 }