Commit 2ba4dec0da5ec18d8bd1302863f422ef54550d3b

Authored by YunaiV
1 parent 62dc3296

完善 notify 单元测试

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImpl.java
@@ -44,13 +44,13 @@ public class NotifySendServiceImpl implements NotifySendService { @@ -44,13 +44,13 @@ public class NotifySendServiceImpl implements NotifySendService {
44 @Override 44 @Override
45 public Long sendSingleNotify(Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) { 45 public Long sendSingleNotify(Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) {
46 // 校验模版 46 // 校验模版
47 - NotifyTemplateDO template = checkNotifyTemplateValid(templateCode); 47 + NotifyTemplateDO template = validateNotifyTemplate(templateCode);
48 if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { 48 if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
49 log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({}/{})发送]", templateCode, userId, userType); 49 log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({}/{})发送]", templateCode, userId, userType);
50 return null; 50 return null;
51 } 51 }
52 // 校验参数 52 // 校验参数
53 - checkTemplateParams(template, templateParams); 53 + validateTemplateParams(template, templateParams);
54 54
55 // 发送站内信 55 // 发送站内信
56 String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams); 56 String content = notifyTemplateService.formatNotifyTemplateContent(template.getContent(), templateParams);
@@ -58,7 +58,7 @@ public class NotifySendServiceImpl implements NotifySendService { @@ -58,7 +58,7 @@ public class NotifySendServiceImpl implements NotifySendService {
58 } 58 }
59 59
60 @VisibleForTesting 60 @VisibleForTesting
61 - public NotifyTemplateDO checkNotifyTemplateValid(String templateCode) { 61 + public NotifyTemplateDO validateNotifyTemplate(String templateCode) {
62 // 获得站内信模板。考虑到效率,从缓存中获取 62 // 获得站内信模板。考虑到效率,从缓存中获取
63 NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode); 63 NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode);
64 // 站内信模板不存在 64 // 站内信模板不存在
@@ -75,7 +75,7 @@ public class NotifySendServiceImpl implements NotifySendService { @@ -75,7 +75,7 @@ public class NotifySendServiceImpl implements NotifySendService {
75 * @param templateParams 参数列表 75 * @param templateParams 参数列表
76 */ 76 */
77 @VisibleForTesting 77 @VisibleForTesting
78 - public void checkTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) { 78 + public void validateTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) {
79 template.getParams().forEach(key -> { 79 template.getParams().forEach(key -> {
80 Object value = templateParams.get(key); 80 Object value = templateParams.get(key);
81 if (value == null) { 81 if (value == null) {
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImplTest.java
@@ -181,7 +181,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { @@ -181,7 +181,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest {
181 } 181 }
182 182
183 @Test 183 @Test
184 - public void testGetTemplate() { 184 + public void testGetMailTemplate() {
185 // mock 数据 185 // mock 数据
186 MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); 186 MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class);
187 mailTemplateMapper.insert(dbMailTemplate); 187 mailTemplateMapper.insert(dbMailTemplate);
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java
@@ -109,6 +109,20 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { @@ -109,6 +109,20 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest {
109 } 109 }
110 110
111 @Test 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
112 public void testGetMyNotifyMessagePage() { 126 public void testGetMyNotifyMessagePage() {
113 // mock 数据 127 // mock 数据
114 NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 128 NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifySendServiceImplTest.java
@@ -4,8 +4,6 @@ import cn.hutool.core.map.MapUtil; @@ -4,8 +4,6 @@ import cn.hutool.core.map.MapUtil;
4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; 4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
5 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; 5 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
6 import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; 6 import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
7 -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;  
8 -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;  
9 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO; 7 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
10 import org.assertj.core.util.Lists; 8 import org.assertj.core.util.Lists;
11 import org.junit.jupiter.api.Test; 9 import org.junit.jupiter.api.Test;
@@ -18,7 +16,8 @@ import java.util.Map; @@ -18,7 +16,8 @@ import java.util.Map;
18 import static cn.hutool.core.util.RandomUtil.randomEle; 16 import static cn.hutool.core.util.RandomUtil.randomEle;
19 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; 17 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
20 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; 18 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
21 -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; 19 +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
  20 +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS;
22 import static org.junit.jupiter.api.Assertions.assertEquals; 21 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNull; 22 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.ArgumentMatchers.eq;
@@ -34,6 +33,62 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { @@ -34,6 +33,62 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
34 @Mock 33 @Mock
35 private NotifyMessageService notifyMessageService; 34 private NotifyMessageService notifyMessageService;
36 35
  36 + @Test
  37 + public void testSendSingleNotifyToAdmin() {
  38 + // 准备参数
  39 + Long userId = randomLongId();
  40 + String templateCode = randomString();
  41 + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
  42 + .put("op", "login").build();
  43 + // mock NotifyTemplateService 的方法
  44 + NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
  45 + o.setStatus(CommonStatusEnum.ENABLE.getStatus());
  46 + o.setContent("验证码为{code}, 操作为{op}");
  47 + o.setParams(Lists.newArrayList("code", "op"));
  48 + });
  49 + when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
  50 + String content = randomString();
  51 + when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams)))
  52 + .thenReturn(content);
  53 + // mock NotifyMessageService 的方法
  54 + Long messageId = randomLongId();
  55 + when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.ADMIN.getValue()),
  56 + eq(template), eq(content), eq(templateParams))).thenReturn(messageId);
  57 +
  58 + // 调用
  59 + Long resultMessageId = notifySendService.sendSingleNotifyToAdmin(userId, templateCode, templateParams);
  60 + // 断言
  61 + assertEquals(messageId, resultMessageId);
  62 + }
  63 +
  64 + @Test
  65 + public void testSendSingleNotifyToMember() {
  66 + // 准备参数
  67 + Long userId = randomLongId();
  68 + String templateCode = randomString();
  69 + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
  70 + .put("op", "login").build();
  71 + // mock NotifyTemplateService 的方法
  72 + NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
  73 + o.setStatus(CommonStatusEnum.ENABLE.getStatus());
  74 + o.setContent("验证码为{code}, 操作为{op}");
  75 + o.setParams(Lists.newArrayList("code", "op"));
  76 + });
  77 + when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
  78 + String content = randomString();
  79 + when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams)))
  80 + .thenReturn(content);
  81 + // mock NotifyMessageService 的方法
  82 + Long messageId = randomLongId();
  83 + when(notifyMessageService.createNotifyMessage(eq(userId), eq(UserTypeEnum.MEMBER.getValue()),
  84 + eq(template), eq(content), eq(templateParams))).thenReturn(messageId);
  85 +
  86 + // 调用
  87 + Long resultMessageId = notifySendService.sendSingleNotifyToMember(userId, templateCode, templateParams);
  88 + // 断言
  89 + assertEquals(messageId, resultMessageId);
  90 + }
  91 +
