提交 | 用户 | 时间
|
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.CommonStatusEnum; |
|
5 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
6 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
7 |
import com.iailab.module.system.dal.dataobject.notify.NotifyTemplateDO; |
|
8 |
import org.assertj.core.util.Lists; |
|
9 |
import org.junit.jupiter.api.Assertions; |
|
10 |
import org.junit.jupiter.api.Test; |
|
11 |
import org.mockito.InjectMocks; |
|
12 |
import org.mockito.Mock; |
|
13 |
|
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
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.NOTICE_NOT_FOUND; |
|
21 |
import static com.iailab.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS; |
|
22 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
23 |
import static org.junit.jupiter.api.Assertions.assertNull; |
|
24 |
import static org.mockito.ArgumentMatchers.eq; |
|
25 |
import static org.mockito.Mockito.*; |
|
26 |
|
|
27 |
class NotifySendServiceImplTest extends BaseMockitoUnitTest { |
|
28 |
|
|
29 |
@InjectMocks |
|
30 |
private NotifySendServiceImpl notifySendService; |
|
31 |
|
|
32 |
@Mock |
|
33 |
private NotifyTemplateService notifyTemplateService; |
|
34 |
@Mock |
|
35 |
private NotifyMessageService notifyMessageService; |
|
36 |
|
|
37 |
@Test |
|
38 |
public void testSendSingleNotifyToAdmin() { |
|
39 |
// 准备参数 |
|
40 |
Long userId = randomLongId(); |
|
41 |
String templateCode = randomString(); |
|
42 |
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
|
43 |
.put("op", "login").build(); |
|
44 |
// mock NotifyTemplateService 的方法 |
|
45 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { |
|
46 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
47 |
o.setContent("验证码为{code}, 操作为{op}"); |
|
48 |
o.setParams(Lists.newArrayList("code", "op")); |
|
49 |
}); |
|
50 |
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
|
51 |
String content = randomString(); |
|
52 |
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams))) |
|
53 |
.thenReturn(content); |
|
54 |
// mock NotifyMessageService 的方法 |
|
55 |
Long messageId = randomLongId(); |
|
56 |
when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), |
|
57 |
eq(template), eq(content), eq(templateParams))).thenReturn(messageId); |
|
58 |
|
|
59 |
// 调用 |
|
60 |
Long resultMessageId = notifySendService.sendSingleNotifyToAdmin(userId, templateCode, templateParams); |
|
61 |
// 断言 |
|
62 |
assertEquals(messageId, resultMessageId); |
|
63 |
} |
|
64 |
|
|
65 |
@Test |
|
66 |
public void testSendSingleNotifyToMember() { |
|
67 |
// 准备参数 |
|
68 |
Long userId = randomLongId(); |
|
69 |
String templateCode = randomString(); |
|
70 |
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
|
71 |
.put("op", "login").build(); |
|
72 |
// mock NotifyTemplateService 的方法 |
|
73 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { |
|
74 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
75 |
o.setContent("验证码为{code}, 操作为{op}"); |
|
76 |
o.setParams(Lists.newArrayList("code", "op")); |
|
77 |
}); |
|
78 |
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
|
79 |
String content = randomString(); |
|
80 |
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams))) |
|
81 |
.thenReturn(content); |
|
82 |
// mock NotifyMessageService 的方法 |
|
83 |
Long messageId = randomLongId(); |
|
84 |
when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), |
|
85 |
eq(template), eq(content), eq(templateParams))).thenReturn(messageId); |
|
86 |
|
|
87 |
// 调用 |
|
88 |
Long resultMessageId = notifySendService.sendSingleNotifyToMember(userId, templateCode, templateParams); |
|
89 |
// 断言 |
|
90 |
assertEquals(messageId, resultMessageId); |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 发送成功,当短信模板开启时 |
|
95 |
*/ |
|
96 |
@Test |
|
97 |
public void testSendSingleNotify_successWhenMailTemplateEnable() { |
|
98 |
// 准备参数 |
|
99 |
Long userId = randomLongId(); |
|
100 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
101 |
String templateCode = randomString(); |
|
102 |
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
|
103 |
.put("op", "login").build(); |
|
104 |
// mock NotifyTemplateService 的方法 |
|
105 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { |
|
106 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
107 |
o.setContent("验证码为{code}, 操作为{op}"); |
|
108 |
o.setParams(Lists.newArrayList("code", "op")); |
|
109 |
}); |
|
110 |
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
|
111 |
String content = randomString(); |
|
112 |
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams))) |
|
113 |
.thenReturn(content); |
|
114 |
// mock NotifyMessageService 的方法 |
|
115 |
Long messageId = randomLongId(); |
|
116 |
when(notifyMessageService.createNotifyMessage(eq(userId), eq(userType), |
|
117 |
eq(template), eq(content), eq(templateParams))).thenReturn(messageId); |
|
118 |
|
|
119 |
// 调用 |
|
120 |
Long resultMessageId = notifySendService.sendSingleNotify(userId, userType, templateCode, templateParams); |
|
121 |
// 断言 |
|
122 |
assertEquals(messageId, resultMessageId); |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* 发送成功,当短信模板关闭时 |
|
127 |
*/ |
|
128 |
@Test |
|
129 |
public void testSendSingleMail_successWhenSmsTemplateDisable() { |
|
130 |
// 准备参数 |
|
131 |
Long userId = randomLongId(); |
|
132 |
Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
|
133 |
String templateCode = randomString(); |
|
134 |
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
|
135 |
.put("op", "login").build(); |
|
136 |
// mock NotifyTemplateService 的方法 |
|
137 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> { |
|
138 |
o.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
139 |
o.setContent("验证码为{code}, 操作为{op}"); |
|
140 |
o.setParams(Lists.newArrayList("code", "op")); |
|
141 |
}); |
|
142 |
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
|
143 |
|
|
144 |
// 调用 |
|
145 |
Long resultMessageId = notifySendService.sendSingleNotify(userId, userType, templateCode, templateParams); |
|
146 |
// 断言 |
|
147 |
assertNull(resultMessageId); |
|
148 |
verify(notifyTemplateService, never()).formatNotifyTemplateContent(anyString(), anyMap()); |
|
149 |
verify(notifyMessageService, never()).createNotifyMessage(anyLong(), anyInt(), any(), anyString(), anyMap()); |
|
150 |
} |
|
151 |
|
|
152 |
@Test |
|
153 |
public void testCheckMailTemplateValid_notExists() { |
|
154 |
// 准备参数 |
|
155 |
String templateCode = randomString(); |
|
156 |
// mock 方法 |
|
157 |
|
|
158 |
// 调用,并断言异常 |
|
159 |
assertServiceException(() -> notifySendService.validateNotifyTemplate(templateCode), |
|
160 |
NOTICE_NOT_FOUND); |
|
161 |
} |
|
162 |
|
|
163 |
@Test |
|
164 |
public void testCheckTemplateParams_paramMiss() { |
|
165 |
// 准备参数 |
|
166 |
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, |
|
167 |
o -> o.setParams(Lists.newArrayList("code"))); |
|
168 |
Map<String, Object> templateParams = new HashMap<>(); |
|
169 |
// mock 方法 |
|
170 |
|
|
171 |
// 调用,并断言异常 |
|
172 |
assertServiceException(() -> notifySendService.validateTemplateParams(template, templateParams), |
|
173 |
NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code"); |
|
174 |
} |
|
175 |
|
|
176 |
@Test |
|
177 |
public void testSendBatchNotify() { |
|
178 |
// 准备参数 |
|
179 |
// mock 方法 |
|
180 |
|
|
181 |
// 调用 |
|
182 |
UnsupportedOperationException exception = Assertions.assertThrows( |
|
183 |
UnsupportedOperationException.class, |
|
184 |
() -> notifySendService.sendBatchNotify(null, null, null, null, null) |
|
185 |
); |
|
186 |
// 断言 |
|
187 |
assertEquals("暂时不支持该操作,感兴趣可以实现该功能哟!", exception.getMessage()); |
|
188 |
} |
|
189 |
|
|
190 |
} |