From 92d3085b1f1eff49e73fb793a47e43d27757df10 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 7 Mar 2021 01:30:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=83=A8=E5=88=86=20dict=20d?= =?UTF-8?q?ata=20=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dict/SysDictDataController.java | 6 +- .../dict/vo/data/SysDictDataBaseVO.java | 2 +- .../system/enums/SysErrorCodeConstants.java | 2 +- .../service/dict/SysDictDataService.java | 6 +- .../dict/impl/SysDictDataServiceImpl.java | 12 +- .../service/dict/SysDictDataServiceTest.java | 173 ++++++++++++++++++ .../service/dict/SysDictTypeServiceTest.java | 2 +- .../iocoder/dashboard/util/RandomUtils.java | 5 + 8 files changed, 193 insertions(+), 15 deletions(-) create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/SysDictDataController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/SysDictDataController.java index 1f37c62b7..baeb68342 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/SysDictDataController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/SysDictDataController.java @@ -32,7 +32,7 @@ public class SysDictDataController { @GetMapping("/list-all-simple") // 无需添加权限认证,因为前端全局都需要 public CommonResult> listSimpleDictDatas() { - List list = dictDataService.listDictDatas(); + List 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> 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 list = dictDataService.listDictDatas(reqVO); + List list = dictDataService.getDictDataList(reqVO); List excelDataList = SysDictDataConvert.INSTANCE.convertList02(list); // 输出 ExcelUtils.write(response, "字典数据.xls", "数据列表", diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/vo/data/SysDictDataBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/vo/data/SysDictDataBaseVO.java index bfcdf570d..702eba0ef 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/vo/data/SysDictDataBaseVO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/dict/vo/data/SysDictDataBaseVO.java @@ -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 = "字典标签不能为空") diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java index cc572033d..21d3e8910 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java @@ -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, "已经存在该值的字典数据"); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataService.java index 800236dfa..3969e420f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataService.java @@ -27,7 +27,7 @@ public interface SysDictDataService extends DictDataFrameworkService { * * @return 字典数据全列表 */ - List listDictDatas(); + List getDictDataList(); /** * 获得字典数据分页列表 @@ -35,7 +35,7 @@ public interface SysDictDataService extends DictDataFrameworkService { * @param reqVO 分页请求 * @return 字典数据分页列表 */ - PageResult pageDictDatas(SysDictDataPageReqVO reqVO); + PageResult getDictDataPage(SysDictDataPageReqVO reqVO); /** * 获得字典数据列表 @@ -43,7 +43,7 @@ public interface SysDictDataService extends DictDataFrameworkService { * @param reqVO 列表请求 * @return 字典数据列表 */ - List listDictDatas(SysDictDataExportReqVO reqVO); + List getDictDataList(SysDictDataExportReqVO reqVO); /** * 获得字典数据详情 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java index 8ddd1c776..e8ebb82dc 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/dict/impl/SysDictDataServiceImpl.java @@ -130,19 +130,19 @@ public class SysDictDataServiceImpl implements SysDictDataService { } @Override - public List listDictDatas() { + public List getDictDataList() { List list = dictDataMapper.selectList(); list.sort(COMPARATOR_TYPE_AND_SORT); return list; } @Override - public PageResult pageDictDatas(SysDictDataPageReqVO reqVO) { + public PageResult getDictDataPage(SysDictDataPageReqVO reqVO) { return dictDataMapper.selectPage(reqVO); } @Override - public List listDictDatas(SysDictDataExportReqVO reqVO) { + public List getDictDataList(SysDictDataExportReqVO reqVO) { List 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); } } diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java new file mode 100644 index 000000000..318a4afca --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictDataServiceTest.java @@ -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 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 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); + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java index 0e1affa64..e25fe2577 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/dict/SysDictTypeServiceTest.java @@ -33,7 +33,7 @@ import static org.mockito.Mockito.when; /** * {@link SysDictTypeServiceImpl} 的单元测试类 * -* @author 芋艿 +* @author 芋道源码 */ public class SysDictTypeServiceTest extends BaseSpringBootUnitTest { diff --git a/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java index 572668647..c668f980c 100644 --- a/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java +++ b/src/test/java/cn/iocoder/dashboard/util/RandomUtils.java @@ -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... consumers) { return randomPojo(SysUserDO.class, consumers);