feat(metadata): 服务流执行 REST 门面 FlowController + 本地自举 + 单测

P1-2 第二步:报表方案 D 类——79 个 selectservice 服务流的统一 REST 化入口。

FlowController(com.eip4.platform.metadata.web):
  GET  /api/flow/{id}?k=v   query 参数 → 服务流入参 map(查询/报表类)
  POST /api/flow/{id} body  JSON body  → 服务流入参 map(参数多/带结构)
  统一封装 {status,msg,data};响应递归归一化(GeneralPOJO→平面行、
  listSelect 结果 {listresult,pagecount}→amis {items,total})。OpenAPI 注解。
  REST 入口读写时机:引擎不区分读写,P1 不在门面做引擎级读写闸,由调用方按
  「流定义 XML 审阅」确认查询/报表类后放行;风险与 P2 三层防护待办写入注释与报告。

适配(P1 服务流脚本编译):SecureGroovyScriptEngineFactory 的 DEFAULTIMPORTSLIST
移除两条 Vaadin 客户端 UI 默认导入(client.groovy.TestService、
com.dsasp.client.viewframework.component.MainComponent)——它们逐脚本注入,
classpath 缺失会使每个服务流脚本编译失败;与服务端取数无关,原值登记于注释供 diff。

LegacyXmlWiring:补 FlowServiceImpl.setJdbcTemplate(等价原 XML <property> 注入)。

本地自举:platform-parity bootstrap-flow.sql(幂等)——在 eip4m0 上建 SYS_SERVICEFLOW
+ 自描述元数据 + 最小查询流 EIP4M0_USERGROUP_SEARCH,参照 parity bootstrap 思路,
无客户库副本时引擎可独立跑通。

单测 18 例:FlowModelParseTest(9,XML 解析/参数绑定/默认流生成/往返序列化)+
FlowControllerTest(8,mock 引擎,GET/POST 参数透传 + 响应归一化)+
FlowExecuteLocalIT(1,@SpringBootTest local profile,自举 SQL + 真实引擎执行 +
二次一致;eip4m0 不可达则 assumeTrue 跳过)。全绿。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 12:01:15 +08:00
parent de72116502
commit f0500647be
7 changed files with 731 additions and 4 deletions

View File

