|
@@ -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<InfConfigDO>... 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);
|
|
|
- }
|
|
|
-
|
|
|
}
|