邮箱模块:完善 log 的单元测试

pull/2/head
YunaiV 2023-01-27 09:38:42 +08:00
parent c0b029b244
commit f0d2c7a58a
5 changed files with 196 additions and 2 deletions

View File

@ -23,7 +23,7 @@ public class MailLogPageReqVO extends PageParam {
private Long userId;
@ApiModelProperty(value = "用户类型", example = "2", notes = "参见 UserTypeEnum 枚举")
private Byte userType;
private Integer userType;
@ApiModelProperty(value = "接收邮箱地址", example = "76854@qq.com", notes = "模糊匹配")
private String toMail;

View File

@ -0,0 +1,169 @@
package cn.iocoder.yudao.module.system.service.mail;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.log.MailLogPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailLogDO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailLogMapper;
import cn.iocoder.yudao.module.system.enums.mail.MailSendStatusEnum;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.Map;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link MailLogServiceImpl}
*
* @author
*/
@Import(MailLogServiceImpl.class)
public class MailLogServiceImplTest extends BaseDbUnitTest {
@Resource
private MailLogServiceImpl mailLogService;
@Resource
private MailLogMapper mailLogMapper;
@Test
public void testCreateMailLog() {
// 准备参数
Long userId = randomLongId();
Integer userType = randomEle(UserTypeEnum.values()).getValue();
String toMail = randomEmail();
MailAccountDO account = randomPojo(MailAccountDO.class);
MailTemplateDO template = randomPojo(MailTemplateDO.class);
String templateContent = randomString();
Map<String, Object> templateParams = randomTemplateParams();
Boolean isSend = true;
// mock 方法
// 调用
Long logId = mailLogService.createMailLog(userId, userType, toMail, account, template, templateContent, templateParams, isSend);
// 断言
MailLogDO log = mailLogMapper.selectById(logId);
assertNotNull(log);
assertEquals(MailSendStatusEnum.INIT.getStatus(), log.getSendStatus());
assertEquals(userId, log.getUserId());
assertEquals(userType, log.getUserType());
assertEquals(toMail, log.getToMail());
assertEquals(account.getId(), log.getAccountId());
assertEquals(account.getMail(), log.getFromMail());
assertEquals(template.getId(), log.getTemplateId());
assertEquals(template.getCode(), log.getTemplateCode());
assertEquals(template.getNickname(), log.getTemplateNickname());
assertEquals(template.getTitle(), log.getTemplateTitle());
assertEquals(templateContent, log.getTemplateContent());
assertEquals(templateParams, log.getTemplateParams());
}
@Test
public void testUpdateMailSendResult_success() {
// mock 数据
MailLogDO log = randomPojo(MailLogDO.class, o -> {
o.setSendStatus(MailSendStatusEnum.INIT.getStatus());
o.setSendTime(null).setSendMessageId(null).setSendException(null)
.setTemplateParams(randomTemplateParams());
});
mailLogMapper.insert(log);
// 准备参数
Long logId = log.getId();
String messageId = randomString();
// 调用
mailLogService.updateMailSendResult(logId, messageId, null);
// 断言
MailLogDO dbLog = mailLogMapper.selectById(logId);
assertEquals(MailSendStatusEnum.SUCCESS.getStatus(), dbLog.getSendStatus());
assertNotNull(dbLog.getSendTime());
assertEquals(messageId, dbLog.getSendMessageId());
assertNull(dbLog.getSendException());
}
@Test
public void testUpdateMailSendResult_exception() {
// mock 数据
MailLogDO log = randomPojo(MailLogDO.class, o -> {
o.setSendStatus(MailSendStatusEnum.INIT.getStatus());
o.setSendTime(null).setSendMessageId(null).setSendException(null)
.setTemplateParams(randomTemplateParams());
});
mailLogMapper.insert(log);
// 准备参数
Long logId = log.getId();
Exception exception = new NullPointerException("测试异常");
// 调用
mailLogService.updateMailSendResult(logId, null, exception);
// 断言
MailLogDO dbLog = mailLogMapper.selectById(logId);
assertEquals(MailSendStatusEnum.FAILURE.getStatus(), dbLog.getSendStatus());
assertNotNull(dbLog.getSendTime());
assertNull(dbLog.getSendMessageId());
assertEquals("NullPointerException: 测试异常", dbLog.getSendException());
}
@Test
public void testGetMailLogPage() {
// mock 数据
MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> { // 等会查询到
o.setUserId(1L);
o.setUserType(UserTypeEnum.ADMIN.getValue());
o.setToMail("768@qq.com");
o.setAccountId(10L);
o.setTemplateId(100L);
o.setSendStatus(MailSendStatusEnum.INIT.getStatus());
o.setSendTime(buildTime(2023, 2, 10));
o.setTemplateParams(randomTemplateParams());
});
mailLogMapper.insert(dbMailLog);
// 测试 userId 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setUserId(2L)));
// 测试 userType 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
// 测试 toMail 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setToMail("788@.qq.com")));
// 测试 accountId 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setAccountId(11L)));
// 测试 templateId 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setTemplateId(101L)));
// 测试 sendStatus 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setSendStatus(MailSendStatusEnum.SUCCESS.getStatus())));
// 测试 sendTime 不匹配
mailLogMapper.insert(cloneIgnoreId(dbMailLog, o -> o.setSendTime(buildTime(2023, 3, 10))));
// 准备参数
MailLogPageReqVO reqVO = new MailLogPageReqVO();
reqVO.setUserId(1L);
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
reqVO.setToMail("768");
reqVO.setAccountId(10L);
reqVO.setTemplateId(100L);
reqVO.setSendStatus(MailSendStatusEnum.INIT.getStatus());
reqVO.setSendTime((buildBetweenTime(2023, 2, 1, 2023, 2, 15)));
// 调用
PageResult<MailLogDO> pageResult = mailLogService.getMailLogPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbMailLog, pageResult.getList().get(0));
}
private static Map<String, Object> randomTemplateParams() {
return MapUtil.<String, Object>builder().put(randomString(), randomString())
.put(randomString(), randomString()).build();
}
}

View File

@ -235,7 +235,6 @@ public class SmsLogServiceTest extends BaseDbUnitTest {
return randomPojo(SmsLogDO.class, ArrayUtils.append(consumer, consumers));
}
private static Map<String, Object> randomTemplateParams() {
return MapUtil.<String, Object>builder().put(randomString(), randomString())
.put(randomString(), randomString()).build();

View File

@ -27,3 +27,4 @@ DELETE FROM "system_oauth2_refresh_token";
DELETE FROM "system_oauth2_code";
DELETE FROM "system_mail_account";
DELETE FROM "system_mail_template";
DELETE FROM "system_mail_log";

View File

@ -601,3 +601,28 @@ CREATE TABLE IF NOT EXISTS "system_mail_template" (
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '';
CREATE TABLE IF NOT EXISTS "system_mail_log" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"user_id" bigint,
"user_type" varchar,
"to_mail" varchar NOT NULL,
"account_id" bigint NOT NULL,
"from_mail" varchar NOT NULL,
"template_id" bigint NOT NULL,
"template_code" varchar NOT NULL,
"template_nickname" varchar,
"template_title" varchar NOT NULL,
"template_content" varchar NOT NULL,
"template_params" varchar NOT NULL,
"send_status" varchar NOT NULL,
"send_time" datetime,
"send_message_id" varchar,
"send_exception" varchar,
"creator" varchar DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '';