BPM 模型重构 7:任务分配规则的后端的单元测试

This commit is contained in:
YunaiV 2022-01-15 11:51:09 +08:00
parent 902f2ecbad
commit 1781e76e19
5 changed files with 281 additions and 12 deletions

View File

@ -11,9 +11,10 @@ import lombok.Getter;
*/
@Getter
@AllArgsConstructor
public class BpmTaskRuleScriptEnum {
public enum BpmTaskRuleScriptEnum {
;
ONE(1L, ""),
TWO(2L, "");
/**
* 脚本编号

View File

@ -1,14 +1,19 @@
package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.config;
import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.BpmActivityBehaviorFactory;
import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.script.BpmTaskAssignScript;
import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.listener.BpmTackActivitiEventListener;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmTaskAssignRuleService;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmUserGroupService;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
import java.util.List;
import static org.activiti.spring.boot.ProcessEngineAutoConfiguration.BEHAVIOR_FACTORY_MAPPING_CONFIGURER;
@ -47,10 +52,18 @@ public class BpmActivitiConfiguration {
@Bean
public BpmActivityBehaviorFactory bpmActivityBehaviorFactory(BpmTaskAssignRuleService taskRuleService,
SysPermissionService permissionService) {
SysPermissionService permissionService,
SysDeptService deptService,
BpmUserGroupService userGroupService,
SysUserService userService,
List<BpmTaskAssignScript> scripts) {
BpmActivityBehaviorFactory bpmActivityBehaviorFactory = new BpmActivityBehaviorFactory();
bpmActivityBehaviorFactory.setBpmTaskRuleService(taskRuleService);
bpmActivityBehaviorFactory.setPermissionService(permissionService);
bpmActivityBehaviorFactory.setDeptService(deptService);
bpmActivityBehaviorFactory.setUserGroupService(userGroupService);
bpmActivityBehaviorFactory.setUserService(userService);
bpmActivityBehaviorFactory.setScripts(scripts);
return bpmActivityBehaviorFactory;
}

View File

@ -1,7 +1,11 @@
package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior;
import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.script.BpmTaskAssignScript;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmTaskAssignRuleService;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmUserGroupService;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Setter;
@ -10,6 +14,9 @@ import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory;
import javax.annotation.Resource;
import java.util.List;
/**
* 自定义的 ActivityBehaviorFactory 实现类目的如下
* 1. 自定义 {@link #createUserTaskActivityBehavior(UserTask)}实现自定义的流程任务的 assignee 负责人的分配
@ -25,12 +32,24 @@ public class BpmActivityBehaviorFactory extends DefaultActivityBehaviorFactory {
private BpmTaskAssignRuleService bpmTaskRuleService;
@Setter
private SysPermissionService permissionService;
@Setter
private SysDeptService deptService;
@Setter
private BpmUserGroupService userGroupService;
@Setter
private SysUserService userService;
@Setter
private List<BpmTaskAssignScript> scripts;
@Override
public UserTaskActivityBehavior createUserTaskActivityBehavior(UserTask userTask) {
BpmUserTaskActivitiBehavior userTaskActivityBehavior = new BpmUserTaskActivitiBehavior(userTask);
userTaskActivityBehavior.setBpmTaskRuleService(bpmTaskRuleService);
userTaskActivityBehavior.setPermissionService(permissionService);
userTaskActivityBehavior.setDeptService(deptService);
userTaskActivityBehavior.setUserGroupService(userGroupService);
userTaskActivityBehavior.setUserService(userService);
userTaskActivityBehavior.setScripts(scripts);
return userTaskActivityBehavior;
}

View File

@ -14,6 +14,8 @@ import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import com.google.common.annotations.VisibleForTesting;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.UserTask;
@ -24,10 +26,7 @@ import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManager;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import static cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants.TASK_ASSIGN_SCRIPT_NOT_EXISTS;
import static cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants.TASK_CREATE_FAIL_NO_CANDIDATE_USER;
@ -52,7 +51,7 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
private SysDeptService deptService;
@Setter
private BpmUserGroupService userGroupService;
@Resource
@Setter
private SysUserService userService;
/**
* 任务分配脚本
@ -105,15 +104,18 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
return CollUtil.get(candidateUserIds, index);
}
private Set<Long> calculateTaskCandidateUsers(TaskEntity task, BpmTaskAssignRuleDO rule) {
@VisibleForTesting
Set<Long> calculateTaskCandidateUsers(TaskEntity task, BpmTaskAssignRuleDO rule) {
Set<Long> assigneeUserIds = null;
if (Objects.equals(BpmTaskAssignRuleTypeEnum.ROLE.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByRole(task, rule);
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByMember(task, rule);
assigneeUserIds = calculateTaskCandidateUsersByDeptMember(task, rule);
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByDeptLeader(task, rule);
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.USER.getType(), rule.getType())) {
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.POST.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByPost(task, rule);
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.USER.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByUser(task, rule);
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.USER_GROUP.getType(), rule.getType())) {
assigneeUserIds = calculateTaskCandidateUsersByUserGroup(task, rule);
@ -121,7 +123,8 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
assigneeUserIds = calculateTaskCandidateUsersByScript(task, rule);
}
// TODO 芋艿统一过滤掉被禁用的用户
// 移除被禁用的用户
removeDisableUsers(assigneeUserIds);
// 如果候选人为空抛出异常 TODO 芋艿没候选人的策略选择1 - 挂起2 - 直接结束3 - 强制一个兜底人
if (CollUtil.isEmpty(assigneeUserIds)) {
log.error("[calculateTaskCandidateUsers][流程任务({}/{}/{}) 任务规则({}) 找不到候选人]",
@ -135,7 +138,7 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
return permissionService.getUserRoleIdListByRoleIds(rule.getOptions());
}
private Set<Long> calculateTaskCandidateUsersByMember(TaskEntity task, BpmTaskAssignRuleDO rule) {
private Set<Long> calculateTaskCandidateUsersByDeptMember(TaskEntity task, BpmTaskAssignRuleDO rule) {
List<SysUserDO> users = userService.getUsersByPostIds(rule.getOptions());
return convertSet(users, SysUserDO::getId);
}
@ -145,6 +148,11 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
return convertSet(depts, SysDeptDO::getLeaderUserId);
}
private Set<Long> calculateTaskCandidateUsersByPost(TaskEntity task, BpmTaskAssignRuleDO rule) {
List<SysUserDO> users = userService.getUsersByPostIds(rule.getOptions());
return convertSet(users, SysUserDO::getId);
}
private Set<Long> calculateTaskCandidateUsersByUser(TaskEntity task, BpmTaskAssignRuleDO rule) {
return rule.getOptions();
}
@ -172,4 +180,16 @@ public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior {
return userIds;
}
@VisibleForTesting
void removeDisableUsers(Set<Long> assigneeUserIds) {
if (CollUtil.isEmpty(assigneeUserIds)) {
return;
}
Map<Long, SysUserDO> userMap = userService.getUserMap(assigneeUserIds);
assigneeUserIds.removeIf(id -> {
SysUserDO user = userMap.get(id);
return user == null || !CommonStatusEnum.ENABLE.getStatus().equals(user.getStatus());
});
}
}

View File

@ -0,0 +1,216 @@
package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.adminserver.modules.bpm.dal.dataobject.definition.BpmTaskAssignRuleDO;
import cn.iocoder.yudao.adminserver.modules.bpm.dal.dataobject.definition.BpmUserGroupDO;
import cn.iocoder.yudao.adminserver.modules.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
import cn.iocoder.yudao.adminserver.modules.bpm.enums.definition.BpmTaskRuleScriptEnum;
import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.script.BpmTaskAssignScript;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmTaskAssignRuleService;
import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmUserGroupService;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.*;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anySet;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
class BpmUserTaskActivitiBehaviorTest extends BaseMockitoUnitTest {
@InjectMocks
private BpmUserTaskActivitiBehavior behavior;
@Mock
private BpmTaskAssignRuleService bpmTaskRuleService;
@Mock
private SysPermissionService permissionService;
@Mock
private SysDeptService deptService;
@Mock
private BpmUserGroupService userGroupService;
@Mock
private SysUserService userService;
@Test
public void testCalculateTaskCandidateUsers_Role() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.ROLE.getType());
// mock 方法
when(permissionService.getUserRoleIdListByRoleIds(eq(rule.getOptions())))
.thenReturn(asSet(11L, 22L));
mockGetUserMap(asSet(11L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 22L), results);
}
@Test
public void testCalculateTaskCandidateUsers_DeptMember() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType());
// mock 方法
List<SysUserDO> users = CollectionUtils.convertList(asSet(11L, 22L),
id -> new SysUserDO().setId(id));
when(userService.getUsersByPostIds(eq(rule.getOptions()))).thenReturn(users);
mockGetUserMap(asSet(11L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 22L), results);
}
@Test
public void testCalculateTaskCandidateUsers_DeptLeader() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType());
// mock 方法
SysDeptDO dept1 = randomPojo(SysDeptDO.class, o -> o.setLeaderUserId(11L));
SysDeptDO dept2 = randomPojo(SysDeptDO.class, o -> o.setLeaderUserId(22L));
when(deptService.getDepts(eq(rule.getOptions()))).thenReturn(Arrays.asList(dept1, dept2));
mockGetUserMap(asSet(11L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 22L), results);
}
@Test
public void testCalculateTaskCandidateUsers_Post() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.POST.getType());
// mock 方法
List<SysUserDO> users = CollectionUtils.convertList(asSet(11L, 22L),
id -> new SysUserDO().setId(id));
when(userService.getUsersByPostIds(eq(rule.getOptions()))).thenReturn(users);
mockGetUserMap(asSet(11L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 22L), results);
}
@Test
public void testCalculateTaskCandidateUsers_User() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.USER.getType());
// mock 方法
mockGetUserMap(asSet(1L, 2L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(1L, 2L), results);
}
@Test
public void testCalculateTaskCandidateUsers_UserGroup() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.USER_GROUP.getType());
// mock 方法
BpmUserGroupDO userGroup1 = randomPojo(BpmUserGroupDO.class, o -> o.setMemberUserIds(asSet(11L, 12L)));
BpmUserGroupDO userGroup2 = randomPojo(BpmUserGroupDO.class, o -> o.setMemberUserIds(asSet(21L, 22L)));
when(userGroupService.getUserGroupList(eq(rule.getOptions()))).thenReturn(Arrays.asList(userGroup1, userGroup2));
mockGetUserMap(asSet(11L, 12L, 21L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 12L, 21L, 22L), results);
}
@Test
public void testCalculateTaskCandidateUsers_Script() {
// 准备参数
BpmTaskAssignRuleDO rule = new BpmTaskAssignRuleDO().setOptions(asSet(1L, 2L))
.setType(BpmTaskAssignRuleTypeEnum.SCRIPT.getType());
// mock 方法
BpmTaskAssignScript script1 = new BpmTaskAssignScript() {
@Override
public List<Long> calculateTaskCandidateUsers(TaskEntity task) {
return singletonList(11L);
}
@Override
public BpmTaskRuleScriptEnum getEnum() {
return BpmTaskRuleScriptEnum.ONE;
}
};
BpmTaskAssignScript script2 = new BpmTaskAssignScript() {
@Override
public List<Long> calculateTaskCandidateUsers(TaskEntity task) {
return singletonList(22L);
}
@Override
public BpmTaskRuleScriptEnum getEnum() {
return BpmTaskRuleScriptEnum.TWO;
}
};
behavior.setScripts(Arrays.asList(script1, script2));
mockGetUserMap(asSet(11L, 22L));
// 调用
Set<Long> results = behavior.calculateTaskCandidateUsers(null, rule);
// 断言
assertEquals(asSet(11L, 22L), results);
}
@Test
public void testRemoveDisableUsers() {
// 准备参数
Set<Long> assigneeUserIds = asSet(1L, 2L, 3L);
// mock 方法
SysUserDO user1 = randomPojo(SysUserDO.class, o -> o.setId(1L));
SysUserDO user2 = randomPojo(SysUserDO.class, o -> o.setId(2L)
.setStatus(CommonStatusEnum.DISABLE.getStatus()));
Map<Long, SysUserDO> userMap = MapUtil.builder(user1.getId(), user1)
.put(user2.getId(), user2).build();
when(userService.getUserMap(eq(assigneeUserIds))).thenReturn(userMap);
// 调用
behavior.removeDisableUsers(assigneeUserIds);
// 断言
assertEquals(asSet(1L), assigneeUserIds);
}
private void mockGetUserMap(Set<Long> assigneeUserIds) {
Map<Long, SysUserDO> userMap = CollectionUtils.convertMap(assigneeUserIds, id -> id,
id -> new SysUserDO().setId(id).setStatus(CommonStatusEnum.ENABLE.getStatus()));
when(userService.getUserMap(eq(assigneeUserIds))).thenReturn(userMap);
}
}