P4-6: platform-flow-gateway 待办→业务页映射(tablename→templateId→pageUrl)

- 侦察口径:sys_entity+view_templateentity+view_template 两步映射;
  多模板优先级 type=7(表单)>1(列表)>其余升序,同 type 按 templateid 字典序取首个
- 新建 PageTemplateService:findTemplateId() 查只读客户库,
  @Cacheable("pageTemplateByTable") Caffeine TTL=10min/maxSize=2000
- FlowGatewayConfig 启用 @EnableCaching + flowGatewayCacheManager(Caffeine)
- TodoItem 增加 pageTemplateId/pageUrl/pageUrlReason;
  查不到映射时 pageUrl=null+reason,前端据此禁用跳转(优雅降级)
- TodoAggregator 注入 PageTemplateService,aggregate 逐条 fillPageTemplate
- PageTemplateMappingTest 10 单测;TodoAggregatorTest 补 @Mock PageTemplateService
- 验证:真实用户(829cdcca)3 条待办带出 templateId=7042cb37(PM_SAFE_CLARIFICATION/安全技术交底),
  与客户库直查一致;全反应堆 31+ArchUnit 绿

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 16:52:50 +08:00
parent f563b472e6
commit ba6e7559fa
8 changed files with 498 additions and 4 deletions

View File