@@ -0,0 +1,127 @@
package com.eip4.platform.metadata.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.dsasp.server.common.core.service.FlowExcuteService;
import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
import com.dsasp.server.common.util.UserSession;
import io.swagger.v3.oas.annotations.Operation;
/**
* 服务流执行 REST 门面(报表迁移方案 D 类:79 个 selectservice 服务流 REST 化的统一入口)。
*
* <p>把保真层服务流引擎 {@code FlowExcuteService.excute(map, ctx, flowId)} 暴露为 HTTP:
* <ul>
* <li>{@code GET /api/flow/{id}?k=v...} —— query 参数作为服务流入参 {@code map}(适合查询/报表类)</li>
* <li>{@code POST /api/flow/{id}} body=JSON —— 请求体作为服务流入参 {@code map}(适合参数多/带结构的流)</li>
* </ul>
* 这正是报表方案 D 节 echart.jsp 第 25 行 {@code flowService.excute(map, UserSession.get(), flowid)} 的 REST 化。
*
* <p><b>读写时机判断(REST 入口仅放行查询类服务流):</b>当前服务流引擎本身不区分读写——
* 一个流既可挂 {@code listSelect}(读)也可挂 {@code save/update/delete}(写)service 节点,
* 引擎统一走 {@code excute}。本 P1 阶段不在门面做引擎级读写闸(避免误判与维护负担),
* 而由调用方/运维按「服务流定义 XML 审阅」确认其为查询/报表类后才经此入口放行;
* 客户库只读纪律下,仅执行确认无写动作的流(挑选过程见 P1 服务流实测报告)。
* <b>风险与 P2 防护待办</b>:引擎不区分读写 ⇒ 该入口理论上可触发写流。P2 需补
* 「流定义静态扫描(service 节点 method 白名单:仅 listSelect/loadPOJO/executeQueryForMap 等读方法放行)
* + 最小权限只读连接 + statement_timeout」三层防护(见架构设计第四节安全方案、迁移手册新增坑位)。
*/
@RestController
@RequestMapping("/api/flow")
public class FlowController {
private final FlowExcuteService flowExcuteService;
public FlowController(FlowExcuteService flowExcuteService) {
this.flowExcuteService = flowExcuteService;
}
/** GET 执行:query 参数透传为服务流入参 map(保留原始大小写键,交由流脚本/服务节点消费)。 */
@GetMapping("/{id}")
@Operation(summary = "执行服务流(GET):query 参数作为入参 map,返回流输出 JSON。报表 D 类查询入口")
public Map<String, Object> executeGet(@PathVariable String id,
@RequestParam Map<String, String> allParams) {
Map<String, Object> map = new HashMap<>(allParams);
return run(id, map);
}
/** POST 执行:JSON body 透传为服务流入参 map。body 为空时按空 map 执行。 */
@PostMapping("/{id}")
@Operation(summary = "执行服务流(POST):JSON body 作为入参 map,返回流输出 JSON")
public Map<String, Object> executePost(@PathVariable String id,
@RequestBody(required = false) Map<String, Object> body) {
Map<String, Object> map = body == null ? new HashMap<>() : new HashMap<>(body);
return run(id, map);
}
/**
* 统一执行 + 响应封装。引擎 {@code excute} 返回流的输出字段 Map;查询/报表类流的输出
* 通常含一个 {@code listSelect} 结果({@code {listresult, pagecount}}),本方法递归归一化:
* GeneralPOJO → 平面行 map;含 {@code listresult} 的子结果 → amis 风格 {@code {items,total}}。
*/
private Map<String, Object> run(String id, Map<String, Object> map) {
Properties ctx = UserSession.getAdmin();
Object output = flowExcuteService.excute(map, ctx, id);
Map<String, Object> resp = new HashMap<>();
resp.put("status", 0);
resp.put("msg", "");
resp.put("data", normalize(output));
return resp;
}
/** 递归归一化引擎返回值,使之可被 Jackson 序列化(GeneralPOJO 非 bean,需手工展平)。 */
@SuppressWarnings("unchecked")
private Object normalize(Object value) {
if (value instanceof GeneralPOJO p) {
return toRow(p);
}
if (value instanceof Map<?, ?> m) {
// listSelect 结果:{listresult:[...], pagecount:N} → amis {items, total}
if (m.containsKey(POJOCRUDService.LIST_RESULT)) {
Map<String, Object> data = new LinkedHashMap<>();
data.put("items", normalize(m.get(POJOCRUDService.LIST_RESULT)));
data.put("total", m.get(POJOCRUDService.PAGE_COUNT));
return data;
}
Map<String, Object> out = new LinkedHashMap<>();
for (Map.Entry<?, ?> e : m.entrySet()) {
out.put(String.valueOf(e.getKey()), normalize(e.getValue()));
}
return out;
}
if (value instanceof List<?> list) {
List<Object> out = new ArrayList<>(list.size());
for (Object o : list) {
out.add(normalize(o));
}
return out;
}
return value;
}
/** GeneralPOJO 值存于内部数组(非 bean getter),展开为平面 map 供 JSON 序列化。 */
private Map<String, Object> toRow(GeneralPOJO p) {
Map<String, Object> row = new LinkedHashMap<>();
row.put("_id", p.get_ID());
for (int i = 0; i < p.get_ColumnCount(); i++) {
row.put(p.get_ColumnName(i), p.get_Value(i));
}
return row;
}
}

View File

