P3-1/2: 新建 flowable-service 独立工作流服务 + 移植 3 个表达式 handler

- 新模块 flowable-service(单客户交付四进程之一,可执行 jar,端口 8089):
  Flowable 6.8.1 引擎(连 eip4flow.eip3,databaseSchemaUpdate=false 严禁自动改表)
  + v5 兼容层 + 双数据源(ACT_ 本地 + 业务组织库客户库只读)
- 3 个 handler 按原包名移植经 cfg.setBeans 注册:CandidateUsersHandler(4761 处)/
  MultiInstanceUsersHandler(33 处会签)/WfContextService(175 处)
- 组织服务全闭包裁剪为 OrgDirectory 窄接口 + OrgDirectoryJdbc 直查业务组织库 SQL
  (逐条对齐 framework AssignmentHandlerImpl/UserFindServiceImpl/OrganizationServiceImpl),
  不拖入保真层元数据引擎;OSGi 取 bean → WfBeanRegistry(§3.1/§5.17 替换类策略);
  com.dsasp.* 零反向依赖 com.eip4(ArchUnit 纪律保持)
- REST 薄封装:待办/已办/发起/办理/挂起激活/委托 + OpenAPI;挂起拒办映射 409
- 26 单测绿;全反应堆 mvn test + ArchUnit 绿
- 实测验证(6.8.1.0 真实迁移数据):1509 定义加载、3 个真实 position 表达式解析
  与客户库组织 SQL 一致、待办口径双向对账(4=4/12=12)、挂起拒办回归、委托语义、会签解析;
  写测试全部可逆还原(act_ru_task 总数 9321 不变)
- 5 个 scriptTask 静态核对:全为 Activiti Explorer demo/report 残留,只读零写库
- 报告 flowable-service/P3流程服务实测报告.md;手册新增 §6.5/§6.6/§6.7 坑位

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 14:40:51 +08:00
parent 3b59e90504
commit 6ced9b15f2
26 changed files with 2161 additions and 1 deletions

View File

@@ -0,0 +1,130 @@
package com.dsasp.server.wf;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.dsasp.server.wf.support.OrgDirectory;
import com.dsasp.server.wf.support.WfBeanRegistry;
import com.dsasp.server.wf.user.assignee.CandidateUsersHandler;
import com.dsasp.server.wf.user.assignee.MultiInstanceUsersHandler;
/**
* 移植 handler 解析逻辑单测:用真实形态的候选人表达式(取自客户库 BPMN),
* 验证 JSON 解析 + 配置项分派 + isCandidate 过滤 + 降级分支,经假 OrgDirectory 隔离数据源。
*
* <p>真实表达式形态(act_ge_bytearray 实测,XML 反转义后):
* <pre>{"items":[{"type":"user","content":"&lt;uid&gt;","isCandidate":true,"isSendMail":false,"isSendPhone":false},
* {"type":"depart","content":"&lt;dep&gt;","isCandidate":false,"isSendMail":true,"isSendPhone":false}]}</pre>
* 引擎运行时把 &amp;quot; 反转义为 ",【】 由 handler 自身 replace 为 {}。</p>
*/
class CandidateUsersHandlerTest {
/** 记录分派调用的假实现,隔离业务库。 */
static class RecordingOrgDirectory implements OrgDirectory {
final List<String> dispatched = new ArrayList<>();
@Override
public List<String> assignmentUser(String type, String content, String initiatorId, String initiatorDepart) {
dispatched.add(type + ":" + content);
if ("user".equals(type)) {
return List.of(content.split(","));
}
if ("depart".equals(type)) {
return List.of("dep-user-1", "dep-user-2");
}
return new ArrayList<>();
}
@Override
public List<String> getUsersByProjectRole(String projectId, String roleContent) {
dispatched.add("projectRoleType:" + roleContent);
return List.of("pr-user-1");
}
@Override
public String getOrgIdByDepartId(String departId) {
return "org-1";
}
}
RecordingOrgDirectory dir;
@BeforeEach
void setUp() {
dir = new RecordingOrgDirectory();
WfBeanRegistry.setOrgDirectory(dir);
}
@AfterEach
void tearDown() {
WfBeanRegistry.setOrgDirectory(null);
}
/** 用真实 JSON 形态(带 【】 与 items),验证仅 isCandidate=true 的项进候选人列表。 */
@Test
void 解析真实表达式仅候选项进列表() {
String expr = "【\"items\":["
+ "【\"type\":\"user\",\"content\":\"u1,u2\",\"isCandidate\":true,\"isSendMail\":false,\"isSendPhone\":false】,"
+ "【\"type\":\"depart\",\"content\":\"dep9\",\"isCandidate\":false,\"isSendMail\":true,\"isSendPhone\":false】"
+ "]】";
CandidateUsersHandler handler = new CandidateUsersHandler();
List<String> users = handler.getCandidateUsers((Object) null, expr);
// user 项 isCandidate=true → 进列表;depart 项 isCandidate=false → 仅 mail,不进候选人
assertThat(users).containsExactly("u1", "u2");
// 两项都被分派(候选与抄送都解析)
assertThat(dir.dispatched).contains("user:u1,u2", "depart:dep9");
}
@Test
void projectRoleType走项目角色分派() {
String expr = "【\"items\":["
+ "【\"type\":\"projectRoleType\",\"content\":\"true,false\",\"isCandidate\":true,\"isSendMail\":false,\"isSendPhone\":false】"
+ "]】";
CandidateUsersHandler handler = new CandidateUsersHandler();
List<String> users = handler.getCandidateUsers((Object) null, expr);
assertThat(users).containsExactly("pr-user-1");
assertThat(dir.dispatched).contains("projectRoleType:true,false");
}
@Test
void scriptUser分支降级返回空不抛异常() {
String expr = "【\"items\":["
+ "【\"type\":\"scriptUser\",\"content\":\"some script\",\"isCandidate\":true,\"isSendMail\":false,\"isSendPhone\":false】"
+ "]】";
CandidateUsersHandler handler = new CandidateUsersHandler();
List<String> users = handler.getCandidateUsers((Object) null, expr);
assertThat(users).isEmpty();
// scriptUser 不进 assignmentUser 分派(WARN 降级)
assertThat(dir.dispatched).noneMatch(s -> s.startsWith("scriptUser"));
}
@Test
void 会签handler委托给候选人解析() {
String expr = "【\"items\":["
+ "【\"type\":\"user\",\"content\":\"m1,m2,m3\",\"isCandidate\":true,\"isSendMail\":false,\"isSendPhone\":false】"
+ "]】";
MultiInstanceUsersHandler handler = new MultiInstanceUsersHandler();
List<String> users = handler.getUsers((Object) null, expr);
assertThat(users).containsExactly("m1", "m2", "m3");
}
@Test
void resolveProcessInstanceId支持字符串与null() {
ExposingHandler handler = new ExposingHandler();
assertThat(handler.pid("pi-123")).isEqualTo("pi-123");
assertThat(handler.pid(null)).isNull();
}
/** 暴露 protected resolveProcessInstanceId 以测试 v5/v6 execution id 提取。 */
static class ExposingHandler extends CandidateUsersHandler {
String pid(Object execution) {
return resolveProcessInstanceId(execution);
}
}
}

View File

