feat(metadata): createEntity 透传 tabletype(子表建模支持)
createEntity 接受可选 req.tabletype(Integer),非空时覆盖克隆源的 TABLETYPE。 子表(tabletype=1)+ 指向主表的引用列 = 主子表级联接线前提。 TDD: MetaControllerReferenceTest.createEntityPassesTableTypeThrough 捕获 保存的实体 map 断言 TABLETYPE=1。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -95,6 +95,12 @@ public class MetaController {
|
||||
if (entityType != null && !String.valueOf(entityType).isBlank()) {
|
||||
entityMap.put("ENTITYTYPE", String.valueOf(entityType).trim());
|
||||
}
|
||||
// 表类型:调用方传 tabletype(0=主表 1=子表 2=中间表)时覆盖克隆值,
|
||||
// 子表(tabletype=1)+ 一个指向主表的引用列 → 主子表级联(MasterDetailLinker 接线)。
|
||||
Object tableType = req.get("tabletype");
|
||||
if (tableType != null && !String.valueOf(tableType).isBlank()) {
|
||||
entityMap.put("TABLETYPE", Integer.parseInt(String.valueOf(tableType).trim()));
|
||||
}
|
||||
GeneralPOJO newEntity = (GeneralPOJO) dataDicService.save(entityMap, ctx);
|
||||
String entityId = newEntity.get_ID();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.eip4.platform.metadata.web;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -11,15 +12,20 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
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.mockito.ArgumentCaptor;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
|
||||
import com.dsasp.server.common.dataDic.service.DataDicService;
|
||||
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||
import com.dsasp.server.dataDic.bean.Entity;
|
||||
import com.eip4.platform.metadata.perf.BacklogIndexScriptGenerator;
|
||||
import com.eip4.platform.metadata.perf.IndexEnhancer;
|
||||
import com.eip4.platform.metadata.perf.PoInfoCacheInvalidator;
|
||||
@@ -72,6 +78,73 @@ class MetaControllerReferenceTest {
|
||||
eq(refId), eq("target-entity-id"), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("createEntity: 传 tabletype=1 → 实体 map 的 TABLETYPE 被覆盖为 1(子表)")
|
||||
void createEntityPassesTableTypeThrough() {
|
||||
DataDicService dataDic = mock(DataDicService.class);
|
||||
POJOCRUDService pojoCrud = mock(POJOCRUDService.class);
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
|
||||
// 目标表不存在(允许新建)
|
||||
when(dataDic.getSYSEntityByTableName("EIP4_CHILD")).thenReturn(null);
|
||||
// 克隆模板实体
|
||||
GeneralPOJO tplEntityPo = stubPojo("SYS_ENTITY", Map.of(
|
||||
"SYS_ENTITY_ID", "tpl-entity-id",
|
||||
"TABLETYPE", 0,
|
||||
"ENTITYTYPE", "usergroup-type"));
|
||||
Entity tplEnt = new Entity(tplEntityPo);
|
||||
when(dataDic.getSYSEntityByTableName("SYS_USERGROUP")).thenReturn(tplEnt);
|
||||
when(pojoCrud.loadPOJO(eq("tpl-entity-id"), eq("SYS_ENTITY"), any(), anyBoolean()))
|
||||
.thenReturn(tplEntityPo);
|
||||
|
||||
// 属性模板(COLUMNNAME=NAME 行)
|
||||
GeneralPOJO attrTplPo = stubPojo("SYS_ENTITYATTRIBUTE", Map.of(
|
||||
"SYS_ENTITYATTRIBUTE_ID", "attr-tpl-id",
|
||||
"COLUMNNAME", "NAME"));
|
||||
Map<String, Object> listResult = new HashMap<>();
|
||||
listResult.put(POJOCRUDService.LIST_RESULT, List.of(attrTplPo));
|
||||
when(pojoCrud.listSelect(any(), eq("SYS_ENTITYATTRIBUTE"), any())).thenReturn(listResult);
|
||||
|
||||
// 新实体 save → 返回带 id 的 POJO
|
||||
GeneralPOJO savedEntity = stubPojo("SYS_ENTITY", Map.of("SYS_ENTITY_ID", "new-entity-id"));
|
||||
ArgumentCaptor<Map<String, Object>> entityCaptor = ArgumentCaptor.forClass(Map.class);
|
||||
when(dataDic.save(entityCaptor.capture(), any())).thenReturn(savedEntity);
|
||||
// 属性 save → 返回任意 POJO(建表需读 REFERENCEID 等;给最小列)
|
||||
GeneralPOJO savedAttr = stubPojo("SYS_ENTITYATTRIBUTE",
|
||||
Map.of("COLUMNNAME", "DUMMY", "REFERENCEID", "NVARCHAR"));
|
||||
when(pojoCrud.save(any(), eq("SYS_ENTITYATTRIBUTE"), any())).thenReturn(savedAttr);
|
||||
|
||||
MetaController controller = new MetaController(dataDic, pojoCrud, jdbc,
|
||||
mock(IndexEnhancer.class), mock(PoInfoCacheInvalidator.class),
|
||||
mock(BacklogIndexScriptGenerator.class));
|
||||
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("tablename", "EIP4_CHILD");
|
||||
req.put("name", "子表");
|
||||
req.put("tabletype", 1);
|
||||
controller.createEntity(req);
|
||||
|
||||
assertEquals(1, entityCaptor.getValue().get("TABLETYPE"),
|
||||
"tabletype=1 应透传为实体 map 的 TABLETYPE");
|
||||
}
|
||||
|
||||
/** 构造一个最小 GeneralPOJO 桩:支持 get_ID/get_TableName/get_ColumnCount/get_ColumnName/get_Value。 */
|
||||
private static GeneralPOJO stubPojo(String tableName, Map<String, Object> cols) {
|
||||
GeneralPOJO po = mock(GeneralPOJO.class);
|
||||
org.mockito.Mockito.doReturn(tableName).when(po).get_TableName();
|
||||
List<String> names = List.copyOf(cols.keySet());
|
||||
org.mockito.Mockito.doReturn(names.size()).when(po).get_ColumnCount();
|
||||
for (int i = 0; i < names.size(); i++) {
|
||||
String name = names.get(i);
|
||||
org.mockito.Mockito.doReturn(name).when(po).get_ColumnName(i);
|
||||
org.mockito.Mockito.doReturn(cols.get(name)).when(po).get_Value(i);
|
||||
org.mockito.Mockito.doReturn(cols.get(name)).when(po).get_Value(name);
|
||||
}
|
||||
Object id = cols.get(tableName + "_ID");
|
||||
org.mockito.Mockito.doReturn(id == null ? null : String.valueOf(id)).when(po).get_ID();
|
||||
return po;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("resolveDescription: 无 description → 中性默认; 有则原样")
|
||||
void resolveDescriptionNeutralDefault() {
|
||||
|
||||
Reference in New Issue
Block a user