From b489f3d834d8d5fd296b366644c835a965f7fb1d Mon Sep 17 00:00:00 2001 From: zhengwr Date: Thu, 2 Jul 2026 11:48:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(gateway):=20models=20=E4=BA=94=E7=AB=AF?= =?UTF-8?q?=E7=82=B9=E8=BD=AC=E5=8F=91(requireSuperAdmin=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E9=97=A8)+wftemplates=20picker=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 阶段 2 网关侧(对齐 flowable-service T2.1 ProcessModelController 契约): FlowableClient +5 方法:listModels/getModel/createModel/updateModel/modelFromProcDef, 全 uri 模板变量严格编码(禁字符串拼接);RestClient 无 PUT 封装 → 照 post 补私有 put() (uriTemplate+uriVars 严格编码);连接级重试 + 4xx/5xx 透传语义复用既有 execute。 ProcessModelerController:抽 requireSuperAdmin(principal) 私有方法(超管返 null,非超管/无身份 返 403 {status:1,msg:"仅系统管理员可用"}),既有两个 source 端点改用它(行为零变,既有 10 测试零改动); 新增五端点 GET /models、GET /models/{id}、POST /models、PUT /models/{id}、POST /models/from-procdef 全部先过门再转发 FlowableClient,返回体原样透传。 GET /api/workflow/wftemplates(同门):formKey 选择器数据源。 **information_schema 实证**:view_wftemplate 无独立名称列(仅 view_wftemplate_id CHAR + xml + isactive + 审计列),
根标签也无 title/name 属性(0/1016);唯一人类可读名来自 XML 的 entityid → sys_entity.name(378/1016 带 entityid,339 解析出中文名如「通知管理」)。故 name = entityid 解析出的实体名(LEFT JOIN + substring 正则抽 entityid),空则回退 id; view_wftemplate_id 是 CHAR 补白 → RowMapper trim;isactive=1 过滤;读库异常降级空列表不 500。 测试:FlowableClientTest 12→18(+list/get/create/update/from-procdef body 透传+4xx 透传); ProcessModelerControllerTest 10→20(五端点各 admin 200 透传+非超管 403 门拦下游 恰调 1 次证明门生效、 无身份 403、422 胶水经 FlowGatewayExceptionHandler 出 FLOW_REMOTE_ERROR、wftemplates 超管 trim 补白+ 无名回退 id、非超管 403 零读库、读库异常降级空列表)。platform-flow-gateway 全模块 98 测试绿、 上游 493 测试绿,BUILD SUCCESS,既有测试零改动。 Co-Authored-By: Claude Fable 5 --- .../flowgateway/client/FlowableClient.java | 69 ++++++++ .../web/ProcessModelerController.java | 166 ++++++++++++++++-- .../flowgateway/FlowableClientTest.java | 119 +++++++++++++ .../ProcessModelerControllerTest.java | 160 ++++++++++++++++- 4 files changed, 502 insertions(+), 12 deletions(-) diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowableClient.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowableClient.java index 664813f..50b0bc4 100644 --- a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowableClient.java +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowableClient.java @@ -260,6 +260,63 @@ public class FlowableClient { .body(Map.class)); } + // ---------------------------------------------------------------- 草稿 Model API 转发(T2.2) + + /** + * 草稿列表。对应 flowable-service {@code GET /api/flow/models},返回体 + * {@code {models:[{id,name,key,lastUpdateTime}]}} 原样透传(flowable-service 已按 EIP4 分类过滤)。 + */ + @SuppressWarnings("unchecked") + public Map listModels() { + return execute("草稿列表", () -> restClient.get() + .uri("/api/flow/models") + .retrieve() + .body(Map.class)); + } + + /** + * 取草稿(editorSource + metaInfo)。对应 flowable-service {@code GET /api/flow/models/{id}},返回体 + * {@code {found,id,name,key,bpmnXml,metaInfo}} 原样透传;草稿不存在下游返 {@code found:false}。 + * + *

{@code id} 走 uri 模板变量严格编码(禁字符串拼接)。