@@ -0,0 +1,146 @@
package com.eip4.flowservice.api;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
* REST 薄层单测:mock Flowable 引擎服务,MockMvc standalone 验证转调正确性、入参校验、出参 DTO 收敛。
*/
@ExtendWith(MockitoExtension.class)
class FlowControllerTest {
@Mock
TaskService taskService;
@Mock
RuntimeService runtimeService;
@Mock
HistoryService historyService;
@Mock
RepositoryService repositoryService;
@Mock
TaskQuery taskQuery;
MockMvc mvc;
@BeforeEach
void setUp() {
FlowController controller = new FlowController(taskService, runtimeService, historyService, repositoryService);
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void 发起流程缺少key和id返回400() throws Exception {
mvc.perform(post("/api/flow/process/start")
.contentType("application/json")
.content("{}"))
.andExpect(status().isBadRequest());
}
@Test
void 发起流程按key成功返回实例() throws Exception {
ProcessInstance pi = org.mockito.Mockito.mock(ProcessInstance.class);
when(pi.getId()).thenReturn("pi-1");
when(pi.getProcessDefinitionKey()).thenReturn("leaveProcess");
when(runtimeService.startProcessInstanceByKey(eq("leaveProcess"), any(), any())).thenReturn(pi);
mvc.perform(post("/api/flow/process/start")
.contentType("application/json")
.content("{\"processDefinitionKey\":\"leaveProcess\",\"businessKey\":\"biz-9\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value("pi-1"))
.andExpect(jsonPath("$.processDefinitionKey").value("leaveProcess"));
}
@Test
void 办理任务转调complete() throws Exception {
mvc.perform(post("/api/flow/tasks/task-7/complete")
.contentType("application/json")
.content("{}"))
.andExpect(status().isNoContent());
verify(taskService).complete("task-7");
}
@Test
void 挂起实例转调suspend() throws Exception {
mvc.perform(post("/api/flow/process/pi-3/suspend"))
.andExpect(status().isNoContent());
verify(runtimeService).suspendProcessInstanceById("pi-3");
}
@Test
void 激活实例转调activate() throws Exception {
mvc.perform(post("/api/flow/process/pi-3/activate"))
.andExpect(status().isNoContent());
verify(runtimeService).activateProcessInstanceById("pi-3");
}
@Test
void 委托缺少受托人返回400() throws Exception {
mvc.perform(post("/api/flow/tasks/task-7/delegate")
.contentType("application/json")
.content("{}"))
.andExpect(status().isBadRequest());
}
@Test
void 委托成功转调delegateTask并回显() throws Exception {
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.taskId("task-7")).thenReturn(taskQuery);
Task t = org.mockito.Mockito.mock(Task.class);
when(t.getId()).thenReturn("task-7");
when(t.getAssignee()).thenReturn("u-delegate");
when(taskQuery.singleResult()).thenReturn(t);
mvc.perform(post("/api/flow/tasks/task-7/delegate")
.contentType("application/json")
.content("{\"delegateUserId\":\"u-delegate\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.assignee").value("u-delegate"));
verify(taskService).delegateTask("task-7", "u-delegate");
}
@Test
void 待办计数按assignee转调() throws Exception {
when(taskService.createTaskQuery()).thenReturn(taskQuery);
when(taskQuery.active()).thenReturn(taskQuery);
when(taskQuery.taskAssignee("u1")).thenReturn(taskQuery);
when(taskQuery.count()).thenReturn(42L);
mvc.perform(get("/api/flow/tasks/todo/count").param("assignee", "u1"))
.andExpect(status().isOk())
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content().string("42"));
}
@Test
void 流程定义计数转调repository() throws Exception {
var defQuery = org.mockito.Mockito.mock(org.flowable.engine.repository.ProcessDefinitionQuery.class);
when(repositoryService.createProcessDefinitionQuery()).thenReturn(defQuery);
when(defQuery.count()).thenReturn(1509L);
mvc.perform(get("/api/flow/process-definitions/count"))
.andExpect(status().isOk())
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content().string("1509"));
}
}

View File

