完善 DictDataServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-01 07:34:57 +08:00
parent bef06ef940
commit 0537421655
7 changed files with 166 additions and 49 deletions

View File

@ -224,7 +224,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER_GROUP.getType())) {
userGroupService.validUserGroups(options);
} else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.SCRIPT.getType())) {
dictDataApi.validDictDatas(DictTypeConstants.TASK_ASSIGN_SCRIPT,
dictDataApi.validateDictDataList(DictTypeConstants.TASK_ASSIGN_SCRIPT,
CollectionUtils.convertSet(options, String::valueOf));
} else {
throw new IllegalArgumentException(format("未知的规则类型({})", type));

View File

@ -19,7 +19,7 @@ public interface DictDataApi {
* @param dictType 字典类型
* @param values 字典数据值的数组
*/
void validDictDatas(String dictType, Collection<String> values);
void validateDictDataList(String dictType, Collection<String> values);
/**
* 获得指定的字典数据从缓存中

View File

@ -21,8 +21,8 @@ public class DictDataApiImpl implements DictDataApi {
private DictDataService dictDataService;
@Override
public void validDictDatas(String dictType, Collection<String> values) {
dictDataService.validDictDatas(dictType, values);
public void validateDictDataList(String dictType, Collection<String> values) {
dictDataService.validateDictDataList(dictType, values);
}
@Override

View File

@ -62,7 +62,7 @@ public class DictDataController {
@ApiOperation(value = "获得全部字典数据列表", notes = "一般用于管理后台缓存字典数据在本地")
// 无需添加权限认证因为前端全局都需要
public CommonResult<List<DictDataSimpleRespVO>> getSimpleDictDatas() {
List<DictDataDO> list = dictDataService.getDictDatas();
List<DictDataDO> list = dictDataService.getDictDataList();
return success(DictDataConvert.INSTANCE.convertList(list));
}
@ -86,7 +86,7 @@ public class DictDataController {
@PreAuthorize("@ss.hasPermission('system:dict:export')")
@OperateLog(type = EXPORT)
public void export(HttpServletResponse response, @Valid DictDataExportReqVO reqVO) throws IOException {
List<DictDataDO> list = dictDataService.getDictDatas(reqVO);
List<DictDataDO> list = dictDataService.getDictDataList(reqVO);
List<DictDataExcelVO> data = DictDataConvert.INSTANCE.convertList02(list);
// 输出
ExcelUtils.write(response, "字典数据.xls", "数据列表", DictDataExcelVO.class, data);

View File

@ -44,7 +44,7 @@ public interface DictDataService {
*
* @return 字典数据全列表
*/
List<DictDataDO> getDictDatas();
List<DictDataDO> getDictDataList();
/**
* 获得字典数据分页列表
@ -60,7 +60,7 @@ public interface DictDataService {
* @param reqVO 列表请求
* @return 字典数据列表
*/
List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO);
List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO);
/**
* 获得字典数据详情
@ -86,7 +86,7 @@ public interface DictDataService {
* @param dictType 字典类型
* @param values 字典数据值的数组
*/
void validDictDatas(String dictType, Collection<String> values);
void validateDictDataList(String dictType, Collection<String> values);
/**
* 获得指定的字典数据

View File

@ -48,7 +48,7 @@ public class DictDataServiceImpl implements DictDataService {
private DictDataMapper dictDataMapper;
@Override
public List<DictDataDO> getDictDatas() {
public List<DictDataDO> getDictDataList() {
List<DictDataDO> list = dictDataMapper.selectList();
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
@ -60,7 +60,7 @@ public class DictDataServiceImpl implements DictDataService {
}
@Override
public List<DictDataDO> getDictDatas(DictDataExportReqVO reqVO) {
public List<DictDataDO> getDictDataList(DictDataExportReqVO reqVO) {
List<DictDataDO> list = dictDataMapper.selectList(reqVO);
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
@ -74,7 +74,7 @@ public class DictDataServiceImpl implements DictDataService {
@Override
public Long createDictData(DictDataCreateReqVO reqVO) {
// 校验正确性
checkCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType());
validateDictDataForCreateOrUpdate(null, reqVO.getValue(), reqVO.getDictType());
// 插入字典类型
DictDataDO dictData = DictDataConvert.INSTANCE.convert(reqVO);
@ -85,7 +85,7 @@ public class DictDataServiceImpl implements DictDataService {
@Override
public void updateDictData(DictDataUpdateReqVO reqVO) {
// 校验正确性
checkCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType());
validateDictDataForCreateOrUpdate(reqVO.getId(), reqVO.getValue(), reqVO.getDictType());
// 更新字典类型
DictDataDO updateObj = DictDataConvert.INSTANCE.convert(reqVO);
@ -95,7 +95,7 @@ public class DictDataServiceImpl implements DictDataService {
@Override
public void deleteDictData(Long id) {
// 校验是否存在
checkDictDataExists(id);
validateDictDataExists(id);
// 删除字典数据
dictDataMapper.deleteById(id);
@ -106,18 +106,17 @@ public class DictDataServiceImpl implements DictDataService {
return dictDataMapper.selectCountByDictType(dictType);
}
private void checkCreateOrUpdate(Long id, String value, String dictType) {
private void validateDictDataForCreateOrUpdate(Long id, String value, String dictType) {
// 校验自己存在
checkDictDataExists(id);
validateDictDataExists(id);
// 校验字典类型有效
checkDictTypeValid(dictType);
validateDictTypeExists(dictType);
// 校验字典数据的值的唯一性
checkDictDataValueUnique(id, dictType, value);
validateDictDataValueUnique(id, dictType, value);
}
@VisibleForTesting
public void checkDictDataValueUnique(Long id, String dictType, String value) {
public void validateDictDataValueUnique(Long id, String dictType, String value) {
DictDataDO dictData = dictDataMapper.selectByDictTypeAndValue(dictType, value);
if (dictData == null) {
return;
@ -132,7 +131,7 @@ public class DictDataServiceImpl implements DictDataService {
}
@VisibleForTesting
public void checkDictDataExists(Long id) {
public void validateDictDataExists(Long id) {
if (id == null) {
return;
}
@ -143,7 +142,7 @@ public class DictDataServiceImpl implements DictDataService {
}
@VisibleForTesting
public void checkDictTypeValid(String type) {
public void validateDictTypeExists(String type) {
DictTypeDO dictType = dictTypeService.getDictType(type);
if (dictType == null) {
throw exception(DICT_TYPE_NOT_EXISTS);
@ -154,7 +153,7 @@ public class DictDataServiceImpl implements DictDataService {
}
@Override
public void validDictDatas(String dictType, Collection<String> values) {
public void validateDictDataList(String dictType, Collection<String> values) {
if (CollUtil.isEmpty(values)) {
return;
}

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.system.service.dict;
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.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO;
@ -20,16 +19,18 @@ import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;
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.*;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@Import(DictDataServiceImpl.class)
public class DictDataServiceTest extends BaseDbUnitTest {
public class DictDataServiceImplTest extends BaseDbUnitTest {
@Resource
private DictDataServiceImpl dictDataService;
@ -39,6 +40,23 @@ public class DictDataServiceTest extends BaseDbUnitTest {
@MockBean
private DictTypeService dictTypeService;
@Test
public void testGetDictDataList() {
// mock 数据
DictDataDO dictDataDO01 = randomDictDataDO().setDictType("yunai").setSort(2);
dictDataMapper.insert(dictDataDO01);
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setSort(1);
dictDataMapper.insert(dictDataDO02);
// 准备参数
// 调用
List<DictDataDO> dictDataDOList = dictDataService.getDictDataList();
// 断言
assertEquals(2, dictDataDOList.size());
assertPojoEquals(dictDataDO02, dictDataDOList.get(0));
assertPojoEquals(dictDataDO01, dictDataDOList.get(1));
}
@Test
public void testGetDictDataPage() {
// mock 数据
@ -49,11 +67,11 @@ public class DictDataServiceTest extends BaseDbUnitTest {
});
dictDataMapper.insert(dbDictData);
// 测试 label 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("")));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("")));
// 测试 dictType 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
// 测试 status 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
DictDataPageReqVO reqVO = new DictDataPageReqVO();
reqVO.setLabel("");
@ -69,7 +87,7 @@ public class DictDataServiceTest extends BaseDbUnitTest {
}
@Test
public void testGetDictDataList() {
public void testGetDictDataList_export() {
// mock 数据
DictDataDO dbDictData = randomPojo(DictDataDO.class, o -> { // 等会查询到
o.setLabel("芋艿");
@ -78,11 +96,11 @@ public class DictDataServiceTest extends BaseDbUnitTest {
});
dictDataMapper.insert(dbDictData);
// 测试 label 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("")));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("")));
// 测试 dictType 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai")));
// 测试 status 不匹配
dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
DictDataExportReqVO reqVO = new DictDataExportReqVO();
reqVO.setLabel("");
@ -90,12 +108,26 @@ public class DictDataServiceTest extends BaseDbUnitTest {
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用
List<DictDataDO> list = dictDataService.getDictDatas(reqVO);
List<DictDataDO> list = dictDataService.getDictDataList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbDictData, list.get(0));
}
@Test
public void testGetDictData() {
// mock 数据
DictDataDO dbDictData = randomDictDataDO();
dictDataMapper.insert(dbDictData);
// 准备参数
Long id = dbDictData.getId();
// 调用
DictDataDO dictData = dictDataService.getDictData(id);
// 断言
assertPojoEquals(dbDictData, dictData);
}
@Test
public void testCreateDictData_success() {
// 准备参数
@ -148,54 +180,54 @@ public class DictDataServiceTest extends BaseDbUnitTest {
}
@Test
public void testCheckDictDataExists_success() {
public void testValidateDictDataExists_success() {
// mock 数据
DictDataDO dbDictData = randomDictDataDO();
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据
// 调用成功
dictDataService.checkDictDataExists(dbDictData.getId());
dictDataService.validateDictDataExists(dbDictData.getId());
}
@Test
public void testCheckDictDataExists_notExists() {
assertServiceException(() -> dictDataService.checkDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS);
public void testValidateDictDataExists_notExists() {
assertServiceException(() -> dictDataService.validateDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS);
}
@Test
public void testCheckDictTypeValid_success() {
public void testValidateDictTypeExists_success() {
// mock 方法数据类型被禁用
String type = randomString();
when(dictTypeService.getDictType(eq(type))).thenReturn(randomDictTypeDO(type));
// 调用, 成功
dictDataService.checkDictTypeValid(type);
dictDataService.validateDictTypeExists(type);
}
@Test
public void testCheckDictTypeValid_notExists() {
assertServiceException(() -> dictDataService.checkDictTypeValid(randomString()), DICT_TYPE_NOT_EXISTS);
public void testValidateDictTypeExists_notExists() {
assertServiceException(() -> dictDataService.validateDictTypeExists(randomString()), DICT_TYPE_NOT_EXISTS);
}
@Test
public void testCheckDictTypeValid_notEnable() {
public void testValidateDictTypeExists_notEnable() {
// mock 方法数据类型被禁用
String dictType = randomString();
when(dictTypeService.getDictType(eq(dictType))).thenReturn(
randomPojo(DictTypeDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 调用, 并断言异常
assertServiceException(() -> dictDataService.checkDictTypeValid(dictType), DICT_TYPE_NOT_ENABLE);
assertServiceException(() -> dictDataService.validateDictTypeExists(dictType), DICT_TYPE_NOT_ENABLE);
}
@Test
public void testCheckDictDataValueUnique_success() {
public void testValidateDictDataValueUnique_success() {
// 调用成功
dictDataService.checkDictDataValueUnique(randomLongId(), randomString(), randomString());
dictDataService.validateDictDataValueUnique(randomLongId(), randomString(), randomString());
}
@Test
public void testCheckDictDataValueUnique_valueDuplicateForCreate() {
public void testValidateDictDataValueUnique_valueDuplicateForCreate() {
// 准备参数
String dictType = randomString();
String value = randomString();
@ -206,12 +238,12 @@ public class DictDataServiceTest extends BaseDbUnitTest {
}));
// 调用校验异常
assertServiceException(() -> dictDataService.checkDictDataValueUnique(null, dictType, value),
assertServiceException(() -> dictDataService.validateDictDataValueUnique(null, dictType, value),
DICT_DATA_VALUE_DUPLICATE);
}
@Test
public void testCheckDictDataValueUnique_valueDuplicateForUpdate() {
public void testValidateDictDataValueUnique_valueDuplicateForUpdate() {
// 准备参数
Long id = randomLongId();
String dictType = randomString();
@ -223,10 +255,96 @@ public class DictDataServiceTest extends BaseDbUnitTest {
}));
// 调用校验异常
assertServiceException(() -> dictDataService.checkDictDataValueUnique(id, dictType, value),
assertServiceException(() -> dictDataService.validateDictDataValueUnique(id, dictType, value),
DICT_DATA_VALUE_DUPLICATE);
}
@Test
public void testCountByDictType() {
// mock 数据
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai")));
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("tudou")));
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai")));
// 准备参数
String dictType = "yunai";
// 调用
long count = dictDataService.countByDictType(dictType);
// 校验
assertEquals(2L, count);
}
@Test
public void testValidateDictDataList_success() {
// mock 数据
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
dictDataMapper.insert(dictDataDO);
// 准备参数
String dictType = dictDataDO.getDictType();
List<String> values = singletonList(dictDataDO.getValue());
// 调用无需断言
dictDataService.validateDictDataList(dictType, values);
}
@Test
public void testValidateDictDataList_notFound() {
// 准备参数
String dictType = randomString();
List<String> values = singletonList(randomString());
// 调用, 并断言异常
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), DICT_DATA_NOT_EXISTS);
}
@Test
public void testValidateDictDataList_notEnable() {
// mock 数据
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
dictDataMapper.insert(dictDataDO);
// 准备参数
String dictType = dictDataDO.getDictType();
List<String> values = singletonList(dictDataDO.getValue());
// 调用, 并断言异常
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values),
DICT_DATA_NOT_ENABLE, dictDataDO.getLabel());
}
@Test
public void testGetDictData_dictType() {
// mock 数据
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setValue("1");
dictDataMapper.insert(dictDataDO);
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setValue("2");
dictDataMapper.insert(dictDataDO02);
// 准备参数
String dictType = "yunai";
String value = "1";
// 调用
DictDataDO dbDictData = dictDataService.getDictData(dictType, value);
// 断言
assertEquals(dictDataDO, dbDictData);
}
@Test
public void testParseDictData() {
// mock 数据
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setLabel("1");
dictDataMapper.insert(dictDataDO);
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setLabel("2");
dictDataMapper.insert(dictDataDO02);
// 准备参数
String dictType = "yunai";
String label = "1";
// 调用
DictDataDO dbDictData = dictDataService.parseDictData(dictType, label);
// 断言
assertEquals(dictDataDO, dbDictData);
}
// ========== 随机对象 ==========
@SafeVarargs