@@ -182,3 +182,103 @@ platform-app 进程存活(引擎宕机不拖垮单体,明确 503 语义而非 50
- 验证完成后 `kill` 本任务起的 8089 / 8090(8084 未动)。 - 验证完成后 `kill` 本任务起的 8089 / 8090(8084 未动)。
- 客户库零写入;本地 eip4m0 测试数据为可重复构造的自举工件(bootstrap-datastatus.sql)。 - 客户库零写入;本地 eip4m0 测试数据为可重复构造的自举工件(bootstrap-datastatus.sql)。
- eip4flow 迁移副本上 1 条真实 complete 已完整登记(实例 20334081 结束)。 - eip4flow 迁移副本上 1 条真实 complete 已完整登记(实例 20334081 结束)。
---
## P4-6 增补:待办→业务页映射(tablename → templateId → pageUrl)
> 日期:2026-06-11 | 任务:P4-6 —— `TodoItem` 增加 pageTemplateId/pageUrl 字段;
> 映射口径侦察;Caffeine 缓存;单测 ≥4;真实待办验证
### P4-6.1 映射规则结论(侦察客户库 view_templateentity + view_template + sys_entity)
**两步映射**:
1. `SYS_DATASTATUS.tablename` → `sys_entity.sys_entity_id`(entityid):`UPPER(tablename)` 归一大小写
2. `entityid` → `view_templateentity.templateid`(经 `view_template.type` 优先级筛选,取最优一条)
**多模板优先级规则**(`TemplateType` 枚举语义 + 待办填写场景):
| 优先级 | type | 名称 | 理由 |
|--------|------|------|------|
| 0(最优) | 7 | EDITFORM 表单模板 | 待办详情 = 填写/审批页面,表单模板最贴近 |
| 1 | 1 | GRID 列表模板 | 无专属表单时退回列表页 |
| 2–5 | 3/4/6/5 | 树/树列表/组合/日历 | 功能性模板,语义次之 |
| 6–7 | 100/101 | 弹窗选择 | 非主页面,兜底 |
| 8 | 200 | 报表模板 | 报表展示,非流程业务页 |
同 type 多条时按 `templateid` 字典序取首个(稳定可重复)。
**数据佐证**(客户库直查):
- `OA_SEALUSEAPPLY`:有 type=7「填写印章使用申请」→ 优先选 type=7 ✅
- `PM_SAFE_CLARIFICATION`:仅 type=1「安全技术交底」→ 退回 type=1 ✅
- `PM_CONTRACTACCOUNTS`:仅 type=1「项目分包合同决算」→ type=1 ✅
### P4-6.2 实现要点
| 文件 | 变更 |
|------|------|
| `pom.xml` | 新增 `spring-boot-starter-cache` + `caffeine` 依赖 |
| `FlowGatewayConfig` | `@EnableCaching` + `flowGatewayCacheManager`(Caffeine,TTL=10min,maxSize=2000) |
| `PageTemplateService`(新建) | `findTemplateId(tableName)` 查客户库两步映射,`@Cacheable("pageTemplateByTable")`;`buildPageUrl()` 静态工具 |
| `TodoItem` | 新增 `pageTemplateId`/`pageUrl`/`pageUrlReason` 三字段 |
| `TodoAggregator` | 注入 `PageTemplateService`;`fillPageTemplate()` 填充新字段;降级写 `pageUrlReason` |
**降级策略**(优雅降级,前端禁用跳转):
- `businessTable=null`(SYS_DATASTATUS 无映射)→ `pageUrl=null`,`pageUrlReason="businessTable 未映射,无法定位模板"`
- `sys_entity/view_templateentity` 找不到模板 → `pageUrl=null`,`pageUrlReason` 含表名
- 数据库异常 → `PageTemplateService` 内部 catch,返回 `Optional.empty()`,降级同上
### P4-6.3 真实待办验证
服务:flowable-service 8089(本地 eip4flow 迁移数据)+ platform-app 8090(local profile,eip4m0)
用户 `829cdcca-4e07-4e12-9c8e-a3da2c105e31`(wangwu)待办 3 条,全部 `businessKey → PM_SAFE_CLARIFICATION`:
| taskId | businessKey | businessTable | pageTemplateId | pageUrl |
|--------|-------------|--------------|----------------|---------|
| 20334061 | 4f89dbc4-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... |
| 20333989 | 36e9763f-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... |
| 20329569 | 1ef1559b-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... |
与客户库直查对照:`PM_SAFE_CLARIFICATION → sys_entity_id=8c3c71e9 → templateid=7042cb37(type=1 安全技术交底)` ✅ 完全一致。
降级形态:`pageUrl=null + pageUrlReason` 非空 —— 由单测 `PageTemplateMappingTest` 3 个降级用例覆盖。
### P4-6.4 单测总计
`PageTemplateMappingTest`(10 单测,新增):
| 用例 | 验证点 |
|------|--------|
| `tableName有映射_返回templateId` | 正常映射路径 |
| `tableName无映射_返回empty` | 数据库 null 结果 |
| `tableName为null_返回empty不查库` | 空保护 |
| `tableName为空串_返回empty不查库` | 空保护 |
| `buildPageUrl_路由格式正确` | `/page/{id}` 格式 |
| `数据库异常_降级返回empty` | 异常静默降级 |
| `聚合_有模板条目pageUrl正确填充` | 聚合层正常路径 |
| `聚合_无模板映射_pageUrl为null且reason含表名` | 模板未找到降级 |
| `聚合_businessTable为null_pageUrl为null且reason含提示` | SYS_DATASTATUS 无映射降级 |
| `聚合_有模板与无模板条目共存_优雅降级` | 混合列表共存 |
全模块 `mvn test`:**31 单测(platform-flow-gateway)全绿**;`ArchitectureRulesTest` 绿;全反应堆 BUILD SUCCESS。
### P4-6.5 数据变更登记
| 操作 | 目标库.行 | 性质 |
|------|--------|------|
| 插入本地 wangwu 用户 | eip4m0.sys_user | 本地自举,可重复 |
| 插入 3 条 SYS_DATASTATUS | eip4m0.sys_datastatus(PM_SAFE_CLARIFICATION) | 本地构造,演示映射 |
| 建 view_template / view_templateentity | eip4m0(两张新表) | 本地镜像,可重复构造 |
| 插入 sys_entity + view_template + view_templateentity | eip4m0,PM_SAFE_CLARIFICATION 的镜像数据 | 本地构造 |
| **客户库** | 零写入 | 只读 ✅ |
### P4-6.6 新坑
1. **`mvn spring-boot:run` 用的是 classpath 而非 jar**:依赖模块(platform-flow-gateway)改动后须先 `mvn install` 再重启 platform-app,否则 platform-app 运行时仍加载旧 .class(本次 platform-app 在 flow-gateway 编译之前已启动,导致新字段/新 Service 未生效,初查误认为缓存或代理问题)。
2. **local profile 无客户库表时 PageTemplateService 静默降级**:view_template/view_templateentity 在本地 eip4m0 需手动建镜像表(最小结构)才能在 local 验证;生产/default profile 直连客户库则无需此步骤。
## 七-B、P4-6 收尾
- 验证完成后已 `kill` 8089(PID 15942)和 8090(PID 18609)。
- 客户库零写入;本地 eip4m0 新增镜像表为可重复构造工件。

View File

@@ -38,6 +38,16 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency> </dependency>
<!-- Caffeine 本地缓存:模板关联低频变更,tablename→templateId 映射缓存以减少只读库查询 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@@ -0,0 +1,111 @@
package com.eip4.platform.flowgateway.aggregate;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* 业务表名 → 前端页面模板 ID 映射服务。
*
* <p><b>映射口径(侦察客户库 view_templateentity + view_template + sys_entity 得出)</b>:
* <ol>
* <li>{@code tablename} → {@code sys_entity.sys_entity_id}(entityid)</li>
* <li>{@code entityid} → {@code view_templateentity.templateid}(经 {@code view_template.type} 优先级筛选)</li>
* </ol>
*
* <p><b>多模板优先级规则</b>(由 TemplateType 枚举语义 + 待办填写场景决定):
* <ul>
* <li>type=7(EDITFORM 表单模板)优先 —— 待办详情对应的填写/审批页面;</li>
* <li>type=1(GRID 列表模板)次之 —— 无专属表单时退回列表页;</li>
* <li>其余 type 按升序兜底(type=3/4/6 等功能模板);</li>
* <li>type=100(SELECTDIALOG)和 type=200(REPORT)语义与待办无关,但不排除(有表单/列表时自然被压后);</li>
* <li>同 type 有多条时按 templateid 字典序取首个(稳定可重复)。</li>
* </ul>
*
* <p><b>缓存</b>:模板关联是低频变更配置数据(设计时确定),用 Caffeine 缓存 {@code tablename→templateId}
* 映射结果,避免每次待办渲染都查只读客户库两张关联表。
* 缓存命名 {@code "pageTemplateByTable"},配置见 {@code FlowGatewayConfig}。
*
* <p><b>优雅降级</b>:任何数据库异常或找不到映射时返回 {@link Optional#empty()},不抛出,由调用方决定降级策略。
*
* <p><b>数据源只读</b>:全程只查 {@code sys_entity} + {@code view_templateentity} + {@code view_template},
* 不写任何表,对齐客户库只读约定。
*/
@Service
public class PageTemplateService {
private static final Logger log = LoggerFactory.getLogger(PageTemplateService.class);
/**
* 优先级映射:type → 排序权重(越小越优先)。
* type=7 (EDITFORM) 最优先;type=1 (GRID) 次之;其余按 type 值本身升序。
* 未在此表中的 type 用 {@code type + 1000} 作兜底权重。
*/
private static final String BEST_TEMPLATE_SQL = """
SELECT vte.templateid
FROM sys_entity se
JOIN view_templateentity vte ON vte.entityid = se.sys_entity_id
JOIN view_template vt ON vt.view_template_id = vte.templateid
WHERE UPPER(se.tablename) = UPPER(?)
ORDER BY
CASE vt.type
WHEN 7 THEN 0
WHEN 1 THEN 1
WHEN 3 THEN 2
WHEN 4 THEN 3
WHEN 6 THEN 4
WHEN 5 THEN 5
WHEN 100 THEN 6
WHEN 101 THEN 7
WHEN 200 THEN 8
ELSE vt.type + 1000
END,
vte.templateid
LIMIT 1
""";
private final JdbcTemplate jdbcTemplate;
public PageTemplateService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 按业务表名查对应的最优前端模板 ID。
*
* <p>结果被 Caffeine 缓存(key = tableName 原始值,不区分大小写 —— SQL 内已 UPPER 归一)。
* 缓存未命中时查只读客户库,任何异常返回 {@link Optional#empty()}。
*
* @param tableName 业务表名(如 {@code PM_PAYMENTAPPLY}),不区分大小写
* @return 最优 templateId;找不到或异常时为 {@link Optional#empty()}
*/
@Cacheable(cacheNames = "pageTemplateByTable", unless = "#result.isEmpty()")
public Optional<String> findTemplateId(String tableName) {
if (tableName == null || tableName.isBlank()) {
return Optional.empty();
}
try {
String id = jdbcTemplate.query(BEST_TEMPLATE_SQL,
rs -> rs.next() ? rs.getString(1) : null,
tableName);
return Optional.ofNullable(id);
} catch (RuntimeException e) {
log.debug("pageTemplate 映射查询降级(tableName={}): {}", tableName, e.getMessage());
return Optional.empty();
}
}
/**
* 按 templateId 构造前端页面 URL(路由形态 {@code /page/{templateId}})。
*
* @param templateId 非空 templateId
* @return {@code /page/<templateId>}
*/
public static String buildPageUrl(String templateId) {
return "/page/" + templateId;
}
}

View File

@@ -2,6 +2,7 @@ package com.eip4.platform.flowgateway.aggregate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -15,21 +16,26 @@ import com.eip4.platform.flowgateway.client.FlowableClient;
* <ul> * <ul>
* <li>当前用户待办——按 assignee + 候选组(JWT roles)聚合;</li> * <li>当前用户待办——按 assignee + 候选组(JWT roles)聚合;</li>
* <li>逐条经 {@link BusinessSummaryService} 拼业务摘要(businessKey → 业务表标题字段);</li> * <li>逐条经 {@link BusinessSummaryService} 拼业务摘要(businessKey → 业务表标题字段);</li>
* <li>拼不到摘要时优雅降级为仅流程字段(由 BusinessSummaryService 内部兜底)。</li> * <li>逐条经 {@link PageTemplateService} 填充前端页面模板(businessTable → templateId → pageUrl);</li>
* <li>拼不到摘要/模板时优雅降级(pageUrl=null + pageUrlReason 说明原因),不拖垮整张列表。</li>
* </ul> * </ul>
* *
* <p>引擎不可达由 {@link FlowableClient} 抛 {@code FlowServiceUnavailableException}(→ 503), * <p>引擎不可达由 {@link FlowableClient} 抛 {@code FlowServiceUnavailableException}(→ 503),
* 本层不吞;但业务摘要侧缺数据一律降级,不影响流程字段返回。 * 本层不吞;但业务摘要/模板侧缺数据一律降级,不影响流程字段返回。
*/ */
@Service @Service
public class TodoAggregator { public class TodoAggregator {
private final FlowableClient flowableClient; private final FlowableClient flowableClient;
private final BusinessSummaryService businessSummaryService; private final BusinessSummaryService businessSummaryService;
private final PageTemplateService pageTemplateService;
public TodoAggregator(FlowableClient flowableClient, BusinessSummaryService businessSummaryService) { public TodoAggregator(FlowableClient flowableClient,
BusinessSummaryService businessSummaryService,
PageTemplateService pageTemplateService) {
this.flowableClient = flowableClient; this.flowableClient = flowableClient;
this.businessSummaryService = businessSummaryService; this.businessSummaryService = businessSummaryService;
this.pageTemplateService = pageTemplateService;
} }
/** /**
@@ -49,7 +55,7 @@ public class TodoAggregator {
return aggregate(tasks); return aggregate(tasks);
} }
/** 逐条拼业务摘要(摘要侧异常已在 BusinessSummaryService 内降级,不在此抛出)。 */ /** 逐条拼业务摘要 + 页面模板(异常已在各 Service 内降级,不在此抛出)。 */
private List<TodoItem> aggregate(List<FlowTaskDto> tasks) { private List<TodoItem> aggregate(List<FlowTaskDto> tasks) {
List<TodoItem> items = new ArrayList<>(tasks == null ? 0 : tasks.size()); List<TodoItem> items = new ArrayList<>(tasks == null ? 0 : tasks.size());
if (tasks == null) { if (tasks == null) {
@@ -60,8 +66,28 @@ public class TodoAggregator {
BusinessSummaryService.Summary summary = businessSummaryService.summarize(t.businessKey); BusinessSummaryService.Summary summary = businessSummaryService.summarize(t.businessKey);
item.businessTable = summary.tableName(); item.businessTable = summary.tableName();
item.businessSummary = summary.fields(); item.businessSummary = summary.fields();
fillPageTemplate(item);
items.add(item); items.add(item);
} }
return items; return items;
} }
/**
* 填充 pageTemplateId / pageUrl / pageUrlReason。
* 依赖链:businessTable(已由业务摘要层填充) → templateId → pageUrl。
* 任何环节缺失时 pageUrl=null 并写入 reason,前端据此禁用跳转。
*/
private void fillPageTemplate(TodoItem item) {
if (item.businessTable == null) {
item.pageUrlReason = "businessTable 未映射,无法定位模板";
return;
}
Optional<String> templateId = pageTemplateService.findTemplateId(item.businessTable);
if (templateId.isEmpty()) {
item.pageUrlReason = "sys_entity/view_templateentity 中找不到表 " + item.businessTable + " 对应的模板";
return;
}
item.pageTemplateId = templateId.get();
item.pageUrl = PageTemplateService.buildPageUrl(item.pageTemplateId);
}
} }

View File

@@ -34,6 +34,22 @@ public class TodoItem {
/** 业务摘要标题字段(loadPOJO 只读取的标题类字段)。拼不到时为空 Map(降级)。 */ /** 业务摘要标题字段(loadPOJO 只读取的标题类字段)。拼不到时为空 Map(降级)。 */
public Map<String, Object> businessSummary = Map.of(); public Map<String, Object> businessSummary = Map.of();
/**
* 前端页面模板 ID(经 sys_entity + view_templateentity + view_template 映射,type=7 表单优先)。
* 查不到映射时为 null(前端根据 pageUrlReason 决定是否禁用跳转)。
*/
public String pageTemplateId;
/**
* 前端页面 URL,形态 {@code /page/{templateId}}。
* 查不到时为 null,前端应禁用跳转按钮。
*/
public String pageUrl;
/**
* 当 {@code pageUrl} 为 null 时,说明查不到映射的原因(优雅降级辅助信息)。
* pageUrl 不为 null 时此字段为 null。
*/
public String pageUrlReason;
static TodoItem fromTask(FlowTaskDto t) { static TodoItem fromTask(FlowTaskDto t) {
TodoItem item = new TodoItem(); TodoItem item = new TodoItem();
item.taskId = t.id; item.taskId = t.id;

View File

@@ -1,23 +1,34 @@
package com.eip4.platform.flowgateway.client; package com.eip4.platform.flowgateway.client;
import java.time.Duration; import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.ClientHttpRequestFactories; import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClient;
import com.github.benmanes.caffeine.cache.Caffeine;
/** /**
* 流程网关客户端装配:把 {@link FlowGatewayProperties} 的基址/超时落到一个专用 {@code RestClient}。 * 流程网关客户端装配:把 {@link FlowGatewayProperties} 的基址/超时落到一个专用 {@code RestClient}。
* *
* <p>用专属 bean 名 {@code flowRestClient},不与应用里可能存在的其他 {@code RestClient} 冲突; * <p>用专属 bean 名 {@code flowRestClient},不与应用里可能存在的其他 {@code RestClient} 冲突;
* 超时经 {@link ClientHttpRequestFactorySettings} 落到请求工厂(JDK {@code SimpleClientHttpRequestFactory}), * 超时经 {@link ClientHttpRequestFactorySettings} 落到请求工厂(JDK {@code SimpleClientHttpRequestFactory}),
* 引擎卡死时按 {@code read-timeout} 抛 {@code SocketTimeoutException} → {@code FlowableClient} 走熔断降级。 * 引擎卡死时按 {@code read-timeout} 抛 {@code SocketTimeoutException} → {@code FlowableClient} 走熔断降级。
*
* <p>同时启用 {@link EnableCaching} 并注册 Caffeine 缓存管理器,供
* {@code PageTemplateService} 缓存 tablename→templateId 映射结果(模板关联低频变更,10 分钟 TTL)。
*/ */
@Configuration @Configuration
@EnableCaching
@EnableConfigurationProperties(FlowGatewayProperties.class) @EnableConfigurationProperties(FlowGatewayProperties.class)
public class FlowGatewayConfig { public class FlowGatewayConfig {
@@ -36,4 +47,18 @@ public class FlowGatewayConfig {
.requestFactory(flowRequestFactory) .requestFactory(flowRequestFactory)
.build(); .build();
} }
/**
* Caffeine 缓存管理器:{@code pageTemplateByTable} 缓存 tablename→templateId 映射。
* TTL 10 分钟(模板关联是设计时低频配置,无需实时刷新);最大 2000 条(实体总量约 1500,留余量)。
*/
@Bean
CacheManager flowGatewayCacheManager() {
CaffeineCacheManager mgr = new CaffeineCacheManager();
mgr.setCacheNames(List.of("pageTemplateByTable"));
mgr.setCaffeine(Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(2000));
return mgr;
}
} }

View File

@@ -0,0 +1,201 @@
package com.eip4.platform.flowgateway;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import com.eip4.platform.flowgateway.aggregate.BusinessSummaryService;
import com.eip4.platform.flowgateway.aggregate.PageTemplateService;
import com.eip4.platform.flowgateway.aggregate.TodoAggregator;
import com.eip4.platform.flowgateway.aggregate.TodoItem;
import com.eip4.platform.flowgateway.client.FlowTaskDto;
import com.eip4.platform.flowgateway.client.FlowableClient;
/**
* P4-6 页面模板映射单测:
* 1. tableName 有映射 → pageTemplateId/pageUrl 正确填充
* 2. tableName 无映射 → pageUrl=null + reason 含表名
* 3. businessTable=null(业务摘要层未映射)→ pageUrl=null + reason 含提示
* 4. PageTemplateService.findTemplateId 空 tableName → Optional.empty()
* 5. buildPageUrl 路由格式正确
* 6. TodoAggregator 聚合:有模板+无模板条目共存(优雅降级)
*/
@ExtendWith(MockitoExtension.class)
class PageTemplateMappingTest {
// ── PageTemplateService 直接单测 ──────────────────────────────────────────
@Mock
JdbcTemplate jdbcTemplate;
@InjectMocks
PageTemplateService pageTemplateService;
@SuppressWarnings("unchecked")
private void givenTemplateId(String tableName, String templateId) {
when(jdbcTemplate.query(any(String.class),
any(ResultSetExtractor.class),
eq(tableName))).thenReturn(templateId);
}
@Test
void tableName有映射_返回templateId() {
givenTemplateId("PM_PAYMENTAPPLY", "a18b1f38-073c-4190-bfd5-b95274cf9f31");
Optional<String> result = pageTemplateService.findTemplateId("PM_PAYMENTAPPLY");
assertThat(result).isPresent();
assertThat(result.get()).isEqualTo("a18b1f38-073c-4190-bfd5-b95274cf9f31");
}
@Test
void tableName无映射_返回empty() {
givenTemplateId("UNKNOWN_TABLE", null);
Optional<String> result = pageTemplateService.findTemplateId("UNKNOWN_TABLE");
assertThat(result).isEmpty();
}
@Test
void tableName为null_返回empty不查库() {
Optional<String> result = pageTemplateService.findTemplateId(null);
assertThat(result).isEmpty();
}
@Test
void tableName为空串_返回empty不查库() {
Optional<String> result = pageTemplateService.findTemplateId(" ");
assertThat(result).isEmpty();
}
@Test
void buildPageUrl_路由格式正确() {
String url = PageTemplateService.buildPageUrl("a18b1f38-073c-4190-bfd5-b95274cf9f31");
assertThat(url).isEqualTo("/page/a18b1f38-073c-4190-bfd5-b95274cf9f31");
}
@Test
void 数据库异常_降级返回empty() {
when(jdbcTemplate.query(any(String.class), any(ResultSetExtractor.class), eq("PM_BADTABLE")))
.thenThrow(new RuntimeException("连接超时"));
Optional<String> result = pageTemplateService.findTemplateId("PM_BADTABLE");
assertThat(result).isEmpty();
}
// ── TodoAggregator 集成:pageTemplate 填充路径 ────────────────────────────
@Mock
FlowableClient flowableClient;
@Mock
BusinessSummaryService businessSummaryService;
@Mock
PageTemplateService mockPageTemplateService;
@InjectMocks
TodoAggregator aggregator;
private FlowTaskDto task(String id, String businessKey) {
FlowTaskDto t = new FlowTaskDto();
t.id = id;
t.name = "付款申请审批";
t.assignee = "U1";
t.businessKey = businessKey;
return t;
}
@Test
void 聚合_有模板条目pageUrl正确填充() {
when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt()))
.thenReturn(List.of(task("T1", "BK-1")));
when(businessSummaryService.summarize("BK-1"))
.thenReturn(new BusinessSummaryService.Summary("PM_PAYMENTAPPLY", Map.of("TITLE", "付款单")));
when(mockPageTemplateService.findTemplateId("PM_PAYMENTAPPLY"))
.thenReturn(Optional.of("a18b1f38-073c-4190-bfd5-b95274cf9f31"));
List<TodoItem> items = aggregator.todo("U1", List.of(), 0, 50);
assertThat(items).hasSize(1);
TodoItem it = items.get(0);
assertThat(it.pageTemplateId).isEqualTo("a18b1f38-073c-4190-bfd5-b95274cf9f31");
assertThat(it.pageUrl).isEqualTo("/page/a18b1f38-073c-4190-bfd5-b95274cf9f31");
assertThat(it.pageUrlReason).isNull();
}
@Test
void 聚合_无模板映射_pageUrl为null且reason含表名() {
when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt()))
.thenReturn(List.of(task("T2", "BK-2")));
when(businessSummaryService.summarize("BK-2"))
.thenReturn(new BusinessSummaryService.Summary("OA_UNKNOWN_TABLE", Map.of()));
when(mockPageTemplateService.findTemplateId("OA_UNKNOWN_TABLE"))
.thenReturn(Optional.empty());
List<TodoItem> items = aggregator.todo("U1", List.of(), 0, 50);
assertThat(items).hasSize(1);
TodoItem it = items.get(0);
assertThat(it.pageTemplateId).isNull();
assertThat(it.pageUrl).isNull();
assertThat(it.pageUrlReason).contains("OA_UNKNOWN_TABLE");
}
@Test
void 聚合_businessTable为null_pageUrl为null且reason含提示() {
when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt()))
.thenReturn(List.of(task("T3", "BK-MISSING")));
when(businessSummaryService.summarize("BK-MISSING"))
.thenReturn(BusinessSummaryService.Summary.EMPTY);
List<TodoItem> items = aggregator.todo("U1", List.of(), 0, 50);
assertThat(items).hasSize(1);
TodoItem it = items.get(0);
assertThat(it.businessTable).isNull();
assertThat(it.pageTemplateId).isNull();
assertThat(it.pageUrl).isNull();
assertThat(it.pageUrlReason).contains("businessTable");
}
@Test
void 聚合_有模板与无模板条目共存_优雅降级() {
when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt()))
.thenReturn(List.of(task("T4", "BK-4"), task("T5", "BK-5")));
when(businessSummaryService.summarize("BK-4"))
.thenReturn(new BusinessSummaryService.Summary("PM_PAYMENTAPPLY", Map.of("TITLE", "付款单")));
when(businessSummaryService.summarize("BK-5"))
.thenReturn(BusinessSummaryService.Summary.EMPTY);
when(mockPageTemplateService.findTemplateId("PM_PAYMENTAPPLY"))
.thenReturn(Optional.of("tmpl-uuid-001"));
List<TodoItem> items = aggregator.todo("U1", List.of(), 0, 50);
assertThat(items).hasSize(2);
// 第一条:有模板
assertThat(items.get(0).pageUrl).isEqualTo("/page/tmpl-uuid-001");
assertThat(items.get(0).pageUrlReason).isNull();
// 第二条:无 businessTable,优雅降级
assertThat(items.get(1).pageUrl).isNull();
assertThat(items.get(1).pageUrlReason).isNotBlank();
}
}

