From 5d1abd5f4447290e63741138849f771f7a85e86e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 27 Jan 2023 10:14:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E7=AE=B1=E6=A8=A1=E5=9D=97=EF=BC=9A?= =?UTF-8?q?=E5=AE=8C=E5=96=84=20send=20=E7=9A=84=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mail/dto/MailSendSingleToUserReqDTO.java | 2 +- .../system/api/mail/MailSendApiImpl.java | 14 +- .../service/mail/MailSendServiceImplTest.java | 154 +++++++++++++++++- 3 files changed, 161 insertions(+), 9 deletions(-) diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendSingleToUserReqDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendSingleToUserReqDTO.java index e2ede96ae..28333a5e7 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/mail/dto/MailSendSingleToUserReqDTO.java +++ b/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 { * 用户编号 */ @NotNull(message = "用户编号不能为空") - private String userId; + private Long userId; /** * 邮箱 */ diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/mail/MailSendApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/mail/MailSendApiImpl.java index eaad0f8ab..59648f5a5 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/mail/MailSendApiImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/mail/MailSendApiImpl.java @@ -1,11 +1,14 @@ package cn.iocoder.yudao.module.system.api.mail; import cn.iocoder.yudao.module.system.api.mail.dto.MailSendSingleToUserReqDTO; +import cn.iocoder.yudao.module.system.service.mail.MailSendService; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import javax.annotation.Resource; + /** - * 邮件发送 API 接口 + * 邮件发送 API 实现类 * * @author wangjingyi */ @@ -13,14 +16,19 @@ import org.springframework.validation.annotation.Validated; @Validated public class MailSendApiImpl implements MailSendApi { + @Resource + private MailSendService mailSendService; + @Override public Long sendSingleSmsToAdmin(MailSendSingleToUserReqDTO reqDTO) { - return null; + return mailSendService.sendSingleMailToAdmin(reqDTO.getMail(), reqDTO.getUserId(), + reqDTO.getTemplateCode(), reqDTO.getTemplateParams()); } @Override public Long sendSingleSmsToMember(MailSendSingleToUserReqDTO reqDTO) { - return null; + return mailSendService.sendSingleMailToMember(reqDTO.getMail(), reqDTO.getUserId(), + reqDTO.getTemplateCode(), reqDTO.getTemplateParams()); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java index 33e17314a..bd93cd00a 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImplTest.java @@ -1,13 +1,44 @@ package cn.iocoder.yudao.module.system.service.mail; +import cn.hutool.core.map.MapUtil; import cn.hutool.extra.mail.MailAccount; import cn.hutool.extra.mail.MailUtil; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; +import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; +import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; +import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; +import org.assertj.core.util.Lists; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; -import static org.junit.jupiter.api.Assertions.*; +import java.util.HashMap; +import java.util.Map; -class MailSendServiceImplTest { +import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +class MailSendServiceImplTest extends BaseMockitoUnitTest { + + @InjectMocks + private MailSendServiceImpl mailSendService; + + @Mock + private MailAccountService mailAccountService; + @Mock + private MailTemplateService mailTemplateService; + @Mock + private MailLogService mailLogService; + @Mock + private MailProducer mailProducer; /** * 用于快速测试你的邮箱账号是否正常 @@ -16,11 +47,124 @@ class MailSendServiceImplTest { @Disabled public void testDemo() { MailAccount mailAccount = new MailAccount() - .setFrom("奥特曼 ") - .setHost("smtp.163.com").setPort(465).setSslEnable(true) - .setAuth(true).setUser("ydym_test@163.com").setPass("WBZTEINMIFVRYSOE"); +// .setFrom("奥特曼 ") + .setFrom("ydym_test@163.com") // 邮箱地址 + .setHost("smtp.163.com").setPort(465).setSslEnable(true) // SMTP 服务器 + .setAuth(true).setUser("ydym_test@163.com").setPass("WBZTEINMIFVRYSOE"); // 登录账号密码 String messageId = MailUtil.send(mailAccount, "7685413@qq.com", "主题", "内容", false); System.out.println("发送结果:" + messageId); } + /** + * 发送成功,当短信模板开启时 + */ + @Test + public void testSendSingleMail_successWhenMailTemplateEnable() { + // 准备参数 + String mail = randomEmail(); + Long userId = randomLongId(); + Integer userType = randomEle(UserTypeEnum.values()).getValue(); + String templateCode = randomString(); + Map templateParams = MapUtil.builder().put("code", "1234") + .put("op", "login").build(); + // mock MailTemplateService 的方法 + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + o.setContent("验证码为{code}, 操作为{op}"); + o.setParams(Lists.newArrayList("code", "op")); + }); + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); + String content = randomString(); + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) + .thenReturn(content); + // mock MailAccountService 的方法 + MailAccountDO account = randomPojo(MailAccountDO.class); + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); + // mock MailLogService 的方法 + Long mailLogId = randomLongId(); + when(mailLogService.createMailLog(eq(userId), eq(userType), eq(mail), + eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); + + // 调用 + Long resultMailLogId = mailSendService.sendSingleMail(mail, userId, userType, templateCode, templateParams); + // 断言 + assertEquals(mailLogId, resultMailLogId); + // 断言调用 + verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail), + eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); + } + + /** + * 发送成功,当短信模板关闭时 + */ + @Test + public void testSendSingleMail_successWhenSmsTemplateDisable() { + // 准备参数 + String mail = randomEmail(); + Long userId = randomLongId(); + Integer userType = randomEle(UserTypeEnum.values()).getValue(); + String templateCode = randomString(); + Map templateParams = MapUtil.builder().put("code", "1234") + .put("op", "login").build(); + // mock MailTemplateService 的方法 + MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { + o.setStatus(CommonStatusEnum.DISABLE.getStatus()); + o.setContent("验证码为{code}, 操作为{op}"); + o.setParams(Lists.newArrayList("code", "op")); + }); + when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); + String content = randomString(); + when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) + .thenReturn(content); + // mock MailAccountService 的方法 + MailAccountDO account = randomPojo(MailAccountDO.class); + when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); + // mock MailLogService 的方法 + Long mailLogId = randomLongId(); + when(mailLogService.createMailLog(eq(userId), eq(userType), eq(mail), + eq(account), eq(template), eq(content), eq(templateParams), eq(false))).thenReturn(mailLogId); + + // 调用 + Long resultMailLogId = mailSendService.sendSingleMail(mail, userId, userType, templateCode, templateParams); + // 断言 + assertEquals(mailLogId, resultMailLogId); + // 断言调用 + verify(mailProducer, times(0)).sendMailSendMessage(anyLong(), anyString(), + anyLong(), anyString(), anyString(), anyString()); + } + + @Test + public void testCheckMailTemplateValid_notExists() { + // 准备参数 + String templateCode = randomString(); + // mock 方法 + + // 调用,并断言异常 + assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode), + MAIL_TEMPLATE_NOT_EXISTS); + } + + @Test + public void testBuildTemplateParams_paramMiss() { + // 准备参数 + MailTemplateDO template = randomPojo(MailTemplateDO.class, + o -> o.setParams(Lists.newArrayList("code"))); + Map templateParams = new HashMap<>(); + // mock 方法 + + // 调用,并断言异常 + assertServiceException(() -> mailSendService.buildTemplateParams(template, templateParams), + MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); + } + + @Test + public void testCheckMail_notExists() { + // 准备参数 + // mock 方法 + + // 调用,并断言异常 + assertServiceException(() -> mailSendService.checkMail(null), + MAIL_SEND_MAIL_NOT_EXISTS); + } + }