@@ -0,0 +1,140 @@
package com.eip4.flowservice.org;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
/**
* 候选人解析逻辑单测:mock 业务库 JdbcTemplate,验证各配置类型走对的 SQL 分派与口径,
* 对齐 framework AssignmentHandlerImpl/UserFindServiceImpl 语义。
*/
@ExtendWith(MockitoExtension.class)
class OrgDirectoryJdbcTest {
@Mock
JdbcTemplate bizJdbc;
OrgDirectoryJdbc directory() {
return new OrgDirectoryJdbc(bizJdbc);
}
/**
* 让 bizJdbc.query(sql, mapper, args...) 始终返回给定结果。
* 用 doAnswer 避开 Mockito 对 JdbcTemplate 可变参数(Object...)的逐元素匹配坑
* (varargs 会被展开为独立实参,固定 arity 的 any() 难以稳定匹配)。
*/
@SuppressWarnings("unchecked")
private void stubQuery(List<String> result) {
lenient().doReturn(result).when(bizJdbc)
.query(anyString(), any(RowMapper.class), (Object[]) any(Object[].class));
}
/** 多次 query 顺序返回不同结果(部门内命中/回退场景)。 */
@SuppressWarnings("unchecked")
private void stubQuerySequence(List<String>... results) {
var stub = lenient().doReturn(results[0]);
for (int i = 1; i < results.length; i++) {
stub = stub.doReturn(results[i]);
}
stub.when(bizJdbc).query(anyString(), any(RowMapper.class), (Object[]) any(Object[].class));
}
@Test
void user类型直接返回content分割的id不查库() {
OrgDirectoryJdbc d = directory();
List<String> users = d.assignmentUser("user", "u1,u2,u3", null, null);
assertThat(users).containsExactly("u1", "u2", "u3");
}
@Test
void user类型content为空返回空列表() {
OrgDirectoryJdbc d = directory();
assertThat(d.assignmentUser("user", "", null, null)).isEmpty();
assertThat(d.assignmentUser("user", null, null, null)).isEmpty();
}
@Test
void position类型查sys_positionuser并去重去padding() {
stubQuery(List.of("user-a ", "user-b ", "user-a "));
OrgDirectoryJdbc d = directory();
List<String> users = d.assignmentUser("position", "pos1", null, null);
// 去 padding(trim)+去重(LinkedHashSet)
assertThat(users).containsExactly("user-a", "user-b");
}
@Test
void roles类型走多表JOIN查询() {
stubQuery(List.of("r-user-1", "r-user-2"));
OrgDirectoryJdbc d = directory();
List<String> users = d.assignmentUser("roles", "role1,role2", null, null);
assertThat(users).containsExactly("r-user-1", "r-user-2");
}
@Test
void initiator类型返回发起人本人() {
OrgDirectoryJdbc d = directory();
assertThat(d.assignmentUser("initiator", null, "boss", null)).containsExactly("boss");
}
@Test
void initiator类型发起人为空返回空() {
OrgDirectoryJdbc d = directory();
assertThat(d.assignmentUser("initiator", null, null, null)).isEmpty();
}
@Test
void initiatorLeader类型查直接领导() {
stubQuery(List.of("leader-1"));
OrgDirectoryJdbc d = directory();
assertThat(d.assignmentUser("initiatorLeader", null, "u1", null)).containsExactly("leader-1");
}
@Test
void formSpecifiedDimension部门内命中岗位则不回退公司() {
// 第一次 query(部门下岗位类别岗位)命中,直接返回其人员,不查 org 回退
stubQuerySequence(
List.of("posX"), // getPositionsByDepartAndPositionTypes
List.of("dim-user-1")); // findUsersByPositions
OrgDirectoryJdbc d = directory();
List<String> users = d.assignmentUser("formSpecifiedDimension", "dim1,typeA", "u1", "dep1");
assertThat(users).containsExactly("dim-user-1");
}
@Test
void 元数据虚拟列依赖类型返回空_initiatorDepartManager() {
// initiatorDepartManager 依赖 MASTERDEPART 元数据虚拟列,纯直查不可复现,返回空(报告登记)
OrgDirectoryJdbc d = directory();
assertThat(d.assignmentUser("initiatorDepartManager", null, "u1", "dep1")).isEmpty();
assertThat(d.assignmentUser("fixedDimension", "dim1,typeA", "u1", "dep1")).isEmpty();
}
@Test
void projectRoleType无解析器时返回空() {
OrgDirectoryJdbc d = directory();
assertThat(d.getUsersByProjectRole("proj1", "true,false")).isEmpty();
}
@Test
void projectRoleType有解析器时委托() {
OrgDirectoryJdbc d = new OrgDirectoryJdbc(bizJdbc,
(projectId, roleContent) -> List.of("pr-user-1"));
assertThat(d.getUsersByProjectRole("proj1", "true")).containsExactly("pr-user-1");
}
@Test
void getOrgIdByDepartId命中返回公司id() {
stubQuery(List.of("org-99"));
OrgDirectoryJdbc d = directory();
assertThat(d.getOrgIdByDepartId("dep1")).isEqualTo("org-99");
}
}