|
|
@@ -0,0 +1,413 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.dto.FamilyMemberVO;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.FamilyRelationship;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberAttributesMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.service.FamilyRelationshipService;
|
|
|
+import com.etotem.cfc.service.RelationshipQualityService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:家庭关系图查看(能力键 {@code family.family.relationGraph})。
|
|
|
+ *
|
|
|
+ * <p><b>需求/设计溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-07 家庭关系图查看 ——
|
|
|
+ * 流程「打开关系图 → 节点 + 关系标签渲染」;边界「关系缺失的节点降级展示」。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.2 家庭与成员 +
|
|
|
+ * §4.20 家庭成员关系质量 —— {@code RelationshipQualityController}
|
|
|
+ * {@code POST /api/family/relationship/pairwise}(以指定成员为中心的所有成员关系),
|
|
|
+ * 返回 {@code Result<List<FamilyMemberVO>>},每行承载关系图节点渲染所需字段
|
|
|
+ * (id/nickname/gender/effectiveRole/memberType/relationship/relativeLabel + 三项评分)。</li>
|
|
|
+ * <li>前端契约:关系图节点渲染({@code components/FamilyRelationGraph.vue})消费
|
|
|
+ * {@code memberType}(节点形状 parent/child)与 {@code relativeLabel}(关系标签),
|
|
|
+ * 关系缺失时需有可渲染的降级展示({@code components/MemberSelector.vue} 的 pairwise 加载)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>被校验的契约行为</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>正常流:pairwise 返回全家庭成员节点行(含中心行),每行携带节点渲染字段;中心行关系标签「我」;
|
|
|
+ * 有关系行成员标签取关系表 relationshipKey;无 relationshipKey 时依辈分差推断标签;
|
|
|
+ * 配偶关系取 3 参重载「丈夫/妻子」;每行携带节点形状字段 memberType(parent/child,非空)。</li>
|
|
|
+ * <li>边界流:关系缺失成员三项评分降级为 0(不 null、不 500);关系缺失成员
|
|
|
+ * {@code relationship}/{@code relativeLabel} 必须有非空降级标签(节点可渲染);
|
|
|
+ * 中心成员不存在则拒绝。</li>
|
|
|
+ * <li>权限流:中心成员不属于查看者家庭则拒绝(仅本家庭成员可见);
|
|
|
+ * 查看者家庭归属不可核验(familyId 缺位)时 fail-closed,不得静默回退中心成员家庭。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito + 反射,无 Spring / 无 DB / 无外部服务。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>(实现未落地的契约缺口,GREEN 阶段补齐后转绿):</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>pairwise VO 未设置 {@code memberType}(节点形状字段缺失,前端全部节点退化为 child 圆形)。</li>
|
|
|
+ * <li>关系缺失成员 {@code relationship}/{@code relativeLabel} 为 null(降级展示标签未实现)。</li>
|
|
|
+ * <li>配偶关系未走 3 参重载({@code computeRelationshipLabel(generationValue, gender, relationType)}),
|
|
|
+ * 辈分差 0 时被解析为「兄弟/姐妹」而非「丈夫/妻子」。</li>
|
|
|
+ * <li>familyId 缺位时静默回退中心成员家庭返回数据(越权 fail-open,非 fail-closed)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p>已落地部分(本测试同时固化):pairwise 端点/入口存在、全成员节点行、中心行「我」标签、
|
|
|
+ * relationshipKey 标签、辈分差推断标签、关系缺失评分降级 0、跨家庭拒绝。</p>
|
|
|
+ */
|
|
|
+@DisplayName("契约级单测:family.family.relationGraph(SC-P-07 家庭关系图查看)")
|
|
|
+class FamilyRelationGraphContractTest {
|
|
|
+
|
|
|
+ /** 契约端点控制器(API_REFERENCE §4.20)。 */
|
|
|
+ private static final String CONTROLLER_CLASS = "com.etotem.cfc.controller.family.RelationshipQualityController";
|
|
|
+
|
|
|
+ /** 契约数据供给服务(pairwise)。 */
|
|
|
+ private static final String SERVICE_CLASS = "com.etotem.cfc.service.RelationshipQualityService";
|
|
|
+
|
|
|
+ /** 家庭/查看者标识。 */
|
|
|
+ private static final Long FAMILY_ID = 888L;
|
|
|
+ private static final Long CENTER_ID = 11L;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private FamilyMemberAttributesMapper familyMemberAttributesMapper;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private FamilyRelationshipService familyRelationshipService;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private RelationshipQualityService service;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ FamilyMember center = member(CENTER_ID, "爸爸", "male", 0, false);
|
|
|
+ when(familyMemberMapper.selectById(CENTER_ID)).thenReturn(center);
|
|
|
+ when(familyMemberMapper.selectList(any())).thenReturn(new ArrayList<>(baselineMembers()));
|
|
|
+ // 默认:中心成员无任何关系行(各用例按需再覆盖)
|
|
|
+ when(familyRelationshipService.getRelationsForMember(CENTER_ID))
|
|
|
+ .thenReturn(new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 契约锚点:端点与服务入口存在(反射结构探测) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:pairwise 端点应以 POST 提供于 /api/family/relationship/pairwise")
|
|
|
+ void 端点_pairwise以POST映射存在() {
|
|
|
+ Class<?> controller = loadClass(CONTROLLER_CLASS);
|
|
|
+ assertNotNull(controller, "[family.family.relationGraph] RelationshipQualityController 不存在(API_REFERENCE §4.20)。");
|
|
|
+ boolean found = false;
|
|
|
+ for (Method m : controller.getDeclaredMethods()) {
|
|
|
+ String path = postMappingPath(m);
|
|
|
+ if ("/pairwise".equals(path)) {
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(found, "[family.family.relationGraph] RelationshipQualityController 应提供 "
|
|
|
+ + "@PostMapping(\"/pairwise\") 端点(API_REFERENCE §4.20 pairwise)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:服务应提供 getPairwiseRelationships 入口,返回 List<FamilyMemberVO>")
|
|
|
+ void 服务_提供pairwise入口_返回FamilyMemberVO列表() {
|
|
|
+ Class<?> clazz = loadClass(SERVICE_CLASS);
|
|
|
+ assertNotNull(clazz, "[family.family.relationGraph] RelationshipQualityService 不存在。");
|
|
|
+ for (Method m : clazz.getMethods()) {
|
|
|
+ if (!"getPairwiseRelationships".equals(m.getName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (m.getParameterCount() != 2) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Type genericReturn = m.getGenericReturnType();
|
|
|
+ if (genericReturn instanceof ParameterizedType) {
|
|
|
+ Type[] args = ((ParameterizedType) genericReturn).getActualTypeArguments();
|
|
|
+ for (Type arg : args) {
|
|
|
+ if ("com.etotem.cfc.dto.FamilyMemberVO".equals(arg.getTypeName())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("[family.family.relationGraph] RelationshipQualityService.getPairwiseRelationships(...) "
|
|
|
+ + "应存在且返回 List<FamilyMemberVO>(关系图节点渲染数据源)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流:节点 + 关系标签渲染数据供给 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:pairwise 返回全家庭成员节点行(含中心行),节点字段齐全")
|
|
|
+ void pairwise_返回全部家庭成员节点行_含中心行() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ assertNotNull(rows, "pairwise 应返回成员节点行列表");
|
|
|
+ assertEquals(5, rows.size(), "家庭 5 名成员应全部返回(含中心成员)");
|
|
|
+ boolean centerPresent = false;
|
|
|
+ for (FamilyMemberVO vo : rows) {
|
|
|
+ assertNotNull(vo.getId(), "节点行应有 id(节点键)");
|
|
|
+ assertNotNull(vo.getNickname(), "节点行应有 nickname(节点显示)");
|
|
|
+ assertNotNull(vo.getEffectiveRole(), "节点行应有 effectiveRole(角色渲染)");
|
|
|
+ assertNotNull(vo.getIsSelf(), "节点行应有 isSelf(中心节点标记)");
|
|
|
+ if (CENTER_ID.equals(vo.getId())) {
|
|
|
+ centerPresent = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(centerPresent, "返回节点行应包含中心成员(isSelf 行)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:中心成员行关系标签应为「我」,isSelf=true")
|
|
|
+ void pairwise_中心成员行_关系标签为我_isSelf() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ FamilyMemberVO center = findRow(rows, CENTER_ID);
|
|
|
+ assertEquals("我", center.getRelationship(), "中心行 relationship 应为「我」");
|
|
|
+ assertEquals("我", center.getRelativeLabel(), "中心行 relativeLabel 应为「我」");
|
|
|
+ assertTrue(center.getIsSelf(), "中心行 isSelf 应为 true");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:有关系行成员,关系标签应取关系表 relationshipKey")
|
|
|
+ void pairwise_有关系行成员_标签取relationshipKey() {
|
|
|
+ FamilyRelationship rel = relationship(22L, null, "妈妈", -1, 90, 80, 70);
|
|
|
+ stubRelations(rel);
|
|
|
+
|
|
|
+ FamilyMemberVO row = findRow(service.getPairwiseRelationships(CENTER_ID, FAMILY_ID), 22L);
|
|
|
+ assertEquals("妈妈", row.getRelationship(), "有关系行成员标签应优先取 relationshipKey");
|
|
|
+ assertEquals("妈妈", row.getRelativeLabel(), "relativeLabel 应与 relationshipKey 一致");
|
|
|
+ assertEquals(new BigDecimal("90"), row.getTrustScore(), "三项评分应来自关系行");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:无 relationshipKey 时,依辈分差推断标签(generationValue=1 男→儿子)")
|
|
|
+ void pairwise_无relationshipKey_依辈分差推断标签() {
|
|
|
+ // 关系行指向 member 33(男,辈分差 1),relationshipKey 为空 → 依辈分差推断「儿子」
|
|
|
+ FamilyRelationship rel = relationship(33L, null, null, 1, 60, 50, 40);
|
|
|
+ stubRelations(rel);
|
|
|
+ when(familyRelationshipService.computeRelationshipLabel(1, "male")).thenReturn("儿子");
|
|
|
+
|
|
|
+ FamilyMemberVO row = findRow(service.getPairwiseRelationships(CENTER_ID, FAMILY_ID), 33L);
|
|
|
+ assertEquals("儿子", row.getRelationship(),
|
|
|
+ "无 relationshipKey 应依辈分差推断标签(computeRelationshipLabel(generationValue, gender))");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:每行应携带节点形状字段 memberType(parent/child,非空)")
|
|
|
+ void pairwise_每行节点形状字段memberType_非空白() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ for (FamilyMemberVO vo : rows) {
|
|
|
+ assertNotNull(vo.getMemberType(), "[family.family.relationGraph] pairwise 节点行未携带 memberType:"
|
|
|
+ + "关系图节点形状渲染依赖 memberType(parent/child),缺失时前端全部节点退化为 child 圆形"
|
|
|
+ + "(SC-P-07 节点渲染 / FamilyMemberVO.memberType)。id=" + vo.getId());
|
|
|
+ assertTrue("parent".equals(vo.getMemberType()) || "child".equals(vo.getMemberType()),
|
|
|
+ "[family.family.relationGraph] pairwise 节点行 memberType 应为 parent/child,实际="
|
|
|
+ + vo.getMemberType() + ",id=" + vo.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:配偶关系(relationType=spouse)应取 3 参重载标签「妻子」")
|
|
|
+ void pairwise_配偶关系_无relationshipKey时应取丈夫妻子() {
|
|
|
+ FamilyRelationship rel = relationship(22L, "spouse", null, 0, 0, 0, 0);
|
|
|
+ stubRelations(rel);
|
|
|
+ // 2 参(辈分差 0,女)真实行为为「姐妹」;3 参 spouse 重载为「妻子」
|
|
|
+ when(familyRelationshipService.computeRelationshipLabel(0, "female")).thenReturn("姐妹");
|
|
|
+ when(familyRelationshipService.computeRelationshipLabel(0, "female", "spouse")).thenReturn("妻子");
|
|
|
+
|
|
|
+ FamilyMemberVO row = findRow(service.getPairwiseRelationships(CENTER_ID, FAMILY_ID), 22L);
|
|
|
+ assertEquals("妻子", row.getRelationship(),
|
|
|
+ "[family.family.relationGraph] 配偶关系应走 computeRelationshipLabel(generationValue, gender, "
|
|
|
+ + "relationType) 3 参重载,辈分差 0 时输出「妻子」而非「姐妹」"
|
|
|
+ + "(FamilyRelationshipService.computeRelationshipLabel(R4))。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流:关系缺失的节点降级展示 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:中心成员不存在 → 拒绝(「成员不存在或不属于该家庭」)")
|
|
|
+ void pairwise_中心成员不存在_拒绝() {
|
|
|
+ when(familyMemberMapper.selectById(999L)).thenReturn(null);
|
|
|
+
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
+ () -> service.getPairwiseRelationships(999L, FAMILY_ID));
|
|
|
+ assertNotNull(ex.getMessage());
|
|
|
+ assertTrue(ex.getMessage().contains("成员不存在"),
|
|
|
+ "中心成员不存在应拒绝并提示成员不存在,实际=" + ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:关系缺失成员三项评分降级为 0(不 null、不 500)")
|
|
|
+ void pairwise_关系缺失成员_评分降级为零() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ // 女儿(44)无关系行,非中心 → 评分应降级为 0
|
|
|
+ FamilyMemberVO row = findRow(rows, 44L);
|
|
|
+ assertNotNull(row.getTrustScore(), "关系缺失成员 trustScore 应降级为 0(非 null)");
|
|
|
+ assertEquals(BigDecimal.ZERO.compareTo(row.getTrustScore()), 0,
|
|
|
+ "关系缺失成员 trustScore 应降级为 0(SC-P-07 边界:关系缺失的节点降级展示)");
|
|
|
+ assertEquals(BigDecimal.ZERO.compareTo(row.getIntimacyScore()), 0,
|
|
|
+ "关系缺失成员 intimacyScore 应降级为 0");
|
|
|
+ assertEquals(BigDecimal.ZERO.compareTo(row.getCommunicationScore()), 0,
|
|
|
+ "关系缺失成员 communicationScore 应降级为 0");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:关系缺失成员 relationship 应有非空降级标签(不得 null)")
|
|
|
+ void pairwise_关系缺失成员_relationship降级标签非空白() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ FamilyMemberVO row = findRow(rows, 44L);
|
|
|
+ assertNotNull(row.getRelationship(), "[family.family.relationGraph] 关系缺失成员 relationship 为 null:"
|
|
|
+ + "SC-P-07 边界「关系缺失的节点降级展示」要求节点仍可渲染关系标签(至少降级为通用「家人」类标签),"
|
|
|
+ + "当前实现未设置降级标签。id=" + row.getId());
|
|
|
+ assertFalse(row.getRelationship().isEmpty(),
|
|
|
+ "[family.family.relationGraph] 关系缺失成员 relationship 降级标签不得为空白,id=" + row.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:关系缺失成员 relativeLabel 应非空白,节点可渲染")
|
|
|
+ void pairwise_关系缺失成员_relativeLabel非空白_节点可渲染() {
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ FamilyMemberVO row = findRow(rows, 44L);
|
|
|
+ assertNotNull(row.getRelativeLabel(), "[family.family.relationGraph] 关系缺失成员 relativeLabel 为 null:"
|
|
|
+ + "前端关系图节点按 relativeLabel 渲染关系标签,缺失时节点降级展示标签未落地(SC-P-07 边界)。"
|
|
|
+ + "id=" + row.getId());
|
|
|
+ assertFalse(row.getRelativeLabel().isEmpty(),
|
|
|
+ "[family.family.relationGraph] 关系缺失成员 relativeLabel 降级标签不得为空白,id=" + row.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:单人家庭(仅中心成员)→ 至少返回中心行,不空、不 500")
|
|
|
+ void pairwise_单人家庭_仅中心行不空不500() {
|
|
|
+ when(familyMemberMapper.selectList(any())).thenReturn(
|
|
|
+ Collections.singletonList(member(CENTER_ID, "爸爸", "male", 0, false)));
|
|
|
+
|
|
|
+ List<FamilyMemberVO> rows = service.getPairwiseRelationships(CENTER_ID, FAMILY_ID);
|
|
|
+
|
|
|
+ assertNotNull(rows, "单人家庭 pairwise 不应返回空");
|
|
|
+ assertEquals(1, rows.size(), "单人家庭应仅返回中心行");
|
|
|
+ assertEquals("我", rows.get(0).getRelationship(), "仅中心行关系标签应为「我」");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流:仅本家庭成员可见 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:中心成员不属于传入家庭 → 拒绝(跨家庭不可见)")
|
|
|
+ void pairwise_中心成员不属于传入家庭_拒绝() {
|
|
|
+ // center 属家庭 888,传入 familyId=999(其他家庭)→ 应拒绝
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
+ () -> service.getPairwiseRelationships(CENTER_ID, 999L));
|
|
|
+ assertNotNull(ex.getMessage());
|
|
|
+ assertTrue(ex.getMessage().contains("不属于该家庭"),
|
|
|
+ "跨家庭 pairwise 应拒绝并提示不属于该家庭(仅本家庭成员可见),实际=" + ex.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:查看者家庭归属不可核验(familyId 缺位)→ fail-closed 拒绝,不得回退中心家庭")
|
|
|
+ void pairwise_viewer家庭核验缺位familyId空_应拒绝() {
|
|
|
+ // familyId=null 表示查看者家庭归属不可核验(controller 侧 userId→familyId 解析缺位);
|
|
|
+ // 按「仅本家庭成员可见」fail-closed,应拒绝而非静默回退 center.getFamilyId() 返回数据。
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
+ () -> service.getPairwiseRelationships(CENTER_ID, null));
|
|
|
+ assertNotNull(ex.getMessage(),
|
|
|
+ "[family.family.relationGraph] 查看者家庭归属不可核验(familyId 缺位)时应拒绝(fail-closed):"
|
|
|
+ + "当前实现在 familyId=null 时静默回退 center.getFamilyId() 返回数据,"
|
|
|
+ + "属越权 fail-open(SC-P-07 边界:仅本家庭成员可见)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 辅助:契约反射/实体构造/桩 ====================
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String postMappingPath(Method m) {
|
|
|
+ org.springframework.web.bind.annotation.PostMapping ann =
|
|
|
+ m.getAnnotation(org.springframework.web.bind.annotation.PostMapping.class);
|
|
|
+ if (ann == null || ann.value().length == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return ann.value()[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyMemberVO findRow(List<FamilyMemberVO> rows, Long id) {
|
|
|
+ for (FamilyMemberVO vo : rows) {
|
|
|
+ if (id.equals(vo.getId())) {
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("节点行未找到 id=" + id);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 基线家庭成员(家庭 888):中心 11(父)、妈妈 22、儿子 33、女儿 44(无关系)、哥哥 55(无关系)。 */
|
|
|
+ private List<FamilyMember> baselineMembers() {
|
|
|
+ List<FamilyMember> list = new ArrayList<>();
|
|
|
+ list.add(member(CENTER_ID, "爸爸", "male", 0, false));
|
|
|
+ list.add(member(22L, "妈妈", "female", -1, true));
|
|
|
+ list.add(member(33L, "儿子", "male", 1, false));
|
|
|
+ list.add(member(44L, "女儿", "female", 1, false));
|
|
|
+ list.add(member(55L, "哥哥", "male", 0, false));
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyMember member(Long id, String nickname, String gender, Integer generation, boolean spouse) {
|
|
|
+ FamilyMember fm = new FamilyMember();
|
|
|
+ fm.setId(id);
|
|
|
+ fm.setFamilyId(FAMILY_ID);
|
|
|
+ fm.setNickname(nickname);
|
|
|
+ fm.setGender(gender);
|
|
|
+ fm.setGeneration(generation);
|
|
|
+ fm.setIsSpouse(spouse);
|
|
|
+ return fm;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyRelationship relationship(Long toMemberId, String relationType, String relationshipKey,
|
|
|
+ Integer generationValue,
|
|
|
+ int trust, int intimacy, int communication) {
|
|
|
+ FamilyRelationship rel = new FamilyRelationship();
|
|
|
+ rel.setFromMemberId(CENTER_ID);
|
|
|
+ rel.setToMemberId(toMemberId);
|
|
|
+ rel.setGenerationValue(generationValue);
|
|
|
+ rel.setRelationType(relationType);
|
|
|
+ rel.setRelationshipKey(relationshipKey);
|
|
|
+ rel.setTrustScore(BigDecimal.valueOf(trust));
|
|
|
+ rel.setIntimacyScore(BigDecimal.valueOf(intimacy));
|
|
|
+ rel.setCommunicationScore(BigDecimal.valueOf(communication));
|
|
|
+ return rel;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stubRelations(FamilyRelationship rel) {
|
|
|
+ when(familyRelationshipService.getRelationsForMember(CENTER_ID))
|
|
|
+ .thenReturn(Collections.singletonList(rel));
|
|
|
+ }
|
|
|
+}
|