完善 DictDataServiceImpl 单元测试

pull/25/head
YunaiV 2023-02-01 07:35:08 +08:00
parent 6d770bba17
commit 30f097ed26
7 changed files with 167 additions and 51 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.validateDictDatas(DictTypeConstants.TASK_ASSIGN_SCRIPT,
CollectionUtils.convertSet(options, String::valueOf));
} else {
throw new IllegalArgumentException(format("未知的规则类型({})", type));

View File

@ -25,8 +25,8 @@ public interface DictDataApi {
@ApiImplicitParam(name = "dictType", value = "字典类型", example = "SEX", required = true, dataTypeClass = String.class),
@ApiImplicitParam(name = "values", value = "字典数据值的数组", example = "1,2", required = true, allowMultiple = true)
})
CommonResult<Boolean> validDictDatas(@RequestParam("dictType") String dictType,
@RequestParam("values") Collection<String> values);
CommonResult<Boolean> validateDictDatas(@RequestParam("dictType") String dictType,
@RequestParam("values") Collection<String> values);
@GetMapping(PREFIX + "/get")
@ApiOperation("获得指定的字典数据")

View File

@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.convert.dict.DictDataConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
@ -25,8 +24,8 @@ public class DictDataApiImpl implements DictDataApi {
private DictDataService dictDataService;
@Override
public CommonResult<Boolean> validDictDatas(String dictType, Collection<String> values) {
dictDataService.validDictDatas(dictType, values);
public CommonResult<Boolean> validateDictDatas(String dictType, Collection<String> values) {
dictDataService.validateDictDataList(dictType, values);
return success(true);
}

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