diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java b/src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java index 53fc5f6a5..f618db87f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/enums/InfErrorCodeConstants.java @@ -10,7 +10,7 @@ import cn.iocoder.dashboard.common.exception.ErrorCode; public interface InfErrorCodeConstants { // ========== 参数配置 1001000000 ========== - ErrorCode CONFIG_NOT_FOUND = new ErrorCode(1001000001, "参数配置不存在"); + ErrorCode CONFIG_NOT_EXISTS = new ErrorCode(1001000001, "参数配置不存在"); ErrorCode CONFIG_KEY_DUPLICATE = new ErrorCode(1001000002, "参数配置 key 重复"); ErrorCode CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE = new ErrorCode(1001000003, "不能删除类型为系统内置的参数配置"); ErrorCode CONFIG_GET_VALUE_ERROR_IF_SENSITIVE = new ErrorCode(1001000004, "不允许获取敏感配置到前端"); diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java index 1f23e878a..41bb5a357 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/service/config/impl/InfConfigServiceImpl.java @@ -105,7 +105,7 @@ public class InfConfigServiceImpl implements InfConfigService { } InfConfigDO config = configMapper.selectById(id); if (config == null) { - throw ServiceExceptionUtil.exception(CONFIG_NOT_FOUND); + throw ServiceExceptionUtil.exception(CONFIG_NOT_EXISTS); } return config; } diff --git a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java index 719f877f9..055643048 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java @@ -17,6 +17,8 @@ import java.util.function.Consumer; import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_KEY_DUPLICATE; +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS; +import static cn.iocoder.dashboard.util.AssertUtils.assertExceptionEquals; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; import static org.junit.jupiter.api.Assertions.*; @@ -37,7 +39,7 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { @Test public void testCreateConfig_success() { // 准备参数 - InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO(); + InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class); // mock // 调用 @@ -55,24 +57,28 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { @Test public void testCreateConfig_keyDuplicate() { // 准备参数 - InfConfigCreateReqVO reqVO = randomInfConfigCreateReqVO(); + InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class); // mock 数据 configMapper.insert(randomInfConfigDO(o -> { // @Sql o.setKey(reqVO.getKey()); // 模拟 key 重复 })); // 调用 - ServiceException serviceException = assertThrows(ServiceException.class, () -> configService.createConfig(reqVO)); - // 断言 - assertPojoEquals(CONFIG_KEY_DUPLICATE, serviceException); + ServiceException serviceException = assertThrows(ServiceException.class, + () -> configService.createConfig(reqVO)); + // 断言异常 + assertExceptionEquals(CONFIG_KEY_DUPLICATE, serviceException); } @Test public void testUpdateConfig_success() { - // 准备参数 - InfConfigUpdateReqVO reqVO = randomInfConfigUpdateReqVO(); // mock 数据 - configMapper.insert(randomInfConfigDO(o -> o.setId(reqVO.getId())));// @Sql: 先插入出一条存在的数据 + InfConfigDO dbConfig = randomInfConfigDO(); + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据 + // 准备参数 + InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class, o -> { + o.setId(dbConfig.getId()); // 设置更新的 ID + }); // 调用 configService.updateConfig(reqVO); @@ -83,21 +89,25 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest { verify(configProducer, times(1)).sendConfigRefreshMessage(); } + @Test + public void testUpdateConfig_notExists() { + // 准备参数 + InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class); + + // 调用 + ServiceException serviceException = assertThrows(ServiceException.class, + () -> configService.updateConfig(reqVO)); + // 断言异常 + assertExceptionEquals(CONFIG_NOT_EXISTS, serviceException); + } + // ========== 随机对象 ========== @SafeVarargs private static InfConfigDO randomInfConfigDO(Consumer... consumers) { InfConfigDO config = randomPojo(InfConfigDO.class, consumers); - config.setType(randomEle(InfConfigTypeEnum.values()).getType()); + config.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围 return config; } - private static InfConfigCreateReqVO randomInfConfigCreateReqVO() { - return randomPojo(InfConfigCreateReqVO.class); - } - - private static InfConfigUpdateReqVO randomInfConfigUpdateReqVO() { - return randomPojo(InfConfigUpdateReqVO.class); - } - } diff --git a/src/test/java/cn/iocoder/dashboard/util/AssertUtils.java b/src/test/java/cn/iocoder/dashboard/util/AssertUtils.java index fb34857ac..957a5028f 100644 --- a/src/test/java/cn/iocoder/dashboard/util/AssertUtils.java +++ b/src/test/java/cn/iocoder/dashboard/util/AssertUtils.java @@ -52,7 +52,7 @@ public class AssertUtils { * @param errorCode 错误码对象 * @param serviceException 业务异常 */ - public static void assertPojoEquals(ErrorCode errorCode, ServiceException serviceException) { + public static void assertExceptionEquals(ErrorCode errorCode, ServiceException serviceException) { Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配"); Assertions.assertEquals(errorCode.getMessage(), serviceException.getMessage(), "错误提示不匹配"); }