Commit 95857ace9a6027aa0cb107a8c5ab8419e6d16786

Authored by YunaiV
1 parent 72817a86

完成 config 模块的单元测试

src/main/java/cn/iocoder/dashboard/common/exception/util/ServiceExceptionUtil.java
... ... @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.common.exception.util;
2 2  
3 3 import cn.iocoder.dashboard.common.exception.ErrorCode;
4 4 import cn.iocoder.dashboard.common.exception.ServiceException;
  5 +import com.google.common.annotations.VisibleForTesting;
5 6 import org.slf4j.Logger;
6 7 import org.slf4j.LoggerFactory;
7 8  
... ... @@ -91,7 +92,8 @@ public class ServiceExceptionUtil {
91 92 * @param params 参数
92 93 * @return 格式化后的提示
93 94 */
94   - private static String doFormat(int code, String messagePattern, Object... params) {
  95 + @VisibleForTesting
  96 + public static String doFormat(int code, String messagePattern, Object... params) {
95 97 StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50);
96 98 int i = 0;
97 99 int j;
... ...
src/main/java/cn/iocoder/dashboard/common/pojo/PageParam.java
... ... @@ -13,14 +13,17 @@ import java.io.Serializable;
13 13 @Data
14 14 public class PageParam implements Serializable {
15 15  
  16 + private static final Integer PAGE_NO = 1;
  17 + private static final Integer PAGE_SIZE = 10;
  18 +
16 19 @ApiModelProperty(value = "页码,从 1 开始", required = true,example = "1")
17 20 @NotNull(message = "页码不能为空")
18 21 @Min(value = 1, message = "页码最小值为 1")
19   - private Integer pageNo;
  22 + private Integer pageNo = PAGE_NO;
20 23  
21 24 @ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10")
22 25 @NotNull(message = "每页条数不能为空")
23 26 @Range(min = 1, max = 100, message = "条数范围为 [1, 100]")
24   - private Integer pageSize;
  27 + private Integer pageSize = PAGE_SIZE;
25 28  
26 29 }
... ...
src/main/java/cn/iocoder/dashboard/util/date/DateUtils.java
1 1 package cn.iocoder.dashboard.util.date;
2 2  
3 3 import java.time.Duration;
  4 +import java.util.Calendar;
4 5 import java.util.Date;
5 6  
6 7 /**
... ... @@ -22,4 +23,40 @@ public class DateUtils {
22 23 return endTime.getTime() - startTime.getTime();
23 24 }
24 25  
  26 + /**
  27 + * 创建指定时间
  28 + *
  29 + * @param year 年
  30 + * @param mouth 月
  31 + * @param day 日
  32 + * @return 指定时间
  33 + */
  34 + public static Date buildTime(int year, int mouth, int day) {
  35 + return buildTime(year, mouth, day, 0, 0, 0);
  36 + }
  37 +
  38 + /**
  39 + * 创建指定时间
  40 + *
  41 + * @param year 年
  42 + * @param mouth 月
  43 + * @param day 日
  44 + * @param hour 小时
  45 + * @param minute 分钟
  46 + * @param second 秒
  47 + * @return 指定时间
  48 + */
  49 + public static Date buildTime(int year, int mouth, int day,
  50 + int hour, int minute, int second) {
  51 + Calendar calendar = Calendar.getInstance();
  52 + calendar.set(Calendar.YEAR, year);
  53 + calendar.set(Calendar.MONTH, mouth - 1);
  54 + calendar.set(Calendar.DAY_OF_MONTH, day);
  55 + calendar.set(Calendar.HOUR_OF_DAY, hour);
  56 + calendar.set(Calendar.MINUTE, minute);
  57 + calendar.set(Calendar.SECOND, second);
  58 + calendar.set(Calendar.MILLISECOND, 0); // 一般情况下,都是 0 毫秒
  59 + return calendar.getTime();
  60 + }
  61 +
25 62 }
... ...
src/main/java/cn/iocoder/dashboard/util/object/ObjectUtils.java 0 → 100644
  1 +package cn.iocoder.dashboard.util.object;
  2 +
  3 +import cn.hutool.core.util.ObjectUtil;
  4 +
  5 +import java.util.function.Consumer;
  6 +
  7 +/**
  8 + * Object 工具类
  9 + *
  10 + * @author 芋道源码
  11 + */
  12 +public class ObjectUtils {
  13 +
  14 + public static <T> T clone(T object, Consumer<T> consumer) {
  15 + T result = ObjectUtil.clone(object);
  16 + if (result != null) {
  17 + consumer.accept(result);
  18 + }
  19 + return result;
  20 + }
  21 +
  22 +}
... ...
src/test/java/cn/iocoder/dashboard/modules/infra/service/config/InfConfigServiceImplTest.java
1 1 package cn.iocoder.dashboard.modules.infra.service.config;
2 2  
  3 +import cn.hutool.core.util.ArrayUtil;
3 4 import cn.iocoder.dashboard.BaseSpringBootUnitTest;
4   -import cn.iocoder.dashboard.common.exception.ServiceException;
  5 +import cn.iocoder.dashboard.common.pojo.PageResult;
5 6 import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigCreateReqVO;
  7 +import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigExportReqVO;
  8 +import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigPageReqVO;
6 9 import cn.iocoder.dashboard.modules.infra.controller.config.vo.InfConfigUpdateReqVO;
7 10 import cn.iocoder.dashboard.modules.infra.dal.dataobject.config.InfConfigDO;
8 11 import cn.iocoder.dashboard.modules.infra.dal.mysql.config.InfConfigMapper;
9 12 import cn.iocoder.dashboard.modules.infra.enums.config.InfConfigTypeEnum;
10 13 import cn.iocoder.dashboard.modules.infra.mq.producer.config.InfConfigProducer;
11 14 import cn.iocoder.dashboard.modules.infra.service.config.impl.InfConfigServiceImpl;
  15 +import cn.iocoder.dashboard.util.object.ObjectUtils;
12 16 import org.junit.jupiter.api.Test;
13 17 import org.springframework.boot.test.mock.mockito.MockBean;
14 18  
15 19 import javax.annotation.Resource;
  20 +import java.util.List;
16 21 import java.util.function.Consumer;
17 22  
18 23 import static cn.hutool.core.util.RandomUtil.randomEle;
19   -import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_KEY_DUPLICATE;
20   -import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.CONFIG_NOT_EXISTS;
21   -import static cn.iocoder.dashboard.util.AssertUtils.assertExceptionEquals;
  24 +import static cn.iocoder.dashboard.modules.infra.enums.InfErrorCodeConstants.*;
22 25 import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
  26 +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
23 27 import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
  28 +import static cn.iocoder.dashboard.util.date.DateUtils.buildTime;
24 29 import static org.junit.jupiter.api.Assertions.*;
25 30 import static org.mockito.Mockito.times;
26 31 import static org.mockito.Mockito.verify;
... ... @@ -37,10 +42,91 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
37 42 private InfConfigProducer configProducer;
38 43  
39 44 @Test
  45 + public void testGetConfigPage() {
  46 + // mock 数据
  47 + InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到
  48 + o.setName("芋艿");
  49 + o.setKey("yunai");
  50 + o.setType(InfConfigTypeEnum.SYSTEM.getType());
  51 + o.setCreateTime(buildTime(2021, 2, 1));
  52 + });
  53 + configMapper.insert(dbConfig);
  54 + // 测试 name 不匹配
  55 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆")));
  56 + // 测试 key 不匹配
  57 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou")));
  58 + // 测试 type 不匹配
  59 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType())));
  60 + // 测试 createTime 不匹配
  61 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
  62 + // 准备参数
  63 + InfConfigPageReqVO reqVO = new InfConfigPageReqVO();
  64 + reqVO.setName("艿");
  65 + reqVO.setKey("nai");
  66 + reqVO.setType(InfConfigTypeEnum.SYSTEM.getType());
  67 + reqVO.setBeginTime(buildTime(2021, 1, 15));
  68 + reqVO.setEndTime(buildTime(2021, 2, 15));
  69 +
  70 + // 调用
  71 + PageResult<InfConfigDO> pageResult = configService.getConfigPage(reqVO);
  72 + // 断言
  73 + assertEquals(1, pageResult.getTotal());
  74 + assertEquals(1, pageResult.getList().size());
  75 + assertPojoEquals(dbConfig, pageResult.getList().get(0));
  76 + }
  77 +
  78 + @Test
  79 + public void testGetConfigList() {
  80 + // mock 数据
  81 + InfConfigDO dbConfig = randomInfConfigDO(o -> { // 等会查询到
  82 + o.setName("芋艿");
  83 + o.setKey("yunai");
  84 + o.setType(InfConfigTypeEnum.SYSTEM.getType());
  85 + o.setCreateTime(buildTime(2021, 2, 1));
  86 + });
  87 + configMapper.insert(dbConfig);
  88 + // 测试 name 不匹配
  89 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆")));
  90 + // 测试 key 不匹配
  91 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou")));
  92 + // 测试 type 不匹配
  93 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType())));
  94 + // 测试 createTime 不匹配
  95 + configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
  96 + // 准备参数
  97 + InfConfigExportReqVO reqVO = new InfConfigExportReqVO();
  98 + reqVO.setName("艿");
  99 + reqVO.setKey("nai");
  100 + reqVO.setType(InfConfigTypeEnum.SYSTEM.getType());
  101 + reqVO.setBeginTime(buildTime(2021, 1, 15));
  102 + reqVO.setEndTime(buildTime(2021, 2, 15));
  103 +
  104 + // 调用
  105 + List<InfConfigDO> list = configService.getConfigList(reqVO);
  106 + // 断言
  107 + assertEquals(1, list.size());
  108 + assertPojoEquals(dbConfig, list.get(0));
  109 + }
  110 +
  111 + @Test
  112 + public void testGetConfigByKey() {
  113 + // mock 数据
  114 + InfConfigDO dbConfig = randomInfConfigDO();
  115 + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
  116 + // 准备参数
  117 + String key = dbConfig.getKey();
  118 +
  119 + // 调用
  120 + InfConfigDO config = configService.getConfigByKey(key);
  121 + // 断言
  122 + assertNotNull(config);
  123 + assertPojoEquals(dbConfig, config);
  124 + }
  125 +
  126 + @Test
