提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.bpm.service.definition; |
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.framework.test.core.util.AssertUtils; |
|
7 |
import com.iailab.framework.test.core.util.RandomUtils; |
|
8 |
import com.iailab.module.bpm.controller.admin.definition.vo.group.BpmUserGroupSaveReqVO; |
|
9 |
import com.iailab.module.bpm.controller.admin.definition.vo.group.BpmUserGroupPageReqVO; |
|
10 |
import com.iailab.module.bpm.dal.dataobject.definition.BpmUserGroupDO; |
|
11 |
import com.iailab.module.bpm.dal.mysql.definition.BpmUserGroupMapper; |
|
12 |
import org.junit.jupiter.api.Assertions; |
|
13 |
import org.junit.jupiter.api.Test; |
|
14 |
import org.springframework.context.annotation.Import; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
import java.time.LocalDateTime; |
|
18 |
|
|
19 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
20 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
21 |
import static com.iailab.module.bpm.enums.ErrorCodeConstants.USER_GROUP_NOT_EXISTS; |
|
22 |
|
|
23 |
/** |
|
24 |
* {@link BpmUserGroupServiceImpl} 的单元测试类 |
|
25 |
* |
|
26 |
* @author iailab |
|
27 |
*/ |
|
28 |
@Import(BpmUserGroupServiceImpl.class) |
|
29 |
public class BpmUserGroupServiceTest extends BaseDbUnitTest { |
|
30 |
|
|
31 |
@Resource |
|
32 |
private BpmUserGroupServiceImpl userGroupService; |
|
33 |
|
|
34 |
@Resource |
|
35 |
private BpmUserGroupMapper userGroupMapper; |
|
36 |
|
|
37 |
@Test |
|
38 |
public void testCreateUserGroup_success() { |
|
39 |
// 准备参数 |
|
40 |
BpmUserGroupSaveReqVO reqVO = RandomUtils.randomPojo(BpmUserGroupSaveReqVO.class); |
|
41 |
|
|
42 |
// 调用 |
|
43 |
Long userGroupId = userGroupService.createUserGroup(reqVO); |
|
44 |
// 断言 |
|
45 |
Assertions.assertNotNull(userGroupId); |
|
46 |
// 校验记录的属性是否正确 |
|
47 |
BpmUserGroupDO userGroup = userGroupMapper.selectById(userGroupId); |
|
48 |
AssertUtils.assertPojoEquals(reqVO, userGroup); |
|
49 |
} |
|
50 |
|
|
51 |
@Test |
|
52 |
public void testUpdateUserGroup_success() { |
|
53 |
// mock 数据 |
|
54 |
BpmUserGroupDO dbUserGroup = RandomUtils.randomPojo(BpmUserGroupDO.class); |
|
55 |
userGroupMapper.insert(dbUserGroup);// @Sql: 先插入出一条存在的数据 |
|
56 |
// 准备参数 |
|
57 |
BpmUserGroupSaveReqVO reqVO = RandomUtils.randomPojo(BpmUserGroupSaveReqVO.class, o -> { |
|
58 |
o.setId(dbUserGroup.getId()); // 设置更新的 ID |
|
59 |
}); |
|
60 |
|
|
61 |
// 调用 |
|
62 |
userGroupService.updateUserGroup(reqVO); |
|
63 |
// 校验是否更新正确 |
|
64 |
BpmUserGroupDO userGroup = userGroupMapper.selectById(reqVO.getId()); // 获取最新的 |
|
65 |
AssertUtils.assertPojoEquals(reqVO, userGroup); |
|
66 |
} |
|
67 |
|
|
68 |
@Test |
|
69 |
public void testUpdateUserGroup_notExists() { |
|
70 |
// 准备参数 |
|
71 |
BpmUserGroupSaveReqVO reqVO = RandomUtils.randomPojo(BpmUserGroupSaveReqVO.class); |
|
72 |
|
|
73 |
// 调用, 并断言异常 |
|
74 |
AssertUtils.assertServiceException(() -> userGroupService.updateUserGroup(reqVO), USER_GROUP_NOT_EXISTS); |
|
75 |
} |
|
76 |
|
|
77 |
@Test |
|
78 |
public void testDeleteUserGroup_success() { |
|
79 |
// mock 数据 |
|
80 |
BpmUserGroupDO dbUserGroup = RandomUtils.randomPojo(BpmUserGroupDO.class); |
|
81 |
userGroupMapper.insert(dbUserGroup);// @Sql: 先插入出一条存在的数据 |
|
82 |
// 准备参数 |
|
83 |
Long id = dbUserGroup.getId(); |
|
84 |
|
|
85 |
// 调用 |
|
86 |
userGroupService.deleteUserGroup(id); |
|
87 |
// 校验数据不存在了 |
|
88 |
Assertions.assertNull(userGroupMapper.selectById(id)); |
|
89 |
} |
|
90 |
|
|
91 |
@Test |
|
92 |
public void testDeleteUserGroup_notExists() { |
|
93 |
// 准备参数 |
|
94 |
Long id = RandomUtils.randomLongId(); |
|
95 |
|
|
96 |
// 调用, 并断言异常 |
|
97 |
AssertUtils.assertServiceException(() -> userGroupService.deleteUserGroup(id), USER_GROUP_NOT_EXISTS); |
|
98 |
} |
|
99 |
|
|
100 |
@Test |
|
101 |
public void testGetUserGroupPage() { |
|
102 |
// mock 数据 |
|
103 |
BpmUserGroupDO dbUserGroup = RandomUtils.randomPojo(BpmUserGroupDO.class, o -> { // 等会查询到 |
|
104 |
o.setName("iailab"); |
|
105 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
106 |
o.setCreateTime(buildTime(2021, 11, 11)); |
|
107 |
}); |
|
108 |
userGroupMapper.insert(dbUserGroup); |
|
109 |
// 测试 name 不匹配 |
|
110 |
userGroupMapper.insert(cloneIgnoreId(dbUserGroup, o -> o.setName("平台"))); |
|
111 |
// 测试 status 不匹配 |
|
112 |
userGroupMapper.insert(cloneIgnoreId(dbUserGroup, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
113 |
// 测试 createTime 不匹配 |
|
114 |
userGroupMapper.insert(cloneIgnoreId(dbUserGroup, o -> o.setCreateTime(buildTime(2021, 12, 12)))); |
|
115 |
// 准备参数 |
|
116 |
BpmUserGroupPageReqVO reqVO = new BpmUserGroupPageReqVO(); |
|
117 |
reqVO.setName("源码"); |
|
118 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
119 |
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 11, 10),buildTime(2021, 11, 12)})); |
|
120 |
|
|
121 |
// 调用 |
|
122 |
PageResult<BpmUserGroupDO> pageResult = userGroupService.getUserGroupPage(reqVO); |
|
123 |
// 断言 |
|
124 |
Assertions.assertEquals(1, pageResult.getTotal()); |
|
125 |
Assertions.assertEquals(1, pageResult.getList().size()); |
|
126 |
AssertUtils.assertPojoEquals(dbUserGroup, pageResult.getList().get(0)); |
|
127 |
} |
|
128 |
|
|
129 |
} |