提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.mail; |
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.test.core.ut.BaseDbUnitTest; |
|
7 |
import com.iailab.module.system.controller.admin.mail.vo.log.MailLogPageReqVO; |
|
8 |
import com.iailab.module.system.dal.dataobject.mail.MailAccountDO; |
|
9 |
import com.iailab.module.system.dal.dataobject.mail.MailLogDO; |
|
10 |
import com.iailab.module.system.dal.dataobject.mail.MailTemplateDO; |
|
11 |
import com.iailab.module.system.dal.mysql.mail.MailLogMapper; |
|
12 |
import com.iailab.module.system.enums.mail.MailSendStatusEnum; |
|
13 |
import org.junit.jupiter.api.Test; |
|
14 |
import org.springframework.context.annotation.Import; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
import java.util.Map; |
|
18 |
|
|
19 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
20 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
21 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
22 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
23 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
24 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
25 |
import static org.junit.jupiter.api.Assertions.*; |
|
26 |
|
|
27 |
/** |
|
28 |
* {@link MailLogServiceImpl} 的单元测试类 |
|
29 |
* |
|
30 |
* @author iailab |
|
31 |
*/ |
|
32 |
@Import(MailLogServiceImpl.class) |
|
33 |
public class MailLogServiceImplTest extends BaseDbUnitTest { |
|
34 |
|
|
35 |
@Resource |
|
36 |
private MailLogServiceImpl mailLogService; |
|
37 |
|
|
38 |
@Resource |
|
39 |
private MailLogMapper mailLogMapper; |
|
40 |
|
|
41 |
@Test |
|
42 |
public void testCreateMailLog() { |
|
43 |
// 准备参数 |
|
44 |
Long userId = randomLongId(); |
|
45 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
46 |
String toMail = randomEmail(); |
|
47 |
MailAccountDO account = randomPojo(MailAccountDO.class); |
|
48 |
MailTemplateDO template = randomPojo(MailTemplateDO.class); |
|
49 |
String templateContent = randomString(); |
|
50 |
Map<String, Object> templateParams = randomTemplateParams(); |
|
51 |
Boolean isSend = true; |
|
52 |
// mock 方法 |
|
53 |
|
|
54 |
// 调用 |
|
55 |
Long logId = mailLogService.createMailLog(userId, userType, toMail, account, template, templateContent, templateParams, isSend); |
|
56 |
// 断言 |
|
57 |
MailLogDO log = mailLogMapper.selectById(logId); |
|
58 |
assertNotNull(log); |
|
59 |
assertEquals(MailSendStatusEnum.INIT.getStatus(), log.getSendStatus()); |
|
60 |
assertEquals(userId, log.getUserId()); |
|
61 |
assertEquals(userType, log.getUserType()); |
|
62 |
assertEquals(toMail, log.getToMail()); |
|
63 |
assertEquals(account.getId(), log.getAccountId()); |
|
64 |
assertEquals(account.getMail(), log.getFromMail()); |
|
65 |
assertEquals(template.getId(), log.getTemplateId()); |
|
66 |
assertEquals(template.getCode(), log.getTemplateCode()); |
|
67 |
assertEquals(template.getNickname(), log.getTemplateNickname()); |
|
68 |
assertEquals(template.getTitle(), log.getTemplateTitle()); |
|
69 |
assertEquals(templateContent, log.getTemplateContent()); |
|
70 |
assertEquals(templateParams, log.getTemplateParams()); |
|
71 |
} |
|
72 |
|
|
73 |
@Test |
|
74 |
public void testUpdateMailSendResult_success() { |
|
75 |
// mock 数据 |
|
76 |
MailLogDO log = randomPojo(MailLogDO.class, o -> { |
|
77 |
o.setSendStatus(MailSendStatusEnum.INIT.getStatus()); |
|
78 |
o.setSendTime(null).setSendMessageId(null).setSendException(null) |
|
79 |
.setTemplateParams(randomTemplateParams()); |
|
80 |
}); |
|
81 |
mailLogMapper.insert(log); |
|
82 |
// 准备参数 |
|
83 |
Long logId = log.getId(); |
|
84 |
String messageId = randomString(); |
|
85 |
|
|
86 |
// 调用 |
|
87 |
mailLogService.updateMailSendResult(logId, messageId, null); |
|
88 |
// 断言 |
|
89 |
MailLogDO dbLog = mailLogMapper.selectById(logId); |
|
90 |
assertEquals(MailSendStatusEnum.SUCCESS.getStatus(), dbLog.getSendStatus()); |
|
91 |
assertNotNull(dbLog.getSendTime()); |
|
92 |
assertEquals(messageId, dbLog.getSendMessageId()); |
|
93 |
assertNull(dbLog.getSendException()); |
|
94 |
} |
|
95 |
|
|
96 |
@Test |
|
97 |
public void testUpdateMailSendResult_exception() { |
|
98 |
// mock 数据 |
|
99 |
MailLogDO log = randomPojo(MailLogDO.class, o -> { |
|
100 |
o.setSendStatus(MailSendStatusEnum.INIT.getStatus()); |
|
101 |
o.setSendTime(null).setSendMessageId(null).setSendException(null) |
|
102 |
.setTemplateParams(randomTemplateParams()); |
|
103 |
}); |
|
104 |
mailLogMapper.insert(log); |
|
105 |
// 准备参数 |
|
106 |
Long logId = log.getId(); |
|
107 |
Exception exception = new NullPointerException("测试异常"); |
|
108 |
|
|
109 |
// 调用 |
|
110 |
mailLogService.updateMailSendResult(logId, null, exception); |
|
111 |
// 断言 |
|
112 |
MailLogDO dbLog = mailLogMapper.selectById(logId); |
|
113 |
assertEquals(MailSendStatusEnum.FAILURE.getStatus(), dbLog.getSendStatus()); |
|
114 |
assertNotNull(dbLog.getSendTime()); |
|
115 |
assertNull(dbLog.getSendMessageId()); |
|
116 |
assertEquals("NullPointerException: 测试异常", dbLog.getSendException()); |
|
117 |
} |
|
118 |
|
|
119 |
@Test |
|
120 |
public void testGetMailLog() { |
|
121 |
// mock 数据 |
|
122 |
MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> o.setTemplateParams(randomTemplateParams())); |
|
123 |
mailLogMapper.insert(dbMailLog); |
|
124 |
// 准备参数 |
|
125 |
Long id = dbMailLog.getId(); |
|
126 |
|
|
127 |
// 调用 |
|
128 |
MailLogDO mailLog = mailLogService.getMailLog(id); |
|
129 |
// 断言 |
|
130 |
assertPojoEquals(dbMailLog, mailLog); |
|
131 |
} |
|
132 |
|
|
133 |
@Test |
|
134 |
public void testGetMailLogPage() { |
|
135 |
// mock 数据 |
|
136 |
MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> { // 等会查询到 |
|
137 |
o.setUserId(1L); |
|
138 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
139 |
o.setToMail("768@qq.com"); |
|
140 |
o.setAccountId(10L); |
|
141 |
o.setTemplateId(100L); |
|
142 |
o.setSendStatus(MailSendStatusEnum.INIT.getStatus()); |
|
143 |
o.setSendTime(buildTime(2023, 2, 10)); |
|
144 |
o.setTemplateParams(randomTemplateParams()); |
|
145 |
}); |
|
146 |
mailLogMapper.insert(dbMailLog); |
|
147 |
// 测试 userId 不匹配 |
|
148 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setUserId(2L))); |
|
149 |
// 测试 userType 不匹配 |
|
150 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
151 |
// 测试 toMail 不匹配 |
|
152 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setToMail("788@.qq.com"))); |
|
153 |
// 测试 accountId 不匹配 |
|
154 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setAccountId(11L))); |
|
155 |
// 测试 templateId 不匹配 |
|
156 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setTemplateId(101L))); |
|
157 |
// 测试 sendStatus 不匹配 |
|
158 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setSendStatus(MailSendStatusEnum.SUCCESS.getStatus()))); |
|
159 |
// 测试 sendTime 不匹配 |
|
160 |
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setSendTime(buildTime(2023, 3, 10)))); |
|
161 |
// 准备参数 |
|
162 |
MailLogPageReqVO reqVO = new MailLogPageReqVO(); |
|
163 |
reqVO.setUserId(1L); |
|
164 |
reqVO.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
165 |
reqVO.setToMail("768"); |
|
166 |
reqVO.setAccountId(10L); |
|
167 |
reqVO.setTemplateId(100L); |
|
168 |
reqVO.setSendStatus(MailSendStatusEnum.INIT.getStatus()); |
|
169 |
reqVO.setSendTime((buildBetweenTime(2023, 2, 1, 2023, 2, 15))); |
|
170 |
|
|
171 |
// 调用 |
|
172 |
PageResult<MailLogDO> pageResult = mailLogService.getMailLogPage(reqVO); |
|
173 |
// 断言 |
|
174 |
assertEquals(1, pageResult.getTotal()); |
|
175 |
assertEquals(1, pageResult.getList().size()); |
|
176 |
assertPojoEquals(dbMailLog, pageResult.getList().get(0)); |
|
177 |
} |
|
178 |
|
|
179 |
private static Map<String, Object> randomTemplateParams() { |
|
180 |
return MapUtil.<String, Object>builder().put(randomString(), randomString()) |
|
181 |
.put(randomString(), randomString()).build(); |
|
182 |
} |
|
183 |
} |