提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.mail; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
6 |
import com.iailab.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
|
7 |
import com.iailab.module.system.controller.admin.mail.vo.template.MailTemplateSaveReqVO; |
|
8 |
import com.iailab.module.system.dal.dataobject.mail.MailTemplateDO; |
|
9 |
import com.iailab.module.system.dal.mysql.mail.MailTemplateMapper; |
|
10 |
import org.junit.jupiter.api.Test; |
|
11 |
import org.springframework.context.annotation.Import; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
19 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
20 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
21 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
22 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
23 |
import static com.iailab.framework.test.core.util.RandomUtils.randomLongId; |
|
24 |
import static com.iailab.framework.test.core.util.RandomUtils.randomPojo; |
|
25 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; |
|
26 |
import static org.junit.jupiter.api.Assertions.*; |
|
27 |
|
|
28 |
/** |
|
29 |
* {@link MailTemplateServiceImpl} 的单元测试类 |
|
30 |
* |
|
31 |
* @author iailab |
|
32 |
*/ |
|
33 |
@Import(MailTemplateServiceImpl.class) |
|
34 |
public class MailTemplateServiceImplTest extends BaseDbUnitTest { |
|
35 |
|
|
36 |
@Resource |
|
37 |
private MailTemplateServiceImpl mailTemplateService; |
|
38 |
|
|
39 |
@Resource |
|
40 |
private MailTemplateMapper mailTemplateMapper; |
|
41 |
|
|
42 |
@Test |
|
43 |
public void testCreateMailTemplate_success() { |
|
44 |
// 准备参数 |
|
45 |
MailTemplateSaveReqVO reqVO = randomPojo(MailTemplateSaveReqVO.class) |
|
46 |
.setId(null); // 防止 id 被赋值 |
|
47 |
|
|
48 |
// 调用 |
|
49 |
Long mailTemplateId = mailTemplateService.createMailTemplate(reqVO); |
|
50 |
// 断言 |
|
51 |
assertNotNull(mailTemplateId); |
|
52 |
// 校验记录的属性是否正确 |
|
53 |
MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); |
|
54 |
assertPojoEquals(reqVO, mailTemplate, "id"); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testUpdateMailTemplate_success() { |
|
59 |
// mock 数据 |
|
60 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); |
|
61 |
mailTemplateMapper.insert(dbMailTemplate);// @Sql: 先插入出一条存在的数据 |
|
62 |
// 准备参数 |
|
63 |
MailTemplateSaveReqVO reqVO = randomPojo(MailTemplateSaveReqVO.class, o -> { |
|
64 |
o.setId(dbMailTemplate.getId()); // 设置更新的 ID |
|
65 |
}); |
|
66 |
|
|
67 |
// 调用 |
|
68 |
mailTemplateService.updateMailTemplate(reqVO); |
|
69 |
// 校验是否更新正确 |
|
70 |
MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 |
|
71 |
assertPojoEquals(reqVO, mailTemplate); |
|
72 |
} |
|
73 |
|
|
74 |
@Test |
|
75 |
public void testUpdateMailTemplate_notExists() { |
|
76 |
// 准备参数 |
|
77 |
MailTemplateSaveReqVO reqVO = randomPojo(MailTemplateSaveReqVO.class); |
|
78 |
|
|
79 |
// 调用, 并断言异常 |
|
80 |
assertServiceException(() -> mailTemplateService.updateMailTemplate(reqVO), MAIL_TEMPLATE_NOT_EXISTS); |
|
81 |
} |
|
82 |
|
|
83 |
@Test |
|
84 |
public void testDeleteMailTemplate_success() { |
|
85 |
// mock 数据 |
|
86 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); |
|
87 |
mailTemplateMapper.insert(dbMailTemplate);// @Sql: 先插入出一条存在的数据 |
|
88 |
// 准备参数 |
|
89 |
Long id = dbMailTemplate.getId(); |
|
90 |
|
|
91 |
// 调用 |
|
92 |
mailTemplateService.deleteMailTemplate(id); |
|
93 |
// 校验数据不存在了 |
|
94 |
assertNull(mailTemplateMapper.selectById(id)); |
|
95 |
} |
|
96 |
|
|
97 |
@Test |
|
98 |
public void testDeleteMailTemplate_notExists() { |
|
99 |
// 准备参数 |
|
100 |
Long id = randomLongId(); |
|
101 |
|
|
102 |
// 调用, 并断言异常 |
|
103 |
assertServiceException(() -> mailTemplateService.deleteMailTemplate(id), MAIL_TEMPLATE_NOT_EXISTS); |
|
104 |
} |
|
105 |
|
|
106 |
@Test |
|
107 |
public void testGetMailTemplatePage() { |
|
108 |
// mock 数据 |
|
109 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class, o -> { // 等会查询到 |
|
110 |
o.setName("源码"); |
|
111 |
o.setCode("test_01"); |
|
112 |
o.setAccountId(1L); |
|
113 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
114 |
o.setCreateTime(buildTime(2023, 2, 3)); |
|
115 |
}); |
|
116 |
mailTemplateMapper.insert(dbMailTemplate); |
|
117 |
// 测试 name 不匹配 |
|
118 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setName("平台"))); |
|
119 |
// 测试 code 不匹配 |
|
120 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCode("test_02"))); |
|
121 |
// 测试 accountId 不匹配 |
|
122 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); |
|
123 |
// 测试 status 不匹配 |
|
124 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
125 |
// 测试 createTime 不匹配 |
|
126 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCreateTime(buildTime(2023, 1, 5)))); |
|
127 |
// 准备参数 |
|
128 |
MailTemplatePageReqVO reqVO = new MailTemplatePageReqVO(); |
|
129 |
reqVO.setName("源"); |
|
130 |
reqVO.setCode("est_01"); |
|
131 |
reqVO.setAccountId(1L); |
|
132 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
133 |
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 5)); |
|
134 |
|
|
135 |
// 调用 |
|
136 |
PageResult<MailTemplateDO> pageResult = mailTemplateService.getMailTemplatePage(reqVO); |
|
137 |
// 断言 |
|
138 |
assertEquals(1, pageResult.getTotal()); |
|
139 |
assertEquals(1, pageResult.getList().size()); |
|
140 |
assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); |
|
141 |
} |
|
142 |
|
|
143 |
@Test |
|
144 |
public void testGetMailTemplateList() { |
|
145 |
// mock 数据 |
|
146 |
MailTemplateDO dbMailTemplate01 = randomPojo(MailTemplateDO.class); |
|
147 |
mailTemplateMapper.insert(dbMailTemplate01); |
|
148 |
MailTemplateDO dbMailTemplate02 = randomPojo(MailTemplateDO.class); |
|
149 |
mailTemplateMapper.insert(dbMailTemplate02); |
|
150 |
|
|
151 |
// 调用 |
|
152 |
List<MailTemplateDO> list = mailTemplateService.getMailTemplateList(); |
|
153 |
// 断言 |
|
154 |
assertEquals(2, list.size()); |
|
155 |
assertEquals(dbMailTemplate01, list.get(0)); |
|
156 |
assertEquals(dbMailTemplate02, list.get(1)); |
|
157 |
} |
|
158 |
|
|
159 |
@Test |
|
160 |
public void testGetMailTemplate() { |
|
161 |
// mock 数据 |
|
162 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); |
|
163 |
mailTemplateMapper.insert(dbMailTemplate); |
|
164 |
// 准备参数 |
|
165 |
Long id = dbMailTemplate.getId(); |
|
166 |
|
|
167 |
// 调用 |
|
168 |
MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(id); |
|
169 |
// 断言 |
|
170 |
assertPojoEquals(dbMailTemplate, mailTemplate); |
|
171 |
} |
|
172 |
|
|
173 |
@Test |
|
174 |
public void testGetMailTemplateByCodeFromCache() { |
|
175 |
// mock 数据 |
|
176 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); |
|
177 |
mailTemplateMapper.insert(dbMailTemplate); |
|
178 |
// 准备参数 |
|
179 |
String code = dbMailTemplate.getCode(); |
|
180 |
|
|
181 |
// 调用 |
|
182 |
MailTemplateDO mailTemplate = mailTemplateService.getMailTemplateByCodeFromCache(code); |
|
183 |
// 断言 |
|
184 |
assertPojoEquals(dbMailTemplate, mailTemplate); |
|
185 |
} |
|
186 |
|
|
187 |
@Test |
|
188 |
public void testFormatMailTemplateContent() { |
|
189 |
// 准备参数 |
|
190 |
Map<String, Object> params = new HashMap<>(); |
|
191 |
params.put("name", "小红"); |
|
192 |
params.put("what", "饭"); |
|
193 |
|
|
194 |
// 调用,并断言 |
|
195 |
assertEquals("小红,你好,饭吃了吗?", |
|
196 |
mailTemplateService.formatMailTemplateContent("{name},你好,{what}吃了吗?", params)); |
|
197 |
} |
|
198 |
|
|
199 |
@Test |
|
200 |
public void testCountByAccountId() { |
|
201 |
// mock 数据 |
|
202 |
MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); |
|
203 |
mailTemplateMapper.insert(dbMailTemplate); |
|
204 |
// 测试 accountId 不匹配 |
|
205 |
mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); |
|
206 |
// 准备参数 |
|
207 |
Long accountId = dbMailTemplate.getAccountId(); |
|
208 |
|
|
209 |
// 调用 |
|
210 |
long count = mailTemplateService.getMailTemplateCountByAccountId(accountId); |
|
211 |
// 断言 |
|
212 |
assertEquals(1, count); |
|
213 |
} |
|
214 |
|
|
215 |
} |