完善 SmsChannelServiceImpl 单元测试

pull/25/head
YunaiV 2023-02-02 21:05:42 +08:00
parent 458a042701
commit 36c8448ed5
4 changed files with 45 additions and 42 deletions

View File

@ -70,7 +70,7 @@ public class SmsChannelController {
@GetMapping("/list-all-simple")
@ApiOperation(value = "获得短信渠道精简列表", notes = "包含被禁用的短信渠道")
public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannels() {
public CommonResult<List<SmsChannelSimpleRespVO>> getSimpleSmsChannelList() {
List<SmsChannelDO> list = smsChannelService.getSmsChannelList();
// 排序后,返回给前端
list.sort(Comparator.comparing(SmsChannelDO::getId));

View File

@ -1,13 +1,12 @@
package cn.iocoder.yudao.module.system.service.sms;
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
/**
@ -53,14 +52,6 @@ public interface SmsChannelService {
*/
SmsChannelDO getSmsChannel(Long id);
/**
*
*
* @param ids
* @return
*/
List<SmsChannelDO> getSmsChannelList(Collection<Long> ids);
/**
*
*

View File

@ -15,7 +15,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -81,7 +80,7 @@ public class SmsChannelServiceImpl implements SmsChannelService {
public void deleteSmsChannel(Long id) {
// 校验存在
this.validateSmsChannelExists(id);
// 校验是否有字典数据
// 校验是否有在使用该账号的模版
if (smsTemplateService.countByChannelId(id) > 0) {
throw exception(SMS_CHANNEL_HAS_CHILDREN);
}
@ -102,11 +101,6 @@ public class SmsChannelServiceImpl implements SmsChannelService {
return smsChannelMapper.selectById(id);
}
@Override
public List<SmsChannelDO> getSmsChannelList(Collection<Long> ids) {
return smsChannelMapper.selectBatchIds(ids);
}
@Override
public List<SmsChannelDO> getSmsChannelList() {
return smsChannelMapper.selectList();

View File

@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.sms;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.sms.core.client.SmsClientFactory;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelCreateReqVO;
@ -17,11 +15,12 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle;
import java.util.List;
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.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
@ -49,9 +48,9 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
@Test
public void testInitLocalCache_success() {
// mock 数据
SmsChannelDO smsChannelDO01 = randomSmsChannelDO();
SmsChannelDO smsChannelDO01 = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(smsChannelDO01);
SmsChannelDO smsChannelDO02 = randomSmsChannelDO();
SmsChannelDO smsChannelDO02 = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(smsChannelDO02);
// 调用
@ -82,7 +81,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
@Test
public void testUpdateSmsChannel_success() {
// mock 数据
SmsChannelDO dbSmsChannel = randomSmsChannelDO();
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
SmsChannelUpdateReqVO reqVO = randomPojo(SmsChannelUpdateReqVO.class, o -> {
@ -112,7 +111,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
@Test
public void testDeleteSmsChannel_success() {
// mock 数据
SmsChannelDO dbSmsChannel = randomSmsChannelDO();
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsChannel.getId();
@ -137,7 +136,7 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
@Test
public void testDeleteSmsChannel_hasChildren() {
// mock 数据
SmsChannelDO dbSmsChannel = randomSmsChannelDO();
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsChannel.getId();
@ -148,6 +147,35 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_HAS_CHILDREN);
}
@Test
public void testGetSmsChannel() {
// mock 数据
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel); // @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsChannel.getId();
// 调用,并断言
assertPojoEquals(dbSmsChannel, smsChannelService.getSmsChannel(id));
}
@Test
public void testGetSmsChannelList() {
// mock 数据
SmsChannelDO dbSmsChannel01 = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel01);
SmsChannelDO dbSmsChannel02 = randomPojo(SmsChannelDO.class);
smsChannelMapper.insert(dbSmsChannel02);
// 准备参数
// 调用
List<SmsChannelDO> list = smsChannelService.getSmsChannelList();
// 断言
assertEquals(2, list.size());
assertPojoEquals(dbSmsChannel01, list.get(0));
assertPojoEquals(dbSmsChannel02, list.get(1));
}
@Test
public void testGetSmsChannelPage() {
// mock 数据
@ -158,16 +186,16 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
});
smsChannelMapper.insert(dbSmsChannel);
// 测试 signature 不匹配
smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码")));
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码")));
// 测试 status 不匹配
smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 测试 createTime 不匹配
smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11))));
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11))));
// 准备参数
SmsChannelPageReqVO reqVO = new SmsChannelPageReqVO();
reqVO.setSignature("芋道");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)}));
reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
// 调用
PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(reqVO);
@ -177,14 +205,4 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
assertPojoEquals(dbSmsChannel, pageResult.getList().get(0));
}
// ========== 随机对象 ==========
@SafeVarargs
private static SmsChannelDO randomSmsChannelDO(Consumer<SmsChannelDO>... consumers) {
Consumer<SmsChannelDO> consumer = (o) -> {
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围
};
return randomPojo(SmsChannelDO.class, ArrayUtils.append(consumer, consumers));
}
}