拟稿人填充(过渡): /api/entity/save 缺失必填用户引用列→当前用户
MandatoryUserColumnFiller:insert 时按元数据(referenceid→SYS_USER+ismandatory) 对缺失的用户引用列填当前用户,仅"缺失才填"不覆盖显式值。 注:此为过渡兜底,已被「服务流分派」架构取代——服务流(如 OA_NOTICEMANAGE_ADD) 自身 getCurrentUserId 设拟稿人。仅服务于尚未转服务流分派的页(/api/entity/save), 全量转换器改服务流分派后移除。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.dsasp.server.common.exception.PrivilegeException;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
import com.eip4.platform.metadata.write.MandatoryUserColumnFiller;
|
||||
import com.eip4.platform.metadata.write.WritePrivilegeChecker;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -49,13 +50,16 @@ public class EntityWriteController {
|
||||
private final POJOCRUDService pojoCRUDService;
|
||||
private final IdentifierWhitelist identifierWhitelist;
|
||||
private final WritePrivilegeChecker writePrivilegeChecker;
|
||||
private final MandatoryUserColumnFiller mandatoryUserColumnFiller;
|
||||
|
||||
public EntityWriteController(POJOCRUDService pojoCRUDService,
|
||||
IdentifierWhitelist identifierWhitelist,
|
||||
WritePrivilegeChecker writePrivilegeChecker) {
|
||||
WritePrivilegeChecker writePrivilegeChecker,
|
||||
MandatoryUserColumnFiller mandatoryUserColumnFiller) {
|
||||
this.pojoCRUDService = pojoCRUDService;
|
||||
this.identifierWhitelist = identifierWhitelist;
|
||||
this.writePrivilegeChecker = writePrivilegeChecker;
|
||||
this.mandatoryUserColumnFiller = mandatoryUserColumnFiller;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,6 +103,9 @@ public class EntityWriteController {
|
||||
if (pkVal != null && !pkVal.toString().isBlank()) {
|
||||
saved = pojoCRUDService.update(normalized, t, ctx);
|
||||
} else {
|
||||
// 新增路径:回填缺失的「必填用户引用列」(如 OA_NOTICEMANAGE.USER_ID 拟稿人=当前用户),
|
||||
// 否则引擎必填校验抛「…是必填项」→ 422。仅缺失才填、绝不覆盖用户显式传值。
|
||||
mandatoryUserColumnFiller.fillForInsert(t, normalized, ctx);
|
||||
saved = pojoCRUDService.save(normalized, t, ctx);
|
||||
}
|
||||
String savedId = saved != null ? saved.get_ID() : null;
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.eip4.platform.metadata.write;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
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.dsasp.server.common.user.service.LoggedInUser;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
/**
|
||||
* F2 写侧缺陷修复:新增时自动回填「必填的用户引用列」为当前登录用户。
|
||||
*
|
||||
* <p><b>背景缺陷</b>:{@code POST /api/entity/OA_NOTICEMANAGE/save} 新增通知返回
|
||||
* 422「拟稿人是必填项」。{@code OA_NOTICEMANAGE.USER_ID}(displayname=拟稿人)元数据
|
||||
* {@code ismandatory=1} 且 {@code referenceid→SYS_USER},但语义是「拟稿人=起草人=当前用户」,
|
||||
* 表单不出现该字段、也不该手填。引擎 {@code GeneralPO.setStandardDefaults()} 只对
|
||||
* 审计列(CREATEBY/UPDATEBY/CREATETIME/UPDATETIME/DEPARTID/ORGID/ISACTIVE/VERSION)
|
||||
* 兜底当前用户/默认值,<strong>不认识「拟稿人」类业务用户引用列</strong>,故 USER_ID 保持 null,
|
||||
* 命中 {@code GeneralPO.set_Value} 的必填校验({@code "...是必填项"})→ ValidationException → 422。
|
||||
*
|
||||
* <p><b>填充规则(元数据驱动、缺失才填、绝不覆盖)</b>:
|
||||
* <ul>
|
||||
* <li>仅<strong>新增路径</strong>(无主键)调用——更新场景拟稿人已固化,不动。</li>
|
||||
* <li>列必须是「必填的用户引用列」:{@code SYS_ENTITYATTRIBUTE.ismandatory=1} 且
|
||||
* {@code referenceid → SYS_REFERENCE → SYS_ENTITY.tablename ∈ {SYS_USER, VIEW_SYS_USER}}
|
||||
* (与 {@link com.eip4.platform.metadata.flow.UserColumnResolver#queryUserColumns} 同口径,
|
||||
* 仅多一道 {@code ismandatory=1} 收口,避免误伤可选的「指派人/审批人/接收人」类用户列)。</li>
|
||||
* <li>排除 {@code CREATEBY}/{@code UPDATEBY}:引擎 {@code setStandardDefaults} 已兜底,重复填多余。</li>
|
||||
* <li><strong>仅当提交体里该列缺失/空白才填</strong>——用户若显式给了值(如代起草指定他人),保留原值不覆盖。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p><b>范围与风险</b>:全库实测「必填用户引用列(除 CREATEBY/UPDATEBY)」共 207 列 / 189 表。
|
||||
* 本填充器是<strong>通用规则</strong>而非 OA_NOTICEMANAGE 窄修,但靠「缺失才填、绝不覆盖、仅新增」
|
||||
* 三重收口把风险压到最低:它只在「必填用户列且用户没传」时补当前用户,等价于「起草人=当前用户」
|
||||
* 的通用语义;任何显式传值或更新场景都不受影响。OA_NOTICEMANAGE 仅 USER_ID 命中。
|
||||
*
|
||||
* <p><b>缓存</b>:表名(大写)→ 待回填列名集合(大写),与 {@link com.eip4.platform.metadata.flow.UserColumnResolver}
|
||||
* 同 Caffeine 策略;空集合亦缓存避免重复查元数据。元数据/用户变更后由 {@link #evictAll()} 失效
|
||||
* (对齐 {@code /api/meta/evict} 钩子口径)。
|
||||
*
|
||||
* <p>纪律:零改 {@code com.dsasp};只查元数据不裸写业务表,写仍全走引擎 save。
|
||||
*/
|
||||
@Component
|
||||
public class MandatoryUserColumnFiller {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MandatoryUserColumnFiller.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
/** 表名(大写)→ 必填用户引用列名集合(大写,已排除 CREATEBY/UPDATEBY)。空集合亦缓存。 */
|
||||
private final Cache<String, Set<String>> mandatoryUserColumnsByTable = Caffeine.newBuilder()
|
||||
.maximumSize(2_000)
|
||||
.build();
|
||||
|
||||
public MandatoryUserColumnFiller(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增路径就地回填:把提交体里缺失/空白的「必填用户引用列」填为当前登录用户 id。
|
||||
*
|
||||
* <p>仅在新增(无主键)时调用。会就地修改 {@code body}。
|
||||
*
|
||||
* @param table 实体表名(任意大小写;null/空 → 跳过)
|
||||
* @param body 归一化为大写键的提交体({@code EntityWriteController.normalizeKeys} 之后),会被就地修改
|
||||
* @param ctx 当前请求引擎上下文(取 {@code CURRENTUSER})
|
||||
*/
|
||||
public void fillForInsert(String table, Map<String, Object> body, Properties ctx) {
|
||||
if (table == null || table.isBlank() || body == null || ctx == null) {
|
||||
return;
|
||||
}
|
||||
Set<String> cols = mandatoryUserColumnsForTable(table);
|
||||
if (cols.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String currentUser = ctx.getProperty(LoggedInUser.CURRENTUSER, "");
|
||||
if (currentUser == null || currentUser.isBlank()) {
|
||||
// 未绑定登录用户(异常/匿名)→ 不强填,交由引擎必填校验如实报错
|
||||
return;
|
||||
}
|
||||
currentUser = currentUser.trim();
|
||||
for (String col : cols) {
|
||||
// 仅当缺失/空白才填,绝不覆盖用户显式传入的值
|
||||
Object existing = body.get(col);
|
||||
if (existing == null || existing.toString().isBlank()) {
|
||||
body.put(col, currentUser);
|
||||
log.debug("[USER-FILL] table={} col={} <- currentUser={}", table, col, currentUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 该表的「必填用户引用列」集合(大写,排除 CREATEBY/UPDATEBY)。结果缓存(含空集合)。
|
||||
* 解析口径与 {@link com.eip4.platform.metadata.flow.UserColumnResolver#queryUserColumns} 一致,
|
||||
* 仅多 {@code ea.ismandatory=1} 收口。
|
||||
*/
|
||||
private Set<String> mandatoryUserColumnsForTable(String table) {
|
||||
return mandatoryUserColumnsByTable.get(table.toUpperCase(), this::queryMandatoryUserColumns);
|
||||
}
|
||||
|
||||
private Set<String> queryMandatoryUserColumns(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 ea.ismandatory = 1 "
|
||||
+ "AND UPPER(e2.tablename) IN ('SYS_USER', 'VIEW_SYS_USER') "
|
||||
+ "AND UPPER(ea.columnname) NOT IN ('CREATEBY', 'UPDATEBY')",
|
||||
String.class, upperTable);
|
||||
if (cols.isEmpty()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
Set<String> set = new HashSet<>(cols);
|
||||
log.debug("[USER-FILL] table={} mandatoryUserRefCols={}", upperTable, set);
|
||||
return set;
|
||||
} catch (org.springframework.dao.DataAccessException ex) {
|
||||
// 元数据查询异常(库不可用等)→ 视作无可填列,绝不阻断写路径(交引擎校验如实报错)
|
||||
log.warn("[USER-FILL] 解析必填用户引用列失败 table={}: {}", upperTable, ex.getMessage());
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
/** 失效全部缓存(与 /api/meta/evict 钩子对齐:元数据/用户改动后重建)。 */
|
||||
public void evictAll() {
|
||||
mandatoryUserColumnsByTable.invalidateAll();
|
||||
}
|
||||
|
||||
/** 失效某表的缓存(表结构/引用关系/必填标志变更时)。 */
|
||||
public void evictTable(String table) {
|
||||
if (table != null && !table.isBlank()) {
|
||||
mandatoryUserColumnsByTable.invalidate(table.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
|
||||
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
import com.eip4.platform.metadata.security.IllegalIdentifierException;
|
||||
import com.eip4.platform.metadata.write.MandatoryUserColumnFiller;
|
||||
import com.eip4.platform.metadata.write.WritePrivilegeChecker;
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,8 @@ class EntityWriteControllerTest {
|
||||
when(engine.save(any(Map.class), eq(TABLE), any(Properties.class))).thenReturn(fakePojo);
|
||||
when(checker.isAdmin(any())).thenReturn(true); // ADMIN 跳过权限
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("NAME", "E2E-测试角色");
|
||||
|
||||
@@ -95,7 +97,8 @@ class EntityWriteControllerTest {
|
||||
when(checker.isAdmin(any())).thenReturn(false);
|
||||
doNothing().when(checker).checkCanWrite(eq(TABLE), any(GeneralPOJO.class), any(Properties.class));
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("SYS_ROLE_ID", "existing-id");
|
||||
body.put("NAME", "E2E-更新角色");
|
||||
@@ -121,7 +124,8 @@ class EntityWriteControllerTest {
|
||||
doThrow(new IllegalIdentifierException("非法实体或元数据不存在: EVIL"))
|
||||
.when(wl).requireLegalTable(eq("EVIL"));
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = Map.of("NAME", "test");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertThrows(IllegalIdentifierException.class,
|
||||
@@ -136,7 +140,8 @@ class EntityWriteControllerTest {
|
||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||
WritePrivilegeChecker checker = mock(WritePrivilegeChecker.class);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = c.save(TABLE, null);
|
||||
|
||||
@@ -161,7 +166,8 @@ class EntityWriteControllerTest {
|
||||
doThrow(new ResponseStatusException(HttpStatus.FORBIDDEN, "没有该数据的写权限(行级规则不满足)"))
|
||||
.when(checker).checkCanWrite(eq(TABLE), any(GeneralPOJO.class), any(Properties.class));
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("SYS_ROLE_ID", "restricted-id");
|
||||
body.put("NAME", "E2E-越权更新");
|
||||
@@ -190,7 +196,8 @@ class EntityWriteControllerTest {
|
||||
doNothing().when(checker).checkCanWrite(eq(TABLE), any(GeneralPOJO.class), any(Properties.class));
|
||||
when(engine.delete(eq("del-001"), eq(TABLE), any(Properties.class))).thenReturn(fakeRow);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = Map.of("id", "del-001");
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = c.delete(TABLE, body);
|
||||
@@ -215,7 +222,8 @@ class EntityWriteControllerTest {
|
||||
when(checker.isAdmin(any())).thenReturn(true);
|
||||
when(engine.delete(anyString(), eq(TABLE), any(Properties.class))).thenReturn(fakeRow);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = Map.of("ids", "del-001,del-002");
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = c.delete(TABLE, body);
|
||||
@@ -233,7 +241,8 @@ class EntityWriteControllerTest {
|
||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||
WritePrivilegeChecker checker = mock(WritePrivilegeChecker.class);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = c.delete(TABLE, new HashMap<>());
|
||||
|
||||
@@ -255,7 +264,8 @@ class EntityWriteControllerTest {
|
||||
doThrow(new ResponseStatusException(HttpStatus.FORBIDDEN, "没有该数据的写权限(无命中角色规则)"))
|
||||
.when(checker).checkCanWrite(eq(TABLE), any(GeneralPOJO.class), any(Properties.class));
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker);
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker,
|
||||
mock(MandatoryUserColumnFiller.class));
|
||||
Map<String, Object> body = Map.of("id", "forbidden-id");
|
||||
|
||||
ResponseStatusException ex = org.junit.jupiter.api.Assertions.assertThrows(
|
||||
@@ -264,4 +274,72 @@ class EntityWriteControllerTest {
|
||||
assertEquals(HttpStatus.FORBIDDEN, ex.getStatusCode());
|
||||
verify(engine, never()).delete(anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
// -------------------------------------------------- 必填用户引用列回填(拟稿人=当前用户)
|
||||
|
||||
@Test
|
||||
@DisplayName("10. save 新增缺 USER_ID → 回填器被调用并把当前用户写入提交体,再交引擎 save")
|
||||
void saveInsertFillsMandatoryUserColumn() {
|
||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||
WritePrivilegeChecker checker = mock(WritePrivilegeChecker.class);
|
||||
MandatoryUserColumnFiller filler = mock(MandatoryUserColumnFiller.class);
|
||||
|
||||
// 模拟回填器把缺失的 USER_ID 填为当前用户(就地修改归一化后的提交体)
|
||||
org.mockito.Mockito.doAnswer(inv -> {
|
||||
Map<String, Object> b = inv.getArgument(1);
|
||||
b.put("USER_ID", "current-user-id");
|
||||
return null;
|
||||
}).when(filler).fillForInsert(eq("OA_NOTICEMANAGE"), any(Map.class), any(Properties.class));
|
||||
|
||||
GeneralPOJO savedPojo = mock(GeneralPOJO.class);
|
||||
when(savedPojo.get_ID()).thenReturn("notice-uuid-001");
|
||||
org.mockito.ArgumentCaptor<Map> captor = org.mockito.ArgumentCaptor.forClass(Map.class);
|
||||
when(engine.save(captor.capture(), eq("OA_NOTICEMANAGE"), any(Properties.class)))
|
||||
.thenReturn(savedPojo);
|
||||
when(checker.isAdmin(any())).thenReturn(true);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker, filler);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("TITLE", "测试通知");
|
||||
body.put("NOTICECONTENT", "正文");
|
||||
// 注意:提交体不含 USER_ID(拟稿人不该手填)
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = c.save("OA_NOTICEMANAGE", body);
|
||||
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
// 回填器在新增路径被调用
|
||||
verify(filler).fillForInsert(eq("OA_NOTICEMANAGE"), any(Map.class), any(Properties.class));
|
||||
// 引擎 save 收到的提交体已含回填的 USER_ID
|
||||
assertEquals("current-user-id", captor.getValue().get("USER_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("11. save 更新(有主键)→ 不调用回填器(拟稿人已固化,更新不动)")
|
||||
void saveUpdateDoesNotFill() {
|
||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||
WritePrivilegeChecker checker = mock(WritePrivilegeChecker.class);
|
||||
MandatoryUserColumnFiller filler = mock(MandatoryUserColumnFiller.class);
|
||||
|
||||
GeneralPOJO fakeRow = mock(GeneralPOJO.class);
|
||||
when(engine.loadPOJO(eq("notice-id"), eq("OA_NOTICEMANAGE"), any(Properties.class), anyBoolean()))
|
||||
.thenReturn(fakeRow);
|
||||
GeneralPOJO savedPojo = mock(GeneralPOJO.class);
|
||||
when(savedPojo.get_ID()).thenReturn("notice-id");
|
||||
when(engine.update(any(Map.class), eq("OA_NOTICEMANAGE"), any(Properties.class)))
|
||||
.thenReturn(savedPojo);
|
||||
when(checker.isAdmin(any())).thenReturn(true);
|
||||
|
||||
EntityWriteController c = new EntityWriteController(engine, wl, checker, filler);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("OA_NOTICEMANAGE_ID", "notice-id");
|
||||
body.put("TITLE", "更新通知");
|
||||
|
||||
c.save("OA_NOTICEMANAGE", body);
|
||||
|
||||
verify(filler, never()).fillForInsert(anyString(), any(Map.class), any(Properties.class));
|
||||
verify(engine).update(any(Map.class), eq("OA_NOTICEMANAGE"), any(Properties.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package com.eip4.platform.metadata.write;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
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.when;
|
||||
|
||||
import java.util.HashMap;
|
||||
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.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||
|
||||
/**
|
||||
* {@link MandatoryUserColumnFiller} 单测(mock JdbcTemplate,不连库)。
|
||||
*
|
||||
* <p>覆盖填充器三重收口语义:
|
||||
* <ul>
|
||||
* <li>缺失的必填用户引用列 → 填当前用户</li>
|
||||
* <li>用户已显式传值 → 绝不覆盖</li>
|
||||
* <li>无必填用户引用列的表 → 不动</li>
|
||||
* <li>未绑定当前用户(空)→ 不强填(交引擎校验报错)</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>沿用本模块单测惯例:直接 new + mock 接口,不起 Spring 上下文。
|
||||
*/
|
||||
@SuppressWarnings({"unchecked"})
|
||||
class MandatoryUserColumnFillerTest {
|
||||
|
||||
private static final String TABLE = "OA_NOTICEMANAGE";
|
||||
|
||||
private static Properties ctxWithUser(String user) {
|
||||
Properties p = new Properties();
|
||||
if (user != null) {
|
||||
p.setProperty(LoggedInUser.CURRENTUSER, user);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("1. 缺 USER_ID(拟稿人)→ 回填为当前用户")
|
||||
void fillsMissingMandatoryUserColumn() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq(TABLE)))
|
||||
.thenReturn(List.of("USER_ID"));
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("TITLE", "测试通知");
|
||||
|
||||
filler.fillForInsert(TABLE, body, ctxWithUser("user-42"));
|
||||
|
||||
assertEquals("user-42", body.get("USER_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2. USER_ID 已显式传值 → 绝不覆盖")
|
||||
void doesNotOverwriteExistingValue() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq(TABLE)))
|
||||
.thenReturn(List.of("USER_ID"));
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("USER_ID", "explicit-drafter");
|
||||
|
||||
filler.fillForInsert(TABLE, body, ctxWithUser("user-42"));
|
||||
|
||||
assertEquals("explicit-drafter", body.get("USER_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("3. USER_ID 空白字符串 → 视为缺失,回填当前用户")
|
||||
void blankValueTreatedAsMissing() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq(TABLE)))
|
||||
.thenReturn(List.of("USER_ID"));
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("USER_ID", " ");
|
||||
|
||||
filler.fillForInsert(TABLE, body, ctxWithUser("user-42"));
|
||||
|
||||
assertEquals("user-42", body.get("USER_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("4. 表无必填用户引用列 → 提交体不动")
|
||||
void noMandatoryUserColumnsLeavesBodyUntouched() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq("SYS_ROLE")))
|
||||
.thenReturn(List.of());
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("NAME", "角色");
|
||||
|
||||
filler.fillForInsert("SYS_ROLE", body, ctxWithUser("user-42"));
|
||||
|
||||
assertFalse(body.containsKey("USER_ID"));
|
||||
assertEquals(1, body.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("5. 未绑定当前用户(空)→ 不强填,交引擎必填校验如实报错")
|
||||
void noCurrentUserDoesNotFill() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), eq(TABLE)))
|
||||
.thenReturn(List.of("USER_ID"));
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("TITLE", "测试通知");
|
||||
|
||||
filler.fillForInsert(TABLE, body, ctxWithUser(""));
|
||||
|
||||
assertFalse(body.containsKey("USER_ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("6. 元数据查询异常 → 静默放行(不阻断写路径),提交体不动")
|
||||
void metadataQueryFailureIsSilent() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
when(jdbc.queryForList(anyString(), eq(String.class), any()))
|
||||
.thenThrow(new org.springframework.dao.DataAccessResourceFailureException("db down"));
|
||||
|
||||
MandatoryUserColumnFiller filler = new MandatoryUserColumnFiller(jdbc);
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("TITLE", "测试通知");
|
||||
|
||||
filler.fillForInsert(TABLE, body, ctxWithUser("user-42"));
|
||||
|
||||
assertFalse(body.containsKey("USER_ID"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user