40 127 public void testCreateConfig_success() {
41 128 // 准备参数
42 129 InfConfigCreateReqVO reqVO = randomPojo(InfConfigCreateReqVO.class);
43   - // mock
44 130  
45 131 // 调用
46 132 Long configId = configService.createConfig(reqVO);
... ... @@ -63,11 +149,8 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
63 149 o.setKey(reqVO.getKey()); // 模拟 key 重复
64 150 }));
65 151  
66   - // 调用
67   - ServiceException serviceException = assertThrows(ServiceException.class,
68   - () -> configService.createConfig(reqVO));
69   - // 断言异常
70   - assertExceptionEquals(CONFIG_KEY_DUPLICATE, serviceException);
  152 + // 调用, 并断言异常
  153 + assertServiceException(() -> configService.createConfig(reqVO), CONFIG_KEY_DUPLICATE);
71 154 }
72 155  
73 156 @Test
... ... @@ -94,20 +177,51 @@ public class InfConfigServiceImplTest extends BaseSpringBootUnitTest {
94 177 // 准备参数
95 178 InfConfigUpdateReqVO reqVO = randomPojo(InfConfigUpdateReqVO.class);
96 179  
  180 + // 调用, 并断言异常
  181 + assertServiceException(() -> configService.updateConfig(reqVO), CONFIG_NOT_EXISTS);
  182 + }
  183 +
  184 + @Test
  185 + public void testDeleteConfig_success() {
  186 + // mock 数据
  187 + InfConfigDO dbConfig = randomInfConfigDO(o -> {
  188 + o.setType(InfConfigTypeEnum.CUSTOM.getType()); // 只能删除 CUSTOM 类型
  189 + });
  190 + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
  191 + // 准备参数
  192 + Long id = dbConfig.getId();
  193 +
97 194 // 调用
98   - ServiceException serviceException = assertThrows(ServiceException.class,
99   - () -> configService.updateConfig(reqVO));
100   - // 断言异常
101   - assertExceptionEquals(CONFIG_NOT_EXISTS, serviceException);
  195 + configService.deleteConfig(id);
  196 + // 校验数据不存在了
  197 + assertNull(configMapper.selectById(id));
  198 + // 校验调用
  199 + verify(configProducer, times(1)).sendConfigRefreshMessage();
  200 + }
  201 +
  202 + @Test
  203 + public void testDeleteConfig_canNotDeleteSystemType() {
  204 + // mock 数据
  205 + InfConfigDO dbConfig = randomInfConfigDO(o -> {
  206 + o.setType(InfConfigTypeEnum.SYSTEM.getType()); // SYSTEM 不允许删除
  207 + });
  208 + configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
  209 + // 准备参数
  210 + Long id = dbConfig.getId();
  211 +
  212 + // 调用, 并断言异常
  213 + assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
102 214 }
103 215  
104 216 // ========== 随机对象 ==========
105 217  
106 218 @SafeVarargs
  219 + @SuppressWarnings("unchecked")
