提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.mail; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.pojo.PageResult; |
|
4 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
5 |
import com.iailab.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; |
|
6 |
import com.iailab.module.system.controller.admin.mail.vo.account.MailAccountSaveReqVO; |
|
7 |
import com.iailab.module.system.dal.dataobject.mail.MailAccountDO; |
|
8 |
import com.iailab.module.system.dal.mysql.mail.MailAccountMapper; |
|
9 |
import org.junit.jupiter.api.Test; |
|
10 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
11 |
import org.springframework.context.annotation.Import; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
import java.util.List; |
|
15 |
|
|
16 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
17 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
18 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
19 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
20 |
import static com.iailab.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; |
|
21 |
import static org.junit.jupiter.api.Assertions.*; |
|
22 |
import static org.mockito.ArgumentMatchers.eq; |
|
23 |
import static org.mockito.Mockito.when; |
|
24 |
|
|
25 |
/** |
|
26 |
* {@link MailAccountServiceImpl} 的单元测试类 |
|
27 |
* |
|
28 |
* @author iailab |
|
29 |
*/ |
|
30 |
@Import(MailAccountServiceImpl.class) |
|
31 |
public class MailAccountServiceImplTest extends BaseDbUnitTest { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private MailAccountServiceImpl mailAccountService; |
|
35 |
|
|
36 |
@Resource |
|
37 |
private MailAccountMapper mailAccountMapper; |
|
38 |
|
|
39 |
@MockBean |
|
40 |
private MailTemplateService mailTemplateService; |
|
41 |
|
|
42 |
@Test |
|
43 |
public void testCreateMailAccount_success() { |
|
44 |
// 准备参数 |
|
45 |
MailAccountSaveReqVO reqVO = randomPojo(MailAccountSaveReqVO.class, o -> o.setMail(randomEmail())) |
|
46 |
.setId(null); // 防止 id 被赋值 |
|
47 |
|
|
48 |
// 调用 |
|
49 |
Long mailAccountId = mailAccountService.createMailAccount(reqVO); |
|
50 |
// 断言 |
|
51 |
assertNotNull(mailAccountId); |
|
52 |
// 校验记录的属性是否正确 |
|
53 |
MailAccountDO mailAccount = mailAccountMapper.selectById(mailAccountId); |
|
54 |
assertPojoEquals(reqVO, mailAccount, "id"); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testUpdateMailAccount_success() { |
|
59 |
// mock 数据 |
|
60 |
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); |
|
61 |
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
|
62 |
// 准备参数 |
|
63 |
MailAccountSaveReqVO reqVO = randomPojo(MailAccountSaveReqVO.class, o -> { |
|
64 |
o.setId(dbMailAccount.getId()); // 设置更新的 ID |
|
65 |
o.setMail(randomEmail()); |
|
66 |
}); |
|
67 |
|
|
68 |
// 调用 |
|
69 |
mailAccountService.updateMailAccount(reqVO); |
|
70 |
// 校验是否更新正确 |
|
71 |
MailAccountDO mailAccount = mailAccountMapper.selectById(reqVO.getId()); // 获取最新的 |
|
72 |
assertPojoEquals(reqVO, mailAccount); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testUpdateMailAccount_notExists() { |
|
77 |
// 准备参数 |
|
78 |
MailAccountSaveReqVO reqVO = randomPojo(MailAccountSaveReqVO.class); |
|
79 |
|
|
80 |
// 调用, 并断言异常 |
|
81 |
assertServiceException(() -> mailAccountService.updateMailAccount(reqVO), MAIL_ACCOUNT_NOT_EXISTS); |
|
82 |
} |
|
83 |
|
|
84 |
@Test |
|
85 |
public void testDeleteMailAccount_success() { |
|
86 |
// mock 数据 |
|
87 |
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); |
|
88 |
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
|
89 |
// 准备参数 |
|
90 |
Long id = dbMailAccount.getId(); |
|
91 |
// mock 方法(无关联模版) |
|
92 |
when(mailTemplateService.getMailTemplateCountByAccountId(eq(id))).thenReturn(0L); |
|
93 |
|
|
94 |
// 调用 |
|
95 |
mailAccountService.deleteMailAccount(id); |
|
96 |
// 校验数据不存在了 |
|
97 |
assertNull(mailAccountMapper.selectById(id)); |
|
98 |
} |
|
99 |
|
|
100 |
@Test |
|
101 |
public void testGetMailAccountFromCache() { |
|
102 |
// mock 数据 |
|
103 |
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); |
|
104 |
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
|
105 |
// 准备参数 |
|
106 |
Long id = dbMailAccount.getId(); |
|
107 |
|
|
108 |
// 调用 |
|
109 |
MailAccountDO mailAccount = mailAccountService.getMailAccountFromCache(id); |
|
110 |
// 断言 |
|
111 |
assertPojoEquals(dbMailAccount, mailAccount); |
|
112 |
} |
|
113 |
|
|
114 |
@Test |
|
115 |
public void testDeleteMailAccount_notExists() { |
|
116 |
// 准备参数 |
|
117 |
Long id = randomLongId(); |
|
118 |
|
|
119 |
// 调用, 并断言异常 |
|
120 |
assertServiceException(() -> mailAccountService.deleteMailAccount(id), MAIL_ACCOUNT_NOT_EXISTS); |
|
121 |
} |
|
122 |
|
|
123 |
@Test |
|
124 |
public void testGetMailAccountPage() { |
|
125 |
// mock 数据 |
|
126 |
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class, o -> { // 等会查询到 |
|
127 |
o.setMail("768@qq.com"); |
|
128 |
o.setUsername("yunai"); |
|
129 |
}); |
|
130 |
mailAccountMapper.insert(dbMailAccount); |
|
131 |
// 测试 mail 不匹配 |
|
132 |
mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setMail("788@qq.com"))); |
|
133 |
// 测试 username 不匹配 |
|
134 |
mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setUsername("tudou"))); |
|
135 |
// 准备参数 |
|
136 |
MailAccountPageReqVO reqVO = new MailAccountPageReqVO(); |
|
137 |
reqVO.setMail("768"); |
|
138 |
reqVO.setUsername("yu"); |
|
139 |
|
|
140 |
// 调用 |
|
141 |
PageResult<MailAccountDO> pageResult = mailAccountService.getMailAccountPage(reqVO); |
|
142 |
// 断言 |
|
143 |
assertEquals(1, pageResult.getTotal()); |
|
144 |
assertEquals(1, pageResult.getList().size()); |
|
145 |
assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); |
|
146 |
} |
|
147 |
|
|
148 |
@Test |
|
149 |
public void testGetMailAccount() { |
|
150 |
// mock 数据 |
|
151 |
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); |
|
152 |
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
|
153 |
// 准备参数 |
|
154 |
Long id = dbMailAccount.getId(); |
|
155 |
|
|
156 |
// 调用 |
|
157 |
MailAccountDO mailAccount = mailAccountService.getMailAccount(id); |
|
158 |
// 断言 |
|
159 |
assertPojoEquals(dbMailAccount, mailAccount); |
|
160 |
} |
|
161 |
|
|
162 |
@Test |
|
163 |
public void testGetMailAccountList() { |
|
164 |
// mock 数据 |
|
165 |
MailAccountDO dbMailAccount01 = randomPojo(MailAccountDO.class); |
|
166 |
mailAccountMapper.insert(dbMailAccount01); |
|
167 |
MailAccountDO dbMailAccount02 = randomPojo(MailAccountDO.class); |
|
168 |
mailAccountMapper.insert(dbMailAccount02); |
|
169 |
// 准备参数 |
|
170 |
|
|
171 |
// 调用 |
|
172 |
List<MailAccountDO> list = mailAccountService.getMailAccountList(); |
|
173 |
// 断言 |
|
174 |
assertEquals(2, list.size()); |
|
175 |
assertPojoEquals(dbMailAccount01, list.get(0)); |
|
176 |
assertPojoEquals(dbMailAccount02, list.get(1)); |
|
177 |
} |
|
178 |
|
|
179 |
} |