提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.notify; |
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.mybatis.core.enums.SqlConstants; |
|
7 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
8 |
import com.iailab.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; |
|
9 |
import com.iailab.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; |
|
10 |
import com.iailab.module.system.dal.dataobject.notify.NotifyMessageDO; |
|
11 |
import com.iailab.module.system.dal.dataobject.notify.NotifyTemplateDO; |
|
12 |
import com.iailab.module.system.dal.mysql.notify.NotifyMessageMapper; |
|
13 |
import com.baomidou.mybatisplus.annotation.DbType; |
|
14 |
import org.junit.jupiter.api.Test; |
|
15 |
import org.springframework.context.annotation.Import; |
|
16 |
|
|
17 |
import javax.annotation.Resource; |
|
18 |
import java.util.Arrays; |
|
19 |
import java.util.Collection; |
|
20 |
import java.util.List; |
|
21 |
import java.util.Map; |
|
22 |
|
|
23 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
24 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
25 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
26 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
27 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
28 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
29 |
import static org.junit.jupiter.api.Assertions.*; |
|
30 |
|
|
31 |
/** |
|
32 |
* {@link NotifyMessageServiceImpl} 的单元测试类 |
|
33 |
* |
|
34 |
* @author iailab |
|
35 |
*/ |
|
36 |
@Import(NotifyMessageServiceImpl.class) |
|
37 |
public class NotifyMessageServiceImplTest extends BaseDbUnitTest { |
|
38 |
|
|
39 |
@Resource |
|
40 |
private NotifyMessageServiceImpl notifyMessageService; |
|
41 |
|
|
42 |
@Resource |
|
43 |
private NotifyMessageMapper notifyMessageMapper; |
|
44 |
|
|
45 |
@Test |
|
46 |
public void testCreateNotifyMessage_success() { |
|
47 |
// 准备参数 |
|
48 |
Long userId = randomLongId(); |
|
49 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
50 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class); |
|
51 |
String templateContent = randomString(); |
|
52 |
Map<String, Object> templateParams = randomTemplateParams(); |
|
53 |
// mock 方法 |
|
54 |
|
|
55 |
// 调用 |
|
56 |
Long messageId = notifyMessageService.createNotifyMessage(userId, userType, |
|
57 |
template, templateContent, templateParams); |
|
58 |
// 断言 |
|
59 |
NotifyMessageDO message = notifyMessageMapper.selectById(messageId); |
|
60 |
assertNotNull(message); |
|
61 |
assertEquals(userId, message.getUserId()); |
|
62 |
assertEquals(userType, message.getUserType()); |
|
63 |
assertEquals(template.getId(), message.getTemplateId()); |
|
64 |
assertEquals(template.getCode(), message.getTemplateCode()); |
|
65 |
assertEquals(template.getType(), message.getTemplateType()); |
|
66 |
assertEquals(template.getNickname(), message.getTemplateNickname()); |
|
67 |
assertEquals(templateContent, message.getTemplateContent()); |
|
68 |
assertEquals(templateParams, message.getTemplateParams()); |
|
69 |
assertEquals(false, message.getReadStatus()); |
|
70 |
assertNull(message.getReadTime()); |
|
71 |
} |
|
72 |
|
|
73 |
@Test |
|
74 |
public void testGetNotifyMessagePage() { |
|
75 |
// mock 数据 |
|
76 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
77 |
o.setUserId(1L); |
|
78 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
79 |
o.setTemplateCode("test_01"); |
|
80 |
o.setTemplateType(10); |
|
81 |
o.setCreateTime(buildTime(2022, 1, 2)); |
|
82 |
o.setTemplateParams(randomTemplateParams()); |
|
83 |
}); |
|
84 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
85 |
// 测试 userId 不匹配 |
|
86 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
87 |
// 测试 userType 不匹配 |
|
88 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
89 |
// 测试 templateCode 不匹配 |
|
90 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setTemplateCode("test_11"))); |
|
91 |
// 测试 templateType 不匹配 |
|
92 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setTemplateType(20))); |
|
93 |
// 测试 createTime 不匹配 |
|
94 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setCreateTime(buildTime(2022, 2, 1)))); |
|
95 |
// 准备参数 |
|
96 |
NotifyMessagePageReqVO reqVO = new NotifyMessagePageReqVO(); |
|
97 |
reqVO.setUserId(1L); |
|
98 |
reqVO.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
99 |
reqVO.setTemplateCode("est_01"); |
|
100 |
reqVO.setTemplateType(10); |
|
101 |
reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10)); |
|
102 |
|
|
103 |
// 调用 |
|
104 |
PageResult<NotifyMessageDO> pageResult = notifyMessageService.getNotifyMessagePage(reqVO); |
|
105 |
// 断言 |
|
106 |
assertEquals(1, pageResult.getTotal()); |
|
107 |
assertEquals(1, pageResult.getList().size()); |
|
108 |
assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0)); |
|
109 |
} |
|
110 |
|
|
111 |
@Test |
|
112 |
public void testGetNotifyMessage() { |
|
113 |
// mock 数据 |
|
114 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, |
|
115 |
o -> o.setTemplateParams(randomTemplateParams())); |
|
116 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
117 |
// 准备参数 |
|
118 |
Long id = dbNotifyMessage.getId(); |
|
119 |
|
|
120 |
// 调用 |
|
121 |
NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id); |
|
122 |
assertPojoEquals(dbNotifyMessage, notifyMessage); |
|
123 |
} |
|
124 |
|
|
125 |
@Test |
|
126 |
public void testGetMyNotifyMessagePage() { |
|
127 |
// mock 数据 |
|
128 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
129 |
o.setUserId(1L); |
|
130 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
131 |
o.setReadStatus(true); |
|
132 |
o.setCreateTime(buildTime(2022, 1, 2)); |
|
133 |
o.setTemplateParams(randomTemplateParams()); |
|
134 |
}); |
|
135 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
136 |
// 测试 userId 不匹配 |
|
137 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
138 |
// 测试 userType 不匹配 |
|
139 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
140 |
// 测试 readStatus 不匹配 |
|
141 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(false))); |
|
142 |
// 测试 createTime 不匹配 |
|
143 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setCreateTime(buildTime(2022, 2, 1)))); |
|
144 |
// 准备参数 |
|
145 |
Long userId = 1L; |
|
146 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
147 |
NotifyMessageMyPageReqVO reqVO = new NotifyMessageMyPageReqVO(); |
|
148 |
reqVO.setReadStatus(true); |
|
149 |
reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10)); |
|
150 |
|
|
151 |
// 调用 |
|
152 |
PageResult<NotifyMessageDO> pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO, userId, userType); |
|
153 |
// 断言 |
|
154 |
assertEquals(1, pageResult.getTotal()); |
|
155 |
assertEquals(1, pageResult.getList().size()); |
|
156 |
assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0)); |
|
157 |
} |
|
158 |
|
|
159 |
@Test |
|
160 |
public void testGetUnreadNotifyMessageList() { |
|
161 |
SqlConstants.init(DbType.MYSQL); |
|
162 |
// mock 数据 |
|
163 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
164 |
o.setUserId(1L); |
|
165 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
166 |
o.setReadStatus(false); |
|
167 |
o.setTemplateParams(randomTemplateParams()); |
|
168 |
}); |
|
169 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
170 |
// 测试 userId 不匹配 |
|
171 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
172 |
// 测试 userType 不匹配 |
|
173 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
174 |
// 测试 readStatus 不匹配 |
|
175 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true))); |
|
176 |
// 准备参数 |
|
177 |
Long userId = 1L; |
|
178 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
179 |
Integer size = 10; |
|
180 |
|
|
181 |
// 调用 |
|
182 |
List<NotifyMessageDO> list = notifyMessageService.getUnreadNotifyMessageList(userId, userType, size); |
|
183 |
// 断言 |
|
184 |
assertEquals(1, list.size()); |
|
185 |
assertPojoEquals(dbNotifyMessage, list.get(0)); |
|
186 |
} |
|
187 |
|
|
188 |
@Test |
|
189 |
public void testGetUnreadNotifyMessageCount() { |
|
190 |
SqlConstants.init(DbType.MYSQL); |
|
191 |
// mock 数据 |
|
192 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
193 |
o.setUserId(1L); |
|
194 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
195 |
o.setReadStatus(false); |
|
196 |
o.setTemplateParams(randomTemplateParams()); |
|
197 |
}); |
|
198 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
199 |
// 测试 userId 不匹配 |
|
200 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
201 |
// 测试 userType 不匹配 |
|
202 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
203 |
// 测试 readStatus 不匹配 |
|
204 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true))); |
|
205 |
// 准备参数 |
|
206 |
Long userId = 1L; |
|
207 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
208 |
|
|
209 |
// 调用,并断言 |
|
210 |
assertEquals(1, notifyMessageService.getUnreadNotifyMessageCount(userId, userType)); |
|
211 |
} |
|
212 |
|
|
213 |
@Test |
|
214 |
public void testUpdateNotifyMessageRead() { |
|
215 |
// mock 数据 |
|
216 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
217 |
o.setUserId(1L); |
|
218 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
219 |
o.setReadStatus(false); |
|
220 |
o.setReadTime(null); |
|
221 |
o.setTemplateParams(randomTemplateParams()); |
|
222 |
}); |
|
223 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
224 |
// 测试 userId 不匹配 |
|
225 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
226 |
// 测试 userType 不匹配 |
|
227 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
228 |
// 测试 readStatus 不匹配 |
|
229 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true))); |
|
230 |
// 准备参数 |
|
231 |
Collection<Long> ids = Arrays.asList(dbNotifyMessage.getId(), dbNotifyMessage.getId() + 1, |
|
232 |
dbNotifyMessage.getId() + 2, dbNotifyMessage.getId() + 3); |
|
233 |
Long userId = 1L; |
|
234 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
235 |
|
|
236 |
// 调用 |
|
237 |
int updateCount = notifyMessageService.updateNotifyMessageRead(ids, userId, userType); |
|
238 |
// 断言 |
|
239 |
assertEquals(1, updateCount); |
|
240 |
NotifyMessageDO notifyMessage = notifyMessageMapper.selectById(dbNotifyMessage.getId()); |
|
241 |
assertTrue(notifyMessage.getReadStatus()); |
|
242 |
assertNotNull(notifyMessage.getReadTime()); |
|
243 |
} |
|
244 |
|
|
245 |
@Test |
|
246 |
public void testUpdateAllNotifyMessageRead() { |
|
247 |
// mock 数据 |
|
248 |
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 |
|
249 |
o.setUserId(1L); |
|
250 |
o.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
251 |
o.setReadStatus(false); |
|
252 |
o.setReadTime(null); |
|
253 |
o.setTemplateParams(randomTemplateParams()); |
|
254 |
}); |
|
255 |
notifyMessageMapper.insert(dbNotifyMessage); |
|
256 |
// 测试 userId 不匹配 |
|
257 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L))); |
|
258 |
// 测试 userType 不匹配 |
|
259 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue()))); |
|
260 |
// 测试 readStatus 不匹配 |
|
261 |
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true))); |
|
262 |
// 准备参数 |
|
263 |
Long userId = 1L; |
|
264 |
Integer userType = UserTypeEnum.ADMIN.getValue(); |
|
265 |
|
|
266 |
// 调用 |
|
267 |
int updateCount = notifyMessageService.updateAllNotifyMessageRead(userId, userType); |
|
268 |
// 断言 |
|
269 |
assertEquals(1, updateCount); |
|
270 |
NotifyMessageDO notifyMessage = notifyMessageMapper.selectById(dbNotifyMessage.getId()); |
|
271 |
assertTrue(notifyMessage.getReadStatus()); |
|
272 |
assertNotNull(notifyMessage.getReadTime()); |
|
273 |
} |
|
274 |
|
|
275 |
private static Map<String, Object> randomTemplateParams() { |
|
276 |
return MapUtil.<String, Object>builder().put(randomString(), randomString()) |
|
277 |
.put(randomString(), randomString()).build(); |
|
278 |
} |
|
279 |
|
|
280 |
} |