107 220 private static InfConfigDO randomInfConfigDO(Consumer<InfConfigDO>... consumers) {
108   - InfConfigDO config = randomPojo(InfConfigDO.class, consumers);
109   - config.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围
110   - return config;
  221 + Consumer<InfConfigDO> consumer = (o) -> {
  222 + o.setType(randomEle(InfConfigTypeEnum.values()).getType()); // 保证 key 的范围
  223 + };
  224 + return randomPojo(InfConfigDO.class, ArrayUtil.append(new Consumer[]{consumer}, consumers));
111 225 }
112 226  
113 227 }
... ...
src/test/java/cn/iocoder/dashboard/util/AssertUtils.java
... ... @@ -4,11 +4,15 @@ import cn.hutool.core.util.ArrayUtil;
4 4 import cn.hutool.core.util.ReflectUtil;
5 5 import cn.iocoder.dashboard.common.exception.ErrorCode;
6 6 import cn.iocoder.dashboard.common.exception.ServiceException;
  7 +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
7 8 import org.junit.jupiter.api.Assertions;
  9 +import org.junit.jupiter.api.function.Executable;
8 10  
9 11 import java.lang.reflect.Field;
10 12 import java.util.Arrays;
11 13  
  14 +import static org.junit.jupiter.api.Assertions.assertThrows;
  15 +