+ */ + @SuppressWarnings("unchecked") + public Map getModel(String id) { + return execute("取草稿", () -> restClient.get() + .uri("/api/flow/models/{id}", id) + .retrieve() + .body(Map.class)); + } + + /** + * 建草稿。对应 flowable-service {@code POST /api/flow/models},请求体 {@code {name,key,bpmnXml,metaInfo?}} + * 经 Jackson 序列化转发,返回 {@code {modelId}};坏 XML 下游 422 → {@link FlowRemoteException} 透传。 + */ + @SuppressWarnings("unchecked") + public Map createModel(Object body) { + return post("建草稿", "/api/flow/models", body, Map.class); + } + + /** + * 更新草稿。对应 flowable-service {@code PUT /api/flow/models/{id}},请求体 {@code {name?,bpmnXml,metaInfo?}} + * 经 Jackson 序列化转发,返回 {@code {status:0}};草稿不存在下游 404 / 坏 XML 422 → {@link FlowRemoteException} 透传。 + * + *

{@code id} 走 uri 模板变量严格编码(禁字符串拼接)。

+ */ + @SuppressWarnings("unchecked") + public Map updateModel(String id, Object body) { + return put("更新草稿", "/api/flow/models/{id}", body, Map.class, id); + } + + /** + * 从流程定义建草稿。对应 flowable-service {@code POST /api/flow/models/from-procdef},请求体 + * {@code {procDefId}} 经 Jackson 序列化转发,返回 {@code {modelId}};procDef 不存在下游返 {@code found:false}。 + */ + @SuppressWarnings("unchecked") + public Map modelFromProcDef(Object body) { + return post("从流程定义建草稿", "/api/flow/models/from-procdef", body, Map.class); + } + /** 按发起人查询历史流程实例。对应 flowable-service {@code GET /api/flow/process/started}。 */ public List started(String startedBy, int firstResult, int maxResults) { return execute("发起流程列表", () -> restClient.get() @@ -327,6 +384,18 @@ public class FlowableClient { .body(respType)); } + /** + * PUT 转发(RestClient 无既有封装 → 照 {@link #post} 补)。{@code uriTemplate}+{@code uriVars} 走 RestClient + * 严格编码({@code id} 作 uri 变量,禁字符串拼接);连接级重试 + 4xx/5xx 透传语义与 post 一致。 + */ + private T put(String op, String uriTemplate, Object body, Class respType, Object... uriVars) { + return execute(op, () -> restClient.put() + .uri(uriTemplate, uriVars) + .body(body == null ? Map.of() : body) + .retrieve() + .body(respType)); + } + /** * 统一执行:连接级故障带退避重试 maxRetries 次,耗尽 → 503 语义;业务错(4xx/5xx)立即透传不重试。 */ diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/web/ProcessModelerController.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/web/ProcessModelerController.java index a4e79c3..1f878f5 100644 --- a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/web/ProcessModelerController.java +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/web/ProcessModelerController.java @@ -1,13 +1,21 @@ package com.eip4.platform.flowgateway.web; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,22 +30,58 @@ import io.swagger.v3.oas.annotations.tags.Tag; * 在线流程 Modeler 网关门面:把 flowable-service 的流程定义/草稿能力经超管门暴露给设计器前端。 * *

flowable-service 裸 8089 无 Spring Security → 流程编辑/部署面的鉴权唯一落点在本网关层。 - * 本控制器所有端点先过超管门({@link EngineContextBinder#isSystemAdmin},与全站 {@code requireAdmin} - * 逐口径对齐),非超管返回 403,过门后转发 {@link FlowableClient},返回体原样透传。 + * 本控制器所有端点先过超管门({@link #requireSuperAdmin},委托 {@link EngineContextBinder#isSystemAdmin}, + * 与全站 {@code requireAdmin} 逐口径对齐),非超管返回 403,过门后转发 {@link FlowableClient},返回体原样透传。 * - *

阶段 1(T1.6):只读载入存量流程源 XML。草稿 CRUD / 发布门在后续阶段接入本控制器。 + *

阶段 1(T1.6):只读载入存量流程源 XML。阶段 2(T2.2):草稿 CRUD 五端点转发(list/get/create/update/ + * from-procdef,对齐 flowable-service {@code ProcessModelController} 契约)+ 办理人/会签 formKey 选择器数据源 + * {@code /wftemplates}(读业务库 {@code view_wftemplate})。发布门在阶段 3 接入。

*/ @Tag(name = "流程 Modeler 网关", description = "流程设计器:超管门 + flowable-service 转发(存量源载入/草稿/发布)") @RestController @RequestMapping("/api/workflow") public class ProcessModelerController { - private final FlowableClient flowableClient; + private static final Logger log = LoggerFactory.getLogger(ProcessModelerController.class); - public ProcessModelerController(FlowableClient flowableClient) { + /** + * formKey 选择器数据源:{@code view_wftemplate}(1016 行已迁移)。实证(information_schema): + * 表无独立名称列(仅 {@code view_wftemplate_id CHAR + xml + isactive + 审计列}),{@code } 根标签 + * 也无 title/name 属性(0/1016);唯一人类可读名来自 XML 的 {@code entityid} → {@code sys_entity.name} + * (378 模板带 entityid,339 解析出中文名,如「通知管理」)。故 name = 解析出的实体名,无则回退 id。 + * {@code view_wftemplate_id} 是 CHAR 补白列 → RowMapper 内 trim。 + */ + static final String WFTEMPLATES_SQL = + "SELECT t.view_wftemplate_id AS id, se.name AS name " + + "FROM view_wftemplate t " + + "LEFT JOIN sys_entity se " + + " ON se.sys_entity_id = substring(CAST(t.xml AS text) FROM 'entityid=\"([0-9a-fA-F-]+)\"') " + + "WHERE t.isactive = 1 " + + "ORDER BY se.name NULLS LAST, t.view_wftemplate_id"; + + /** 单行 → {@code {id(trim 补白),name(实体名,空则回退 id)}}。 */ + static final RowMapper> WFTEMPLATE_ROW = (rs, rowNum) -> { + String id = rs.getString("id"); + if (id != null) { + id = id.trim(); + } + String name = rs.getString("name"); + Map row = new LinkedHashMap<>(); + row.put("id", id); + row.put("name", (name != null && !name.isBlank()) ? name : id); + return row; + }; + + private final FlowableClient flowableClient; + private final JdbcTemplate jdbcTemplate; + + public ProcessModelerController(FlowableClient flowableClient, JdbcTemplate jdbcTemplate) { this.flowableClient = flowableClient; + this.jdbcTemplate = jdbcTemplate; } + // ---------------------------------------------------------------- 存量流程源载入(T1.6) + @Operation(summary = "按流程定义 id 取净化源 XML(超管)", description = "超管门后转发 flowable-service /api/flow/process-definitions/{procDefId}/source," + "返回 {procDefId, found, bpmnXml(净化后), idMap, procDefKey, version} 供 bpmn-js Modeler 载入。" @@ -45,8 +89,9 @@ public class ProcessModelerController { @GetMapping("/process-definitions/{procDefId}/source") public ResponseEntity> source(@AuthenticationPrincipal AuthPrincipal principal, @PathVariable String procDefId) { - if (!EngineContextBinder.isSystemAdmin(principal)) { - return forbidden(); + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; } return ResponseEntity.ok(flowableClient.procDefSource(procDefId)); } @@ -59,14 +104,113 @@ public class ProcessModelerController { @GetMapping("/process-definitions/by-key/{key}/source") public ResponseEntity> sourceByKey(@AuthenticationPrincipal AuthPrincipal principal, @PathVariable String key) { - if (!EngineContextBinder.isSystemAdmin(principal)) { - return forbidden(); + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; } return ResponseEntity.ok(flowableClient.procDefSourceByKey(key)); } - /** 超管门未过:403 + {status:1,msg} 风格对齐既有网关端点。 */ - private static ResponseEntity> forbidden() { + // ---------------------------------------------------------------- 草稿 CRUD 转发(T2.2) + + @Operation(summary = "草稿列表(超管)", + description = "超管门后转发 flowable-service GET /api/flow/models,返回 {models:[{id,name,key,lastUpdateTime}]}。") + @GetMapping("/models") + public ResponseEntity> listModels(@AuthenticationPrincipal AuthPrincipal principal) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + return ResponseEntity.ok(flowableClient.listModels()); + } + + @Operation(summary = "取草稿(超管)", + description = "超管门后转发 flowable-service GET /api/flow/models/{id},返回 {found,id,name,key,bpmnXml,metaInfo}。" + + "id 由 FlowableClient 以 uri 变量严格编码转发。") + @GetMapping("/models/{id}") + public ResponseEntity> getModel(@AuthenticationPrincipal AuthPrincipal principal, + @PathVariable String id) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + return ResponseEntity.ok(flowableClient.getModel(id)); + } + + @Operation(summary = "建草稿(超管)", + description = "超管门后转发 flowable-service POST /api/flow/models,body {name,key,bpmnXml,metaInfo?}," + + "返回 {modelId}。坏 XML 下游 422 经既有 handler 透传。") + @PostMapping("/models") + public ResponseEntity> createModel(@AuthenticationPrincipal AuthPrincipal principal, + @RequestBody Map body) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + return ResponseEntity.ok(flowableClient.createModel(body)); + } + + @Operation(summary = "更新草稿(超管)", + description = "超管门后转发 flowable-service PUT /api/flow/models/{id},body {name?,bpmnXml,metaInfo?}," + + "返回 {status:0}。草稿不存在 404 / 坏 XML 422 经既有 handler 透传。id 以 uri 变量严格编码转发。") + @PutMapping("/models/{id}") + public ResponseEntity> updateModel(@AuthenticationPrincipal AuthPrincipal principal, + @PathVariable String id, + @RequestBody Map body) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + return ResponseEntity.ok(flowableClient.updateModel(id, body)); + } + + @Operation(summary = "从流程定义建草稿(超管)", + description = "超管门后转发 flowable-service POST /api/flow/models/from-procdef,body {procDefId}," + + "返回 {modelId}(净化 id 映射落 metaInfo)。procDef 不存在下游返 {found:false}。") + @PostMapping("/models/from-procdef") + public ResponseEntity> modelFromProcDef(@AuthenticationPrincipal AuthPrincipal principal, + @RequestBody Map body) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + return ResponseEntity.ok(flowableClient.modelFromProcDef(body)); + } + + // ---------------------------------------------------------------- formKey 选择器数据源(T2.2) + + @Operation(summary = "wftemplate 选择器数据源(超管)", + description = "超管门后读业务库 view_wftemplate(isactive=1) → {templates:[{id,name}]},供办理人/会签 formKey" + + " 下拉选择。name 取 XML entityid→sys_entity.name(无则回退 id),view_wftemplate_id 去 CHAR 补白。" + + "读库异常降级为空列表(不 500 阻断设计器)。") + @GetMapping("/wftemplates") + public ResponseEntity> wftemplates(@AuthenticationPrincipal AuthPrincipal principal) { + ResponseEntity> denied = requireSuperAdmin(principal); + if (denied != null) { + return denied; + } + List> templates; + try { + templates = jdbcTemplate.query(WFTEMPLATES_SQL, WFTEMPLATE_ROW); + } catch (RuntimeException e) { + log.warn("view_wftemplate 选择器读取降级为空列表: {}", e.toString()); + templates = List.of(); + } + Map resp = new LinkedHashMap<>(); + resp.put("templates", templates); + return ResponseEntity.ok(resp); + } + + // ---------------------------------------------------------------- 超管门 + + /** + * 统一超管门:超管返回 {@code null}(放行),非超管/无身份返回 403 ResponseEntity({status:1,msg})。 + * 所有端点入口调用,防漏门(T1.6 评审 M-3)。403 信封与既有网关端点逐字对齐。 + */ + private static ResponseEntity> requireSuperAdmin(AuthPrincipal principal) { + if (EngineContextBinder.isSystemAdmin(principal)) { + return null; + } Map body = new LinkedHashMap<>(); body.put("status", 1); body.put("msg", "仅系统管理员可用"); diff --git a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/FlowableClientTest.java b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/FlowableClientTest.java index 997c64f..ed8758d 100644 --- a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/FlowableClientTest.java +++ b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/FlowableClientTest.java @@ -278,4 +278,123 @@ class FlowableClientTest { .satisfies(e -> assertThat(((FlowRemoteException) e).getStatus().value()).isEqualTo(400)); server.verify(); } + + // ---- 草稿 Model API 转发(T2.2):list/get/create/update/from-procdef ---- + // flowable-service ProcessModelController(T2.1) 契约:GET/POST/PUT /api/flow/models[/{id}] + // + POST /api/flow/models/from-procdef。全部返 Map 原样透传;POST/PUT body 经 Jackson 序列化转发; + // {id} 走 uri 模板变量严格编码(禁拼接);下游 4xx 透传 FlowRemoteException。 + + @Test + void 草稿列表_GET_models解析models数组() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), requestTo("http://localhost:8089/api/flow/models")) + .andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess( + "{\"models\":[{\"id\":\"m1\",\"name\":\"总包合同交底\",\"key\":\"总包合同交底\"}]}", + MediaType.APPLICATION_JSON)); + FlowableClient client = new FlowableClient(builder.build(), p); + + Map resp = client.listModels(); + + @SuppressWarnings("unchecked") + List> models = (List>) resp.get("models"); + assertThat(models).hasSize(1); + assertThat(models.get(0)).containsEntry("id", "m1").containsEntry("name", "总包合同交底"); + server.verify(); + } + + @Test + void 取草稿_GET_models按id透传() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), requestTo("http://localhost:8089/api/flow/models/m1")) + .andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess("{\"found\":true,\"id\":\"m1\",\"bpmnXml\":\"\"}", + MediaType.APPLICATION_JSON)); + FlowableClient client = new FlowableClient(builder.build(), p); + + Map resp = client.getModel("m1"); + + assertThat(resp).containsEntry("found", true).containsEntry("bpmnXml", ""); + server.verify(); + } + + @Test + void 建草稿_POST_models_body原样透传() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), requestTo("http://localhost:8089/api/flow/models")) + .andExpect(method(HttpMethod.POST)) + .andExpect(jsonPath("$.name").value("总包合同交底")) + .andExpect(jsonPath("$.key").value("总包合同交底")) + .andExpect(jsonPath("$.bpmnXml").value("")) + .andRespond(withSuccess("{\"modelId\":\"m9\"}", MediaType.APPLICATION_JSON)); + FlowableClient client = new FlowableClient(builder.build(), p); + + Map body = new java.util.LinkedHashMap<>(); + body.put("name", "总包合同交底"); + body.put("key", "总包合同交底"); + body.put("bpmnXml", ""); + Map resp = client.createModel(body); + + assertThat(resp).containsEntry("modelId", "m9"); + server.verify(); + } + + @Test + void 更新草稿_PUT_models按id_body原样透传() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), requestTo("http://localhost:8089/api/flow/models/m1")) + .andExpect(method(HttpMethod.PUT)) + .andExpect(jsonPath("$.bpmnXml").value("")) + .andRespond(withSuccess("{\"status\":0}", MediaType.APPLICATION_JSON)); + FlowableClient client = new FlowableClient(builder.build(), p); + + Map body = new java.util.LinkedHashMap<>(); + body.put("bpmnXml", ""); + Map resp = client.updateModel("m1", body); + + assertThat(resp).containsEntry("status", 0); + server.verify(); + } + + @Test + void 从流程定义建草稿_POST_from_procdef_body原样透传() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), requestTo("http://localhost:8089/api/flow/models/from-procdef")) + .andExpect(method(HttpMethod.POST)) + .andExpect(jsonPath("$.procDefId").value("总包合同交底:3:abc123")) + .andRespond(withSuccess("{\"modelId\":\"m7\"}", MediaType.APPLICATION_JSON)); + FlowableClient client = new FlowableClient(builder.build(), p); + + Map resp = client.modelFromProcDef(Map.of("procDefId", "总包合同交底:3:abc123")); + + assertThat(resp).containsEntry("modelId", "m7"); + server.verify(); + } + + @Test + void 草稿转发_下游4xx透传FlowRemote且不重试() { + FlowGatewayProperties p = props(); + RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl()); + MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build(); + server.expect(ExpectedCount.once(), // 业务错不重试 + requestTo("http://localhost:8089/api/flow/models")) + .andExpect(method(HttpMethod.POST)) + .andRespond(withStatus(HttpStatus.UNPROCESSABLE_ENTITY).body("BPMN 解析失败")); + FlowableClient client = new FlowableClient(builder.build(), p); + + assertThatThrownBy(() -> client.createModel(Map.of("bpmnXml", ""))) + .isInstanceOf(FlowRemoteException.class) + .satisfies(e -> assertThat(((FlowRemoteException) e).getStatus().value()).isEqualTo(422)); + server.verify(); + } } diff --git a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java index 76e617a..c393268 100644 --- a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java +++ b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/ProcessModelerControllerTest.java @@ -2,22 +2,28 @@ package com.eip4.platform.flowgateway; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; 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 java.sql.ResultSet; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; import org.springframework.core.MethodParameter; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.bind.support.WebDataBinderFactory; @@ -26,6 +32,7 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import com.eip4.platform.auth.security.AuthPrincipal; +import com.eip4.platform.flowgateway.client.FlowRemoteException; import com.eip4.platform.flowgateway.client.FlowServiceUnavailableException; import com.eip4.platform.flowgateway.client.FlowableClient; import com.eip4.platform.flowgateway.web.FlowGatewayExceptionHandler; @@ -38,7 +45,9 @@ import com.eip4.platform.flowgateway.web.ProcessModelerController; class ProcessModelerControllerTest { private final FlowableClient flowableClient = mock(FlowableClient.class); - private final ProcessModelerController controller = new ProcessModelerController(flowableClient); + private final JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class); + private final ProcessModelerController controller = + new ProcessModelerController(flowableClient, jdbcTemplate); /** 真实形态:中文 key + 冒号分隔的 procDefId。 */ private static final String CN_PROCDEF = "总包合同交底:3:abc123"; @@ -191,4 +200,153 @@ class ProcessModelerControllerTest { .andExpect(status().isServiceUnavailable()) .andExpect(jsonPath("$.error").value("FLOW_SERVICE_UNAVAILABLE")); } + + // ---- 草稿 CRUD 五端点转发(T2.2):每端点 admin 200 透传 + 非超管 403。 + // 两次调用(admin+非超管同参)后 verify 下游恰调 1 次 → 证明门确实拦下了非超管调用(若漏门会是 2 次)。---- + + @Test + void 草稿列表_超管透传200_非超管403门拦下游() { + Map downstream = Map.of("models", List.of(Map.of("id", "m1", "name", "总包合同交底"))); + when(flowableClient.listModels()).thenReturn(downstream); + + ResponseEntity> ok = controller.listModels(admin()); + assertThat(ok.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(ok.getBody()).isEqualTo(downstream); + + ResponseEntity> denied = controller.listModels(normalUser()); + assertThat(denied.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + assertThat(denied.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用"); + verify(flowableClient).listModels(); // 恰 1 次(仅超管):非超管被门拦,未落下游 + } + + @Test + void 取草稿_超管透传200_非超管403门拦下游() { + Map downstream = Map.of("found", true, "id", "m1", "bpmnXml", ""); + when(flowableClient.getModel("m1")).thenReturn(downstream); + + ResponseEntity> ok = controller.getModel(admin(), "m1"); + assertThat(ok.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(ok.getBody()).isEqualTo(downstream); + + ResponseEntity> denied = controller.getModel(normalUser(), "m1"); + assertThat(denied.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + verify(flowableClient).getModel("m1"); // 恰 1 次(仅超管) + } + + @Test + void 建草稿_超管透传200_非超管403门拦下游() { + Map req = Map.of("name", "总包合同交底", "key", "总包合同交底", "bpmnXml", ""); + Map downstream = Map.of("modelId", "m9"); + when(flowableClient.createModel(req)).thenReturn(downstream); + + ResponseEntity> ok = controller.createModel(admin(), req); + assertThat(ok.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(ok.getBody()).isEqualTo(downstream); + + ResponseEntity> denied = controller.createModel(normalUser(), req); + assertThat(denied.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + verify(flowableClient).createModel(req); // 恰 1 次(仅超管):漏门会是 2 次 + } + + @Test + void 更新草稿_超管透传200_非超管403门拦下游() { + Map req = Map.of("bpmnXml", ""); + Map downstream = Map.of("status", 0); + when(flowableClient.updateModel("m1", req)).thenReturn(downstream); + + ResponseEntity> ok = controller.updateModel(admin(), "m1", req); + assertThat(ok.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(ok.getBody()).isEqualTo(downstream); + + ResponseEntity> denied = controller.updateModel(normalUser(), "m1", req); + assertThat(denied.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + verify(flowableClient).updateModel("m1", req); // 恰 1 次(仅超管) + } + + @Test + void 从流程定义建草稿_超管透传200_非超管403门拦下游() { + Map req = Map.of("procDefId", CN_PROCDEF); + Map downstream = Map.of("modelId", "m7"); + when(flowableClient.modelFromProcDef(req)).thenReturn(downstream); + + ResponseEntity> ok = controller.modelFromProcDef(admin(), req); + assertThat(ok.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(ok.getBody()).isEqualTo(downstream); + + ResponseEntity> denied = controller.modelFromProcDef(normalUser(), req); + assertThat(denied.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + verify(flowableClient).modelFromProcDef(req); // 恰 1 次(仅超管) + } + + @Test + void 无身份_建草稿403且不触发下游() { + ResponseEntity> resp = controller.createModel(null, Map.of("name", "x")); + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + assertThat(resp.getBody()).containsEntry("status", 1); + verify(flowableClient, never()).createModel(any()); + } + + // ---- 422 胶水透传:下游坏 XML 422 经既有 FlowGatewayExceptionHandler 出 FLOW_REMOTE_ERROR 信封 ---- + + @Test + void 胶水_建草稿_下游422经handler透传() throws Exception { + when(flowableClient.createModel(any())) + .thenThrow(new FlowRemoteException(HttpStatus.UNPROCESSABLE_ENTITY, "flowable-service 建草稿返回 422")); + + glueMvc().perform(post("/api/workflow/models") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"name\":\"x\",\"key\":\"k\",\"bpmnXml\":\"\"}")) + .andExpect(status().isUnprocessableEntity()) + .andExpect(jsonPath("$.status").value(422)) + .andExpect(jsonPath("$.error").value("FLOW_REMOTE_ERROR")); + } + + // ---- wftemplates 选择器数据源:超管读库 trim CHAR 补白 + 无实体名回退 id;非超管 403 零读库 ---- + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Test + void wftemplates_超管_读库trim补白并回退id() { + // mock JdbcTemplate 反射调真实 RowMapper:第一行有实体名(取名+trim补白),第二行无名(回退 trim 后的 id) + when(jdbcTemplate.query(anyString(), any(RowMapper.class))).thenAnswer(inv -> { + RowMapper> rm = inv.getArgument(1); + ResultSet named = mock(ResultSet.class); + when(named.getString("id")).thenReturn("wft-0001 "); // CHAR(36) 补白 + when(named.getString("name")).thenReturn("通知管理"); + ResultSet unnamed = mock(ResultSet.class); + when(unnamed.getString("id")).thenReturn("wft-0002 "); + when(unnamed.getString("name")).thenReturn(null); // 无 entityid/实体名 → 回退 id + return List.of(rm.mapRow(named, 0), rm.mapRow(unnamed, 1)); + }); + + ResponseEntity> resp = controller.wftemplates(admin()); + + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); + List> templates = (List>) resp.getBody().get("templates"); + assertThat(templates).hasSize(2); + assertThat(templates.get(0)).containsEntry("id", "wft-0001").containsEntry("name", "通知管理"); + assertThat(templates.get(1)).containsEntry("id", "wft-0002").containsEntry("name", "wft-0002"); + } + + @SuppressWarnings("rawtypes") + @Test + void wftemplates_非超管_403且不读库() { + ResponseEntity> resp = controller.wftemplates(normalUser()); + + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN); + assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用"); + verify(jdbcTemplate, never()).query(anyString(), any(RowMapper.class)); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Test + void wftemplates_读库异常_降级空列表不500() { + when(jdbcTemplate.query(anyString(), any(RowMapper.class))) + .thenThrow(new org.springframework.dao.DataAccessResourceFailureException("db down")); + + ResponseEntity> resp = controller.wftemplates(admin()); + + assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); + List> templates = (List>) resp.getBody().get("templates"); + assertThat(templates).isEmpty(); + } }