提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.bpm.framework.flowable.core.candidate; |
H |
2 |
|
|
3 |
import cn.hutool.core.map.MapUtil; |
|
4 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
5 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
6 |
import com.iailab.module.bpm.framework.flowable.core.candidate.strategy.BpmTaskCandidateUserStrategy; |
|
7 |
import com.iailab.module.bpm.framework.flowable.core.enums.BpmTaskCandidateStrategyEnum; |
|
8 |
import com.iailab.module.bpm.framework.flowable.core.enums.BpmnModelConstants; |
|
9 |
import com.iailab.module.system.api.user.AdminUserApi; |
|
10 |
import com.iailab.module.system.api.user.dto.AdminUserRespDTO; |
|
11 |
import org.flowable.bpmn.model.UserTask; |
|
12 |
import org.flowable.engine.delegate.DelegateExecution; |
|
13 |
import org.junit.jupiter.api.Test; |
|
14 |
import org.mockito.InjectMocks; |
|
15 |
import org.mockito.Mock; |
|
16 |
import org.mockito.Spy; |
|
17 |
|
|
18 |
import java.util.Collections; |
|
19 |
import java.util.List; |
|
20 |
import java.util.Map; |
|
21 |
import java.util.Set; |
|
22 |
|
|
23 |
import static com.iailab.framework.common.util.collection.SetUtils.asSet; |
|
24 |
import static com.iailab.framework.test.core.util.RandomUtils.randomPojo; |
|
25 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
26 |
import static org.mockito.ArgumentMatchers.eq; |
|
27 |
import static org.mockito.Mockito.mock; |
|
28 |
import static org.mockito.Mockito.when; |
|
29 |
|
|
30 |
/** |
|
31 |
* {@link BpmTaskCandidateInvoker} 的单元测试 |
|
32 |
* |
|
33 |
* @author iailab |
|
34 |
*/ |
|
35 |
public class BpmTaskCandidateInvokerTest extends BaseMockitoUnitTest { |
|
36 |
|
|
37 |
@InjectMocks |
|
38 |
private BpmTaskCandidateInvoker taskCandidateInvoker; |
|
39 |
|
|
40 |
@Mock |
|
41 |
private AdminUserApi adminUserApi; |
|
42 |
@Spy |
|
43 |
private BpmTaskCandidateStrategy strategy = new BpmTaskCandidateUserStrategy(); |
|
44 |
@Spy |
|
45 |
private List<BpmTaskCandidateStrategy> strategyList = Collections.singletonList(strategy); |
|
46 |
|
|
47 |
@Test |
|
48 |
public void testCalculateUsers() { |
|
49 |
// 准备参数 |
|
50 |
String param = "1,2"; |
|
51 |
DelegateExecution execution = mock(DelegateExecution.class); |
|
52 |
// mock 方法(DelegateExecution) |
|
53 |
UserTask userTask = mock(UserTask.class); |
|
54 |
when(execution.getCurrentFlowElement()).thenReturn(userTask); |
|
55 |
when(userTask.getAttributeValue(eq(BpmnModelConstants.NAMESPACE), eq(BpmnModelConstants.USER_TASK_CANDIDATE_STRATEGY))) |
|
56 |
.thenReturn(BpmTaskCandidateStrategyEnum.USER.getStrategy().toString()); |
|
57 |
when(userTask.getAttributeValue(eq(BpmnModelConstants.NAMESPACE), eq(BpmnModelConstants.USER_TASK_CANDIDATE_PARAM))) |
|
58 |
.thenReturn(param); |
|
59 |
// mock 方法(adminUserApi) |
|
60 |
AdminUserRespDTO user1 = randomPojo(AdminUserRespDTO.class, o -> o.setId(1L) |
|
61 |
.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|
62 |
AdminUserRespDTO user2 = randomPojo(AdminUserRespDTO.class, o -> o.setId(2L) |
|
63 |
.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|
64 |
Map<Long, AdminUserRespDTO> userMap = MapUtil.builder(user1.getId(), user1) |
|
65 |
.put(user2.getId(), user2).build(); |
|
66 |
when(adminUserApi.getUserMap(eq(asSet(1L, 2L)))).thenReturn(userMap); |
|
67 |
|
|
68 |
// 调用 |
|
69 |
Set<Long> results = taskCandidateInvoker.calculateUsers(execution); |
|
70 |
// 断言 |
|
71 |
assertEquals(asSet(1L, 2L), results); |
|
72 |
} |
|
73 |
|
|
74 |
@Test |
|
75 |
public void testRemoveDisableUsers() { |
|
76 |
// 准备参数. 1L 可以找到;2L 是禁用的;3L 找不到 |
|
77 |
Set<Long> assigneeUserIds = asSet(1L, 2L, 3L); |
|
78 |
// mock 方法 |
|
79 |
AdminUserRespDTO user1 = randomPojo(AdminUserRespDTO.class, o -> o.setId(1L) |
|
80 |
.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|
81 |
AdminUserRespDTO user2 = randomPojo(AdminUserRespDTO.class, o -> o.setId(2L) |
|
82 |
.setStatus(CommonStatusEnum.DISABLE.getStatus())); |
|
83 |
Map<Long, AdminUserRespDTO> userMap = MapUtil.builder(user1.getId(), user1) |
|
84 |
.put(user2.getId(), user2).build(); |
|
85 |
when(adminUserApi.getUserMap(eq(assigneeUserIds))).thenReturn(userMap); |
|
86 |
|
|
87 |
// 调用 |
|
88 |
taskCandidateInvoker.removeDisableUsers(assigneeUserIds); |
|
89 |
// 断言 |
|
90 |
assertEquals(asSet(1L), assigneeUserIds); |
|
91 |
} |
|
92 |
|
|
93 |
} |