12 16 /**
13 17 * 单元测试,assert 断言工具类
14 18 *
... ... @@ -47,14 +51,19 @@ public class AssertUtils {
47 51 }
48 52  
49 53 /**
50   - * 比对抛出的 ServiceException 是否匹配
  54 + * 执行方法,校验抛出的 Service 是否符合条件
51 55 *
  56 + * @param executable 业务异常
52 57 * @param errorCode 错误码对象
53   - * @param serviceException 业务异常
  58 + * @param messageParams 消息参数
54 59 */
55   - public static void assertExceptionEquals(ErrorCode errorCode, ServiceException serviceException) {
  60 + public static void assertServiceException(Executable executable, ErrorCode errorCode, Object... messageParams) {
  61 + // 调用方法
  62 + ServiceException serviceException = assertThrows(ServiceException.class, executable);
  63 + // 校验错误码
56 64 Assertions.assertEquals(errorCode.getCode(), serviceException.getCode(), "错误码不匹配");
57   - Assertions.assertEquals(errorCode.getMessage(), serviceException.getMessage(), "错误提示不匹配");
  65 + String message = ServiceExceptionUtil.doFormat(errorCode.getCode(), errorCode.getMessage(), messageParams);
  66 + Assertions.assertEquals(message, serviceException.getMessage(), "错误提示不匹配");
58 67 }
59 68  
60 69 }
... ...