F2-17根因: 列表人员列 id→姓名解析(UserColumnResolver,568列/385实体)
按 SYS_ENTITYATTRIBUTE.referenceid→SYS_REFERENCE→SYS_USER 判定用户引用列, 读路径(_SEARCH)把 id→DISPLAYNAME(Caffeine 双缓存,/api/meta/evict 联动失效); FlowController 3参构造补 @Autowired(多构造器歧义致启动崩溃,运行时实测捕获)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,211 @@
|
||||
package com.eip4.platform.metadata.flow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
/**
|
||||
* 用户引用列后处理器(read-path issue D1=603:列表/_SEARCH 人员列显示 id/工号而非姓名)。
|
||||
*
|
||||
* <p><b>问题</b>:合成搜索流 {@code <TABLE>_SEARCH} 与命名查询流只把裸列值(用户引用列存的是
|
||||
* {@code SYS_USER} 主键 = {@code view_sys_user_id},如 {@code 402881...} 或字面 {@code ADMIN})
|
||||
* 平展到 amis 行里;前端列表遂显示 id/工号而非姓名。元数据里 {@code _DISPLAY__} 翻译列在转换器
|
||||
* 阶段已被剥离(TemplateToAmisConverter:189,注「REST 层待补显示值」),合成搜索流不返回 display 值,
|
||||
* 故必须在 REST 读响应层把「元数据标记为用户引用的列」的 id → 姓名(口径与 PickerController 一致:
|
||||
* {@code VIEW_SYS_USER.DISPLAYNAME} 形如「罗来伟[130218]」)。
|
||||
*
|
||||
* <p><b>元数据驱动,零误伤</b>:仅当列的 {@code SYS_ENTITYATTRIBUTE.referenceid} 经
|
||||
* {@code SYS_REFERENCE} 解析到 {@code SYS_USER}/{@code VIEW_SYS_USER} 实体时才视为用户引用列
|
||||
* (与引擎 {@code POInfo.getReferenceTable} 同口径,但走自包含元数据查询,便于单测、不依赖 OSGi bean)。
|
||||
* 形如 {@code CREATEBY}/{@code UPDATEBY}(多数实体 referenceid=NVARCHAR,非引用)一律不动。
|
||||
* 全库实测 568 列 / 385 实体属用户引用列,目标实体 OA_NOTICEMANAGE 仅 USER_ID 命中。
|
||||
*
|
||||
* <p><b>缓存</b>(与 FlowNameResolver 同 Caffeine 策略):
|
||||
* <ul>
|
||||
* <li>{@code userColumnsByTable}:表名 → 用户引用列名集合(避免每次请求查元数据)。</li>
|
||||
* <li>{@code idToDisplay}:用户 id(大写)→ DISPLAYNAME(避免每行查库;批量补缓存)。</li>
|
||||
* </ul>
|
||||
* 失效跟 {@link FlowNameResolver#evict} 的 {@code /api/meta/evict} 钩子对齐口径(见 {@link #evictAll})。
|
||||
*/
|
||||
@Component
|
||||
public class UserColumnResolver {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(UserColumnResolver.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
/** 表名(大写)→ 用户引用列名集合(大写)。空集合表示该表无用户引用列(亦缓存,避免重复查元数据)。 */
|
||||
private final Cache<String, Set<String>> userColumnsByTable = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
/** 用户 id(大写、已去空白)→ DISPLAYNAME(姓名[工号])。未命中的 id 不入缓存,回退原值。 */
|
||||
private final Cache<String, String> idToDisplay = Caffeine.newBuilder()
|
||||
.maximumSize(50_000)
|
||||
.build();
|
||||
|
||||
public UserColumnResolver(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 就地解析一批行里「元数据标记为用户引用的列」的 id → 姓名。
|
||||
*
|
||||
* @param table 实体表名(任意大小写;null/空 → 直接跳过)
|
||||
* @param rows amis 平面行列表({@code List<Map<String,Object>>},会被就地修改)
|
||||
*/
|
||||
public void resolveRows(String table, List<Map<String, Object>> rows) {
|
||||
if (table == null || table.isBlank() || rows == null || rows.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<String> userCols = userColumnsForTable(table);
|
||||
if (userCols.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1) 收集本批所有待解析 id(大写、去空白),批量补缓存,避免每行查库
|
||||
Set<String> needIds = new HashSet<>();
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (String col : userCols) {
|
||||
String id = normalizeId(row.get(col));
|
||||
if (id != null && idToDisplay.getIfPresent(id) == null) {
|
||||
needIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!needIds.isEmpty()) {
|
||||
loadDisplayNames(needIds);
|
||||
}
|
||||
|
||||
// 2) 就地替换:命中缓存 → 姓名;未命中 → 保留原值(不破坏数据)
|
||||
for (Map<String, Object> row : rows) {
|
||||
for (String col : userCols) {
|
||||
if (!row.containsKey(col)) {
|
||||
continue;
|
||||
}
|
||||
String id = normalizeId(row.get(col));
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
String name = idToDisplay.getIfPresent(id);
|
||||
if (name != null && !name.isBlank()) {
|
||||
row.put(col, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 该表的用户引用列名集合(大写)。按 {@code SYS_ENTITYATTRIBUTE.referenceid → SYS_REFERENCE.entityid →
|
||||
* SYS_ENTITY} 解析,仅保留目标实体为 {@code SYS_USER}/{@code VIEW_SYS_USER} 的列。结果缓存(含空集合)。
|
||||
*/
|
||||
private Set<String> userColumnsForTable(String table) {
|
||||
String key = table.toUpperCase();
|
||||
return userColumnsByTable.get(key, this::queryUserColumns);
|
||||
}
|
||||
|
||||
private Set<String> queryUserColumns(String upperTable) {
|
||||
try {
|
||||
List<String> cols = jdbcTemplate.queryForList(
|
||||
"SELECT UPPER(ea.columnname) AS col "
|
||||
+ "FROM eip3.sys_entityattribute ea "
|
||||
+ "JOIN eip3.sys_entity e1 ON UPPER(e1.sys_entity_id) = UPPER(ea.entityid) "
|
||||
+ "JOIN eip3.sys_reference r ON UPPER(r.sys_reference_id) = UPPER(ea.referenceid) "
|
||||
+ "JOIN eip3.sys_entity e2 ON UPPER(e2.sys_entity_id) = UPPER(r.entityid) "
|
||||
+ "WHERE UPPER(e1.tablename) = ? "
|
||||
+ "AND UPPER(e2.tablename) IN ('SYS_USER', 'VIEW_SYS_USER')",
|
||||
String.class, upperTable);
|
||||
if (cols.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
Set<String> set = new HashSet<>(cols);
|
||||
log.debug("[USER-COL] table={} userRefColumns={}", upperTable, set);
|
||||
return set;
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
// 元数据查询异常(库不可用等)→ 视作无用户引用列,绝不影响主读路径
|
||||
log.warn("[USER-COL] 解析用户引用列失败 table={}: {}", upperTable, ex.getMessage());
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
/** 单批 IN 子句最大占位符数(PG 参数上限 65535,留足余量;超出则分批)。 */
|
||||
private static final int MAX_IN_BATCH = 1_000;
|
||||
|
||||
/**
|
||||
* 批量加载 id → DISPLAYNAME 进缓存(按 {@code VIEW_SYS_USER},大小写不敏感匹配 view_sys_user_id)。
|
||||
* 用动态 {@code IN (?,?,...)} 占位符全参数化(id 来自库内数据,仍走绑定不拼值);超过单批上限自动分批。
|
||||
* 失败时静默跳过(保留原 id 值,不破坏读路径)。
|
||||
*/
|
||||
private void loadDisplayNames(Set<String> ids) {
|
||||
List<String> all = new ArrayList<>(ids);
|
||||
for (int start = 0; start < all.size(); start += MAX_IN_BATCH) {
|
||||
List<String> batch = all.subList(start, Math.min(start + MAX_IN_BATCH, all.size()));
|
||||
loadDisplayNameBatch(batch);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadDisplayNameBatch(List<String> idBatch) {
|
||||
StringBuilder placeholders = new StringBuilder(idBatch.size() * 2);
|
||||
for (int i = 0; i < idBatch.size(); i++) {
|
||||
if (i > 0) {
|
||||
placeholders.append(',');
|
||||
}
|
||||
placeholders.append('?');
|
||||
}
|
||||
String sql = "SELECT UPPER(view_sys_user_id) AS id, displayname, name "
|
||||
+ "FROM eip3.view_sys_user "
|
||||
+ "WHERE UPPER(view_sys_user_id) IN (" + placeholders + ")";
|
||||
try {
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, idBatch.toArray());
|
||||
for (Map<String, Object> r : rows) {
|
||||
String id = r.get("id") != null ? String.valueOf(r.get("id")) : null;
|
||||
Object display = r.get("displayname");
|
||||
Object name = r.get("name");
|
||||
String label = display != null && !String.valueOf(display).isBlank()
|
||||
? String.valueOf(display)
|
||||
: (name != null ? String.valueOf(name) : null);
|
||||
if (id != null && label != null && !label.isBlank()) {
|
||||
idToDisplay.put(id, label);
|
||||
}
|
||||
}
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
log.warn("[USER-COL] 批量加载用户姓名失败: {}", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** id 归一化:null/空白 → null;否则去首尾空白(char 列定长补空格)并大写(与 view_sys_user_id 比较口径一致)。 */
|
||||
private static String normalizeId(Object raw) {
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String s = String.valueOf(raw).trim();
|
||||
if (s.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return s.toUpperCase();
|
||||
}
|
||||
|
||||
/** 失效全部缓存(与 /api/meta/evict 钩子对齐:元数据/用户改动后重建)。 */
|
||||
public void evictAll() {
|
||||
userColumnsByTable.invalidateAll();
|
||||
idToDisplay.invalidateAll();
|
||||
}
|
||||
|
||||
/** 失效某表的用户引用列缓存(表结构/引用关系变更时)。 */
|
||||
public void evictTable(String table) {
|
||||
if (table != null && !table.isBlank()) {
|
||||
userColumnsByTable.invalidate(table.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import org.springframework.stereotype.Component;
|
||||
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
|
||||
import com.dsasp.server.common.dataDic.bean.POJOInfo;
|
||||
import com.dsasp.server.dataDic.bean.POInfo;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
|
||||
/**
|
||||
@@ -23,9 +24,12 @@ import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
public class PoInfoCacheInvalidator {
|
||||
|
||||
private final IdentifierWhitelist identifierWhitelist;
|
||||
private final UserColumnResolver userColumnResolver;
|
||||
|
||||
public PoInfoCacheInvalidator(IdentifierWhitelist identifierWhitelist) {
|
||||
public PoInfoCacheInvalidator(IdentifierWhitelist identifierWhitelist,
|
||||
UserColumnResolver userColumnResolver) {
|
||||
this.identifierWhitelist = identifierWhitelist;
|
||||
this.userColumnResolver = userColumnResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,6 +57,8 @@ public class PoInfoCacheInvalidator {
|
||||
// 白名单缓存仍按表名失效(下行),不让引擎侧异常阻断现代层流程。
|
||||
}
|
||||
identifierWhitelist.evict(upper);
|
||||
// read-path D1=603:属性变更可能改动列的 referenceid(用户引用与否),同步失效用户引用列缓存
|
||||
userColumnResolver.evictTable(upper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,8 +74,11 @@ public class PoInfoCacheInvalidator {
|
||||
}
|
||||
if (table != null && !table.isBlank()) {
|
||||
identifierWhitelist.evict(table);
|
||||
userColumnResolver.evictTable(table);
|
||||
} else {
|
||||
identifierWhitelist.evictAll();
|
||||
// read-path D1=603:表名不可查的粗粒度变更 → 全量失效用户引用列缓存
|
||||
userColumnResolver.evictAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver.ResolvedFlow;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
||||
@@ -56,10 +57,21 @@ public class FlowController {
|
||||
|
||||
private final FlowExcuteService flowExcuteService;
|
||||
private final FlowNameResolver flowNameResolver;
|
||||
private final UserColumnResolver userColumnResolver;
|
||||
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver) {
|
||||
/** Spring 注入入口:带用户引用列解析器(read-path issue D1=603 修复)。
|
||||
* 多构造器场景须显式 @Autowired 指明注入构造,否则 Spring 回退无参构造致启动失败。 */
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver,
|
||||
UserColumnResolver userColumnResolver) {
|
||||
this.flowExcuteService = flowExcuteService;
|
||||
this.flowNameResolver = flowNameResolver;
|
||||
this.userColumnResolver = userColumnResolver;
|
||||
}
|
||||
|
||||
/** 兼容老单测的两参构造(无用户列解析,行为同未修复前)。生产由 Spring 走三参构造。 */
|
||||
public FlowController(FlowExcuteService flowExcuteService, FlowNameResolver flowNameResolver) {
|
||||
this(flowExcuteService, flowNameResolver, null);
|
||||
}
|
||||
|
||||
/** amis crud 默认每页条数(与前端 amis 默认 perPage 一致)。 */
|
||||
@@ -136,18 +148,21 @@ public class FlowController {
|
||||
|
||||
return switch (resolved.type()) {
|
||||
case UUID_PASSTHROUGH, NAMED_HIT -> {
|
||||
// UUID 或命名命中:直接走引擎(引擎按 PK 查 sys_serviceflow)
|
||||
// UUID 或命名命中:直接走引擎(引擎按 PK 查 sys_serviceflow)。
|
||||
// 非合成路径无法稳妥推断实体表,故不做用户列解析(table=null)。
|
||||
Properties ctx = UserSession.getAdmin();
|
||||
Object output = flowExcuteService.excute(map, ctx, id);
|
||||
yield ok(output);
|
||||
yield ok(output, null);
|
||||
}
|
||||
case SYNTHESIZED -> {
|
||||
// _SEARCH 合成:合成流已注册到 SynthesizedFlowRegistry,
|
||||
// FlowServiceWithSynthesis.getFlowDefinition 会按 key 命中,
|
||||
// 直接用原始 flowId(已大写 = TABLE_SEARCH)调引擎。
|
||||
Properties ctx = UserSession.getAdmin();
|
||||
Object output = flowExcuteService.excute(map, ctx, resolved.definition().getKey());
|
||||
yield ok(output);
|
||||
String flowKey = resolved.definition().getKey();
|
||||
Object output = flowExcuteService.excute(map, ctx, flowKey);
|
||||
// 合成搜索流的 key = <TABLE>_SEARCH,剥后缀即数据源实体表名,用于用户引用列 id→姓名解析
|
||||
yield ok(output, tableFromSearchKey(flowKey));
|
||||
}
|
||||
case WRITE_REDIRECT -> {
|
||||
Map<String, Object> err = new LinkedHashMap<>();
|
||||
@@ -164,14 +179,52 @@ public class FlowController {
|
||||
};
|
||||
}
|
||||
|
||||
private ResponseEntity<Map<String, Object>> ok(Object output) {
|
||||
private ResponseEntity<Map<String, Object>> ok(Object output, String table) {
|
||||
Object data = unwrapCrudResult(normalize(output));
|
||||
// read-path issue D1=603:把元数据标记为用户引用的列 id/工号 → 姓名(仅在已知数据源实体的合成搜索流上)
|
||||
resolveUserColumns(data, table);
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", unwrapCrudResult(normalize(output)));
|
||||
resp.put("data", data);
|
||||
return ResponseEntity.ok(resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合成搜索流 key({@code <TABLE>_SEARCH})→ 数据源实体表名(剥 {@code _SEARCH} 后缀)。
|
||||
* 非 {@code _SEARCH} 形态(不应发生在 SYNTHESIZED 路径)→ null,跳过用户列解析。
|
||||
*/
|
||||
private static String tableFromSearchKey(String flowKey) {
|
||||
if (flowKey == null) {
|
||||
return null;
|
||||
}
|
||||
String upper = flowKey.toUpperCase();
|
||||
if (upper.endsWith("_SEARCH")) {
|
||||
return upper.substring(0, upper.length() - "_SEARCH".length());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对归一化后的 amis data 应用用户引用列解析(id→姓名)。仅处理 crud 形态 {@code {items:[...]}}
|
||||
* 的行集;解析器为空(两参构造的老单测)或 table 缺省时静默跳过。对非用户列零影响。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void resolveUserColumns(Object data, String table) {
|
||||
if (userColumnResolver == null || table == null) {
|
||||
return;
|
||||
}
|
||||
if (data instanceof Map<?, ?> m && m.get("items") instanceof List<?> items) {
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
for (Object o : items) {
|
||||
if (o instanceof Map<?, ?> row) {
|
||||
rows.add((Map<String, Object>) row);
|
||||
}
|
||||
}
|
||||
userColumnResolver.resolveRows(table, rows);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* F2-14b:流输出变量名为 result(DefaultFlowHelper 默认搜索流口径),查询结果被包成
|
||||
* {result:{items,total}},而 amis crud 契约读 data.items/data.total——一层之差导致
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
package com.eip4.platform.metadata.flow;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
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.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* 用户引用列解析器单测(mock JdbcTemplate,不连库)。
|
||||
*
|
||||
* <p>验证 read-path issue D1=603 修复核心契约:
|
||||
* <ol>
|
||||
* <li>元数据标记为用户引用的列(USER_ID)的 id/字面 ADMIN → DISPLAYNAME(姓名[工号]);</li>
|
||||
* <li>非用户列(TITLE)与未标记用户列(CREATEBY,元数据回空集)零影响;</li>
|
||||
* <li>char 列定长补空格的 id("ADMIN ")去空白后仍能解析;</li>
|
||||
* <li>未命中库的 id 保留原值(不破坏数据);</li>
|
||||
* <li>列集合与姓名均走缓存,重复请求不二次查库。</li>
|
||||
* </ol>
|
||||
*/
|
||||
class UserColumnResolverTest {
|
||||
|
||||
/** 桩:元数据查询返回 table 的用户引用列集合。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private void stubUserColumns(JdbcTemplate jdbc, String upperTable, List<String> cols) {
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq(upperTable))).thenReturn(cols);
|
||||
}
|
||||
|
||||
/** 桩:DISPLAYNAME 批量查询返回 id→姓名行(匹配 queryForList(sql, Object...) 重载)。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private void stubDisplayNames(JdbcTemplate jdbc, List<Map<String, Object>> rows) {
|
||||
// 用户列查询走 (sql, Class, Object...) 重载;姓名查询走 (sql, Object...) 重载——后者无 Class 实参
|
||||
when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(rows);
|
||||
}
|
||||
|
||||
private Map<String, Object> userRow(String id, String displayname, String name) {
|
||||
Map<String, Object> r = new LinkedHashMap<>();
|
||||
r.put("id", id);
|
||||
r.put("displayname", displayname);
|
||||
r.put("name", name);
|
||||
return r;
|
||||
}
|
||||
|
||||
private Map<String, Object> dataRow(String userId, String title, String createby) {
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("USER_ID", userId);
|
||||
row.put("TITLE", title);
|
||||
row.put("CREATEBY", createby);
|
||||
return row;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("1. USER_ID(用户引用列) id → 姓名[工号];TITLE/CREATEBY 不动")
|
||||
void resolvesUserColumnOnly() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "OA_NOTICEMANAGE", List.of("USER_ID"));
|
||||
stubDisplayNames(jdbc, List.of(
|
||||
userRow("402881343826DC6F01382767994E0016", "沈波恩[000008]", "沈波恩")));
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(dataRow("402881343826dc6f01382767994e0016", "放假通知", "402881343826dc6f01382767994e0016"));
|
||||
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows);
|
||||
|
||||
assertEquals("沈波恩[000008]", rows.get(0).get("USER_ID"), "用户引用列应解析为姓名[工号]");
|
||||
assertEquals("放假通知", rows.get(0).get("TITLE"), "非用户列不动");
|
||||
assertEquals("402881343826dc6f01382767994e0016", rows.get(0).get("CREATEBY"),
|
||||
"未被元数据标记的 CREATEBY 不动");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2. 字面 ADMIN(char 列补空格)去空白后解析为 系统管理员[admin]")
|
||||
void resolvesPaddedAdminId() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "OA_NOTICEMANAGE", List.of("USER_ID"));
|
||||
stubDisplayNames(jdbc, List.of(
|
||||
userRow("ADMIN", "系统管理员[admin]", "系统管理员")));
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
// char 列定长补空格的真实样态
|
||||
rows.add(dataRow("ADMIN ", "通知", "ADMIN"));
|
||||
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows);
|
||||
|
||||
assertEquals("系统管理员[admin]", rows.get(0).get("USER_ID"),
|
||||
"补空格的字面 ADMIN 去空白后应解析为姓名");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("3. 无用户引用列的表(元数据回空集):整行零改,且不查姓名库")
|
||||
void noUserColumnsNoOp() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "SOME_TABLE", List.of()); // 空集合
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(dataRow("402881343826dc6f01382767994e0016", "x", "y"));
|
||||
|
||||
resolver.resolveRows("SOME_TABLE", rows);
|
||||
|
||||
assertEquals("402881343826dc6f01382767994e0016", rows.get(0).get("USER_ID"),
|
||||
"无用户引用列时该列不动");
|
||||
// 姓名查询(Object... 重载)不应被调用
|
||||
verify(jdbc, times(0)).queryForList(anyString(), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("4. 库里查不到的 id:保留原值(不破坏数据)")
|
||||
void unresolvedIdKeptAsIs() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "OA_NOTICEMANAGE", List.of("USER_ID"));
|
||||
stubDisplayNames(jdbc, List.of()); // 没有任何匹配行
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(dataRow("UNKNOWN-GUID-XYZ", "t", "c"));
|
||||
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows);
|
||||
|
||||
assertEquals("UNKNOWN-GUID-XYZ", rows.get(0).get("USER_ID"),
|
||||
"查不到姓名的 id 应保留原值");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("5. 缓存:同表重复解析只查一次元数据列集合、同 id 只查一次姓名")
|
||||
void cachesColumnsAndNames() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "OA_NOTICEMANAGE", List.of("USER_ID"));
|
||||
stubDisplayNames(jdbc, List.of(
|
||||
userRow("402881343826DC6F01382767994E0016", "沈波恩[000008]", "沈波恩")));
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
|
||||
List<Map<String, Object>> rows1 = new ArrayList<>();
|
||||
rows1.add(dataRow("402881343826dc6f01382767994e0016", "a", "b"));
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows1);
|
||||
|
||||
List<Map<String, Object>> rows2 = new ArrayList<>();
|
||||
rows2.add(dataRow("402881343826dc6f01382767994e0016", "c", "d"));
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows2);
|
||||
|
||||
assertEquals("沈波恩[000008]", rows2.get(0).get("USER_ID"), "第二次仍解析正确(走缓存)");
|
||||
// 元数据列集合查询(带 Class 重载)只发生一次
|
||||
verify(jdbc, times(1)).queryForList(anyString(), eq(String.class), eq("OA_NOTICEMANAGE"));
|
||||
// 姓名查询只发生一次(第二次 id 已在缓存,needIds 为空不再查库)
|
||||
verify(jdbc, times(1)).queryForList(anyString(), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("6. 入参防御:table 空 / rows 空 → 不抛、不查库")
|
||||
void guardsEmptyInputs() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
|
||||
resolver.resolveRows(null, new ArrayList<>());
|
||||
resolver.resolveRows("", new ArrayList<>());
|
||||
resolver.resolveRows("T", null);
|
||||
resolver.resolveRows("T", new ArrayList<>()); // 空行集
|
||||
|
||||
// 空行集合直接返回,不应触发任何元数据/姓名查询
|
||||
verify(jdbc, times(0)).queryForList(anyString(), eq(String.class), anyString());
|
||||
verify(jdbc, times(0)).queryForList(anyString(), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("7. displayname 缺失时回退 name")
|
||||
void fallsBackToNameWhenDisplayBlank() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
stubUserColumns(jdbc, "OA_NOTICEMANAGE", List.of("USER_ID"));
|
||||
stubDisplayNames(jdbc, List.of(
|
||||
userRow("ID1", "", "罗来伟")));
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(dataRow("ID1", "t", "c"));
|
||||
|
||||
resolver.resolveRows("OA_NOTICEMANAGE", rows);
|
||||
|
||||
assertEquals("罗来伟", rows.get(0).get("USER_ID"), "displayname 空白时回退 name");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("8. 多用户引用列同时解析(如 USER_ID + 标记的 CREATEBY)")
|
||||
void resolvesMultipleUserColumns() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
// 该表 USER_ID 与 CREATEBY 都被元数据标记为用户引用
|
||||
stubUserColumns(jdbc, "X", List.of("USER_ID", "CREATEBY"));
|
||||
stubDisplayNames(jdbc, List.of(
|
||||
userRow("A", "张三[001]", "张三"),
|
||||
userRow("B", "李四[002]", "李四")));
|
||||
|
||||
UserColumnResolver resolver = new UserColumnResolver(jdbc);
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("USER_ID", "A");
|
||||
row.put("CREATEBY", "B");
|
||||
row.put("TITLE", "keep");
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
rows.add(row);
|
||||
|
||||
resolver.resolveRows("X", rows);
|
||||
|
||||
assertEquals("张三[001]", rows.get(0).get("USER_ID"));
|
||||
assertEquals("李四[002]", rows.get(0).get("CREATEBY"));
|
||||
assertEquals("keep", rows.get(0).get("TITLE"));
|
||||
assertTrue(rows.get(0).containsKey("TITLE"));
|
||||
assertFalse(rows.get(0).get("USER_ID").equals("A"));
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.dsasp.server.common.dataDic.bean.AbstractPOJOInfo;
|
||||
import com.dsasp.server.common.dataDic.bean.POJOInfo;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
|
||||
/**
|
||||
@@ -37,27 +38,32 @@ class PoInfoCacheInvalidatorTest {
|
||||
@Test
|
||||
void 按entityId失效_精确表名时只evict该表() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist, userResolver);
|
||||
|
||||
inv.evictByEntityId("entity-123", "EIP4PT_ORDER");
|
||||
|
||||
verify(whitelist).evict("EIP4PT_ORDER");
|
||||
verify(userResolver).evictTable("EIP4PT_ORDER");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 按entityId失效_无表名时全量evict() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist, userResolver);
|
||||
|
||||
inv.evictByEntityId("entity-123", null);
|
||||
|
||||
verify(whitelist).evictAll();
|
||||
verify(userResolver).evictAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void evict钩子调setChangePOInfo清引擎缓存() throws Exception {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist, userResolver);
|
||||
|
||||
// 预置 id_cache[entityId]→tableName 与 s_cache[tableName],模拟缓存已热
|
||||
Field idf = AbstractPOJOInfo.class.getDeclaredField("id_cache");
|
||||
@@ -82,7 +88,8 @@ class PoInfoCacheInvalidatorTest {
|
||||
@Test
|
||||
void 空入参不抛异常() {
|
||||
IdentifierWhitelist whitelist = mock(IdentifierWhitelist.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist);
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
PoInfoCacheInvalidator inv = new PoInfoCacheInvalidator(whitelist, userResolver);
|
||||
inv.evictByTable(null);
|
||||
inv.evictByTable(" ");
|
||||
inv.evictByEntityId(null, null);
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver.ResolveType;
|
||||
import com.eip4.platform.metadata.flow.FlowNameResolver.ResolvedFlow;
|
||||
import com.eip4.platform.metadata.flow.UserColumnResolver;
|
||||
|
||||
/**
|
||||
* 服务流 REST 门面单测(mock 引擎 + mock 解析器,不连库)。
|
||||
@@ -312,6 +313,69 @@ class FlowControllerTest {
|
||||
assertEquals(5, sent.get(POJOCRUDService.PAGE_SIZE), "perPage=5");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("15. SYNTHESIZED 搜索流: 用户引用列解析器以剥后缀的表名 + items 行被调用(read-path D1=603 接线)")
|
||||
@SuppressWarnings("unchecked")
|
||||
void synthesizedSearchInvokesUserColumnResolver() {
|
||||
String flowId = "OA_NOTICEMANAGE_SEARCH";
|
||||
String flowKey = "OA_NOTICEMANAGE_SEARCH";
|
||||
|
||||
// 引擎返回合成搜索流默认输出形态 {result:{listresult:[POJO], pagecount:1}}
|
||||
Map<String, Object> listResult = new LinkedHashMap<>();
|
||||
Map<String, Object> cols = new LinkedHashMap<>();
|
||||
cols.put("USER_ID", "ADMIN");
|
||||
cols.put("TITLE", "通知");
|
||||
List<GeneralPOJO> pojos = new ArrayList<>();
|
||||
pojos.add(fakeRow("row-1", cols));
|
||||
listResult.put(POJOCRUDService.LIST_RESULT, pojos);
|
||||
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(), eq(flowKey))).thenReturn(output);
|
||||
|
||||
// 解析器桩:把 USER_ID 改写为姓名(验证就地改写反映到响应)
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
org.mockito.Mockito.doAnswer(inv -> {
|
||||
List<Map<String, Object>> rows = inv.getArgument(1);
|
||||
for (Map<String, Object> r : rows) {
|
||||
if ("ADMIN".equals(r.get("USER_ID"))) {
|
||||
r.put("USER_ID", "系统管理员[admin]");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}).when(userResolver).resolveRows(eq("OA_NOTICEMANAGE"), any(List.class));
|
||||
|
||||
FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowKey)),
|
||||
userResolver);
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = controller.executeGet(flowId, new HashMap<>());
|
||||
|
||||
// 解析器被以「剥 _SEARCH 后缀的表名」调用
|
||||
org.mockito.Mockito.verify(userResolver).resolveRows(eq("OA_NOTICEMANAGE"), any(List.class));
|
||||
// 改写反映到最终响应
|
||||
Map<String, Object> data = (Map<String, Object>) resp.getBody().get("data");
|
||||
List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("items");
|
||||
assertEquals("系统管理员[admin]", items.get(0).get("USER_ID"), "用户引用列 id → 姓名");
|
||||
assertEquals("通知", items.get(0).get("TITLE"), "非用户列不动");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("16. UUID/命名路径: 不调用用户列解析器(无法稳妥推断实体表,table=null)")
|
||||
void nonSynthesizedDoesNotInvokeResolver() {
|
||||
String flowId = "COMMON_REPORT";
|
||||
FlowExcuteService engine = mock(FlowExcuteService.class);
|
||||
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
|
||||
UserColumnResolver userResolver = mock(UserColumnResolver.class);
|
||||
FlowController controller = new FlowController(engine, resolverReturning(flowId, namedHit()), userResolver);
|
||||
|
||||
controller.executeGet(flowId, new HashMap<>());
|
||||
|
||||
org.mockito.Mockito.verify(userResolver, org.mockito.Mockito.never())
|
||||
.resolveRows(org.mockito.ArgumentMatchers.anyString(), any(List.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("14. 无分页参数: 不注入分页键(命名/报表流无分页语义时不凭空加键)")
|
||||
void noPagingKeysWhenAbsent() {
|
||||
|
||||
Reference in New Issue
Block a user