|
| 1 | +package com.codingapi.flow.service; |
| 2 | + |
| 3 | +import com.codingapi.flow.action.IFlowAction; |
| 4 | +import com.codingapi.flow.builder.FormFieldPermissionsBuilder; |
| 5 | +import com.codingapi.flow.builder.NodeStrategyBuilder; |
| 6 | +import com.codingapi.flow.context.GatewayContext; |
| 7 | +import com.codingapi.flow.factory.MyFlowServiceFactory; |
| 8 | +import com.codingapi.flow.form.DataType; |
| 9 | +import com.codingapi.flow.form.FlowForm; |
| 10 | +import com.codingapi.flow.form.FlowFormBuilder; |
| 11 | +import com.codingapi.flow.form.permission.PermissionType; |
| 12 | +import com.codingapi.flow.node.nodes.ApprovalNode; |
| 13 | +import com.codingapi.flow.node.nodes.EndNode; |
| 14 | +import com.codingapi.flow.node.nodes.ManualBranchNode; |
| 15 | +import com.codingapi.flow.node.nodes.ManualNode; |
| 16 | +import com.codingapi.flow.node.nodes.StartNode; |
| 17 | +import com.codingapi.flow.pojo.body.FlowAdviceBody; |
| 18 | +import com.codingapi.flow.pojo.request.FlowActionRequest; |
| 19 | +import com.codingapi.flow.pojo.request.FlowCreateRequest; |
| 20 | +import com.codingapi.flow.pojo.response.ActionResponse; |
| 21 | +import com.codingapi.flow.record.FlowRecord; |
| 22 | +import com.codingapi.flow.strategy.node.FormFieldPermissionStrategy; |
| 23 | +import com.codingapi.flow.strategy.node.OperatorLoadStrategy; |
| 24 | +import com.codingapi.flow.user.User; |
| 25 | +import com.codingapi.flow.workflow.Workflow; |
| 26 | +import com.codingapi.flow.workflow.WorkflowBuilder; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 35 | + |
| 36 | +/** |
| 37 | + * issue #259 复现测试:人工节点分支下存在审批人设定节点 |
| 38 | + * |
| 39 | + * <p>流程设计:A(开始) -> B(人工节点) -> C1(审批人设定) / C2(审批人设定) -> D(结束)。 |
| 40 | + * |
| 41 | + * <p>问题:发起人提交开始节点后首先弹出人工节点的分支选择框;选择 C1 所在分支后, |
| 42 | + * 引擎未继续弹出 C1 节点的审批人设定框,流程直接流转到 C1,导致 C1 无审批人而异常。 |
| 43 | + * |
| 44 | + * <p>期望:选择分支后紧跟着提示为 C1 设定审批人;只有选中的分支才会被提示, |
| 45 | + * 未选中分支(C2)不应出现在提示中。 |
| 46 | + */ |
| 47 | +class FlowIssue259ManualBranchApproverSelectTest { |
| 48 | + |
| 49 | + private final MyFlowServiceFactory factory = new MyFlowServiceFactory(); |
| 50 | + |
| 51 | + private ApprovalNode c1Node; |
| 52 | + private ApprovalNode c2Node; |
| 53 | + private ManualBranchNode c1BranchNode; |
| 54 | + |
| 55 | + /** |
| 56 | + * 选择 C1 分支后应提示为 C1 设定审批人,指定后流程流转到 C1。 |
| 57 | + * |
| 58 | + * <p>前置条件:流程为 A(开始) -> B(人工) -> C1/C2(审批人设定) -> D(结束)。 |
| 59 | + * <p>执行步骤:发起流程 -> 提交开始节点 -> 选择 C1 分支 -> 为 C1 指定审批人。 |
| 60 | + * <p>期望:选择分支的响应为 OPERATOR_SELECT 且只包含 C1;指定审批人后 C1 收到待办。 |
| 61 | + */ |
| 62 | + @Test |
| 63 | + void shouldPromptApproverSelectAfterSelectingManualBranch() { |
| 64 | + // given |
| 65 | + User user = new User(1, "user"); |
| 66 | + User c1User = new User(2, "c1User"); |
| 67 | + User c2User = new User(3, "c2User"); |
| 68 | + factory.userGateway.save(user); |
| 69 | + factory.userGateway.save(c1User); |
| 70 | + factory.userGateway.save(c2User); |
| 71 | + GatewayContext.getInstance().setFlowOperatorGateway(factory.userGateway); |
| 72 | + |
| 73 | + Workflow workflow = buildWorkflow(user); |
| 74 | + factory.workflowService.saveWorkflow(workflow); |
| 75 | + |
| 76 | + Map<String, Object> data = Map.of("name", "lorne", "reason", "leave"); |
| 77 | + |
| 78 | + List<IFlowAction> startActions = workflow.getStartNode().actionManager().getActions(); |
| 79 | + createFlow(workflow, user, data, startActions); |
| 80 | + FlowRecord startRecord = todoOf(user, "发起人应收到开始节点待办"); |
| 81 | + |
| 82 | + // when: 第一次提交开始节点,应提示选择人工节点分支 |
| 83 | + ActionResponse manualResponse = submitStart(startActions, startRecord, user, data, null, null); |
| 84 | + |
| 85 | + // then |
| 86 | + assertNotNull(manualResponse, "开始节点提交后应提示选择人工节点分支"); |
| 87 | + assertEquals(ActionResponse.ResponseType.MANUAL_NODE_SELECT, manualResponse.getResponseType()); |
| 88 | + assertEquals(2, manualResponse.getOptions().size()); |
| 89 | + |
| 90 | + // when: 选择 C1 所在分支后再次提交 |
| 91 | + ActionResponse operatorResponse = submitStart( |
| 92 | + startActions, startRecord, user, data, c1BranchNode.getId(), null); |
| 93 | + |
| 94 | + // then: 应紧跟着提示为 C1 设定审批人,且只提示选中分支内的 C1 |
| 95 | + assertNotNull(operatorResponse, "选择 C1 分支后应提示为 C1 节点设定审批人"); |
| 96 | + assertEquals(ActionResponse.ResponseType.OPERATOR_SELECT, operatorResponse.getResponseType()); |
| 97 | + assertEquals(1, operatorResponse.getOptions().size()); |
| 98 | + assertEquals(c1Node.getId(), operatorResponse.getOptions().get(0).getId()); |
| 99 | + |
| 100 | + // when: 为 C1 指定审批人后再次提交 |
| 101 | + ActionResponse finishResponse = submitStart( |
| 102 | + startActions, startRecord, user, data, c1BranchNode.getId(), |
| 103 | + Map.of(c1Node.getId(), List.of(c1User.getUserId()))); |
| 104 | + |
| 105 | + // then: 流程流转到 C1,未选中分支的 C2 不产生待办 |
| 106 | + assertNull(finishResponse); |
| 107 | + assertEquals(1, factory.flowRecordRepository.findTodoByOperator(c1User.getUserId()).size(), |
| 108 | + "为 C1 指定审批人后,流程应流转到 C1 节点"); |
| 109 | + assertEquals(0, factory.flowRecordRepository.findTodoByOperator(c2User.getUserId()).size(), |
| 110 | + "未选中分支的 C2 不应产生待办"); |
| 111 | + } |
| 112 | + |
| 113 | + // ---------- 辅助方法 ---------- |
| 114 | + |
| 115 | + private Workflow buildWorkflow(User user) { |
| 116 | + FlowForm form = FlowFormBuilder.builder() |
| 117 | + .name("请假流程") |
| 118 | + .code("leave") |
| 119 | + .addField("请假人", "name", DataType.STRING) |
| 120 | + .addField("请假事由", "reason", DataType.STRING) |
| 121 | + .build(); |
| 122 | + |
| 123 | + StartNode startNode = StartNode.builder() |
| 124 | + .strategies(NodeStrategyBuilder.builder().addStrategy(writePermission()).build()) |
| 125 | + .build(); |
| 126 | + |
| 127 | + // C1、C2 均为审批人设定(APPROVER_SELECT)节点 |
| 128 | + c1Node = approverSelectNode("C1审批人设定"); |
| 129 | + c2Node = approverSelectNode("C2审批人设定"); |
| 130 | + |
| 131 | + c1BranchNode = ManualBranchNode.builder() |
| 132 | + .name("C1分支") |
| 133 | + .blocks(c1Node) |
| 134 | + .order(1) |
| 135 | + .build(); |
| 136 | + |
| 137 | + ManualBranchNode c2BranchNode = ManualBranchNode.builder() |
| 138 | + .name("C2分支") |
| 139 | + .blocks(c2Node) |
| 140 | + .order(2) |
| 141 | + .build(); |
| 142 | + |
| 143 | + ManualNode manualNode = ManualNode.builder() |
| 144 | + .name("B人工节点") |
| 145 | + .blocks(c1BranchNode, c2BranchNode) |
| 146 | + .build(); |
| 147 | + |
| 148 | + EndNode endNode = EndNode.builder().build(); |
| 149 | + |
| 150 | + return WorkflowBuilder.builder() |
| 151 | + .title("请假流程") |
| 152 | + .code("leave") |
| 153 | + .createdOperator(user) |
| 154 | + .form(form) |
| 155 | + .addNode(startNode) |
| 156 | + .addNode(manualNode) |
| 157 | + .addNode(endNode) |
| 158 | + .build(); |
| 159 | + } |
| 160 | + |
| 161 | + private void createFlow(Workflow workflow, User user, Map<String, Object> data, List<IFlowAction> startActions) { |
| 162 | + FlowCreateRequest createRequest = new FlowCreateRequest(); |
| 163 | + createRequest.setWorkCode(workflow.getCode()); |
| 164 | + createRequest.setFormData(data); |
| 165 | + createRequest.setActionId(startActions.get(0).id()); |
| 166 | + createRequest.setOperatorId(user.getUserId()); |
| 167 | + factory.flowService.create(createRequest); |
| 168 | + } |
| 169 | + |
| 170 | + private ActionResponse submitStart(List<IFlowAction> startActions, |
| 171 | + FlowRecord record, |
| 172 | + User user, |
| 173 | + Map<String, Object> data, |
| 174 | + String manualNodeId, |
| 175 | + Map<String, List<Long>> operatorSelectMap) { |
| 176 | + FlowActionRequest request = new FlowActionRequest(); |
| 177 | + request.setFormData(data); |
| 178 | + request.setRecordId(record.getId()); |
| 179 | + FlowAdviceBody advice = new FlowAdviceBody(startActions.get(0).id(), "提交", user.getUserId()); |
| 180 | + advice.setManualNodeId(manualNodeId); |
| 181 | + advice.setOperatorSelectMap(operatorSelectMap); |
| 182 | + request.setAdvice(advice); |
| 183 | + return factory.flowService.action(request); |
| 184 | + } |
| 185 | + |
| 186 | + private FlowRecord todoOf(User target, String message) { |
| 187 | + List<FlowRecord> records = factory.flowRecordRepository.findTodoByOperator(target.getUserId()); |
| 188 | + assertEquals(1, records.size(), message); |
| 189 | + return records.get(0); |
| 190 | + } |
| 191 | + |
| 192 | + private ApprovalNode approverSelectNode(String name) { |
| 193 | + return ApprovalNode.builder() |
| 194 | + .name(name) |
| 195 | + .strategies(NodeStrategyBuilder.builder() |
| 196 | + .addStrategy(readPermission()) |
| 197 | + .addStrategy(OperatorLoadStrategy.approverSelectStrategy()) |
| 198 | + .build()) |
| 199 | + .build(); |
| 200 | + } |
| 201 | + |
| 202 | + private FormFieldPermissionStrategy writePermission() { |
| 203 | + return new FormFieldPermissionStrategy(FormFieldPermissionsBuilder.builder() |
| 204 | + .addPermission("leave", "name", PermissionType.WRITE) |
| 205 | + .addPermission("leave", "reason", PermissionType.WRITE) |
| 206 | + .build()); |
| 207 | + } |
| 208 | + |
| 209 | + private FormFieldPermissionStrategy readPermission() { |
| 210 | + return new FormFieldPermissionStrategy(FormFieldPermissionsBuilder.builder() |
| 211 | + .addPermission("leave", "name", PermissionType.READ) |
| 212 | + .addPermission("leave", "reason", PermissionType.READ) |
| 213 | + .build()); |
| 214 | + } |
| 215 | +} |
0 commit comments