mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-17 20:45:06 +08:00
邮箱模块:完善 account 的单元测试
This commit is contained in:
@ -0,0 +1,154 @@
|
||||
package cn.iocoder.yudao.module.system.service.mail;
|
||||
|
||||
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.account.MailAccountCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
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.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* {@link MailAccountServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(MailAccountServiceImpl.class)
|
||||
public class MailAccountServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private MailAccountServiceImpl mailAccountService;
|
||||
|
||||
@Resource
|
||||
private MailAccountMapper mailAccountMapper;
|
||||
|
||||
@MockBean
|
||||
private MailTemplateService mailTemplateService;
|
||||
@MockBean
|
||||
private MailProducer mailProducer;
|
||||
|
||||
@Test
|
||||
public void testInitLocalCache() {
|
||||
MailAccountDO accountDO1 = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(accountDO1);
|
||||
MailAccountDO accountDO02 = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(accountDO02);
|
||||
|
||||
// 调用
|
||||
mailAccountService.initLocalCache();
|
||||
// 断言 mailAccountCache 缓存
|
||||
Map<Long, MailAccountDO> mailAccountCache = mailAccountService.getMailAccountCache();
|
||||
assertPojoEquals(accountDO1, mailAccountCache.get(accountDO1.getId()));
|
||||
assertPojoEquals(accountDO02, mailAccountCache.get(accountDO02.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateMailAccount_success() {
|
||||
// 准备参数
|
||||
MailAccountCreateReqVO reqVO = randomPojo(MailAccountCreateReqVO.class, o -> o.setMail(randomEmail()));
|
||||
|
||||
// 调用
|
||||
Long mailAccountId = mailAccountService.createMailAccount(reqVO);
|
||||
// 断言
|
||||
assertNotNull(mailAccountId);
|
||||
// 校验记录的属性是否正确
|
||||
MailAccountDO mailAccount = mailAccountMapper.selectById(mailAccountId);
|
||||
assertPojoEquals(reqVO, mailAccount);
|
||||
verify(mailProducer).sendMailAccountRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateMailAccount_success() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
MailAccountUpdateReqVO reqVO = randomPojo(MailAccountUpdateReqVO.class, o -> {
|
||||
o.setId(dbMailAccount.getId()); // 设置更新的 ID
|
||||
o.setMail(randomEmail());
|
||||
});
|
||||
|
||||
// 调用
|
||||
mailAccountService.updateMailAccount(reqVO);
|
||||
// 校验是否更新正确
|
||||
MailAccountDO mailAccount = mailAccountMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, mailAccount);
|
||||
verify(mailProducer).sendMailAccountRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateMailAccount_notExists() {
|
||||
// 准备参数
|
||||
MailAccountUpdateReqVO reqVO = randomPojo(MailAccountUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> mailAccountService.updateMailAccount(reqVO), MAIL_ACCOUNT_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteMailAccount_success() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class);
|
||||
mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbMailAccount.getId();
|
||||
|
||||
// 调用
|
||||
mailAccountService.deleteMailAccount(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(mailAccountMapper.selectById(id));
|
||||
verify(mailProducer).sendMailAccountRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteMailAccount_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> mailAccountService.deleteMailAccount(id), MAIL_ACCOUNT_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMailAccountPage() {
|
||||
// mock 数据
|
||||
MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class, o -> { // 等会查询到
|
||||
o.setMail("768@qq.com");
|
||||
o.setUsername("yunai");
|
||||
});
|
||||
mailAccountMapper.insert(dbMailAccount);
|
||||
// 测试 mail 不匹配
|
||||
mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setMail("788@qq.com")));
|
||||
// 测试 username 不匹配
|
||||
mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setUsername("tudou")));
|
||||
// 准备参数
|
||||
MailAccountPageReqVO reqVO = new MailAccountPageReqVO();
|
||||
reqVO.setMail("768");
|
||||
reqVO.setUsername("yu");
|
||||
|
||||
// 调用
|
||||
PageResult<MailAccountDO> pageResult = mailAccountService.getMailAccountPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbMailAccount, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
@ -25,3 +25,4 @@ DELETE FROM "system_oauth2_approve";
|
||||
DELETE FROM "system_oauth2_access_token";
|
||||
DELETE FROM "system_oauth2_refresh_token";
|
||||
DELETE FROM "system_oauth2_code";
|
||||
DELETE FROM "system_mail_account";
|
||||
|
@ -566,3 +566,19 @@ CREATE TABLE IF NOT EXISTS "system_oauth2_code" (
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT 'OAuth2 刷新令牌';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "system_mail_account" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"mail" varchar NOT NULL,
|
||||
"username" varchar NOT NULL,
|
||||
"password" varchar NOT NULL,
|
||||
"host" varchar NOT NULL,
|
||||
"port" int NOT NULL,
|
||||
"ssl_enable" bit NOT NULL,
|
||||
"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 '邮箱账号表';
|
||||
|
Reference in New Issue
Block a user