Commit 5d1abd5f4447290e63741138849f771f7a85e86e
1 parent
f0d2c7a5
邮箱模块:完善 send 的单元测试
Showing
3 changed files
with
161 additions
and
9 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendSingleToUserReqDTO.java
| @@ -18,7 +18,7 @@ public class MailSendSingleToUserReqDTO { | @@ -18,7 +18,7 @@ public class MailSendSingleToUserReqDTO { | ||
| 18 | * 用户编号 | 18 | * 用户编号 |
| 19 | */ | 19 | */ |
| 20 | @NotNull(message = "用户编号不能为空") | 20 | @NotNull(message = "用户编号不能为空") |
| 21 | - private String userId; | 21 | + private Long userId; |
| 22 | /** | 22 | /** |
| 23 | * 邮箱 | 23 | * 邮箱 |
| 24 | */ | 24 | */ |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/mail/MailSendApiImpl.java
| 1 | package cn.iocoder.yudao.module.system.api.mail; | 1 | package cn.iocoder.yudao.module.system.api.mail; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.system.api.mail.dto.MailSendSingleToUserReqDTO; | 3 | import cn.iocoder.yudao.module.system.api.mail.dto.MailSendSingleToUserReqDTO; |
| 4 | +import cn.iocoder.yudao.module.system.service.mail.MailSendService; | ||
| 4 | import org.springframework.stereotype.Service; | 5 | import org.springframework.stereotype.Service; |
| 5 | import org.springframework.validation.annotation.Validated; | 6 | import org.springframework.validation.annotation.Validated; |
| 6 | 7 | ||
| 8 | +import javax.annotation.Resource; | ||
| 9 | + | ||
| 7 | /** | 10 | /** |
| 8 | - * 邮件发送 API 接口 | 11 | + * 邮件发送 API 实现类 |
| 9 | * | 12 | * |
| 10 | * @author wangjingyi | 13 | * @author wangjingyi |
| 11 | */ | 14 | */ |
| @@ -13,14 +16,19 @@ import org.springframework.validation.annotation.Validated; | @@ -13,14 +16,19 @@ import org.springframework.validation.annotation.Validated; | ||
| 13 | @Validated | 16 | @Validated |
| 14 | public class MailSendApiImpl implements MailSendApi { | 17 | public class MailSendApiImpl implements MailSendApi { |
| 15 | 18 | ||
| 19 | + @Resource | ||
| 20 | + private MailSendService mailSendService; | ||
| 21 | + | ||
| 16 | @Override | 22 | @Override |
| 17 | public Long sendSingleSmsToAdmin(MailSendSingleToUserReqDTO reqDTO) { | 23 | public Long sendSingleSmsToAdmin(MailSendSingleToUserReqDTO reqDTO) { |
| 18 | - return null; | 24 | + return mailSendService.sendSingleMailToAdmin(reqDTO.getMail(), reqDTO.getUserId(), |
| 25 | + reqDTO.getTemplateCode(), reqDTO.getTemplateParams()); | ||
| 19 | } | 26 | } |
| 20 | 27 | ||
| 21 | @Override | 28 | @Override |
| 22 | public Long sendSingleSmsToMember(MailSendSingleToUserReqDTO reqDTO) { | 29 | public Long sendSingleSmsToMember(MailSendSingleToUserReqDTO reqDTO) { |
| 23 | - return null; | 30 | + return mailSendService.sendSingleMailToMember(reqDTO.getMail(), reqDTO.getUserId(), |
| 31 | + reqDTO.getTemplateCode(), reqDTO.getTemplateParams()); | ||
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | } | 34 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java
| 1 | package cn.iocoder.yudao.module.system.service.mail; | 1 | package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.map.MapUtil; | ||
| 3 | import cn.hutool.extra.mail.MailAccount; | 4 | import cn.hutool.extra.mail.MailAccount; |
| 4 | import cn.hutool.extra.mail.MailUtil; | 5 | import cn.hutool.extra.mail.MailUtil; |
| 6 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||
| 7 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 8 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | ||
| 9 | +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.mq.producer.mail.MailProducer; | ||
| 12 | +import org.assertj.core.util.Lists; | ||
| 5 | import org.junit.jupiter.api.Disabled; | 13 | import org.junit.jupiter.api.Disabled; |
| 6 | import org.junit.jupiter.api.Test; | 14 | import org.junit.jupiter.api.Test; |
| 15 | +import org.mockito.InjectMocks; | ||
| 16 | +import org.mockito.Mock; | ||
| 7 | 17 | ||
| 8 | -import static org.junit.jupiter.api.Assertions.*; | 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; | ||
| 9 | 20 | ||
| 10 | -class MailSendServiceImplTest { | 21 | +import static cn.hutool.core.util.RandomUtil.randomEle; |
| 22 | +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | ||
| 23 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||
| 24 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 26 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 27 | +import static org.mockito.Mockito.*; | ||
| 28 | + | ||
| 29 | +class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||
| 30 | + | ||
| 31 | + @InjectMocks | ||
| 32 | + private MailSendServiceImpl mailSendService; | ||
| 33 | + | ||
| 34 | + @Mock | ||
| 35 | + private MailAccountService mailAccountService; | ||
| 36 | + @Mock | ||
| 37 | + private MailTemplateService mailTemplateService; | ||
| 38 | + @Mock | ||
| 39 | + private MailLogService mailLogService; | ||
| 40 | + @Mock | ||
| 41 | + private MailProducer mailProducer; | ||
| 11 | 42 | ||
| 12 | /** | 43 | /** |
| 13 | * 用于快速测试你的邮箱账号是否正常 | 44 | * 用于快速测试你的邮箱账号是否正常 |
| @@ -16,11 +47,124 @@ class MailSendServiceImplTest { | @@ -16,11 +47,124 @@ class MailSendServiceImplTest { | ||
| 16 | @Disabled | 47 | @Disabled |
| 17 | public void testDemo() { | 48 | public void testDemo() { |
| 18 | MailAccount mailAccount = new MailAccount() | 49 | MailAccount mailAccount = new MailAccount() |
| 19 | - .setFrom("奥特曼 <ydym_test@163.com>") | ||
| 20 | - .setHost("smtp.163.com").setPort(465).setSslEnable(true) | ||
| 21 | - .setAuth(true).setUser("ydym_test@163.com").setPass("WBZTEINMIFVRYSOE"); | 50 | +// .setFrom("奥特曼 <ydym_test@163.com>") |
| 51 | + .setFrom("ydym_test@163.com") // 邮箱地址 | ||
| 52 | + .setHost("smtp.163.com").setPort(465).setSslEnable(true) // SMTP 服务器 | ||
| 53 | + .setAuth(true).setUser("ydym_test@163.com").setPass("WBZTEINMIFVRYSOE"); // 登录账号密码 | ||
| 22 | String messageId = MailUtil.send(mailAccount, "7685413@qq.com", "主题", "内容", false); | 54 | String messageId = MailUtil.send(mailAccount, "7685413@qq.com", "主题", "内容", false); |
| 23 | System.out.println("发送结果:" + messageId); | 55 | System.out.println("发送结果:" + messageId); |
| 24 | } | 56 | } |
| 25 | 57 | ||
| 58 | + /** | ||
| 59 | + * 发送成功,当短信模板开启时 | ||
| 60 | + */ | ||
| 61 | + @Test | ||
| 62 | + public void testSendSingleMail_successWhenMailTemplateEnable() { | ||
| 63 | + // 准备参数 | ||
| 64 | + String mail = randomEmail(); | ||
| 65 | + Long userId = randomLongId(); | ||
| 66 | + Integer userType = randomEle(UserTypeEnum.values()).getValue(); | ||
| 67 | + String templateCode = randomString(); | ||
| 68 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||
| 69 | + .put("op", "login").build(); | ||
| 70 | + // mock MailTemplateService 的方法 | ||
| 71 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||
| 72 | + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 73 | + o.setContent("验证码为{code}, 操作为{op}"); | ||
| 74 | + o.setParams(Lists.newArrayList("code", "op")); | ||
| 75 | + }); | ||
| 76 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||
| 77 | + String content = randomString(); | ||
| 78 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||
| 79 | + .thenReturn(content); | ||
| 80 | + // mock MailAccountService 的方法 | ||
| 81 | + MailAccountDO account = randomPojo(MailAccountDO.class); | ||
| 82 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||
| 83 | + // mock MailLogService 的方法 | ||
| 84 | + Long mailLogId = randomLongId(); | ||
| 85 | + when(mailLogService.createMailLog(eq(userId), eq(userType), eq(mail), | ||
| 86 | + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | ||
| 87 | + | ||
| 88 | + // 调用 | ||
| 89 | + Long resultMailLogId = mailSendService.sendSingleMail(mail, userId, userType, templateCode, templateParams); | ||
| 90 | + // 断言 | ||
| 91 | + assertEquals(mailLogId, resultMailLogId); | ||
| 92 | + // 断言调用 | ||
| 93 | + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail), | ||
| 94 | + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * 发送成功,当短信模板关闭时 | ||
| 99 | + */ | ||
| 100 | + @Test | ||
| 101 | + public void testSendSingleMail_successWhenSmsTemplateDisable() { | ||
| 102 | + // 准备参数 | ||
| 103 | + String mail = randomEmail(); | ||
| 104 | + Long userId = randomLongId(); | ||
| 105 | + Integer userType = randomEle(UserTypeEnum.values()).getValue(); | ||
| 106 | + String templateCode = randomString(); | ||
| 107 | + Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||
| 108 | + .put("op", "login").build(); | ||
| 109 | + // mock MailTemplateService 的方法 | ||
| 110 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||
| 111 | + o.setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||
| 112 | + o.setContent("验证码为{code}, 操作为{op}"); | ||
| 113 | + o.setParams(Lists.newArrayList("code", "op")); | ||
| 114 | + }); | ||
| 115 | + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||
| 116 | + String content = randomString(); | ||
| 117 | + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||
| 118 | + .thenReturn(content); | ||
| 119 | + // mock MailAccountService 的方法 | ||
| 120 | + MailAccountDO account = randomPojo(MailAccountDO.class); | ||
| 121 | + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||
| 122 | + // mock MailLogService 的方法 | ||
| 123 | + Long mailLogId = randomLongId(); | ||
| 124 | + when(mailLogService.createMailLog(eq(userId), eq(userType), eq(mail), | ||
| 125 | + eq(account), eq(template), eq(content), eq(templateParams), eq(false))).thenReturn(mailLogId); | ||
| 126 | + | ||
| 127 | + // 调用 | ||
| 128 | + Long resultMailLogId = mailSendService.sendSingleMail(mail, userId, userType, templateCode, templateParams); | ||
| 129 | + // 断言 | ||
| 130 | + assertEquals(mailLogId, resultMailLogId); | ||
| 131 | + // 断言调用 | ||
| 132 | + verify(mailProducer, times(0)).sendMailSendMessage(anyLong(), anyString(), | ||
| 133 | + anyLong(), anyString(), anyString(), anyString()); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + @Test | ||
| 137 | + public void testCheckMailTemplateValid_notExists() { | ||
| 138 | + // 准备参数 | ||
| 139 | + String templateCode = randomString(); | ||
| 140 | + // mock 方法 | ||
| 141 | + | ||
| 142 | + // 调用,并断言异常 | ||
| 143 | + assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode), | ||
| 144 | + MAIL_TEMPLATE_NOT_EXISTS); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + @Test | ||
| 148 | + public void testBuildTemplateParams_paramMiss() { | ||
| 149 | + // 准备参数 | ||
| 150 | + MailTemplateDO template = randomPojo(MailTemplateDO.class, | ||
| 151 | + o -> o.setParams(Lists.newArrayList("code"))); | ||
| 152 | + Map<String, Object> templateParams = new HashMap<>(); | ||
| 153 | + // mock 方法 | ||
| 154 | + | ||
| 155 | + // 调用,并断言异常 | ||
| 156 | + assertServiceException(() -> mailSendService.buildTemplateParams(template, templateParams), | ||
| 157 | + MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + @Test | ||
| 161 | + public void testCheckMail_notExists() { | ||
| 162 | + // 准备参数 | ||
| 163 | + // mock 方法 | ||
| 164 | + | ||
| 165 | + // 调用,并断言异常 | ||
| 166 | + assertServiceException(() -> mailSendService.checkMail(null), | ||
| 167 | + MAIL_SEND_MAIL_NOT_EXISTS); | ||
| 168 | + } | ||
| 169 | + | ||
| 26 | } | 170 | } |