View File

@@ -17,11 +17,14 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import com.eip4.platform.flowgateway.aggregate.BusinessSummaryService; import com.eip4.platform.flowgateway.aggregate.BusinessSummaryService;
import com.eip4.platform.flowgateway.aggregate.PageTemplateService;
import com.eip4.platform.flowgateway.aggregate.TodoAggregator; import com.eip4.platform.flowgateway.aggregate.TodoAggregator;
import com.eip4.platform.flowgateway.aggregate.TodoItem; import com.eip4.platform.flowgateway.aggregate.TodoItem;
import com.eip4.platform.flowgateway.client.FlowTaskDto; import com.eip4.platform.flowgateway.client.FlowTaskDto;
import com.eip4.platform.flowgateway.client.FlowableClient; import com.eip4.platform.flowgateway.client.FlowableClient;
import java.util.Optional;
/** /**
* 待办聚合拼接单测:验证流程任务 + 业务摘要连接,以及摘要拼不到时优雅降级为仅流程字段。 * 待办聚合拼接单测:验证流程任务 + 业务摘要连接,以及摘要拼不到时优雅降级为仅流程字段。
*/ */
@@ -32,6 +35,8 @@ class TodoAggregatorTest {
FlowableClient flowableClient; FlowableClient flowableClient;
@Mock @Mock
BusinessSummaryService businessSummaryService; BusinessSummaryService businessSummaryService;
@Mock
PageTemplateService pageTemplateService;
@InjectMocks @InjectMocks
TodoAggregator aggregator; TodoAggregator aggregator;