37 /** 92 /**
38 * 发送成功,当短信模板开启时 93 * 发送成功,当短信模板开启时
39 */ 94 */
@@ -100,7 +155,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { @@ -100,7 +155,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
100 // mock 方法 155 // mock 方法
101 156
102 // 调用,并断言异常 157 // 调用,并断言异常
103 - assertServiceException(() -> notifySendService.checkNotifyTemplateValid(templateCode), 158 + assertServiceException(() -> notifySendService.validateNotifyTemplate(templateCode),
104 NOTICE_NOT_FOUND); 159 NOTICE_NOT_FOUND);
105 } 160 }
106 161
@@ -113,7 +168,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest { @@ -113,7 +168,7 @@ class NotifySendServiceImplTest extends BaseMockitoUnitTest {
113 // mock 方法 168 // mock 方法
114 169
115 // 调用,并断言异常 170 // 调用,并断言异常
116 - assertServiceException(() -> notifySendService.checkTemplateParams(template, templateParams), 171 + assertServiceException(() -> notifySendService.validateTemplateParams(template, templateParams),
117 NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code"); 172 NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code");
118 } 173 }
119 174
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyTemplateServiceImplTest.java
@@ -14,6 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; @@ -14,6 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean;
14 import org.springframework.context.annotation.Import; 14 import org.springframework.context.annotation.Import;
15 15
16 import javax.annotation.Resource; 16 import javax.annotation.Resource;
  17 +import java.util.HashMap;
  18 +import java.util.Map;
17 19
18 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; 20 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
19 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; 21 import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
@@ -143,4 +145,44 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { @@ -143,4 +145,44 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest {
143 assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0)); 145 assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0));
144 } 146 }
145 147
  148 + @Test
  149 + public void testGetNotifyTemplate() {
  150 + // mock 数据
  151 + NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class);
  152 + notifyTemplateMapper.insert(dbNotifyTemplate);
  153 + // 准备参数
  154 + Long id = dbNotifyTemplate.getId();
  155 +
  156 + // 调用
  157 + NotifyTemplateDO notifyTemplate = notifyTemplateService.getNotifyTemplate(id);
  158 + // 断言
  159 + assertPojoEquals(dbNotifyTemplate, notifyTemplate);
  160 + }
  161 +
  162 + @Test
  163 + public void testGetNotifyTemplateByCodeFromCache() {
  164 + // mock 数据
  165 + NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class);
  166 + notifyTemplateMapper.insert(dbNotifyTemplate);
  167 + notifyTemplateService.initLocalCache();
  168 + // 准备参数
  169 + String code = dbNotifyTemplate.getCode();
  170 +
  171 + // 调用
  172 + NotifyTemplateDO notifyTemplate = notifyTemplateService.getNotifyTemplateByCodeFromCache(code);
  173 + // 断言
  174 + assertPojoEquals(dbNotifyTemplate, notifyTemplate);
  175 + }
  176 +
  177 + @Test
  178 + public void testFormatNotifyTemplateContent() {
  179 + // 准备参数
  180 + Map<String, Object> params = new HashMap<>();
  181 + params.put("name", "小红");
  182 + params.put("what", "饭");
  183 +
  184 + // 调用,并断言
  185 + assertEquals("小红,你好,饭吃了吗?",
  186 + notifyTemplateService.formatNotifyTemplateContent("{name},你好,{what}吃了吗?", params));
  187 + }
146 } 188 }