@@ -0,0 +1,182 @@
package com.eip4.platform.metadata.web;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import com.dsasp.server.common.core.service.FlowExcuteService;
import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
/**
* 服务流 REST 门面单测(mock 引擎,不连库)。
*
* <p>验证 {@link FlowController} 的入参绑定(GET query → map / POST body → map)与
* 响应归一化:GeneralPOJO → 平面行、listSelect 结果({listresult,pagecount})→
* amis {items,total}、嵌套结构递归归一化。这是 D 类报表服务流 REST 化契约的核心。
*/
class FlowControllerTest {
/** 构造一个最小 GeneralPOJO 桩(只实现 toRow 用到的方法)。 */
private GeneralPOJO fakeRow(String id, Map<String, Object> cols) {
GeneralPOJO p = mock(GeneralPOJO.class);
when(p.get_ID()).thenReturn(id);
List<String> names = new ArrayList<>(cols.keySet());
when(p.get_ColumnCount()).thenReturn(names.size());
for (int i = 0; i < names.size(); i++) {
when(p.get_ColumnName(i)).thenReturn(names.get(i));
when(p.get_Value(i)).thenReturn(cols.get(names.get(i)));
}
return p;
}
@Test
@DisplayName("1. GET 执行:query 参数透传为入参 map,flowId 透传给引擎")
void getPassesQueryParamsAsMap() {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), eq("EIP4M0_USERGROUP_SEARCH"))).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine);
Map<String, String> params = new HashMap<>();
params.put("NAME", "管理员组");
controller.executeGet("EIP4M0_USERGROUP_SEARCH", params);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq("EIP4M0_USERGROUP_SEARCH"));
assertEquals("管理员组", mapCap.getValue().get("NAME"), "query 参数应进入入参 map");
}
@Test
@DisplayName("2. POST 执行:JSON body 透传为入参 map")
void postPassesBodyAsMap() {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine);
Map<String, Object> body = new HashMap<>();
body.put("sql", "select 1");
controller.executePost("COMMON_SQL_QUERY", body);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(), eq("COMMON_SQL_QUERY"));
assertEquals("select 1", mapCap.getValue().get("sql"));
}
@Test
@DisplayName("3. POST body 为 null:按空 map 执行,不抛")
void postNullBodyTolerant() {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(null);
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executePost("F", null);
assertEquals(0, resp.get("status"));
}
@Test
@DisplayName("4. 响应外壳:status=0/msg/data 三段")
void responseEnvelope() {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executeGet("F", new HashMap<>());
assertEquals(0, resp.get("status"));
assertEquals("", resp.get("msg"));
assertEquals(true, resp.containsKey("data"));
}
@Test
@DisplayName("5. listSelect 结果归一化:{listresult,pagecount} → {items,total}")
void normalizeListSelectResult() {
// 引擎输出:output 字段 result = listSelect 结果 map
Map<String, Object> listResult = new LinkedHashMap<>();
List<GeneralPOJO> rows = new ArrayList<>();
Map<String, Object> c1 = new LinkedHashMap<>();
c1.put("NAME", "系统管理员组");
rows.add(fakeRow("b0000000-0000-0000-0000-000000000001", c1));
listResult.put(POJOCRUDService.LIST_RESULT, rows);
listResult.put(POJOCRUDService.PAGE_COUNT, 1);
Map<String, Object> output = new LinkedHashMap<>();
output.put("result", listResult);
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(output);
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executeGet("F", new HashMap<>());
@SuppressWarnings("unchecked")
Map<String, Object> data = (Map<String, Object>) resp.get("data");
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) data.get("result");
assertEquals(1, result.get("total"), "pagecount → total");
assertInstanceOf(List.class, result.get("items"), "listresult → items");
@SuppressWarnings("unchecked")
List<Map<String, Object>> items = (List<Map<String, Object>>) result.get("items");
assertEquals("系统管理员组", items.get(0).get("NAME"), "GeneralPOJO 行应展平");
assertEquals("b0000000-0000-0000-0000-000000000001", items.get(0).get("_id"));
}
@Test
@DisplayName("6. 单个 GeneralPOJO 输出归一化为平面行")
void normalizeSinglePojo() {
Map<String, Object> cols = new LinkedHashMap<>();
cols.put("NAME", "x");
GeneralPOJO row = fakeRow("id-1", cols);
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(row);
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executeGet("F", new HashMap<>());
@SuppressWarnings("unchecked")
Map<String, Object> data = (Map<String, Object>) resp.get("data");
assertEquals("id-1", data.get("_id"));
assertEquals("x", data.get("NAME"));
}
@Test
@DisplayName("7. List<GeneralPOJO> 直接输出:递归归一化为行列表")
void normalizeListOfPojo() {
Map<String, Object> cols = new LinkedHashMap<>();
cols.put("NAME", "y");
List<GeneralPOJO> list = List.of(fakeRow("id-2", cols));
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(list);
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executeGet("F", new HashMap<>());
@SuppressWarnings("unchecked")
List<Map<String, Object>> data = (List<Map<String, Object>>) resp.get("data");
assertEquals("y", data.get(0).get("NAME"));
}
@Test
@DisplayName("8. 标量输出原样返回(非 POJO/Map/List)")
void normalizeScalar() {
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(42);
FlowController controller = new FlowController(engine);
Map<String, Object> resp = controller.executeGet("F", new HashMap<>());
assertEquals(42, resp.get("data"));
}
}