潘志宝
2024-12-23 1461727e8df4dccf8c5593f9d649d89395681171
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.framework.flowable.core.candidate.expression;
H 2
3 import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
4 import com.iailab.module.bpm.service.task.BpmProcessInstanceService;
5 import com.iailab.module.system.api.dept.DeptApi;
6 import com.iailab.module.system.api.dept.dto.DeptRespDTO;
7 import com.iailab.module.system.api.user.AdminUserApi;
8 import com.iailab.module.system.api.user.dto.AdminUserRespDTO;
9 import org.flowable.engine.delegate.DelegateExecution;
10 import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
11 import org.junit.jupiter.api.Test;
12 import org.mockito.InjectMocks;
13 import org.mockito.Mock;
14
15 import java.util.Set;
16
17 import static com.iailab.framework.common.pojo.CommonResult.success;
18 import static com.iailab.framework.common.util.collection.SetUtils.asSet;
19 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
20 import static com.iailab.framework.test.core.util.RandomUtils.randomString;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.when;
24
25 public class BpmTaskAssignLeaderExpressionTest extends BaseMockitoUnitTest {
26
27     @InjectMocks
28     private BpmTaskAssignLeaderExpression expression;
29
30     @Mock
31     private AdminUserApi adminUserApi;
32     @Mock
33     private DeptApi deptApi;
34
35     @Mock
36     private BpmProcessInstanceService processInstanceService;
37
38     @Test
39     public void testCalculateUsers_noDept() {
40         // 准备参数
41         DelegateExecution execution = mockDelegateExecution(1L);
42         // mock 方法(startUser)
43         AdminUserRespDTO startUser = randomPojo(AdminUserRespDTO.class, o -> o.setDeptId(10L));
44         when(adminUserApi.getUser(eq(1L))).thenReturn(success(startUser));
45         // mock 方法(getStartUserDept)没有部门
46         when(deptApi.getDept(eq(10L))).thenReturn(success(null));
47
48         // 调用
49         Set<Long> result = expression.calculateUsers(execution, 1);
50         // 断言
51         assertEquals(0, result.size());
52     }
53
54     @Test
55     public void testCalculateUsers_noParentDept() {
56         // 准备参数
57         DelegateExecution execution = mockDelegateExecution(1L);
58         // mock 方法(startUser)
59         AdminUserRespDTO startUser = randomPojo(AdminUserRespDTO.class, o -> o.setDeptId(10L));
60         when(adminUserApi.getUser(eq(1L))).thenReturn(success(startUser));
61         DeptRespDTO startUserDept = randomPojo(DeptRespDTO.class, o -> o.setId(10L).setParentId(100L)
62                 .setLeaderUserId(20L));
63         // mock 方法(getDept)
64         when(deptApi.getDept(eq(10L))).thenReturn(success(startUserDept));
65         when(deptApi.getDept(eq(100L))).thenReturn(success(null));
66
67         // 调用
68         Set<Long> result = expression.calculateUsers(execution, 2);
69         // 断言
70         assertEquals(asSet(20L), result);
71     }
72
73     @Test
74     public void testCalculateUsers_existParentDept() {
75         // 准备参数
76         DelegateExecution execution = mockDelegateExecution(1L);
77         // mock 方法(startUser)
78         AdminUserRespDTO startUser = randomPojo(AdminUserRespDTO.class, o -> o.setDeptId(10L));
79         when(adminUserApi.getUser(eq(1L))).thenReturn(success(startUser));
80         DeptRespDTO startUserDept = randomPojo(DeptRespDTO.class, o -> o.setId(10L).setParentId(100L)
81                 .setLeaderUserId(20L));
82         when(deptApi.getDept(eq(10L))).thenReturn(success(startUserDept));
83         // mock 方法(父 dept)
84         DeptRespDTO parentDept = randomPojo(DeptRespDTO.class, o -> o.setId(100L).setParentId(1000L)
85                 .setLeaderUserId(200L));
86         when(deptApi.getDept(eq(100L))).thenReturn(success(parentDept));
87
88         // 调用
89         Set<Long> result = expression.calculateUsers(execution, 2);
90         // 断言
91         assertEquals(asSet(200L), result);
92     }
93
94     @SuppressWarnings("SameParameterValue")
95     private DelegateExecution mockDelegateExecution(Long startUserId) {
96         ExecutionEntityImpl execution = new ExecutionEntityImpl();
97         execution.setProcessInstanceId(randomString());
98         // mock 返回 startUserId
99         ExecutionEntityImpl processInstance = new ExecutionEntityImpl();
100         processInstance.setStartUserId(String.valueOf(startUserId));
101         when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId())))
102                 .thenReturn(processInstance);
103         return execution;
104     }
105
106 }