完成部分 dict data 的单元测试

This commit is contained in:
YunaiV 2021-03-07 01:30:29 +08:00 committed by wangkai
parent 00aa082a5d
commit 84fe818c1c
8 changed files with 193 additions and 15 deletions

View File

@ -32,7 +32,7 @@ public class SysDictDataController {
@GetMapping("/list-all-simple")
// 无需添加权限认证因为前端全局都需要
public CommonResult<List<SysDictDataSimpleVO>> listSimpleDictDatas() {
List<SysDictDataDO> list = dictDataService.listDictDatas();
List<SysDictDataDO> list = dictDataService.getDictDataList();
return success(SysDictDataConvert.INSTANCE.convertList(list));
}
@ -40,7 +40,7 @@ public class SysDictDataController {
@GetMapping("/page")
// @PreAuthorize("@ss.hasPermi('system:dict:list')")
public CommonResult<PageResult<SysDictDataRespVO>> pageDictTypes(@Validated SysDictDataPageReqVO reqVO) {
return success(SysDictDataConvert.INSTANCE.convertPage(dictDataService.pageDictDatas(reqVO)));
return success(SysDictDataConvert.INSTANCE.convertPage(dictDataService.getDictDataPage(reqVO)));
}
@ApiOperation("/查询字典数据详细")
@ -83,7 +83,7 @@ public class SysDictDataController {
// @Log(title = "字典类型", businessType = BusinessType.EXPORT)
// @PreAuthorize("@ss.hasPermi('system:dict:export')")
public void export(HttpServletResponse response, @Validated SysDictDataExportReqVO reqVO) throws IOException {
List<SysDictDataDO> list = dictDataService.listDictDatas(reqVO);
List<SysDictDataDO> list = dictDataService.getDictDataList(reqVO);
List<SysDictDataExcelVO> excelDataList = SysDictDataConvert.INSTANCE.convertList02(list);
// 输出
ExcelUtils.write(response, "字典数据.xls", "数据列表",

View File

@ -16,7 +16,7 @@ public class SysDictDataBaseVO {
@ApiModelProperty(value = "显示顺序不能为空", required = true, example = "1024")
@NotBlank(message = "显示顺序不能为空")
private String sort;
private Integer sort;
@ApiModelProperty(value = "字典标签", required = true, example = "芋道")
@NotBlank(message = "字典标签不能为空")

View File

@ -65,7 +65,7 @@ public interface SysErrorCodeConstants {
ErrorCode DICT_TYPE_HAS_CHILDREN = new ErrorCode(1002006004, "无法删除,该字典类型还有字典数据");
// ========== 字典数据 1002007000 ==========
ErrorCode DICT_DATA_NOT_FOUND = new ErrorCode(1002007001, "当前字典数据不存在");
ErrorCode DICT_DATA_NOT_EXISTS = new ErrorCode(1002007001, "当前字典数据不存在");
ErrorCode DICT_DATA_NOT_ENABLE = new ErrorCode(1002007002, "字典数据不处于开启状态,不允许选择");
ErrorCode DICT_DATA_VALUE_DUPLICATE = new ErrorCode(1002007003, "已经存在该值的字典数据");

View File

@ -27,7 +27,7 @@ public interface SysDictDataService extends DictDataFrameworkService {
*
* @return 字典数据全列表
*/
List<SysDictDataDO> listDictDatas();
List<SysDictDataDO> getDictDataList();
/**
* 获得字典数据分页列表
@ -35,7 +35,7 @@ public interface SysDictDataService extends DictDataFrameworkService {
* @param reqVO 分页请求
* @return 字典数据分页列表
*/
PageResult<SysDictDataDO> pageDictDatas(SysDictDataPageReqVO reqVO);
PageResult<SysDictDataDO> getDictDataPage(SysDictDataPageReqVO reqVO);
/**
* 获得字典数据列表
@ -43,7 +43,7 @@ public interface SysDictDataService extends DictDataFrameworkService {
* @param reqVO 列表请求
* @return 字典数据列表
*/
List<SysDictDataDO> listDictDatas(SysDictDataExportReqVO reqVO);
List<SysDictDataDO> getDictDataList(SysDictDataExportReqVO reqVO);
/**
* 获得字典数据详情

View File

@ -130,19 +130,19 @@ public class SysDictDataServiceImpl implements SysDictDataService {
}
@Override
public List<SysDictDataDO> listDictDatas() {
public List<SysDictDataDO> getDictDataList() {
List<SysDictDataDO> list = dictDataMapper.selectList();
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
}
@Override
public PageResult<SysDictDataDO> pageDictDatas(SysDictDataPageReqVO reqVO) {
public PageResult<SysDictDataDO> getDictDataPage(SysDictDataPageReqVO reqVO) {
return dictDataMapper.selectPage(reqVO);
}
@Override
public List<SysDictDataDO> listDictDatas(SysDictDataExportReqVO reqVO) {
public List<SysDictDataDO> getDictDataList(SysDictDataExportReqVO reqVO) {
List<SysDictDataDO> list = dictDataMapper.selectList(reqVO);
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
@ -194,10 +194,10 @@ public class SysDictDataServiceImpl implements SysDictDataService {
private void checkCreateOrUpdate(Long id, String label, String dictType) {
// 校验自己存在
checkDictDataExists(id);
// 校验字典数据的值的唯一性
checkDictDataValueUnique(id, dictType, label);
// 校验字典类型有效
checkDictTypeValid(dictType);
// 校验字典数据的值的唯一性
checkDictDataValueUnique(id, dictType, label);
}
private void checkDictDataValueUnique(Long id, String dictType, String label) {
@ -220,7 +220,7 @@ public class SysDictDataServiceImpl implements SysDictDataService {
}
SysDictDataDO dictData = dictDataMapper.selectById(id);
if (dictData == null) {
throw ServiceExceptionUtil.exception(DICT_DATA_NOT_FOUND);
throw ServiceExceptionUtil.exception(DICT_DATA_NOT_EXISTS);
}
}

View File

@ -0,0 +1,173 @@
package cn.iocoder.dashboard.modules.system.service.dict;
import cn.iocoder.dashboard.BaseSpringBootUnitTest;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataPageReqVO;
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictDataDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.dict.SysDictTypeDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dict.SysDictDataMapper;
import cn.iocoder.dashboard.modules.system.mq.producer.dict.SysDictDataProducer;
import cn.iocoder.dashboard.modules.system.service.dict.impl.SysDictDataServiceImpl;
import cn.iocoder.dashboard.util.object.ObjectUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.DICT_DATA_NOT_EXISTS;
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 org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
/**
* {@link SysDictDataServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
public class SysDictDataServiceTest extends BaseSpringBootUnitTest {
@Resource
private SysDictDataServiceImpl dictDataService;
@Resource
private SysDictDataMapper dictDataMapper;
@MockBean
private SysDictTypeService dictTypeService;
@MockBean
private SysDictDataProducer dictDataProducer;
@Test
public void testGetDictDataPage() {
// mock 数据
SysDictDataDO dbDictData = randomPojo(SysDictDataDO.class, o -> { // 等会查询到
o.setLabel("芋艿");
o.setDictType("yunai");
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
dictDataMapper.insert(dbDictData);
// 测试 label 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setLabel("")));
// 测试 dictType 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setDictType("nai")));
// 测试 status 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
SysDictDataPageReqVO reqVO = new SysDictDataPageReqVO();
reqVO.setLabel("");
reqVO.setDictType("yu");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用
PageResult<SysDictDataDO> pageResult = dictDataService.getDictDataPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbDictData, pageResult.getList().get(0));
}
@Test
public void testGetDictDataList() {
// mock 数据
SysDictDataDO dbDictData = randomPojo(SysDictDataDO.class, o -> { // 等会查询到
o.setLabel("芋艿");
o.setDictType("yunai");
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
dictDataMapper.insert(dbDictData);
// 测试 label 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setLabel("")));
// 测试 dictType 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setDictType("nai")));
// 测试 status 不匹配
dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
SysDictDataExportReqVO reqVO = new SysDictDataExportReqVO();
reqVO.setLabel("");
reqVO.setDictType("yu");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用
List<SysDictDataDO> list = dictDataService.getDictDataList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbDictData, list.get(0));
}
@Test
public void testCreateDictData_success() {
// 准备参数
SysDictDataCreateReqVO reqVO = randomPojo(SysDictDataCreateReqVO.class,
o -> o.setStatus(randomCommonStatus()));
// mock 方法
when(dictTypeService.getDictType(eq(reqVO.getDictType())))
.thenReturn(randomPojo(SysDictTypeDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())));
// 调用
Long dictDataId = dictDataService.createDictData(reqVO);
// 断言
assertNotNull(dictDataId);
// 校验记录的属性是否正确
SysDictDataDO dictData = dictDataMapper.selectById(dictDataId);
assertPojoEquals(reqVO, dictData);
// 校验调用
verify(dictDataProducer, times(1)).sendDictDataRefreshMessage();
}
@Test
public void testUpdateDictData_success() {
// mock 数据
SysDictDataDO dbDictData = randomPojo(SysDictDataDO.class);
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据
// 准备参数
SysDictDataUpdateReqVO reqVO = randomPojo(SysDictDataUpdateReqVO.class, o -> {
o.setId(dbDictData.getId()); // 设置更新的 ID
});
// 调用
dictDataService.updateDictData(reqVO);
// 校验是否更新正确
SysDictDataDO dictData = dictDataMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, dictData);
}
@Test
public void testUpdateDictData_notExists() {
// 准备参数
SysDictDataUpdateReqVO reqVO = randomPojo(SysDictDataUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> dictDataService.updateDictData(reqVO), DICT_DATA_NOT_EXISTS);
}
@Test
public void testDeleteDictData_success() {
// mock 数据
SysDictDataDO dbDictData = randomPojo(SysDictDataDO.class);
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbDictData.getId();
// 调用
dictDataService.deleteDictData(id);
// 校验数据不存在了
assertNull(dictDataMapper.selectById(id));
}
@Test
public void testDeleteDictData_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> dictDataService.deleteDictData(id), DICT_DATA_NOT_EXISTS);
}
}

View File

@ -33,7 +33,7 @@ import static org.mockito.Mockito.when;
/**
* {@link SysDictTypeServiceImpl} 的单元测试类
*
* @author
* @author 道源码
*/
public class SysDictTypeServiceTest extends BaseSpringBootUnitTest {

View File

@ -2,6 +2,7 @@ package cn.iocoder.dashboard.util;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;
@ -67,6 +68,10 @@ public class RandomUtils {
.map(i -> randomPojo(clazz)).collect(Collectors.toSet());
}
public static Integer randomCommonStatus() {
return RandomUtil.randomEle(CommonStatusEnum.values()).getStatus();
}
@SafeVarargs
public static SysUserDO randomUserDO(Consumer<SysUserDO>... consumers) {
return randomPojo(SysUserDO.class, consumers);