增加 sms 的缓存的单元测试
This commit is contained in:
parent
953d270dd9
commit
6aeeeac90f
|
@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.service.permission;
|
|||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.iocoder.dashboard.BaseDbUnitTest;
|
||||
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
|
||||
|
@ -52,18 +53,20 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
SysMenuDO menuDO2 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L);
|
||||
menuMapper.insert(menuDO2);
|
||||
|
||||
//调用
|
||||
// 调用
|
||||
sysMenuService.initLocalCache();
|
||||
|
||||
// 获取代理对象
|
||||
SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService);
|
||||
|
||||
Map<Long, SysMenuDO> menuCache = (Map<Long, SysMenuDO>) BeanUtil.getFieldValue(target, "menuCache");
|
||||
Map<Long, SysMenuDO> menuCache =
|
||||
(Map<Long, SysMenuDO>) BeanUtil.getFieldValue(target, "menuCache");
|
||||
Assert.isTrue(menuCache.size() == 2);
|
||||
assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId()));
|
||||
assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId()));
|
||||
|
||||
Multimap<String, SysMenuDO> permissionMenuCache = (Multimap<String, SysMenuDO>) BeanUtil.getFieldValue(target, "permissionMenuCache");
|
||||
Multimap<String, SysMenuDO> permissionMenuCache =
|
||||
(Multimap<String, SysMenuDO>) BeanUtil.getFieldValue(target, "permissionMenuCache");
|
||||
Assert.isTrue(permissionMenuCache.size() == 2);
|
||||
assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission()));
|
||||
assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission()));
|
||||
|
@ -184,10 +187,9 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
|
||||
//断言
|
||||
Assert.isTrue(menuDOS.size() == idMenuMap.size());
|
||||
menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetMenusReqVo_success() {
|
||||
Map<Long, SysMenuDO> idMenuMap = new HashMap<>();
|
||||
|
@ -218,7 +220,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
|
||||
//断言
|
||||
Assert.isTrue(menuDOS.size() == idMenuMap.size());
|
||||
menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -244,12 +246,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2);
|
||||
mockCacheMap.put(menuDO.getId(), menuDO);
|
||||
|
||||
List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1));
|
||||
List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()),
|
||||
Collections.singletonList(CommonStatusEnum.DISABLE.getStatus()));
|
||||
Assert.isTrue(menuDOS.size() == idMenuMap.size());
|
||||
menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testListMenusFromCache2_success() throws Exception {
|
||||
Map<Long, SysMenuDO> mockCacheMap = new HashMap<>();
|
||||
|
@ -306,14 +308,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCheckResource_success(){
|
||||
SysMenuDO sonMenu=initParentAndSonMenuDO();
|
||||
Long parentId=sonMenu.getParentId();
|
||||
public void testCheckResource_success() {
|
||||
SysMenuDO sonMenu = initParentAndSonMenuDO();
|
||||
Long parentId = sonMenu.getParentId();
|
||||
|
||||
Long otherSonMenuId=randomLongId();
|
||||
String otherSonMenuName=randomString();
|
||||
Long otherSonMenuId = randomLongId();
|
||||
String otherSonMenuName = randomString();
|
||||
|
||||
sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId);
|
||||
sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -324,7 +326,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
Long otherSonMenuId=randomLongId();
|
||||
String otherSonMenuName=sonMenu.getName(); //相同名称
|
||||
|
||||
assertServiceException(()->sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId), MENU_NAME_DUPLICATE);
|
||||
assertServiceException(() -> sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -361,5 +363,4 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.dashboard.modules.system.service.sms;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.iocoder.dashboard.BaseDbUnitTest;
|
||||
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||
|
@ -9,6 +10,7 @@ import cn.iocoder.dashboard.modules.system.controller.sms.vo.channel.SysSmsChann
|
|||
import cn.iocoder.dashboard.modules.system.controller.sms.vo.channel.SysSmsChannelUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsChannelMapper;
|
||||
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer;
|
||||
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsChannelServiceImpl;
|
||||
import cn.iocoder.dashboard.util.collection.ArrayUtils;
|
||||
import cn.iocoder.dashboard.util.object.ObjectUtils;
|
||||
|
@ -17,18 +19,19 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
|
||||
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS;
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.*;
|
||||
import static cn.iocoder.dashboard.util.RandomUtils.*;
|
||||
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.dashboard.util.object.ObjectUtils.max;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link SysSmsChannelServiceImpl} 的单元测试类
|
||||
|
@ -41,14 +44,35 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
|
|||
@Resource
|
||||
private SysSmsChannelServiceImpl smsChannelService;
|
||||
|
||||
@MockBean
|
||||
private SmsClientFactory smsClientFactory;
|
||||
|
||||
@Resource
|
||||
private SysSmsChannelMapper smsChannelMapper;
|
||||
|
||||
@MockBean
|
||||
private SmsClientFactory smsClientFactory;
|
||||
@MockBean
|
||||
private SysSmsTemplateService smsTemplateService;
|
||||
@MockBean
|
||||
private SysSmsProducer smsProducer;
|
||||
|
||||
@Test
|
||||
public void testInitLocalCache_success() {
|
||||
// mock 数据s
|
||||
SysSmsChannelDO smsChannelDO01 = randomSmsChannelDO();
|
||||
smsChannelMapper.insert(smsChannelDO01);
|
||||
SysSmsChannelDO smsChannelDO02 = randomSmsChannelDO();
|
||||
smsChannelMapper.insert(smsChannelDO02);
|
||||
|
||||
// 调用
|
||||
smsChannelService.initSmsClients();
|
||||
// 校验 maxUpdateTime 属性
|
||||
Date maxUpdateTime = (Date) BeanUtil.getFieldValue(smsChannelService, "maxUpdateTime");
|
||||
assertEquals(max(smsChannelDO01.getUpdateTime(), smsChannelDO02.getUpdateTime()), maxUpdateTime);
|
||||
// 校验调用
|
||||
verify(smsClientFactory, times(1)).createOrUpdateSmsClient(
|
||||
argThat(properties -> isPojoEquals(smsChannelDO01, properties)));
|
||||
verify(smsClientFactory, times(1)).createOrUpdateSmsClient(
|
||||
argThat(properties -> isPojoEquals(smsChannelDO02, properties)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSmsChannel_success() {
|
||||
|
@ -62,6 +86,8 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
|
|||
// 校验记录的属性是否正确
|
||||
SysSmsChannelDO smsChannel = smsChannelMapper.selectById(smsChannelId);
|
||||
assertPojoEquals(reqVO, smsChannel);
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsChannelRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,6 +107,8 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
|
|||
// 校验是否更新正确
|
||||
SysSmsChannelDO smsChannel = smsChannelMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, smsChannel);
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsChannelRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -104,6 +132,8 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest {
|
|||
smsChannelService.deleteSmsChannel(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(smsChannelMapper.selectById(id));
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsChannelRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -109,6 +109,7 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest {
|
|||
assertEquals(apiSerialNo, dbSmsLog.getApiSerialNo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateSmsReceiveResult() {
|
||||
// mock 数据
|
||||
SysSmsLogDO dbSmsLog = randomSmsLogDO(
|
||||
|
@ -128,7 +129,7 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest {
|
|||
assertEquals(success ? SysSmsReceiveStatusEnum.SUCCESS.getStatus()
|
||||
: SysSmsReceiveStatusEnum.FAILURE.getStatus(), dbSmsLog.getReceiveStatus());
|
||||
assertEquals(receiveTime, dbSmsLog.getReceiveTime());
|
||||
assertEquals(apiReceiveCode, dbSmsLog.getApiSendCode());
|
||||
assertEquals(apiReceiveCode, dbSmsLog.getApiReceiveCode());
|
||||
assertEquals(apiReceiveMsg, dbSmsLog.getApiReceiveMsg());
|
||||
}
|
||||
|
||||
|
@ -233,6 +234,7 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest {
|
|||
o.setTemplateParams(randomTemplateParams());
|
||||
o.setTemplateType(randomEle(SysSmsTemplateTypeEnum.values()).getType()); // 保证 templateType 的范围
|
||||
o.setUserType(randomEle(UserTypeEnum.values()).getValue()); // 保证 userType 的范围
|
||||
o.setSendStatus(randomEle(SysSmsSendStatusEnum.values()).getStatus()); // 保证 sendStatus 的范围
|
||||
o.setReceiveStatus(randomEle(SysSmsReceiveStatusEnum.values()).getStatus()); // 保证 receiveStatus 的范围
|
||||
};
|
||||
return randomPojo(SysSmsLogDO.class, ArrayUtils.append(consumer, consumers));
|
||||
|
|
|
@ -16,6 +16,7 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
|
|||
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsTemplateMapper;
|
||||
import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsTemplateTypeEnum;
|
||||
import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer;
|
||||
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsTemplateServiceImpl;
|
||||
import cn.iocoder.dashboard.util.collection.ArrayUtils;
|
||||
import cn.iocoder.dashboard.util.object.ObjectUtils;
|
||||
|
@ -25,18 +26,22 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static cn.hutool.core.bean.BeanUtil.getFieldValue;
|
||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.dashboard.util.RandomUtils.*;
|
||||
import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
|
||||
import static cn.iocoder.dashboard.util.object.ObjectUtils.max;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link SysSmsTemplateServiceImpl} 的单元测试类
|
||||
|
@ -54,11 +59,33 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
|
||||
@MockBean
|
||||
private SysSmsChannelService smsChannelService;
|
||||
|
||||
@MockBean
|
||||
private SmsClientFactory smsClientFactory;
|
||||
@MockBean
|
||||
private SmsClient smsClient;
|
||||
@MockBean
|
||||
private SysSmsProducer smsProducer;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void testInitLocalCache() {
|
||||
// mock 数据
|
||||
SysSmsTemplateDO smsTemplate01 = randomSmsTemplateDO();
|
||||
smsTemplateMapper.insert(smsTemplate01);
|
||||
SysSmsTemplateDO smsTemplate02 = randomSmsTemplateDO();
|
||||
smsTemplateMapper.insert(smsTemplate02);
|
||||
|
||||
// 调用
|
||||
smsTemplateService.initLocalCache();
|
||||
// 断言 deptCache 缓存
|
||||
Map<String, SysSmsTemplateDO> smsTemplateCache = (Map<String, SysSmsTemplateDO>) getFieldValue(smsTemplateService, "smsTemplateCache");
|
||||
assertEquals(2, smsTemplateCache.size());
|
||||
assertPojoEquals(smsTemplate01, smsTemplateCache.get(smsTemplate01.getCode()));
|
||||
assertPojoEquals(smsTemplate02, smsTemplateCache.get(smsTemplate02.getCode()));
|
||||
// 断言 maxUpdateTime 缓存
|
||||
Date maxUpdateTime = (Date) getFieldValue(smsTemplateService, "maxUpdateTime");
|
||||
assertEquals(max(smsTemplate01.getUpdateTime(), smsTemplate02.getUpdateTime()), maxUpdateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTemplateContentParams() {
|
||||
|
@ -101,6 +128,8 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
assertPojoEquals(reqVO, smsTemplate);
|
||||
assertEquals(Lists.newArrayList("operation", "code"), smsTemplate.getParams());
|
||||
assertEquals(channelDO.getCode(), smsTemplate.getChannelCode());
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -134,6 +163,8 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
assertPojoEquals(reqVO, smsTemplate);
|
||||
assertEquals(Lists.newArrayList("operation", "code"), smsTemplate.getParams());
|
||||
assertEquals(channelDO.getCode(), smsTemplate.getChannelCode());
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -157,6 +188,8 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
smsTemplateService.deleteSmsTemplate(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(smsTemplateMapper.selectById(id));
|
||||
// 校验调用
|
||||
verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -309,7 +342,7 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCheckDictDataValueUnique_valueDuplicateForCreate() {
|
||||
public void testCheckSmsTemplateCodeDuplicate_valueDuplicateForCreate() {
|
||||
// 准备参数
|
||||
String code = randomString();
|
||||
// mock 数据
|
||||
|
@ -317,7 +350,7 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> smsTemplateService.checkSmsTemplateCodeDuplicate(null, code),
|
||||
SMS_TEMPLATE_CODE_DUPLICATE);
|
||||
SMS_TEMPLATE_CODE_DUPLICATE, code);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -330,7 +363,7 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
|
|||
|
||||
// 调用,校验异常
|
||||
assertServiceException(() -> smsTemplateService.checkSmsTemplateCodeDuplicate(id, code),
|
||||
SMS_TEMPLATE_CODE_DUPLICATE);
|
||||
SMS_TEMPLATE_CODE_DUPLICATE, code);
|
||||
}
|
||||
|
||||
// ========== 随机对象 ==========
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.junit.jupiter.api.function.Executable;
|
|||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
@ -50,6 +51,33 @@ public class AssertUtils {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 比对两个对象的属性是否一致
|
||||
*
|
||||
* 注意,如果 expected 存在的属性,actual 不存在的时候,会进行忽略
|
||||
*
|
||||
* @param expected 期望对象
|
||||
* @param actual 实际对象
|
||||
* @param ignoreFields 忽略的属性数组
|
||||
* @return 是否一致
|
||||
*/
|
||||
public static boolean isPojoEquals(Object expected, Object actual, String... ignoreFields) {
|
||||
Field[] expectedFields = ReflectUtil.getFields(expected.getClass());
|
||||
return Arrays.stream(expectedFields).allMatch(expectedField -> {
|
||||
// 如果是忽略的属性,则不进行比对
|
||||
if (ArrayUtil.contains(ignoreFields, expectedField.getName())) {
|
||||
return true;
|
||||
}
|
||||
// 忽略不存在的属性
|
||||
Field actualField = ReflectUtil.getField(actual.getClass(), expectedField.getName());
|
||||
if (actualField == null) {
|
||||
return true;
|
||||
}
|
||||
return Objects.equals(ReflectUtil.getFieldValue(expected, expectedField),
|
||||
ReflectUtil.getFieldValue(actual, actualField));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行方法,校验抛出的 Service 是否符合条件
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue