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