mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-17 12:35:07 +08:00
站内信模块:整体功能实现
This commit is contained in:
@ -145,7 +145,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildTemplateParams_paramMiss() {
|
||||
public void testCheckTemplateParams_paramMiss() {
|
||||
// 准备参数
|
||||
MailTemplateDO template = randomPojo(MailTemplateDO.class,
|
||||
o -> o.setParams(Lists.newArrayList("code")));
|
||||
@ -153,7 +153,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> mailSendService.buildTemplateParams(template, templateParams),
|
||||
assertServiceException(() -> mailSendService.checkTemplateParams(template, templateParams),
|
||||
MAIL_SEND_TEMPLATE_PARAM_MISS, "code");
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,266 @@
|
||||
package cn.iocoder.yudao.module.system.service.notify;
|
||||
|
||||
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.mybatis.core.enums.SqlConstants;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
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 NotifyMessageServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(NotifyMessageServiceImpl.class)
|
||||
public class NotifyMessageServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private NotifyMessageServiceImpl notifyMessageService;
|
||||
|
||||
@Resource
|
||||
private NotifyMessageMapper notifyMessageMapper;
|
||||
|
||||
@Test
|
||||
public void testCreateNotifyMessage_success() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
Integer userType = randomEle(UserTypeEnum.values()).getValue();
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class);
|
||||
String templateContent = randomString();
|
||||
Map<String, Object> templateParams = randomTemplateParams();
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
Long messageId = notifyMessageService.createNotifyMessage(userId, userType,
|
||||
template, templateContent, templateParams);
|
||||
// 断言
|
||||
NotifyMessageDO message = notifyMessageMapper.selectById(messageId);
|
||||
assertNotNull(message);
|
||||
assertEquals(userId, message.getUserId());
|
||||
assertEquals(userType, message.getUserType());
|
||||
assertEquals(template.getId(), message.getTemplateId());
|
||||
assertEquals(template.getCode(), message.getTemplateCode());
|
||||
assertEquals(template.getType(), message.getTemplateType());
|
||||
assertEquals(template.getNickname(), message.getTemplateNickname());
|
||||
assertEquals(templateContent, message.getTemplateContent());
|
||||
assertEquals(templateParams, message.getTemplateParams());
|
||||
assertEquals(false, message.getReadStatus());
|
||||
assertNull(message.getReadTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotifyMessagePage() {
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setTemplateCode("test_01");
|
||||
o.setTemplateType(10);
|
||||
o.setCreateTime(buildTime(2022, 1, 2));
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 templateCode 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setTemplateCode("test_11")));
|
||||
// 测试 templateType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setTemplateType(20)));
|
||||
// 测试 createTime 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setCreateTime(buildTime(2022, 2, 1))));
|
||||
// 准备参数
|
||||
NotifyMessagePageReqVO reqVO = new NotifyMessagePageReqVO();
|
||||
reqVO.setUserId(1L);
|
||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqVO.setTemplateCode("est_01");
|
||||
reqVO.setTemplateType(10);
|
||||
reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10));
|
||||
|
||||
// 调用
|
||||
PageResult<NotifyMessageDO> pageResult = notifyMessageService.getNotifyMessagePage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMyNotifyMessagePage() {
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setReadStatus(true);
|
||||
o.setCreateTime(buildTime(2022, 1, 2));
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 readStatus 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(false)));
|
||||
// 测试 createTime 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setCreateTime(buildTime(2022, 2, 1))));
|
||||
// 准备参数
|
||||
Long userId = 1L;
|
||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||
NotifyMessageMyPageReqVO reqVO = new NotifyMessageMyPageReqVO();
|
||||
reqVO.setReadStatus(true);
|
||||
reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10));
|
||||
|
||||
// 调用
|
||||
PageResult<NotifyMessageDO> pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO, userId, userType);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUnreadNotifyMessageList() {
|
||||
SqlConstants.init(DbType.MYSQL);
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setReadStatus(false);
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 readStatus 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true)));
|
||||
// 准备参数
|
||||
Long userId = 1L;
|
||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||
Integer size = 10;
|
||||
|
||||
// 调用
|
||||
List<NotifyMessageDO> list = notifyMessageService.getUnreadNotifyMessageList(userId, userType, size);
|
||||
// 断言
|
||||
assertEquals(1, list.size());
|
||||
assertPojoEquals(dbNotifyMessage, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUnreadNotifyMessageCount() {
|
||||
SqlConstants.init(DbType.MYSQL);
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setReadStatus(false);
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 readStatus 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true)));
|
||||
// 准备参数
|
||||
Long userId = 1L;
|
||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||
|
||||
// 调用,并断言
|
||||
assertEquals(1, notifyMessageService.getUnreadNotifyMessageCount(userId, userType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateNotifyMessageRead() {
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setReadStatus(false);
|
||||
o.setReadTime(null);
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 readStatus 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true)));
|
||||
// 准备参数
|
||||
Collection<Long> ids = Arrays.asList(dbNotifyMessage.getId(), dbNotifyMessage.getId() + 1,
|
||||
dbNotifyMessage.getId() + 2, dbNotifyMessage.getId() + 3);
|
||||
Long userId = 1L;
|
||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||
|
||||
// 调用
|
||||
int updateCount = notifyMessageService.updateNotifyMessageRead(ids, userId, userType);
|
||||
// 断言
|
||||
assertEquals(1, updateCount);
|
||||
NotifyMessageDO notifyMessage = notifyMessageMapper.selectById(dbNotifyMessage.getId());
|
||||
assertTrue(notifyMessage.getReadStatus());
|
||||
assertNotNull(notifyMessage.getReadTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAllNotifyMessageRead() {
|
||||
// mock 数据
|
||||
NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到
|
||||
o.setUserId(1L);
|
||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
o.setReadStatus(false);
|
||||
o.setReadTime(null);
|
||||
o.setTemplateParams(randomTemplateParams());
|
||||
});
|
||||
notifyMessageMapper.insert(dbNotifyMessage);
|
||||
// 测试 userId 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserId(2L)));
|
||||
// 测试 userType 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
||||
// 测试 readStatus 不匹配
|
||||
notifyMessageMapper.insert(cloneIgnoreId(dbNotifyMessage, o -> o.setReadStatus(true)));
|
||||
// 准备参数
|
||||
Long userId = 1L;
|
||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||
|
||||
// 调用
|
||||
int updateCount = notifyMessageService.updateAllNotifyMessageRead(userId, userType);
|
||||
// 断言
|
||||
assertEquals(1, updateCount);
|
||||
NotifyMessageDO notifyMessage = notifyMessageMapper.selectById(dbNotifyMessage.getId());
|
||||
assertTrue(notifyMessage.getReadStatus());
|
||||
assertNotNull(notifyMessage.getReadTime());
|
||||
}
|
||||
|
||||
private static Map<String, Object> randomTemplateParams() {
|
||||
return MapUtil.<String, Object>builder().put(randomString(), randomString())
|
||||
.put(randomString(), randomString()).build();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package cn.iocoder.yudao.module.system.service.notify;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
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.dal.dataobject.notify.NotifyTemplateDO;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class NotifySendServiceImplTest extends BaseMockitoUnitTest {
|
||||
|
||||
@InjectMocks
|
||||
private NotifySendServiceImpl notifySendService;
|
||||
|
||||
@Mock
|
||||
private NotifyTemplateService notifyTemplateService;
|
||||
@Mock
|
||||
private NotifyMessageService notifyMessageService;
|
||||
|
||||
/**
|
||||
* 发送成功,当短信模板开启时
|
||||
*/
|
||||
@Test
|
||||
public void testSendSingleNotify_successWhenMailTemplateEnable() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
Integer userType = randomEle(UserTypeEnum.values()).getValue();
|
||||
String templateCode = randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock NotifyTemplateService 的方法
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
String content = randomString();
|
||||
when(notifyTemplateService.formatNotifyTemplateContent(eq(template.getContent()), eq(templateParams)))
|
||||
.thenReturn(content);
|
||||
// mock NotifyMessageService 的方法
|
||||
Long messageId = randomLongId();
|
||||
when(notifyMessageService.createNotifyMessage(eq(userId), eq(userType),
|
||||
eq(template), eq(content), eq(templateParams))).thenReturn(messageId);
|
||||
|
||||
// 调用
|
||||
Long resultMessageId = notifySendService.sendSingleNotify(userId, userType, templateCode, templateParams);
|
||||
// 断言
|
||||
assertEquals(messageId, resultMessageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送成功,当短信模板关闭时
|
||||
*/
|
||||
@Test
|
||||
public void testSendSingleMail_successWhenSmsTemplateDisable() {
|
||||
// 准备参数
|
||||
Long userId = randomLongId();
|
||||
Integer userType = randomEle(UserTypeEnum.values()).getValue();
|
||||
String templateCode = randomString();
|
||||
Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234")
|
||||
.put("op", "login").build();
|
||||
// mock NotifyTemplateService 的方法
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class, o -> {
|
||||
o.setStatus(CommonStatusEnum.DISABLE.getStatus());
|
||||
o.setContent("验证码为{code}, 操作为{op}");
|
||||
o.setParams(Lists.newArrayList("code", "op"));
|
||||
});
|
||||
when(notifyTemplateService.getNotifyTemplateByCodeFromCache(eq(templateCode))).thenReturn(template);
|
||||
|
||||
// 调用
|
||||
Long resultMessageId = notifySendService.sendSingleNotify(userId, userType, templateCode, templateParams);
|
||||
// 断言
|
||||
assertNull(resultMessageId);
|
||||
verify(notifyTemplateService, never()).formatNotifyTemplateContent(anyString(), anyMap());
|
||||
verify(notifyMessageService, never()).createNotifyMessage(anyLong(), anyInt(), any(), anyString(), anyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckMailTemplateValid_notExists() {
|
||||
// 准备参数
|
||||
String templateCode = randomString();
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> notifySendService.checkNotifyTemplateValid(templateCode),
|
||||
NOTICE_NOT_FOUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckTemplateParams_paramMiss() {
|
||||
// 准备参数
|
||||
NotifyTemplateDO template = randomPojo(NotifyTemplateDO.class,
|
||||
o -> o.setParams(Lists.newArrayList("code")));
|
||||
Map<String, Object> templateParams = new HashMap<>();
|
||||
// mock 方法
|
||||
|
||||
// 调用,并断言异常
|
||||
assertServiceException(() -> notifySendService.checkTemplateParams(template, templateParams),
|
||||
NOTIFY_SEND_TEMPLATE_PARAM_MISS, "code");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
package cn.iocoder.yudao.module.system.service.notify;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
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.notify.vo.template.NotifyTemplateCreateReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplatePageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplateUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyTemplateMapper;
|
||||
import cn.iocoder.yudao.module.system.mq.producer.notify.NotifyProducer;
|
||||
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 static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
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.NOTIFY_TEMPLATE_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* {@link NotifyTemplateServiceImpl} 的单元测试类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Import(NotifyTemplateServiceImpl.class)
|
||||
public class NotifyTemplateServiceImplTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private NotifyTemplateServiceImpl notifyTemplateService;
|
||||
|
||||
@Resource
|
||||
private NotifyTemplateMapper notifyTemplateMapper;
|
||||
|
||||
@MockBean
|
||||
private NotifyProducer notifyProducer;
|
||||
|
||||
@Test
|
||||
public void testCreateNotifyTemplate_success() {
|
||||
// 准备参数
|
||||
NotifyTemplateCreateReqVO reqVO = randomPojo(NotifyTemplateCreateReqVO.class,
|
||||
o -> o.setStatus(randomCommonStatus()));
|
||||
|
||||
// 调用
|
||||
Long notifyTemplateId = notifyTemplateService.createNotifyTemplate(reqVO);
|
||||
// 断言
|
||||
assertNotNull(notifyTemplateId);
|
||||
// 校验记录的属性是否正确
|
||||
NotifyTemplateDO notifyTemplate = notifyTemplateMapper.selectById(notifyTemplateId);
|
||||
assertPojoEquals(reqVO, notifyTemplate);
|
||||
verify(notifyProducer).sendNotifyTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateNotifyTemplate_success() {
|
||||
// mock 数据
|
||||
NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class);
|
||||
notifyTemplateMapper.insert(dbNotifyTemplate);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
NotifyTemplateUpdateReqVO reqVO = randomPojo(NotifyTemplateUpdateReqVO.class, o -> {
|
||||
o.setId(dbNotifyTemplate.getId()); // 设置更新的 ID
|
||||
o.setStatus(randomCommonStatus());
|
||||
});
|
||||
|
||||
// 调用
|
||||
notifyTemplateService.updateNotifyTemplate(reqVO);
|
||||
// 校验是否更新正确
|
||||
NotifyTemplateDO notifyTemplate = notifyTemplateMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, notifyTemplate);
|
||||
verify(notifyProducer).sendNotifyTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateNotifyTemplate_notExists() {
|
||||
// 准备参数
|
||||
NotifyTemplateUpdateReqVO reqVO = randomPojo(NotifyTemplateUpdateReqVO.class);
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> notifyTemplateService.updateNotifyTemplate(reqVO), NOTIFY_TEMPLATE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNotifyTemplate_success() {
|
||||
// mock 数据
|
||||
NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class);
|
||||
notifyTemplateMapper.insert(dbNotifyTemplate);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbNotifyTemplate.getId();
|
||||
|
||||
// 调用
|
||||
notifyTemplateService.deleteNotifyTemplate(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(notifyTemplateMapper.selectById(id));
|
||||
verify(notifyProducer).sendNotifyTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNotifyTemplate_notExists() {
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
|
||||
// 调用, 并断言异常
|
||||
assertServiceException(() -> notifyTemplateService.deleteNotifyTemplate(id), NOTIFY_TEMPLATE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotifyTemplatePage() {
|
||||
// mock 数据
|
||||
NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class, o -> { // 等会查询到
|
||||
o.setName("芋头");
|
||||
o.setCode("test_01");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setCreateTime(buildTime(2022, 2, 3));
|
||||
});
|
||||
notifyTemplateMapper.insert(dbNotifyTemplate);
|
||||
// 测试 name 不匹配
|
||||
notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setName("投")));
|
||||
// 测试 code 不匹配
|
||||
notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCode("test_02")));
|
||||
// 测试 status 不匹配
|
||||
notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCreateTime(buildTime(2022, 1, 5))));
|
||||
// 准备参数
|
||||
NotifyTemplatePageReqVO reqVO = new NotifyTemplatePageReqVO();
|
||||
reqVO.setName("芋");
|
||||
reqVO.setCode("est_01");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 5));
|
||||
|
||||
// 调用
|
||||
PageResult<NotifyTemplateDO> pageResult = notifyTemplateService.getNotifyTemplatePage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
}
|
@ -28,3 +28,5 @@ DELETE FROM "system_oauth2_code";
|
||||
DELETE FROM "system_mail_account";
|
||||
DELETE FROM "system_mail_template";
|
||||
DELETE FROM "system_mail_log";
|
||||
DELETE FROM "system_notify_template";
|
||||
DELETE FROM "system_notify_message";
|
||||
|
@ -626,3 +626,43 @@ CREATE TABLE IF NOT EXISTS "system_mail_log" (
|
||||
"deleted" bit NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '邮件日志表';
|
||||
|
||||
-- 将该建表 SQL 语句,添加到 yudao-module-system-biz 模块的 test/resources/sql/create_tables.sql 文件里
|
||||
CREATE TABLE IF NOT EXISTS "system_notify_template" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"name" varchar NOT NULL,
|
||||
"code" varchar NOT NULL,
|
||||
"nickname" varchar NOT NULL,
|
||||
"content" varchar NOT NULL,
|
||||
"type" varchar NOT NULL,
|
||||
"params" varchar,
|
||||
"status" varchar NOT NULL,
|
||||
"remark" 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 '站内信模板表';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "system_notify_message" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"user_id" bigint NOT NULL,
|
||||
"user_type" varchar NOT NULL,
|
||||
"template_id" bigint NOT NULL,
|
||||
"template_code" varchar NOT NULL,
|
||||
"template_nickname" varchar NOT NULL,
|
||||
"template_content" varchar NOT NULL,
|
||||
"template_type" int NOT NULL,
|
||||
"template_params" varchar NOT NULL,
|
||||
"read_status" bit NOT NULL,
|
||||
"read_time" 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,
|
||||
"tenant_id" bigint not null default '0',
|
||||
PRIMARY KEY ("id")
|
||||
) COMMENT '站内信消息表';
|
||||
|
Reference in New Issue
Block a user