refactor(ai): AI 本地发布裸写下沉到 AiReleaseWriter + 补值断言测试
A5 安全半步:把 AiLocalReleaseExecutor 里所有 sys_serviceflow/sys_menu 裸 JDBC(含 ensureAiMenuParent/tableExists/实体复用查询)收拢到新类 AiReleaseWriter,执行器只剩编排;SQL 逐字保留=零行为变更。 新增 AiReleaseWriterTest 4 值断言(serviceflow 审计/?::xml/bool→int、菜单 INSERT 全列值、UPDATE 全字段、自动建「AI Agent」目录),补上原黄金测试只查表名的宽松缺口。 platform-ai 30/30 绿(黄金测试经搬迁仍绿=行为未变)。与人走五环 MenuRegistrar 的完整统一(两者已分叉)留作专项,见总览 §10 A5。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -197,7 +197,7 @@ EIP3(Vaadin / 老引擎)→ EIP4(amis 前端 + 保真层 + 转换器)迁
|
|||||||
- **测试金字塔倒置 · high · L**:根目录 71 png + 前端 121 png 人工 dogfooding 是主回归手段;前端 vitest 仅 17 个;3 套 e2e config 故意不配 webServer(复用已跑环境)无法干净 CI 跑起。→ 关键闭环固化成自启动可进 CI 的稳定 e2e;扩 vitest 把「翻译正确但运行时哑火」下沉到单测。
|
- **测试金字塔倒置 · high · L**:根目录 71 png + 前端 121 png 人工 dogfooding 是主回归手段;前端 vitest 仅 17 个;3 套 e2e config 故意不配 webServer(复用已跑环境)无法干净 CI 跑起。→ 关键闭环固化成自启动可进 CI 的稳定 e2e;扩 vitest 把「翻译正确但运行时哑火」下沉到单测。
|
||||||
|
|
||||||
**【可维护性核心】**
|
**【可维护性核心】**
|
||||||
- **【采纳自原文档 A5】AiLocalReleaseExecutor 裸写 sys_serviceflow/sys_menu · high · M**:`:126-148` 裸 INSERT serviceflow、`:181-211` 裸写 menu,仅 `sys_page_schema` 收口到 `PageSchemaWriter`(`:154-159`)。→ 复用已有 `MenuRegistrar` + 新建 serviceflow writer,令其走受控写入口(与 M3a 单一写真相纪律齐平)。
|
- **🟡【已完成安全半步 2026-06-24】【采纳自原文档 A5】AiLocalReleaseExecutor 裸写 sys_serviceflow/sys_menu**:**已做**——把执行器里所有裸 JDBC(sys_serviceflow UPSERT + sys_menu INSERT/UPDATE + ensureAiMenuParent + tableExists + 实体复用查询)下沉到新类 `platform-ai/.../service/AiReleaseWriter`,执行器只剩编排不再直接碰库;SQL 逐字保留(纯搬迁零行为变更)。**顺带补缺口**:新增 `AiReleaseWriterTest`(4 值断言:serviceflow 审计/`?::xml`/bool→int、菜单 INSERT 全列值、UPDATE 全字段、自动建「AI Agent」目录)——补上原 `AiLocalReleaseExecutorTest` 只查「SQL 含表名」的宽松缺口。**验证**:platform-ai 30/30 绿(黄金测试经搬迁仍绿=行为未变)。**未做(推迟为专项)**:与人走五环 `MenuRegistrar` 统一成单一写器——调查发现两者已实质分叉(本类 UPDATE 全字段 + operator 字面量 'AI_AGENT' + 顶级目录「AI Agent」vs MenuRegistrar 窄 UPDATE + `?` operator + 「新功能」),且各有钉死测试,真统一需重写两路测试 + 重验两条红线邻近路径(建议 live e2e),风险/工作量超「快速增量」,留作 A5 完整版专项。
|
||||||
- **✅【已完成 2026-06-24】【采纳自原文档 A4】ArchUnit 规则扩充**:`ArchitectureRulesTest.java` 从 1 条扩到 **6 条**——3 条强制(保真层禁依赖现代层 / 库模块[converter·parity]不得依赖应用编排层 / 模块切片 `beFreeOfCycles` 无环)+ 3 条**棘轮**(控制器不得新增直用 `JdbcTemplate` / 不得新增 Controller→Controller 互注入 / 不得新增 `Poc*` 调试控制器)。棘轮以「已知债务清单」基线化存量(JdbcTemplate 26 个 / 互注入 4 个 / Poc 3 个),只挡新增、清单即待偿台账。**验证**:`mvn -pl platform-app test -Dtest=ArchitectureRulesTest` 6/6 绿;并以临时探针走完整 RED→GREEN(探针触发恰好 3 条棘轮失败、强制规则不受影响,证明非空绿)。**一处刻意偏离原文档**:未加「禁控制器调 `POJOCRUDService`/引擎」——因 `com.eip4.*→com.dsasp.*` 是双层制明确允许(现代层=保真层薄门面),真正的坏味道是控制器直拼原始 SQL(已由 JdbcTemplate 棘轮覆盖)。**后续**:随 A1/A3 把控制器 SQL 收口到 Service 后,从清单删项,清空即把棘轮升为强制规则。
|
- **✅【已完成 2026-06-24】【采纳自原文档 A4】ArchUnit 规则扩充**:`ArchitectureRulesTest.java` 从 1 条扩到 **6 条**——3 条强制(保真层禁依赖现代层 / 库模块[converter·parity]不得依赖应用编排层 / 模块切片 `beFreeOfCycles` 无环)+ 3 条**棘轮**(控制器不得新增直用 `JdbcTemplate` / 不得新增 Controller→Controller 互注入 / 不得新增 `Poc*` 调试控制器)。棘轮以「已知债务清单」基线化存量(JdbcTemplate 26 个 / 互注入 4 个 / Poc 3 个),只挡新增、清单即待偿台账。**验证**:`mvn -pl platform-app test -Dtest=ArchitectureRulesTest` 6/6 绿;并以临时探针走完整 RED→GREEN(探针触发恰好 3 条棘轮失败、强制规则不受影响,证明非空绿)。**一处刻意偏离原文档**:未加「禁控制器调 `POJOCRUDService`/引擎」——因 `com.eip4.*→com.dsasp.*` 是双层制明确允许(现代层=保真层薄门面),真正的坏味道是控制器直拼原始 SQL(已由 JdbcTemplate 棘轮覆盖)。**后续**:随 A1/A3 把控制器 SQL 收口到 Service 后,从清单删项,清空即把棘轮升为强制规则。
|
||||||
- **【采纳自原文档 D1】render-plan 无执行闭环 · high · M**:`AiAgentService.validateRenderPlanGate:987-1085` 纯字段校验,Playwright 全是注释,`READY_FOR_HUMAN_REVIEW` 不含渲染证据。→ 新增 headless render runner 真跑 renderTests 后回写 `runEvidence`。
|
- **【采纳自原文档 D1】render-plan 无执行闭环 · high · M**:`AiAgentService.validateRenderPlanGate:987-1085` 纯字段校验,Playwright 全是注释,`READY_FOR_HUMAN_REVIEW` 不含渲染证据。→ 新增 headless render runner 真跑 renderTests 后回写 `runEvidence`。
|
||||||
- **AiAgentService(1846 LOC)SRP 严重违反 · high · L**:~28 public 入口 + 内联 9+ `validate*Gate` + 部署/回滚 SQL 生成 + 运行记录映射揉一类。→ 门禁抽 `AiDraftGateValidator`、SQL 抽 `AiDeploymentSqlBuilder`、持久化抽 `AiRunStore`,主类收敛为薄编排门面。
|
- **AiAgentService(1846 LOC)SRP 严重违反 · high · L**:~28 public 入口 + 内联 9+ `validate*Gate` + 部署/回滚 SQL 生成 + 运行记录映射揉一类。→ 门禁抽 `AiDraftGateValidator`、SQL 抽 `AiDeploymentSqlBuilder`、持久化抽 `AiRunStore`,主类收敛为薄编排门面。
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
@@ -18,24 +17,21 @@ import org.springframework.web.server.ResponseStatusException;
|
|||||||
import com.eip4.platform.ai.model.AiApplyStepResult;
|
import com.eip4.platform.ai.model.AiApplyStepResult;
|
||||||
import com.eip4.platform.ai.model.AiArtifactType;
|
import com.eip4.platform.ai.model.AiArtifactType;
|
||||||
import com.eip4.platform.ai.model.AiDraft;
|
import com.eip4.platform.ai.model.AiDraft;
|
||||||
import com.eip4.platform.metadata.schema.PageSchemaWriter;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class AiLocalReleaseExecutor {
|
public class AiLocalReleaseExecutor {
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
private final AiReleaseWriter writer;
|
||||||
private final AiSmokeHttpClient httpClient;
|
private final AiSmokeHttpClient httpClient;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final PageSchemaWriter pageSchemaWriter;
|
|
||||||
|
|
||||||
public AiLocalReleaseExecutor(JdbcTemplate jdbcTemplate, AiSmokeHttpClient httpClient, ObjectMapper objectMapper) {
|
public AiLocalReleaseExecutor(JdbcTemplate jdbcTemplate, AiSmokeHttpClient httpClient, ObjectMapper objectMapper) {
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
// A5 收口:所有 sys_serviceflow/sys_menu 裸写下沉到 AiReleaseWriter(页面 schema 仍经其内的 PageSchemaWriter)
|
||||||
|
this.writer = new AiReleaseWriter(jdbcTemplate);
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
// M3a 收口:标准层页面 schema 写统一走 PageSchemaWriter(与设计中心新建端点共用单一写真相)
|
|
||||||
this.pageSchemaWriter = new PageSchemaWriter(jdbcTemplate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AiApplyStepResult> apply(String baseUrl, List<AiDraft> drafts) {
|
public List<AiApplyStepResult> apply(String baseUrl, List<AiDraft> drafts) {
|
||||||
@@ -93,9 +89,7 @@ public class AiLocalReleaseExecutor {
|
|||||||
if (root.path("status").asInt(1) != 0) {
|
if (root.path("status").asInt(1) != 0) {
|
||||||
String message = root.path("msg").asText("unknown");
|
String message = root.path("msg").asText("unknown");
|
||||||
if (message.contains("实体已存在")) {
|
if (message.contains("实体已存在")) {
|
||||||
List<String> ids = jdbcTemplate.queryForList(
|
List<String> ids = writer.findActiveEntityIdsByTable(tableName);
|
||||||
"SELECT TRIM(sys_entity_id) FROM sys_entity WHERE tablename=? AND isactive=1",
|
|
||||||
String.class, tableName);
|
|
||||||
if (!ids.isEmpty()) {
|
if (!ids.isEmpty()) {
|
||||||
ctx.entityId(ids.get(0));
|
ctx.entityId(ids.get(0));
|
||||||
ctx.tableName(tableName);
|
ctx.tableName(tableName);
|
||||||
@@ -123,28 +117,13 @@ public class AiLocalReleaseExecutor {
|
|||||||
for (JsonNode flow : draft.payload().path("flows")) {
|
for (JsonNode flow : draft.payload().path("flows")) {
|
||||||
String key = flow.path("key").asText();
|
String key = flow.path("key").asText();
|
||||||
String xml = flow.path("contentXml").asText().replace("${entityId}", ctx.entityId());
|
String xml = flow.path("contentXml").asText().replace("${entityId}", ctx.entityId());
|
||||||
jdbcTemplate.update("""
|
writer.upsertServiceFlow(
|
||||||
INSERT INTO sys_serviceflow
|
|
||||||
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
|
|
||||||
isactive, createtime, updatetime, createby, updateby, version)
|
|
||||||
VALUES (?, ?, ?::xml, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
|
|
||||||
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
|
|
||||||
displayname = EXCLUDED.displayname,
|
|
||||||
contentxml = EXCLUDED.contentxml,
|
|
||||||
entityid = EXCLUDED.entityid,
|
|
||||||
iscontrol = EXCLUDED.iscontrol,
|
|
||||||
isupdateable = EXCLUDED.isupdateable,
|
|
||||||
updatetime = EXCLUDED.updatetime,
|
|
||||||
updateby = EXCLUDED.updateby,
|
|
||||||
version = sys_serviceflow.version + 1
|
|
||||||
""",
|
|
||||||
key,
|
key,
|
||||||
flow.path("displayName").asText(key),
|
flow.path("displayName").asText(key),
|
||||||
xml,
|
xml,
|
||||||
ctx.entityId(),
|
ctx.entityId(),
|
||||||
flow.path("control").asBoolean(true) ? 1 : 0,
|
flow.path("control").asBoolean(true),
|
||||||
flow.path("updateable").asBoolean(true) ? 1 : 0,
|
flow.path("updateable").asBoolean(true),
|
||||||
now,
|
|
||||||
now);
|
now);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
@@ -155,106 +134,23 @@ public class AiLocalReleaseExecutor {
|
|||||||
// M3a 收口:不再裸 INSERT sys_page_schema;走 PageSchemaWriter 唯一写真相(typed 校验告警 + etag + UPSERT)
|
// M3a 收口:不再裸 INSERT sys_page_schema;走 PageSchemaWriter 唯一写真相(typed 校验告警 + etag + UPSERT)
|
||||||
String schemaJson = objectMapper.writeValueAsString(draft.payload());
|
String schemaJson = objectMapper.writeValueAsString(draft.payload());
|
||||||
String name = draft.payload().path("title").asText(draft.targetId());
|
String name = draft.payload().path("title").asText(draft.targetId());
|
||||||
String etag = pageSchemaWriter.upsertStandard(draft.targetId(), name, schemaJson, "AI_AGENT");
|
String etag = writer.upsertPageSchema(draft.targetId(), name, schemaJson);
|
||||||
return applied(index, draft, "页面 schema 已发布", Map.of("templateId", draft.targetId(), "etag", etag));
|
return applied(index, draft, "页面 schema 已发布", Map.of("templateId", draft.targetId(), "etag", etag));
|
||||||
}
|
}
|
||||||
|
|
||||||
private AiApplyStepResult applyMenu(ApplyContext ctx, AiDraft draft, int index) {
|
private AiApplyStepResult applyMenu(ApplyContext ctx, AiDraft draft, int index) {
|
||||||
if (!tableExists("sys_menu")) {
|
if (!writer.tableExists("sys_menu")) {
|
||||||
return skipped(index, draft, "本地库缺少 sys_menu,菜单挂载跳过,部署包保留人工 SQL");
|
return skipped(index, draft, "本地库缺少 sys_menu,菜单挂载跳过,部署包保留人工 SQL");
|
||||||
}
|
}
|
||||||
String templateId = draft.payload().path("viewTemplateId").asText(draft.targetId());
|
String templateId = draft.payload().path("viewTemplateId").asText(draft.targetId());
|
||||||
String menuName = draft.payload().path("name").asText(templateId);
|
String menuName = draft.payload().path("name").asText(templateId);
|
||||||
String parentId = textOrBlank(draft.payload().get("parentId"));
|
String parentId = textOrBlank(draft.payload().get("parentId"));
|
||||||
String menuRoot = textOrBlank(draft.payload().get("menuroot"));
|
String menuRoot = textOrBlank(draft.payload().get("menuroot"));
|
||||||
if (parentId.isBlank()) {
|
String typename = draft.payload().path("typename").asText("功能菜单");
|
||||||
ParentMenu parent = ensureAiMenuParent(menuRoot);
|
int seq = draft.payload().path("seq").asInt(0);
|
||||||
parentId = parent.parentId();
|
AiReleaseWriter.MenuWrite mw = writer.upsertMenu(templateId, menuName, parentId, menuRoot, typename, seq,
|
||||||
menuRoot = parent.menuRoot();
|
System.currentTimeMillis());
|
||||||
}
|
return applied(index, draft, "菜单已挂载", Map.of("menuId", mw.menuId(), "parentId", mw.parentId()));
|
||||||
List<String> existing = jdbcTemplate.queryForList(
|
|
||||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1",
|
|
||||||
String.class, templateId);
|
|
||||||
String menuId = existing.isEmpty() ? UUID.randomUUID().toString() : existing.get(0);
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
if (existing.isEmpty()) {
|
|
||||||
jdbcTemplate.update("""
|
|
||||||
INSERT INTO sys_menu
|
|
||||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
|
||||||
isactive, version, createtime, createby, updatetime, updateby)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
|
||||||
""",
|
|
||||||
menuId,
|
|
||||||
menuName,
|
|
||||||
parentId,
|
|
||||||
templateId,
|
|
||||||
draft.payload().path("typename").asText("功能菜单"),
|
|
||||||
menuRoot.isBlank() ? null : menuRoot,
|
|
||||||
draft.payload().path("seq").asInt(0),
|
|
||||||
now,
|
|
||||||
now);
|
|
||||||
} else {
|
|
||||||
jdbcTemplate.update("""
|
|
||||||
UPDATE sys_menu
|
|
||||||
SET name=?, parentid=?, view_templateid=?, typename=?, menuroot=?, seq=?,
|
|
||||||
updatetime=?, updateby='AI_AGENT', version=version+1
|
|
||||||
WHERE TRIM(sys_menu_id)=?
|
|
||||||
""",
|
|
||||||
menuName,
|
|
||||||
parentId,
|
|
||||||
templateId,
|
|
||||||
draft.payload().path("typename").asText("功能菜单"),
|
|
||||||
menuRoot.isBlank() ? null : menuRoot,
|
|
||||||
draft.payload().path("seq").asInt(0),
|
|
||||||
now,
|
|
||||||
menuId);
|
|
||||||
}
|
|
||||||
return applied(index, draft, "菜单已挂载", Map.of("menuId", menuId, "parentId", parentId));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ParentMenu ensureAiMenuParent(String preferredRoot) {
|
|
||||||
List<String> existingDir = jdbcTemplate.queryForList(
|
|
||||||
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='AI Agent' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1",
|
|
||||||
String.class);
|
|
||||||
if (!existingDir.isEmpty()) {
|
|
||||||
String root = preferredRoot;
|
|
||||||
if (root == null || root.isBlank()) {
|
|
||||||
List<String> roots = jdbcTemplate.queryForList(
|
|
||||||
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?",
|
|
||||||
String.class, existingDir.get(0));
|
|
||||||
root = roots.isEmpty() ? "" : roots.get(0);
|
|
||||||
}
|
|
||||||
return new ParentMenu(existingDir.get(0), root == null ? "" : root);
|
|
||||||
}
|
|
||||||
String root = preferredRoot;
|
|
||||||
if (root == null || root.isBlank()) {
|
|
||||||
List<String> roots = jdbcTemplate.queryForList(
|
|
||||||
"SELECT TRIM(sys_menu_root_id) FROM sys_menu_root WHERE isactive=1 ORDER BY seq LIMIT 1",
|
|
||||||
String.class);
|
|
||||||
if (roots.isEmpty()) {
|
|
||||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "缺少 sys_menu_root,不能自动挂载菜单");
|
|
||||||
}
|
|
||||||
root = roots.get(0);
|
|
||||||
}
|
|
||||||
String dirId = UUID.randomUUID().toString();
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
jdbcTemplate.update("""
|
|
||||||
INSERT INTO sys_menu
|
|
||||||
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
|
||||||
isactive, version, createtime, createby, updatetime, updateby)
|
|
||||||
VALUES (?, 'AI Agent', NULL, NULL, '目录', ?, 9999, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
|
||||||
""",
|
|
||||||
dirId, root, now, now);
|
|
||||||
return new ParentMenu(dirId, root);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean tableExists(String tableName) {
|
|
||||||
try {
|
|
||||||
Boolean exists = jdbcTemplate.queryForObject("SELECT to_regclass(?) IS NOT NULL", Boolean.class, tableName);
|
|
||||||
return Boolean.TRUE.equals(exists);
|
|
||||||
} catch (RuntimeException ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static AiApplyStepResult applied(int index, AiDraft draft, String message, Map<String, Object> details) {
|
private static AiApplyStepResult applied(int index, AiDraft draft, String message, Map<String, Object> details) {
|
||||||
@@ -341,6 +237,4 @@ public class AiLocalReleaseExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private record ParentMenu(String parentId, String menuRoot) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,157 @@
|
|||||||
|
package com.eip4.platform.ai.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import com.eip4.platform.metadata.schema.PageSchemaWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AI 本地发布的<strong>受控写真相</strong>(总览 §10 A5)。
|
||||||
|
*
|
||||||
|
* <p>把原先散在 {@link AiLocalReleaseExecutor} 里的裸 {@code JdbcTemplate} 写
|
||||||
|
* (sys_serviceflow / sys_menu)收拢到本类,使执行器只管编排、不直接碰库;
|
||||||
|
* 标准层页面 schema 继续走 {@link PageSchemaWriter}(M3a 单一写真相)。
|
||||||
|
*
|
||||||
|
* <p>SQL 与原执行器逐字一致(纯搬迁、零行为变更)。菜单挂载逻辑刻意<em>未</em>复用
|
||||||
|
* 人走五环的 {@code MenuRegistrar}:两者已分叉(本类 UPDATE 全字段 + operator 字面量
|
||||||
|
* 'AI_AGENT' + 顶级目录「AI Agent」),统一为单一写器属后续专项(见 §10 A5)。
|
||||||
|
*/
|
||||||
|
class AiReleaseWriter {
|
||||||
|
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final PageSchemaWriter pageSchemaWriter;
|
||||||
|
|
||||||
|
AiReleaseWriter(JdbcTemplate jdbcTemplate) {
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
this.pageSchemaWriter = new PageSchemaWriter(jdbcTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 物理表是否存在(本地库可能缺 sys_menu 等)。 */
|
||||||
|
boolean tableExists(String tableName) {
|
||||||
|
try {
|
||||||
|
Boolean exists = jdbcTemplate.queryForObject("SELECT to_regclass(?) IS NOT NULL", Boolean.class, tableName);
|
||||||
|
return Boolean.TRUE.equals(exists);
|
||||||
|
} catch (RuntimeException ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 实体「已存在」时按表名查回既有 sys_entity_id(发布幂等)。 */
|
||||||
|
List<String> findActiveEntityIdsByTable(String tableName) {
|
||||||
|
return jdbcTemplate.queryForList(
|
||||||
|
"SELECT TRIM(sys_entity_id) FROM sys_entity WHERE tablename=? AND isactive=1",
|
||||||
|
String.class, tableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** UPSERT 单条服务流(主键冲突则更新 + version+1);createby/updateby 恒 'AI_AGENT'。 */
|
||||||
|
void upsertServiceFlow(String key, String displayName, String contentXml, String entityId,
|
||||||
|
boolean control, boolean updateable, long now) {
|
||||||
|
jdbcTemplate.update("""
|
||||||
|
INSERT INTO sys_serviceflow
|
||||||
|
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
|
||||||
|
isactive, createtime, updatetime, createby, updateby, version)
|
||||||
|
VALUES (?, ?, ?::xml, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
|
||||||
|
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
|
||||||
|
displayname = EXCLUDED.displayname,
|
||||||
|
contentxml = EXCLUDED.contentxml,
|
||||||
|
entityid = EXCLUDED.entityid,
|
||||||
|
iscontrol = EXCLUDED.iscontrol,
|
||||||
|
isupdateable = EXCLUDED.isupdateable,
|
||||||
|
updatetime = EXCLUDED.updatetime,
|
||||||
|
updateby = EXCLUDED.updateby,
|
||||||
|
version = sys_serviceflow.version + 1
|
||||||
|
""",
|
||||||
|
key, displayName, contentXml, entityId,
|
||||||
|
control ? 1 : 0, updateable ? 1 : 0, now, now);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 标准层页面 schema 写,委托 PageSchemaWriter(typed 校验告警 + etag + UPSERT)。返回 etag。 */
|
||||||
|
String upsertPageSchema(String targetId, String name, String schemaJson) {
|
||||||
|
return pageSchemaWriter.upsertStandard(targetId, name, schemaJson, "AI_AGENT");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 挂载/更新功能菜单:dedup by view_templateid(命中走 UPDATE 全字段),parentId 为空则
|
||||||
|
* 自动 ensure/复用顶级目录「AI Agent」。返回最终 menuId 与 parentId。
|
||||||
|
*/
|
||||||
|
MenuWrite upsertMenu(String templateId, String menuName, String parentId, String menuRoot,
|
||||||
|
String typename, int seq, long now) {
|
||||||
|
String pid = parentId;
|
||||||
|
String root = menuRoot;
|
||||||
|
if (pid == null || pid.isBlank()) {
|
||||||
|
ParentMenu parent = ensureAiMenuParent(root);
|
||||||
|
pid = parent.parentId();
|
||||||
|
root = parent.menuRoot();
|
||||||
|
}
|
||||||
|
List<String> existing = jdbcTemplate.queryForList(
|
||||||
|
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1",
|
||||||
|
String.class, templateId);
|
||||||
|
String menuId = existing.isEmpty() ? UUID.randomUUID().toString() : existing.get(0);
|
||||||
|
Object rootArg = (root == null || root.isBlank()) ? null : root;
|
||||||
|
if (existing.isEmpty()) {
|
||||||
|
jdbcTemplate.update("""
|
||||||
|
INSERT INTO sys_menu
|
||||||
|
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||||
|
isactive, version, createtime, createby, updatetime, updateby)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||||
|
""",
|
||||||
|
menuId, menuName, pid, templateId, typename, rootArg, seq, now, now);
|
||||||
|
} else {
|
||||||
|
jdbcTemplate.update("""
|
||||||
|
UPDATE sys_menu
|
||||||
|
SET name=?, parentid=?, view_templateid=?, typename=?, menuroot=?, seq=?,
|
||||||
|
updatetime=?, updateby='AI_AGENT', version=version+1
|
||||||
|
WHERE TRIM(sys_menu_id)=?
|
||||||
|
""",
|
||||||
|
menuName, pid, templateId, typename, rootArg, seq, now, menuId);
|
||||||
|
}
|
||||||
|
return new MenuWrite(menuId, pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ParentMenu ensureAiMenuParent(String preferredRoot) {
|
||||||
|
List<String> existingDir = jdbcTemplate.queryForList(
|
||||||
|
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='AI Agent' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1",
|
||||||
|
String.class);
|
||||||
|
if (!existingDir.isEmpty()) {
|
||||||
|
String root = preferredRoot;
|
||||||
|
if (root == null || root.isBlank()) {
|
||||||
|
List<String> roots = jdbcTemplate.queryForList(
|
||||||
|
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?",
|
||||||
|
String.class, existingDir.get(0));
|
||||||
|
root = roots.isEmpty() ? "" : roots.get(0);
|
||||||
|
}
|
||||||
|
return new ParentMenu(existingDir.get(0), root == null ? "" : root);
|
||||||
|
}
|
||||||
|
String root = preferredRoot;
|
||||||
|
if (root == null || root.isBlank()) {
|
||||||
|
List<String> roots = jdbcTemplate.queryForList(
|
||||||
|
"SELECT TRIM(sys_menu_root_id) FROM sys_menu_root WHERE isactive=1 ORDER BY seq LIMIT 1",
|
||||||
|
String.class);
|
||||||
|
if (roots.isEmpty()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.CONFLICT, "缺少 sys_menu_root,不能自动挂载菜单");
|
||||||
|
}
|
||||||
|
root = roots.get(0);
|
||||||
|
}
|
||||||
|
String dirId = UUID.randomUUID().toString();
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
jdbcTemplate.update("""
|
||||||
|
INSERT INTO sys_menu
|
||||||
|
(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,
|
||||||
|
isactive, version, createtime, createby, updatetime, updateby)
|
||||||
|
VALUES (?, 'AI Agent', NULL, NULL, '目录', ?, 9999, 1, 0, ?, 'AI_AGENT', ?, 'AI_AGENT')
|
||||||
|
""",
|
||||||
|
dirId, root, now, now);
|
||||||
|
return new ParentMenu(dirId, root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 菜单写结果:最终 menuId 与 parentId。 */
|
||||||
|
record MenuWrite(String menuId, String parentId) {
|
||||||
|
}
|
||||||
|
|
||||||
|
private record ParentMenu(String parentId, String menuRoot) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package com.eip4.platform.ai.service;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.ArgumentMatchers.contains;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AiReleaseWriter 值断言单测(总览 §10 A5):钉死 sys_serviceflow / sys_menu 的<em>列值</em>,
|
||||||
|
* 补上 AiLocalReleaseExecutorTest 只查「SQL 含表名」的宽松缺口,使 A5 搬迁可证行为无变。
|
||||||
|
*/
|
||||||
|
class AiReleaseWriterTest {
|
||||||
|
|
||||||
|
private static final String DEDUP_SQL =
|
||||||
|
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE TRIM(view_templateid)=? AND isactive=1";
|
||||||
|
private static final String DIR_SQL =
|
||||||
|
"SELECT TRIM(sys_menu_id) FROM sys_menu WHERE name='AI Agent' AND parentid IS NULL AND isactive=1 ORDER BY seq LIMIT 1";
|
||||||
|
private static final String ROOT_OF_DIR_SQL =
|
||||||
|
"SELECT TRIM(menuroot) FROM sys_menu WHERE TRIM(sys_menu_id)=?";
|
||||||
|
private static final String MENU_ROOT_TABLE_SQL =
|
||||||
|
"SELECT TRIM(sys_menu_root_id) FROM sys_menu_root WHERE isactive=1 ORDER BY seq LIMIT 1";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("服务流 UPSERT:?::xml 转型 + bool→int + 审计恒 'AI_AGENT' + 同批 now")
|
||||||
|
void serviceFlowUpsertWritesExactValues() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
AiReleaseWriter w = new AiReleaseWriter(jdbc);
|
||||||
|
|
||||||
|
w.upsertServiceFlow("FLOW_ADD", "新增流", "<flow/>", "e-1", true, false, 123L);
|
||||||
|
|
||||||
|
ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(jdbc, times(1)).update(sql.capture(),
|
||||||
|
eq("FLOW_ADD"), eq("新增流"), eq("<flow/>"), eq("e-1"), eq(1), eq(0), eq(123L), eq(123L));
|
||||||
|
assertTrue(sql.getValue().contains("INSERT INTO sys_serviceflow"));
|
||||||
|
assertTrue(sql.getValue().contains("?::xml"));
|
||||||
|
assertTrue(sql.getValue().contains("ON CONFLICT (sys_serviceflow_id) DO UPDATE"));
|
||||||
|
assertTrue(sql.getValue().contains("'AI_AGENT'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("菜单首挂:dedup 空 → INSERT 全列值正确,parentId=既有「AI Agent」目录")
|
||||||
|
void menuInsertUnderExistingAiDir() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
when(jdbc.queryForList(eq(DEDUP_SQL), eq(String.class), eq("TPL"))).thenReturn(List.of());
|
||||||
|
when(jdbc.queryForList(eq(DIR_SQL), eq(String.class))).thenReturn(List.of("DIR-AI"));
|
||||||
|
when(jdbc.queryForList(eq(ROOT_OF_DIR_SQL), eq(String.class), eq("DIR-AI"))).thenReturn(List.of("ROOT"));
|
||||||
|
AiReleaseWriter w = new AiReleaseWriter(jdbc);
|
||||||
|
|
||||||
|
AiReleaseWriter.MenuWrite mw = w.upsertMenu("TPL", "菜单名", "", "", "功能菜单", 0, 999L);
|
||||||
|
|
||||||
|
assertEquals("DIR-AI", mw.parentId());
|
||||||
|
assertTrue(mw.menuId() != null && !mw.menuId().isBlank());
|
||||||
|
ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(jdbc, times(1)).update(sql.capture(),
|
||||||
|
eq(mw.menuId()), eq("菜单名"), eq("DIR-AI"), eq("TPL"), eq("功能菜单"), eq("ROOT"),
|
||||||
|
eq(0), eq(999L), eq(999L));
|
||||||
|
assertTrue(sql.getValue().contains("INSERT INTO sys_menu"));
|
||||||
|
assertTrue(sql.getValue().contains("'AI_AGENT'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("菜单重挂:dedup 命中 → 走 UPDATE 全字段(含 parentid/typename/menuroot/seq,区别于人路径窄更新)")
|
||||||
|
void menuUpdateWritesAllFields() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
when(jdbc.queryForList(eq(DEDUP_SQL), eq(String.class), eq("TPL"))).thenReturn(List.of("MENU-X"));
|
||||||
|
AiReleaseWriter w = new AiReleaseWriter(jdbc);
|
||||||
|
|
||||||
|
AiReleaseWriter.MenuWrite mw = w.upsertMenu("TPL", "改名", "P-1", "R-1", "功能菜单", 5, 999L);
|
||||||
|
|
||||||
|
assertEquals("MENU-X", mw.menuId());
|
||||||
|
assertEquals("P-1", mw.parentId());
|
||||||
|
ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
|
||||||
|
verify(jdbc, times(1)).update(sql.capture(),
|
||||||
|
eq("改名"), eq("P-1"), eq("TPL"), eq("功能菜单"), eq("R-1"), eq(5), eq(999L), eq("MENU-X"));
|
||||||
|
assertTrue(sql.getValue().contains("UPDATE sys_menu"));
|
||||||
|
assertTrue(sql.getValue().contains("version=version+1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("无「AI Agent」目录时自动建之(typename '目录' / seq 9999 / 取首个 menu_root)")
|
||||||
|
void ensureCreatesAiDirWhenMissing() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
when(jdbc.queryForList(eq(DEDUP_SQL), eq(String.class), eq("TPL"))).thenReturn(List.of());
|
||||||
|
when(jdbc.queryForList(eq(DIR_SQL), eq(String.class))).thenReturn(List.of());
|
||||||
|
when(jdbc.queryForList(eq(MENU_ROOT_TABLE_SQL), eq(String.class))).thenReturn(List.of("ROOT-9"));
|
||||||
|
AiReleaseWriter w = new AiReleaseWriter(jdbc);
|
||||||
|
|
||||||
|
AiReleaseWriter.MenuWrite mw = w.upsertMenu("TPL", "菜单名", "", "", "功能菜单", 0, 999L);
|
||||||
|
|
||||||
|
// 目录 INSERT:'AI Agent' 字面量 + root='ROOT-9'(dirId/now 为生成值)
|
||||||
|
verify(jdbc, times(1)).update(contains("'AI Agent'"), anyString(), eq("ROOT-9"), anyLong(), anyLong());
|
||||||
|
// 菜单挂到新建目录下(parentId=新 dirId,非空)
|
||||||
|
assertTrue(mw.parentId() != null && !mw.parentId().isBlank());
|
||||||
|
verify(jdbc, times(1)).update(contains("INSERT INTO sys_menu"),
|
||||||
|
eq(mw.menuId()), eq("菜单名"), eq(mw.parentId()), eq("TPL"), eq("功能菜单"), eq("ROOT-9"),
|
||||||
|
eq(0), eq(999L), eq(999L));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user