houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.service.category;
H 2
3 import com.iailab.framework.common.enums.CommonStatusEnum;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
6 import com.iailab.module.bpm.controller.admin.definition.vo.category.BpmCategoryPageReqVO;
7 import com.iailab.module.bpm.controller.admin.definition.vo.category.BpmCategorySaveReqVO;
8 import com.iailab.module.bpm.dal.dataobject.definition.BpmCategoryDO;
9 import com.iailab.module.bpm.dal.mysql.category.BpmCategoryMapper;
10 import com.iailab.module.bpm.service.definition.BpmCategoryServiceImpl;
11 import org.junit.jupiter.api.Test;
12 import org.springframework.context.annotation.Import;
13
14 import javax.annotation.Resource;
15
16 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
17 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime;
18 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
19 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
20 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
21 import static com.iailab.framework.test.core.util.RandomUtils.*;
22 import static com.iailab.module.bpm.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS;
23 import static org.junit.jupiter.api.Assertions.*;
24
25 /**
26  * {@link BpmCategoryServiceImpl} 的单元测试类
27  *
28  * @author iailab
29  */
30 @Import(BpmCategoryServiceImpl.class)
31 public class BpmCategoryServiceImplTest extends BaseDbUnitTest {
32
33     @Resource
34     private BpmCategoryServiceImpl categoryService;
35
36     @Resource
37     private BpmCategoryMapper categoryMapper;
38
39     @Test
40     public void testCreateCategory_success() {
41         // 准备参数
42         BpmCategorySaveReqVO createReqVO = randomPojo(BpmCategorySaveReqVO.class).setId(null)
43                 .setStatus(randomCommonStatus());
44
45         // 调用
46         Long categoryId = categoryService.createCategory(createReqVO);
47         // 断言
48         assertNotNull(categoryId);
49         // 校验记录的属性是否正确
50         BpmCategoryDO category = categoryMapper.selectById(categoryId);
51         assertPojoEquals(createReqVO, category, "id");
52     }
53
54     @Test
55     public void testUpdateCategory_success() {
56         // mock 数据
57         BpmCategoryDO dbCategory = randomPojo(BpmCategoryDO.class);
58         categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
59         // 准备参数
60         BpmCategorySaveReqVO updateReqVO = randomPojo(BpmCategorySaveReqVO.class, o -> {
61             o.setId(dbCategory.getId()); // 设置更新的 ID
62             o.setStatus(randomCommonStatus());
63         });
64
65         // 调用
66         categoryService.updateCategory(updateReqVO);
67         // 校验是否更新正确
68         BpmCategoryDO category = categoryMapper.selectById(updateReqVO.getId()); // 获取最新的
69         assertPojoEquals(updateReqVO, category);
70     }
71
72     @Test
73     public void testUpdateCategory_notExists() {
74         // 准备参数
75         BpmCategorySaveReqVO updateReqVO = randomPojo(BpmCategorySaveReqVO.class);
76
77         // 调用, 并断言异常
78         assertServiceException(() -> categoryService.updateCategory(updateReqVO), CATEGORY_NOT_EXISTS);
79     }
80
81     @Test
82     public void testDeleteCategory_success() {
83         // mock 数据
84         BpmCategoryDO dbCategory = randomPojo(BpmCategoryDO.class);
85         categoryMapper.insert(dbCategory);// @Sql: 先插入出一条存在的数据
86         // 准备参数
87         Long id = dbCategory.getId();
88
89         // 调用
90         categoryService.deleteCategory(id);
91        // 校验数据不存在了
92        assertNull(categoryMapper.selectById(id));
93     }
94
95     @Test
96     public void testDeleteCategory_notExists() {
97         // 准备参数
98         Long id = randomLongId();
99
100         // 调用, 并断言异常
101         assertServiceException(() -> categoryService.deleteCategory(id), CATEGORY_NOT_EXISTS);
102     }
103
104     @Test
105     public void testGetCategoryPage() {
106        // mock 数据
107        BpmCategoryDO dbCategory = randomPojo(BpmCategoryDO.class, o -> { // 等会查询到
108            o.setName("芋头");
109            o.setCode("xiaodun");
110            o.setStatus(CommonStatusEnum.ENABLE.getStatus());
111            o.setCreateTime(buildTime(2023, 2, 2));
112        });
113        categoryMapper.insert(dbCategory);
114        // 测试 name 不匹配
115        categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setName("小盾")));
116        // 测试 code 不匹配
117        categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCode("tudou")));
118        // 测试 status 不匹配
119        categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
120        // 测试 createTime 不匹配
121        categoryMapper.insert(cloneIgnoreId(dbCategory, o -> o.setCreateTime(buildTime(2024, 2, 2))));
122        // 准备参数
123        BpmCategoryPageReqVO reqVO = new BpmCategoryPageReqVO();
124        reqVO.setName("芋");
125        reqVO.setCode("xiao");
126        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
127        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
128
129        // 调用
130        PageResult<BpmCategoryDO> pageResult = categoryService.getCategoryPage(reqVO);
131        // 断言
132        assertEquals(1, pageResult.getTotal());
133        assertEquals(1, pageResult.getList().size());
134        assertPojoEquals(dbCategory, pageResult.getList().get(0));
135     }
136
137 }