|
| 1 | +package com.codingapi.springboot.flow.test; |
| 2 | + |
| 3 | +import com.codingapi.springboot.flow.build.FlowWorkBuilder; |
| 4 | +import com.codingapi.springboot.flow.domain.FlowWork; |
| 5 | +import com.codingapi.springboot.flow.domain.Opinion; |
| 6 | +import com.codingapi.springboot.flow.em.ApprovalType; |
| 7 | +import com.codingapi.springboot.flow.flow.Leave; |
| 8 | +import com.codingapi.springboot.flow.matcher.OperatorMatcher; |
| 9 | +import com.codingapi.springboot.flow.record.FlowRecord; |
| 10 | +import com.codingapi.springboot.flow.repository.*; |
| 11 | +import com.codingapi.springboot.flow.service.FlowService; |
| 12 | +import com.codingapi.springboot.flow.user.User; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.data.domain.PageRequest; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 20 | + |
| 21 | +public class FlowRemoveTest { |
| 22 | + |
| 23 | + private final UserRepository userRepository = new UserRepository(); |
| 24 | + private final FlowWorkRepository flowWorkRepository = new FlowWorkRepositoryImpl(); |
| 25 | + private final FlowRecordRepositoryImpl flowRecordRepository = new FlowRecordRepositoryImpl(); |
| 26 | + private final FlowBindDataRepositoryImpl flowBindDataRepository = new FlowBindDataRepositoryImpl(); |
| 27 | + private final LeaveRepository leaveRepository = new LeaveRepository(); |
| 28 | + private final FlowBackupRepository flowBackupRepository = new FlowBackupRepositoryImpl(); |
| 29 | + private final FlowProcessRepository flowProcessRepository = new FlowProcessRepositoryImpl(flowBackupRepository, userRepository); |
| 30 | + private final FlowService flowService = new FlowService(flowWorkRepository, flowRecordRepository, flowBindDataRepository, userRepository, flowProcessRepository, flowBackupRepository); |
| 31 | + |
| 32 | + /** |
| 33 | + * 删除流程测试 (在未提交之前删除流程) |
| 34 | + */ |
| 35 | + @Test |
| 36 | + void removeTest1() { |
| 37 | + PageRequest pageRequest = PageRequest.of(0, 1000); |
| 38 | + |
| 39 | + User user = new User("张飞"); |
| 40 | + userRepository.save(user); |
| 41 | + |
| 42 | + User dept = new User("刘备"); |
| 43 | + userRepository.save(dept); |
| 44 | + |
| 45 | + User boss = new User("诸葛亮"); |
| 46 | + userRepository.save(boss); |
| 47 | + |
| 48 | + FlowWork flowWork = FlowWorkBuilder.builder(user) |
| 49 | + .title("请假流程") |
| 50 | + .nodes() |
| 51 | + .node("开始节点", "start", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 52 | + .node("部门领导审批", "dept", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(dept.getUserId())) |
| 53 | + .node("总经理审批", "manager", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(boss.getUserId())) |
| 54 | + .node("结束节点", "over", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 55 | + .relations() |
| 56 | + .relation("部门领导审批", "start", "dept") |
| 57 | + .relation("总经理审批", "dept", "manager") |
| 58 | + .relation("结束节点", "manager", "over") |
| 59 | + .build(); |
| 60 | + |
| 61 | + flowWorkRepository.save(flowWork); |
| 62 | + |
| 63 | + String workCode = flowWork.getCode(); |
| 64 | + |
| 65 | + Leave leave = new Leave("我要出去看看"); |
| 66 | + leaveRepository.save(leave); |
| 67 | + |
| 68 | + // 创建流程 |
| 69 | + flowService.startFlow(workCode, user, leave, "发起流程"); |
| 70 | + |
| 71 | + // 查看我的待办 |
| 72 | + List<FlowRecord> userTodos = flowRecordRepository.findTodoByOperatorId(user.getUserId(), pageRequest).getContent(); |
| 73 | + assertEquals(1, userTodos.size()); |
| 74 | + |
| 75 | + // 提交流程 |
| 76 | + FlowRecord userTodo = userTodos.get(0); |
| 77 | + assertEquals(0, userTodo.getTimeoutTime()); |
| 78 | + // 保存流程 |
| 79 | + leave.setTitle("我要出去看看~~"); |
| 80 | + flowService.remove(userTodo.getId(), user); |
| 81 | + |
| 82 | + List<FlowRecord> records = flowRecordRepository.findFlowRecordByProcessId(userTodo.getProcessId()); |
| 83 | + assertEquals(0, records.size()); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 删除流程测试 (提交以后删除异常) |
| 89 | + */ |
| 90 | + @Test |
| 91 | + void removeTest2() { |
| 92 | + PageRequest pageRequest = PageRequest.of(0, 1000); |
| 93 | + |
| 94 | + User user = new User("张飞"); |
| 95 | + userRepository.save(user); |
| 96 | + |
| 97 | + User dept = new User("刘备"); |
| 98 | + userRepository.save(dept); |
| 99 | + |
| 100 | + User boss = new User("诸葛亮"); |
| 101 | + userRepository.save(boss); |
| 102 | + |
| 103 | + FlowWork flowWork = FlowWorkBuilder.builder(user) |
| 104 | + .title("请假流程") |
| 105 | + .nodes() |
| 106 | + .node("开始节点", "start", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 107 | + .node("部门领导审批", "dept", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(dept.getUserId())) |
| 108 | + .node("总经理审批", "manager", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(boss.getUserId())) |
| 109 | + .node("结束节点", "over", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 110 | + .relations() |
| 111 | + .relation("部门领导审批", "start", "dept") |
| 112 | + .relation("总经理审批", "dept", "manager") |
| 113 | + .relation("结束节点", "manager", "over") |
| 114 | + .build(); |
| 115 | + |
| 116 | + flowWorkRepository.save(flowWork); |
| 117 | + |
| 118 | + String workCode = flowWork.getCode(); |
| 119 | + |
| 120 | + Leave leave = new Leave("我要出去看看"); |
| 121 | + leaveRepository.save(leave); |
| 122 | + |
| 123 | + // 创建流程 |
| 124 | + flowService.startFlow(workCode, user, leave, "发起流程"); |
| 125 | + |
| 126 | + // 查看我的待办 |
| 127 | + List<FlowRecord> userTodos = flowRecordRepository.findTodoByOperatorId(user.getUserId(), pageRequest).getContent(); |
| 128 | + assertEquals(1, userTodos.size()); |
| 129 | + |
| 130 | + // 提交流程 |
| 131 | + FlowRecord userTodo = userTodos.get(0); |
| 132 | + flowService.submitFlow(userTodo.getId(), user,leave, Opinion.pass("同意")); |
| 133 | + |
| 134 | + |
| 135 | + List<FlowRecord> deptTodos = flowRecordRepository.findTodoByOperatorId(dept.getUserId(), pageRequest).getContent(); |
| 136 | + assertEquals(1, deptTodos.size()); |
| 137 | + |
| 138 | + FlowRecord deptTodo = deptTodos.get(0); |
| 139 | + assertThrows(Exception.class, () -> { |
| 140 | + flowService.remove(deptTodo.getId(), dept); |
| 141 | + }); |
| 142 | + |
| 143 | + List<FlowRecord> records = flowRecordRepository.findFlowRecordByProcessId(userTodo.getProcessId()); |
| 144 | + assertEquals(2, records.size()); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + /** |
| 150 | + * 删除流程测试 (提交以后再撤回然后继续删除) |
| 151 | + */ |
| 152 | + @Test |
| 153 | + void removeTest3() { |
| 154 | + PageRequest pageRequest = PageRequest.of(0, 1000); |
| 155 | + |
| 156 | + User user = new User("张飞"); |
| 157 | + userRepository.save(user); |
| 158 | + |
| 159 | + User dept = new User("刘备"); |
| 160 | + userRepository.save(dept); |
| 161 | + |
| 162 | + User boss = new User("诸葛亮"); |
| 163 | + userRepository.save(boss); |
| 164 | + |
| 165 | + FlowWork flowWork = FlowWorkBuilder.builder(user) |
| 166 | + .title("请假流程") |
| 167 | + .nodes() |
| 168 | + .node("开始节点", "start", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 169 | + .node("部门领导审批", "dept", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(dept.getUserId())) |
| 170 | + .node("总经理审批", "manager", "default", ApprovalType.UN_SIGN, OperatorMatcher.specifyOperatorMatcher(boss.getUserId())) |
| 171 | + .node("结束节点", "over", "default", ApprovalType.UN_SIGN, OperatorMatcher.anyOperatorMatcher()) |
| 172 | + .relations() |
| 173 | + .relation("部门领导审批", "start", "dept") |
| 174 | + .relation("总经理审批", "dept", "manager") |
| 175 | + .relation("结束节点", "manager", "over") |
| 176 | + .build(); |
| 177 | + |
| 178 | + flowWorkRepository.save(flowWork); |
| 179 | + |
| 180 | + String workCode = flowWork.getCode(); |
| 181 | + |
| 182 | + Leave leave = new Leave("我要出去看看"); |
| 183 | + leaveRepository.save(leave); |
| 184 | + |
| 185 | + // 创建流程 |
| 186 | + flowService.startFlow(workCode, user, leave, "发起流程"); |
| 187 | + |
| 188 | + // 查看我的待办 |
| 189 | + List<FlowRecord> userTodos = flowRecordRepository.findTodoByOperatorId(user.getUserId(), pageRequest).getContent(); |
| 190 | + assertEquals(1, userTodos.size()); |
| 191 | + |
| 192 | + // 提交流程 |
| 193 | + FlowRecord userTodo = userTodos.get(0); |
| 194 | + flowService.submitFlow(userTodo.getId(), user,leave, Opinion.pass("同意")); |
| 195 | + |
| 196 | + |
| 197 | + List<FlowRecord> deptTodos = flowRecordRepository.findTodoByOperatorId(dept.getUserId(), pageRequest).getContent(); |
| 198 | + assertEquals(1, deptTodos.size()); |
| 199 | + |
| 200 | + FlowRecord deptTod = deptTodos.get(0); |
| 201 | + flowService.submitFlow(deptTod.getId(), dept,leave, Opinion.reject("不同意")); |
| 202 | + |
| 203 | + userTodos = flowRecordRepository.findTodoByOperatorId(user.getUserId(), pageRequest).getContent(); |
| 204 | + assertEquals(1, userTodos.size()); |
| 205 | + |
| 206 | + userTodo = userTodos.get(0); |
| 207 | + flowService.remove(userTodo.getId(), user); |
| 208 | + |
| 209 | + List<FlowRecord> records = flowRecordRepository.findFlowRecordByProcessId(userTodo.getProcessId()); |
| 210 | + assertEquals(0, records.size()); |
| 211 | + |
| 212 | + } |
| 213 | +} |
0 commit comments