Commit 62dc3296d65e2b4f03b95da40cbab10ef0ca9095
1 parent
3d3c88dd
完善 mail 单元测试
Showing
6 changed files
with
317 additions
and
31 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImpl.java
| @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.mail; | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.mail; | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.hutool.extra.mail.MailAccount; | 4 | import cn.hutool.extra.mail.MailAccount; |
| 5 | import cn.hutool.extra.mail.MailUtil; | 5 | import cn.hutool.extra.mail.MailUtil; |
| 6 | -import cn.iocoder.yudao.framework.common.core.KeyValue; | ||
| 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 9 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; | 8 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; |
| @@ -20,15 +19,13 @@ import org.springframework.stereotype.Service; | @@ -20,15 +19,13 @@ import org.springframework.stereotype.Service; | ||
| 20 | import org.springframework.validation.annotation.Validated; | 19 | import org.springframework.validation.annotation.Validated; |
| 21 | 20 | ||
| 22 | import javax.annotation.Resource; | 21 | import javax.annotation.Resource; |
| 23 | -import java.util.List; | ||
| 24 | import java.util.Map; | 22 | import java.util.Map; |
| 25 | -import java.util.stream.Collectors; | ||
| 26 | 23 | ||
| 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 25 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 29 | 26 | ||
| 30 | /** | 27 | /** |
| 31 | - * 邮箱模版 服务实现类 | 28 | + * 邮箱发送 Service 实现类 |
| 32 | * | 29 | * |
| 33 | * @author wangjingyi | 30 | * @author wangjingyi |
| 34 | * @since 2022-03-21 | 31 | * @since 2022-03-21 |
| @@ -82,13 +79,13 @@ public class MailSendServiceImpl implements MailSendService { | @@ -82,13 +79,13 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 82 | public Long sendSingleMail(String mail, Long userId, Integer userType, | 79 | public Long sendSingleMail(String mail, Long userId, Integer userType, |
| 83 | String templateCode, Map<String, Object> templateParams) { | 80 | String templateCode, Map<String, Object> templateParams) { |
| 84 | // 校验邮箱模版是否合法 | 81 | // 校验邮箱模版是否合法 |
| 85 | - MailTemplateDO template = checkMailTemplateValid(templateCode); | 82 | + MailTemplateDO template = validateMailTemplate(templateCode); |
| 86 | // 校验邮箱账号是否合法 | 83 | // 校验邮箱账号是否合法 |
| 87 | - MailAccountDO account = checkMailAccountValid(template.getAccountId()); | 84 | + MailAccountDO account = validateMailAccount(template.getAccountId()); |
| 88 | 85 | ||
| 89 | // 校验邮箱是否存在 | 86 | // 校验邮箱是否存在 |
| 90 | - mail = checkMail(mail); | ||
| 91 | - checkTemplateParams(template, templateParams); | 87 | + mail = validateMail(mail); |
| 88 | + validateTemplateParams(template, templateParams); | ||
| 92 | 89 | ||
| 93 | // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 | 90 | // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 |
| 94 | Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()); | 91 | Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()); |
| @@ -106,7 +103,7 @@ public class MailSendServiceImpl implements MailSendService { | @@ -106,7 +103,7 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 106 | @Override | 103 | @Override |
| 107 | public void doSendMail(MailSendMessage message) { | 104 | public void doSendMail(MailSendMessage message) { |
| 108 | // 1. 创建发送账号 | 105 | // 1. 创建发送账号 |
| 109 | - MailAccountDO account = checkMailAccountValid(message.getAccountId()); | 106 | + MailAccountDO account = validateMailAccount(message.getAccountId()); |
| 110 | MailAccount mailAccount = MailAccountConvert.INSTANCE.convert(account, message.getNickname()); | 107 | MailAccount mailAccount = MailAccountConvert.INSTANCE.convert(account, message.getNickname()); |
| 111 | // 2. 发送邮件 | 108 | // 2. 发送邮件 |
| 112 | try { | 109 | try { |
| @@ -121,7 +118,7 @@ public class MailSendServiceImpl implements MailSendService { | @@ -121,7 +118,7 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 121 | } | 118 | } |
| 122 | 119 | ||
| 123 | @VisibleForTesting | 120 | @VisibleForTesting |
| 124 | - public MailTemplateDO checkMailTemplateValid(String templateCode) { | 121 | + MailTemplateDO validateMailTemplate(String templateCode) { |
| 125 | // 获得邮件模板。考虑到效率,从缓存中获取 | 122 | // 获得邮件模板。考虑到效率,从缓存中获取 |
| 126 | MailTemplateDO template = mailTemplateService.getMailTemplateByCodeFromCache(templateCode); | 123 | MailTemplateDO template = mailTemplateService.getMailTemplateByCodeFromCache(templateCode); |
| 127 | // 邮件模板不存在 | 124 | // 邮件模板不存在 |
| @@ -132,7 +129,7 @@ public class MailSendServiceImpl implements MailSendService { | @@ -132,7 +129,7 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 132 | } | 129 | } |
| 133 | 130 | ||
| 134 | @VisibleForTesting | 131 | @VisibleForTesting |
| 135 | - public MailAccountDO checkMailAccountValid(Long accountId) { | 132 | + MailAccountDO validateMailAccount(Long accountId) { |
| 136 | // 获得邮箱账号。考虑到效率,从缓存中获取 | 133 | // 获得邮箱账号。考虑到效率,从缓存中获取 |
| 137 | MailAccountDO account = mailAccountService.getMailAccountFromCache(accountId); | 134 | MailAccountDO account = mailAccountService.getMailAccountFromCache(accountId); |
| 138 | // 邮箱账号不存在 | 135 | // 邮箱账号不存在 |
| @@ -143,7 +140,7 @@ public class MailSendServiceImpl implements MailSendService { | @@ -143,7 +140,7 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 143 | } | 140 | } |
| 144 | 141 | ||
| 145 | @VisibleForTesting | 142 | @VisibleForTesting |
| 146 | - public String checkMail(String mail) { | 143 | + String validateMail(String mail) { |
| 147 | if (StrUtil.isEmpty(mail)) { | 144 | if (StrUtil.isEmpty(mail)) { |
| 148 | throw exception(MAIL_SEND_MAIL_NOT_EXISTS); | 145 | throw exception(MAIL_SEND_MAIL_NOT_EXISTS); |
| 149 | } | 146 | } |
| @@ -157,7 +154,7 @@ public class MailSendServiceImpl implements MailSendService { | @@ -157,7 +154,7 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 157 | * @param templateParams 参数列表 | 154 | * @param templateParams 参数列表 |
| 158 | */ | 155 | */ |
| 159 | @VisibleForTesting | 156 | @VisibleForTesting |
| 160 | - public void checkTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) { | 157 | + void validateTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) { |
| 161 | template.getParams().forEach(key -> { | 158 | template.getParams().forEach(key -> { |
| 162 | Object value = templateParams.get(key); | 159 | Object value = templateParams.get(key); |
| 163 | if (value == null) { | 160 | if (value == null) { |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailAccountServiceImplTest.java
| @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou | ||
| 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; | 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; |
| 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; |
| 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 9 | -import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | ||
| 10 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; | 9 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; |
| 11 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 10 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 12 | import org.junit.jupiter.api.Test; | 11 | import org.junit.jupiter.api.Test; |
| @@ -14,7 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; | @@ -14,7 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 14 | import org.springframework.context.annotation.Import; | 13 | import org.springframework.context.annotation.Import; |
| 15 | 14 | ||
| 16 | import javax.annotation.Resource; | 15 | import javax.annotation.Resource; |
| 17 | - | 16 | +import java.util.List; |
| 18 | import java.util.Map; | 17 | import java.util.Map; |
| 19 | 18 | ||
| 20 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | 19 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| @@ -23,7 +22,9 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | @@ -23,7 +22,9 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||
| 23 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | 22 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 24 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; | 23 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; |
| 25 | import static org.junit.jupiter.api.Assertions.*; | 24 | import static org.junit.jupiter.api.Assertions.*; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 26 | import static org.mockito.Mockito.verify; | 26 | import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; | ||
| 27 | 28 | ||
| 28 | /** | 29 | /** |
| 29 | * {@link MailAccountServiceImpl} 的单元测试类 | 30 | * {@link MailAccountServiceImpl} 的单元测试类 |
| @@ -109,6 +110,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | @@ -109,6 +110,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||
| 109 | mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | 110 | mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 |
| 110 | // 准备参数 | 111 | // 准备参数 |
| 111 | Long id = dbMailAccount.getId(); | 112 | Long id = dbMailAccount.getId(); |
| 113 | + // mock 方法(无关联模版) | ||
| 114 | + when(mailTemplateService.countByAccountId(eq(id))).thenReturn(0L); | ||
| 112 | 115 | ||
| 113 | // 调用 | 116 | // 调用 |
| 114 | mailAccountService.deleteMailAccount(id); | 117 | mailAccountService.deleteMailAccount(id); |
| @@ -118,6 +121,21 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | @@ -118,6 +121,21 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||
| 118 | } | 121 | } |
| 119 | 122 | ||
| 120 | @Test | 123 | @Test |
| 124 | + public void testGetMailAccountFromCache() { | ||
| 125 | + // mock 数据 | ||
| 126 | + MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | ||
| 127 | + mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||
| 128 | + mailAccountService.initLocalCache(); | ||
| 129 | + // 准备参数 | ||
| 130 | + Long id = dbMailAccount.getId(); | ||
| 131 | + | ||
| 132 | + // 调用 | ||
| 133 | + MailAccountDO mailAccount = mailAccountService.getMailAccountFromCache(id); | ||
| 134 | + // 断言 | ||
| 135 | + assertPojoEquals(dbMailAccount, mailAccount); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + @Test | ||
| 121 | public void testDeleteMailAccount_notExists() { | 139 | public void testDeleteMailAccount_notExists() { |
| 122 | // 准备参数 | 140 | // 准备参数 |
| 123 | Long id = randomLongId(); | 141 | Long id = randomLongId(); |
| @@ -151,4 +169,35 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | @@ -151,4 +169,35 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||
| 151 | assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); | 169 | assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); |
| 152 | } | 170 | } |
| 153 | 171 | ||
| 172 | + @Test | ||
| 173 | + public void testGetMailAccount() { | ||
| 174 | + // mock 数据 | ||
| 175 | + MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | ||
| 176 | + mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||
| 177 | + // 准备参数 | ||
| 178 | + Long id = dbMailAccount.getId(); | ||
| 179 | + | ||
| 180 | + // 调用 | ||
| 181 | + MailAccountDO mailAccount = mailAccountService.getMailAccount(id); | ||
| 182 | + // 断言 | ||
| 183 | + assertPojoEquals(dbMailAccount, mailAccount); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + @Test | ||
| 187 | + public void testGetMailAccountList() { | ||
| 188 | + // mock 数据 | ||
| 189 | + MailAccountDO dbMailAccount01 = randomPojo(MailAccountDO.class); | ||
| 190 | + mailAccountMapper.insert(dbMailAccount01); | ||
| 191 | + MailAccountDO dbMailAccount02 = randomPojo(MailAccountDO.class); | ||
| 192 | + mailAccountMapper.insert(dbMailAccount02); | ||
| 193 | + // 准备参数 | ||
| 194 | + | ||
| 195 | + // 调用 | ||
| 196 | + List<MailAccountDO> list = mailAccountService.getMailAccountList(); | ||
| 197 | + // 断言 | ||
| 198 | + assertEquals(2, list.size()); | ||
| 199 | + assertPojoEquals(dbMailAccount01, list.get(0)); | ||
| 200 | + assertPojoEquals(dbMailAccount02, list.get(1)); | ||
| 201 | + } | ||
| 202 | + | ||
| 154 | } | 203 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailLogServiceImplTest.java
| @@ -117,6 +117,20 @@ public class MailLogServiceImplTest extends BaseDbUnitTest { | @@ -117,6 +117,20 @@ public class MailLogServiceImplTest extends BaseDbUnitTest { | ||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | @Test | 119 | @Test |
| 120 | + public void testGetMailLog() { | ||
| 121 | + // mock 数据 | ||
| 122 | + MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> o.setTemplateParams(randomTemplateParams())); | ||
| 123 | + mailLogMapper.insert(dbMailLog); | ||
| 124 | + // 准备参数 | ||
| 125 | + Long id = dbMailLog.getId(); | ||
| 126 | + | ||
| 127 | + // 调用 | ||
| 128 | + MailLogDO mailLog = mailLogService.getMailLog(id); | ||
| 129 | + // 断言 | ||
| 130 | + assertPojoEquals(dbMailLog, mailLog); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + @Test | ||
| 120 | public void testGetMailLogPage() { | 134 | public void testGetMailLogPage() { |
| 121 | // mock 数据 | 135 | // mock 数据 |
| 122 | MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> { // 等会查询到 | 136 | MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> { // 等会查询到 |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java
| @@ -6,14 +6,20 @@ import cn.hutool.extra.mail.MailUtil; | @@ -6,14 +6,20 @@ import cn.hutool.extra.mail.MailUtil; | ||
| 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 8 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | 8 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 9 | +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; | ||
| 9 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 10 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 12 | +import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||
| 13 | +import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; | ||
| 11 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 14 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 15 | +import cn.iocoder.yudao.module.system.service.member.MemberService; | ||
| 16 | +import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||
| 12 | import org.assertj.core.util.Lists; | 17 | import org.assertj.core.util.Lists; |
| 13 | import org.junit.jupiter.api.Disabled; | 18 | import org.junit.jupiter.api.Disabled; |
| 14 | import org.junit.jupiter.api.Test; | 19 | import org.junit.jupiter.api.Test; |
| 15 | import org.mockito.InjectMocks; | 20 | import org.mockito.InjectMocks; |
| 16 | import org.mockito.Mock; | 21 | import org.mockito.Mock; |
| 22 | +import org.mockito.MockedStatic; | ||
| 17 | 23 | ||
| 18 | import java.util.HashMap; | 24 | import java.util.HashMap; |
| 19 | import java.util.Map; | 25 | import java.util.Map; |
| @@ -23,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | @@ -23,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||
| 23 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 24 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 25 | import static org.junit.jupiter.api.Assertions.assertEquals; | 31 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 26 | import static org.mockito.ArgumentMatchers.eq; | 33 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.Mockito.*; | 34 | import static org.mockito.Mockito.*; |
| 28 | 35 | ||
| @@ -32,6 +39,10 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -32,6 +39,10 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 32 | private MailSendServiceImpl mailSendService; | 39 | private MailSendServiceImpl mailSendService; |
| 33 | 40 | ||
| 34 | @Mock | 41 | @Mock |
| 42 | + private AdminUserService adminUserService; | ||
| 43 | + @Mock | ||
| 44 | + private MemberService memberService; | ||
| 45 | + @Mock | ||
| 35 | private MailAccountService mailAccountService; | 46 | private MailAccountService mailAccountService; |
| 36 | @Mock | 47 | @Mock |
| 37 | private MailTemplateService mailTemplateService; | 48 | private MailTemplateService mailTemplateService; |
| @@ -55,6 +66,82 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -55,6 +66,82 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 55 | System.out.println("发送结果:" + messageId); | 66 | System.out.println("发送结果:" + messageId); |
| 56 | } | 67 | } |
| 57 | 68 | ||
| 69 | + @Test | ||
| 70 | + public void testSendSingleMailToAdmin() { | ||
| 71 | + // 准备参数 | ||
| 72 | + Long userId = randomLongId(); | ||
| 73 | + String templateCode = RandomUtils.randomString(); | ||
| 74 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||
| 75 | + .put("op", "login").build(); | ||
| 76 | + // mock adminUserService 的方法 | ||
| 77 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setMobile("15601691300")); | ||
| 78 | + when(adminUserService.getUser(eq(userId))).thenReturn(user); | ||
| 79 | + | ||
| 80 | + // mock MailTemplateService 的方法 | ||
| 81 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||
| 82 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 83 | + o.setContent("验证码为{code}, 操作为{op}"); | ||
| 84 | + o.setParams(Lists.newArrayList("code", "op")); | ||
| 85 | + }); | ||
| 86 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||
| 87 | + String content = RandomUtils.randomString(); | ||
| 88 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||
| 89 | + .thenReturn(content); | ||
| 90 | + // mock MailAccountService 的方法 | ||
| 91 | + MailAccountDO account = randomPojo(MailAccountDO.class); | ||
| 92 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||
| 93 | + // mock MailLogService 的方法 | ||
| 94 | + Long mailLogId = randomLongId(); | ||
| 95 | + when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), eq(user.getEmail()), | ||
| 96 | + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | ||
| 97 | + | ||
| 98 | + // 调用 | ||
| 99 | + Long resultMailLogId = mailSendService.sendSingleMailToAdmin(null, userId, templateCode, templateParams); | ||
| 100 | + // 断言 | ||
| 101 | + assertEquals(mailLogId, resultMailLogId); | ||
| 102 | + // 断言调用 | ||
| 103 | + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(user.getEmail()), | ||
| 104 | + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + @Test | ||
| 108 | + public void testSendSingleMailToMember() { | ||
| 109 | + // 准备参数 | ||
| 110 | + Long userId = randomLongId(); | ||
| 111 | + String templateCode = RandomUtils.randomString(); | ||
| 112 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||
| 113 | + .put("op", "login").build(); | ||
| 114 | + // mock memberService 的方法 | ||
| 115 | + String mail = randomEmail(); | ||
| 116 | + when(memberService.getMemberUserEmail(eq(userId))).thenReturn(mail); | ||
| 117 | + | ||
| 118 | + // mock MailTemplateService 的方法 | ||
| 119 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||
| 120 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 121 | + o.setContent("验证码为{code}, 操作为{op}"); | ||
| 122 | + o.setParams(Lists.newArrayList("code", "op")); | ||
| 123 | + }); | ||
| 124 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||
| 125 | + String content = RandomUtils.randomString(); | ||
| 126 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||
| 127 | + .thenReturn(content); | ||
| 128 | + // mock MailAccountService 的方法 | ||
| 129 | + MailAccountDO account = randomPojo(MailAccountDO.class); | ||
| 130 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||
| 131 | + // mock MailLogService 的方法 | ||
| 132 | + Long mailLogId = randomLongId(); | ||
| 133 | + when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), eq(mail), | ||
| 134 | + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | ||
| 135 | + | ||
| 136 | + // 调用 | ||
| 137 | + Long resultMailLogId = mailSendService.sendSingleMailToMember(null, userId, templateCode, templateParams); | ||
| 138 | + // 断言 | ||
| 139 | + assertEquals(mailLogId, resultMailLogId); | ||
| 140 | + // 断言调用 | ||
| 141 | + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail), | ||
| 142 | + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | ||
| 143 | + } | ||
| 144 | + | ||
| 58 | /** | 145 | /** |
| 59 | * 发送成功,当短信模板开启时 | 146 | * 发送成功,当短信模板开启时 |
| 60 | */ | 147 | */ |
| @@ -64,7 +151,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -64,7 +151,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 64 | String mail = randomEmail(); | 151 | String mail = randomEmail(); |
| 65 | Long userId = randomLongId(); | 152 | Long userId = randomLongId(); |
| 66 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); | 153 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
| 67 | - String templateCode = randomString(); | 154 | + String templateCode = RandomUtils.randomString(); |
| 68 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | 155 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
| 69 | .put("op", "login").build(); | 156 | .put("op", "login").build(); |
| 70 | // mock MailTemplateService 的方法 | 157 | // mock MailTemplateService 的方法 |
| @@ -74,7 +161,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -74,7 +161,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 74 | o.setParams(Lists.newArrayList("code", "op")); | 161 | o.setParams(Lists.newArrayList("code", "op")); |
| 75 | }); | 162 | }); |
| 76 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | 163 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
| 77 | - String content = randomString(); | 164 | + String content = RandomUtils.randomString(); |
| 78 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | 165 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) |
| 79 | .thenReturn(content); | 166 | .thenReturn(content); |
| 80 | // mock MailAccountService 的方法 | 167 | // mock MailAccountService 的方法 |
| @@ -103,7 +190,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -103,7 +190,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 103 | String mail = randomEmail(); | 190 | String mail = randomEmail(); |
| 104 | Long userId = randomLongId(); | 191 | Long userId = randomLongId(); |
| 105 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); | 192 | Integer userType = randomEle(UserTypeEnum.values()).getValue(); |
| 106 | - String templateCode = randomString(); | 193 | + String templateCode = RandomUtils.randomString(); |
| 107 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | 194 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
| 108 | .put("op", "login").build(); | 195 | .put("op", "login").build(); |
| 109 | // mock MailTemplateService 的方法 | 196 | // mock MailTemplateService 的方法 |
| @@ -113,7 +200,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -113,7 +200,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 113 | o.setParams(Lists.newArrayList("code", "op")); | 200 | o.setParams(Lists.newArrayList("code", "op")); |
| 114 | }); | 201 | }); |
| 115 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | 202 | when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); |
| 116 | - String content = randomString(); | 203 | + String content = RandomUtils.randomString(); |
| 117 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | 204 | when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) |
| 118 | .thenReturn(content); | 205 | .thenReturn(content); |
| 119 | // mock MailAccountService 的方法 | 206 | // mock MailAccountService 的方法 |
| @@ -134,18 +221,18 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -134,18 +221,18 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 134 | } | 221 | } |
| 135 | 222 | ||
| 136 | @Test | 223 | @Test |
| 137 | - public void testCheckMailTemplateValid_notExists() { | 224 | + public void testValidateMailTemplateValid_notExists() { |
| 138 | // 准备参数 | 225 | // 准备参数 |
| 139 | - String templateCode = randomString(); | 226 | + String templateCode = RandomUtils.randomString(); |
| 140 | // mock 方法 | 227 | // mock 方法 |
| 141 | 228 | ||
| 142 | // 调用,并断言异常 | 229 | // 调用,并断言异常 |
| 143 | - assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode), | 230 | + assertServiceException(() -> mailSendService.validateMailTemplate(templateCode), |
| 144 | MAIL_TEMPLATE_NOT_EXISTS); | 231 | MAIL_TEMPLATE_NOT_EXISTS); |
| 145 | } | 232 | } |
| 146 | 233 | ||
| 147 | @Test | 234 | @Test |
| 148 | - public void testCheckTemplateParams_paramMiss() { | 235 | + public void testValidateTemplateParams_paramMiss() { |
| 149 | // 准备参数 | 236 | // 准备参数 |
| 150 | MailTemplateDO template = randomPojo(MailTemplateDO.class, | 237 | MailTemplateDO template = randomPojo(MailTemplateDO.class, |
| 151 | o -> o.setParams(Lists.newArrayList("code"))); | 238 | o -> o.setParams(Lists.newArrayList("code"))); |
| @@ -153,18 +240,80 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | @@ -153,18 +240,80 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 153 | // mock 方法 | 240 | // mock 方法 |
| 154 | 241 | ||
| 155 | // 调用,并断言异常 | 242 | // 调用,并断言异常 |
| 156 | - assertServiceException(() -> mailSendService.checkTemplateParams(template, templateParams), | 243 | + assertServiceException(() -> mailSendService.validateTemplateParams(template, templateParams), |
| 157 | MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); | 244 | MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); |
| 158 | } | 245 | } |
| 159 | 246 | ||
| 160 | @Test | 247 | @Test |
| 161 | - public void testCheckMail_notExists() { | 248 | + public void testValidateMail_notExists() { |
| 162 | // 准备参数 | 249 | // 准备参数 |
| 163 | // mock 方法 | 250 | // mock 方法 |
| 164 | 251 | ||
| 165 | // 调用,并断言异常 | 252 | // 调用,并断言异常 |
| 166 | - assertServiceException(() -> mailSendService.checkMail(null), | 253 | + assertServiceException(() -> mailSendService.validateMail(null), |
| 167 | MAIL_SEND_MAIL_NOT_EXISTS); | 254 | MAIL_SEND_MAIL_NOT_EXISTS); |
| 168 | } | 255 | } |
| 169 | 256 | ||
| 257 | + @Test | ||
| 258 | + public void testDoSendMail_success() { | ||
| 259 | + try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | ||
| 260 | + // 准备参数 | ||
| 261 | + MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | ||
| 262 | + // mock 方法(获得邮箱账号) | ||
| 263 | + MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | ||
| 264 | + when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | ||
| 265 | + .thenReturn(account); | ||
| 266 | + | ||
| 267 | + // mock 方法(发送邮件) | ||
| 268 | + String messageId = randomString(); | ||
| 269 | + mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | ||
| 270 | + assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | ||
| 271 | + assertTrue(mailAccount.isAuth()); | ||
| 272 | + assertEquals(account.getUsername(), mailAccount.getUser()); | ||
| 273 | + assertEquals(account.getPassword(), mailAccount.getPass()); | ||
| 274 | + assertEquals(account.getHost(), mailAccount.getHost()); | ||
| 275 | + assertEquals(account.getPort(), mailAccount.getPort()); | ||
| 276 | + assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | ||
| 277 | + return true; | ||
| 278 | + }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | ||
| 279 | + .thenReturn(messageId); | ||
| 280 | + | ||
| 281 | + // 调用 | ||
| 282 | + mailSendService.doSendMail(message); | ||
| 283 | + // 断言 | ||
| 284 | + verify(mailLogService).updateMailSendResult(eq(message.getLogId()), eq(messageId), isNull()); | ||
| 285 | + } | ||
| 286 | + } | ||
| 287 | + | ||
| 288 | + @Test | ||
| 289 | + public void testDoSendMail_exception() { | ||
| 290 | + try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | ||
| 291 | + // 准备参数 | ||
| 292 | + MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | ||
| 293 | + // mock 方法(获得邮箱账号) | ||
| 294 | + MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | ||
| 295 | + when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | ||
| 296 | + .thenReturn(account); | ||
| 297 | + | ||
| 298 | + // mock 方法(发送邮件) | ||
| 299 | + Exception e = new NullPointerException("啦啦啦"); | ||
| 300 | + mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | ||
| 301 | + assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | ||
| 302 | + assertTrue(mailAccount.isAuth()); | ||
| 303 | + assertEquals(account.getUsername(), mailAccount.getUser()); | ||
| 304 | + assertEquals(account.getPassword(), mailAccount.getPass()); | ||
| 305 | + assertEquals(account.getHost(), mailAccount.getHost()); | ||
| 306 | + assertEquals(account.getPort(), mailAccount.getPort()); | ||
| 307 | + assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | ||
| 308 | + return true; | ||
| 309 | + }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | ||
| 310 | + .thenThrow(e); | ||
| 311 | + | ||
| 312 | + // 调用 | ||
| 313 | + mailSendService.doSendMail(message); | ||
| 314 | + // 断言 | ||
| 315 | + verify(mailLogService).updateMailSendResult(eq(message.getLogId()), isNull(), same(e)); | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + | ||
| 170 | } | 319 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImplTest.java
| @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||
| 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; | 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; |
| 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
| 8 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; | 8 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; |
| 9 | -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | ||
| 10 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 11 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; | 10 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; |
| 12 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 11 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| @@ -15,7 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; | @@ -15,7 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 15 | import org.springframework.context.annotation.Import; | 14 | import org.springframework.context.annotation.Import; |
| 16 | 15 | ||
| 17 | import javax.annotation.Resource; | 16 | import javax.annotation.Resource; |
| 18 | - | 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; | ||
| 19 | import java.util.Map; | 19 | import java.util.Map; |
| 20 | 20 | ||
| 21 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | 21 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
| @@ -27,6 +27,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId | @@ -27,6 +27,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId | ||
| 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; | 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; |
| 29 | import static org.junit.jupiter.api.Assertions.*; | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | +import static org.mockito.Mockito.verify; | ||
| 30 | 31 | ||
| 31 | /** | 32 | /** |
| 32 | * {@link MailTemplateServiceImpl} 的单元测试类 | 33 | * {@link MailTemplateServiceImpl} 的单元测试类 |
| @@ -72,6 +73,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | @@ -72,6 +73,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||
| 72 | // 校验记录的属性是否正确 | 73 | // 校验记录的属性是否正确 |
| 73 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); | 74 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); |
| 74 | assertPojoEquals(reqVO, mailTemplate); | 75 | assertPojoEquals(reqVO, mailTemplate); |
| 76 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | ||
| 75 | } | 77 | } |
| 76 | 78 | ||
| 77 | @Test | 79 | @Test |
| @@ -89,6 +91,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | @@ -89,6 +91,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||
| 89 | // 校验是否更新正确 | 91 | // 校验是否更新正确 |
| 90 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 | 92 | MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 |
| 91 | assertPojoEquals(reqVO, mailTemplate); | 93 | assertPojoEquals(reqVO, mailTemplate); |
| 94 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | ||
| 92 | } | 95 | } |
| 93 | 96 | ||
| 94 | @Test | 97 | @Test |
| @@ -110,8 +113,9 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | @@ -110,8 +113,9 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||
| 110 | 113 | ||
| 111 | // 调用 | 114 | // 调用 |
| 112 | mailTemplateService.deleteMailTemplate(id); | 115 | mailTemplateService.deleteMailTemplate(id); |
| 113 | - // 校验数据不存在了 | ||
| 114 | - assertNull(mailTemplateMapper.selectById(id)); | 116 | + // 校验数据不存在了 |
| 117 | + assertNull(mailTemplateMapper.selectById(id)); | ||
| 118 | + verify(mailProducer).sendMailTemplateRefreshMessage(); | ||
| 115 | } | 119 | } |
| 116 | 120 | ||
| 117 | @Test | 121 | @Test |
| @@ -160,4 +164,77 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | @@ -160,4 +164,77 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||
| 160 | assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); | 164 | assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); |
| 161 | } | 165 | } |
| 162 | 166 | ||
| 167 | + @Test | ||
| 168 | + public void testGetMailTemplateList() { | ||
| 169 | + // mock 数据 | ||
| 170 | + MailTemplateDO dbMailTemplate01 = randomPojo(MailTemplateDO.class); | ||
| 171 | + mailTemplateMapper.insert(dbMailTemplate01); | ||
| 172 | + MailTemplateDO dbMailTemplate02 = randomPojo(MailTemplateDO.class); | ||
| 173 | + mailTemplateMapper.insert(dbMailTemplate02); | ||
| 174 | + | ||
| 175 | + // 调用 | ||
| 176 | + List<MailTemplateDO> list = mailTemplateService.getMailTemplateList(); | ||
| 177 | + // 断言 | ||
| 178 | + assertEquals(2, list.size()); | ||
| 179 | + assertEquals(dbMailTemplate01, list.get(0)); | ||
| 180 | + assertEquals(dbMailTemplate02, list.get(1)); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + @Test | ||
| 184 | + public void testGetTemplate() { | ||
| 185 | + // mock 数据 | ||
| 186 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||
| 187 | + mailTemplateMapper.insert(dbMailTemplate); | ||
| 188 | + // 准备参数 | ||
| 189 | + Long id = dbMailTemplate.getId(); | ||
| 190 | + | ||
| 191 | + // 调用 | ||
| 192 | + MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(id); | ||
| 193 | + // 断言 | ||
| 194 | + assertPojoEquals(dbMailTemplate, mailTemplate); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + @Test | ||
| 198 | + public void testGetMailTemplateByCodeFromCache() { | ||
| 199 | + // mock 数据 | ||
| 200 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||
| 201 | + mailTemplateMapper.insert(dbMailTemplate); | ||
| 202 | + mailTemplateService.initLocalCache(); | ||
| 203 | + // 准备参数 | ||
| 204 | + String code = dbMailTemplate.getCode(); | ||
| 205 | + | ||
| 206 | + // 调用 | ||
| 207 | + MailTemplateDO mailTemplate = mailTemplateService.getMailTemplateByCodeFromCache(code); | ||
| 208 | + // 断言 | ||
| 209 | + assertPojoEquals(dbMailTemplate, mailTemplate); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @Test | ||
| 213 | + public void testFormatMailTemplateContent() { | ||
| 214 | + // 准备参数 | ||
| 215 | + Map<String, Object> params = new HashMap<>(); | ||
| 216 | + params.put("name", "小红"); | ||
| 217 | + params.put("what", "饭"); | ||
| 218 | + | ||
| 219 | + // 调用,并断言 | ||
| 220 | + assertEquals("小红,你好,饭吃了吗?", | ||
| 221 | + mailTemplateService.formatMailTemplateContent("{name},你好,{what}吃了吗?", params)); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + @Test | ||
| 225 | + public void testCountByAccountId() { | ||
| 226 | + // mock 数据 | ||
| 227 | + MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||
| 228 | + mailTemplateMapper.insert(dbMailTemplate); | ||
| 229 | + // 测试 accountId 不匹配 | ||
| 230 | + mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); | ||
| 231 | + // 准备参数 | ||
| 232 | + Long accountId = dbMailTemplate.getAccountId(); | ||
| 233 | + | ||
| 234 | + // 调用 | ||
| 235 | + long count = mailTemplateService.countByAccountId(accountId); | ||
| 236 | + // 断言 | ||
| 237 | + assertEquals(1, count); | ||
| 238 | + } | ||
| 239 | + | ||
| 163 | } | 240 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java
| @@ -101,7 +101,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { | @@ -101,7 +101,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 101 | String templateCode = randomString(); | 101 | String templateCode = randomString(); |
| 102 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | 102 | Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") |
| 103 | .put("op", "login").build(); | 103 | .put("op", "login").build(); |
| 104 | - // mock adminUserService 的方法 | 104 | + // mock memberService 的方法 |
| 105 | String mobile = "15601691300"; | 105 | String mobile = "15601691300"; |
| 106 | when(memberService.getMemberUserMobile(eq(userId))).thenReturn(mobile); | 106 | when(memberService.getMemberUserMobile(eq(userId))).thenReturn(mobile); |
| 107 | 107 |