|
|
@@ -0,0 +1,462 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:隐私设置 / 数据共享范围(能力键 {@code privacy.setting.config},S-cfc-287,TDD-RED 阶段)。
|
|
|
+ *
|
|
|
+ * <p><b>需求 / 设计锚点</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>{@code tests/user-scenarios/应用场景总览.md} SC-P-66(隐私设置:设数据共享范围,最小权限保底);
|
|
|
+ * {@code tests/user-scenarios/02-parent.md} P-66;业务域 H 设置与通知</li>
|
|
|
+ * <li>{@code tests/test-cases/parent.md} TC-P-66(设数据共享范围 → 最小权限保底)、
|
|
|
+ * TC-P-66-E(非法范围 400;敏感字段最小权限保底不外露)、TC-P-66-P(越权访问他人数据 403/401)</li>
|
|
|
+ * <li>{@code docs/superpowers/api/API_REFERENCE.md} §4.15 统计与通知({@code /api/notification} list/read,
|
|
|
+ * 范围变更通知通道)+ §4.2 家庭与成员可见性({@code family-member-editable} /
|
|
|
+ * {@code show_to_family},数据共享范围的下游载体);模块:隐私设置</li>
|
|
|
+ * <li>{@code docs/tdd/orchestration/sc-key-map.md} SC-P-66 → {@code privacy.setting.config}(S-cfc-287)</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约目标(GREEN 阶段按此实现,本测试即验收口径)</b></p>
|
|
|
+ * <ol>
|
|
|
+ * <li>实体 {@code com.etotem.cfc.entity.PrivacySetting}、DTO
|
|
|
+ * {@code com.etotem.cfc.dto.PrivacySettingDTO}:字段 {@code userId / dataSharingScope /
|
|
|
+ * sensitiveFieldsExposed / allowFamilyView / notifyOnScopeChange}</li>
|
|
|
+ * <li>服务 {@code com.etotem.cfc.service.PrivacySettingService}:
|
|
|
+ * {@code getConfig(Long)} 返回 DTO(未配置回显最小权限默认值);{@code saveConfig(Long, DTO)} 保存;
|
|
|
+ * {@code effectiveSharingScope(Long userId, int requestedScope)} 返回
|
|
|
+ * {@code min(用户配置范围, 请求范围)}(不得超过用户配置,未配置/脏数据降级 0);
|
|
|
+ * {@code isSensitiveFieldExposed(Long userId)} 告知下游敏感字段是否可外露(恒 false)</li>
|
|
|
+ * <li>默认值(最小权限保底):{@code dataSharingScope=0}(0=仅自己 / 1=家庭成员 / 2=全部、
|
|
|
+ * 由用户主动放大)、{@code sensitiveFieldsExposed=0}、{@code allowFamilyView=0}、
|
|
|
+ * {@code notifyOnScopeChange=0}</li>
|
|
|
+ * <li>校验:{@code dataSharingScope} 仅允许 0/1/2,其他(含 -1/3)抛
|
|
|
+ * {@code IllegalArgumentException("数据共享范围不合法")};
|
|
|
+ * {@code sensitiveFieldsExposed=1} 一律拒绝,抛
|
|
|
+ * {@code IllegalArgumentException("敏感字段不可外露")}(TC-P-66-E)</li>
|
|
|
+ * <li>保底:即使配置被脏写为超范围值(如 99),{@code effectiveSharingScope} 仍降级为 0,
|
|
|
+ * {@code isSensitiveFieldExposed} 仍为 false(不得放大共享范围 / 外露敏感字段)</li>
|
|
|
+ * <li>控制器 {@code com.etotem.cfc.controller.PrivacySettingController}:
|
|
|
+ * POST {@code /api/privacy/setting/get|save},参数带 {@code @RequestAttribute("userId")}
|
|
|
+ * (只能读写本人配置,越权被拒,TC-P-66-P)</li>
|
|
|
+ * <li>DB:{@code schema.sql} 含 {@code privacy_settings} 表与上述配置列,全部 {@code DEFAULT 0}</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:本分支 src/main 尚无上述服务/DTO/控制器/表(已有 {@code ConsentController}
|
|
|
+ * 隐私授权同意与 {@code show_to_family} 可见性,但无独立的「数据共享范围配置 + 最小权限保底」载体),
|
|
|
+ * 全部用例以反射/结构探测断言失败(红),失败信息清晰可贴。
|
|
|
+ * 纯 Mockito + 反射,无 Spring 上下文 / 无 DB / 无外部服务。</p>
|
|
|
+ */
|
|
|
+class PrivacySettingConfigContractTest {
|
|
|
+
|
|
|
+ private static final String TAG = "RED[privacy.setting.config] ";
|
|
|
+
|
|
|
+ private static final String SERVICE = "com.etotem.cfc.service.PrivacySettingService";
|
|
|
+ private static final String ENTITY = "com.etotem.cfc.entity.PrivacySetting";
|
|
|
+ private static final String DTO = "com.etotem.cfc.dto.PrivacySettingDTO";
|
|
|
+ private static final String CONTROLLER = "com.etotem.cfc.controller.PrivacySettingController";
|
|
|
+ private static final String TABLE = "privacy_settings";
|
|
|
+
|
|
|
+ /** 数据共享范围枚举:0=仅自己(最小权限)/ 1=家庭成员 / 2=全部。 */
|
|
|
+ private static final int SCOPE_PRIVATE = 0;
|
|
|
+ private static final int SCOPE_FAMILY = 1;
|
|
|
+ private static final int SCOPE_ALL = 2;
|
|
|
+
|
|
|
+ private static final String MSG_BAD_SCOPE = "数据共享范围不合法";
|
|
|
+ private static final String MSG_SENSITIVE = "敏感字段不可外露";
|
|
|
+
|
|
|
+ private static final Long USER_ID = 100L;
|
|
|
+
|
|
|
+ // ==================== 正常流:契约载体应落地 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:PrivacySettingService 配置服务类应存在")
|
|
|
+ void service_classExists() {
|
|
|
+ requireClass(SERVICE, "配置服务 PrivacySettingService 不存在:契约要求提供"
|
|
|
+ + " getConfig/saveConfig/effectiveSharingScope/isSensitiveFieldExposed"
|
|
|
+ + "(SC-P-66 数据共享范围 + 最小权限保底)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:配置服务应提供 getConfig/saveConfig/effectiveSharingScope/isSensitiveFieldExposed 入口")
|
|
|
+ void service_hasContractMethods() {
|
|
|
+ Class<?> service = requireClass(SERVICE, "无法验证服务方法:配置服务类未落地。");
|
|
|
+ assertTrue(findMethod(service, "getConfig", 1) != null,
|
|
|
+ TAG + "应提供 getConfig(Long userId) 读取用户隐私配置(含最小权限默认值回显)。");
|
|
|
+ assertTrue(findMethod(service, "saveConfig", 2) != null,
|
|
|
+ TAG + "应提供 saveConfig(Long userId, DTO dto) 保存数据共享范围配置。");
|
|
|
+ Method effective = findMethod(service, "effectiveSharingScope", 2);
|
|
|
+ assertTrue(effective != null && effective.getReturnType() == int.class,
|
|
|
+ TAG + "应提供 int effectiveSharingScope(Long userId, int requestedScope)"
|
|
|
+ + " 返回 min(配置范围, 请求范围)(最小权限保底)。");
|
|
|
+ Method sensitive = findMethod(service, "isSensitiveFieldExposed", 1);
|
|
|
+ assertTrue(sensitive != null && sensitive.getReturnType() == boolean.class,
|
|
|
+ TAG + "应提供 boolean isSensitiveFieldExposed(Long userId)"
|
|
|
+ + " 告知下游敏感字段是否可外露(恒 false,TC-P-66-E)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:实体与 DTO 应声明五项配置字段")
|
|
|
+ void entityAndDto_haveConfigFields() {
|
|
|
+ String[] fields = {"userId", "dataSharingScope", "sensitiveFieldsExposed",
|
|
|
+ "allowFamilyView", "notifyOnScopeChange"};
|
|
|
+ for (String fqcn : new String[]{ENTITY, DTO}) {
|
|
|
+ Class<?> clazz = requireClass(fqcn, "配置载体未落地,无法验证字段契约。");
|
|
|
+ for (String field : fields) {
|
|
|
+ boolean found = false;
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(field)) {
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(found, TAG + simpleName(fqcn) + " 缺字段 " + field
|
|
|
+ + ":契约要求 userId/dataSharingScope/sensitiveFieldsExposed/"
|
|
|
+ + "allowFamilyView/notifyOnScopeChange(SC-P-66)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:未配置用户 getConfig 应回显最小权限默认值(全 0)")
|
|
|
+ void getConfig_unconfigured_returnsLeastPrivilegeDefaults() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ Object dto = invoke(service, "getConfig", 1, USER_ID);
|
|
|
+ assertNotNull(dto, TAG + "getConfig 未配置时应返回带默认值的 DTO,而非 null。");
|
|
|
+
|
|
|
+ assertEquals(SCOPE_PRIVATE, intProp(dto, "getDataSharingScope"),
|
|
|
+ TAG + "dataSharingScope 默认必须为 0(仅自己,最小权限保底;由用户主动放大)。");
|
|
|
+ assertEquals(0, intProp(dto, "getSensitiveFieldsExposed"),
|
|
|
+ TAG + "sensitiveFieldsExposed 默认必须为 0(敏感字段不外露)。");
|
|
|
+ assertEquals(0, intProp(dto, "getAllowFamilyView"),
|
|
|
+ TAG + "allowFamilyView 默认必须为 0(家庭成员不可见,对齐 show_to_family 保底口径)。");
|
|
|
+ assertEquals(0, intProp(dto, "getNotifyOnScopeChange"),
|
|
|
+ TAG + "notifyOnScopeChange 默认必须为 0(范围变更通知默认关闭)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:合法范围(家庭成员=1)配置应可保存")
|
|
|
+ void saveConfig_acceptsValidScope() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ Object dto = newDto();
|
|
|
+ setProp(dto, "setDataSharingScope", SCOPE_FAMILY);
|
|
|
+ setProp(dto, "setSensitiveFieldsExposed", 0);
|
|
|
+ setProp(dto, "setAllowFamilyView", 1);
|
|
|
+ setProp(dto, "setNotifyOnScopeChange", 0);
|
|
|
+
|
|
|
+ invoke(service, "saveConfig", 2, USER_ID, dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流:非法范围 + 敏感字段不外露 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1:saveConfig 应拒绝非法范围 -1(枚举越界)")
|
|
|
+ void saveConfig_rejectsScopeNegative() throws Exception {
|
|
|
+ assertRejectsBadScope(-1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-2:saveConfig 应拒绝非法范围 3(枚举越界)")
|
|
|
+ void saveConfig_rejectsScopeThree() throws Exception {
|
|
|
+ assertRejectsBadScope(3);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-3:即使设为完全公开(2),敏感字段仍不可外露")
|
|
|
+ void saveConfig_cannotExposeSensitiveFields() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ Object dto = newDto();
|
|
|
+ setProp(dto, "setDataSharingScope", SCOPE_ALL);
|
|
|
+ setProp(dto, "setSensitiveFieldsExposed", 1); // 试图连敏感字段一起公开 → 必须被拒
|
|
|
+ setProp(dto, "setAllowFamilyView", 1);
|
|
|
+ setProp(dto, "setNotifyOnScopeChange", 0);
|
|
|
+
|
|
|
+ try {
|
|
|
+ invoke(service, "saveConfig", 2, USER_ID, dto);
|
|
|
+ fail(TAG + "sensitiveFieldsExposed=1 被接受:违反「敏感字段最小权限保底不外露」"
|
|
|
+ + "(SC-P-66 / TC-P-66-E)。");
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ assertTrue(cause instanceof IllegalArgumentException,
|
|
|
+ TAG + "外露敏感字段应抛 IllegalArgumentException,实际=" + cause);
|
|
|
+ assertTrue(cause.getMessage() != null && cause.getMessage().contains(MSG_SENSITIVE),
|
|
|
+ TAG + "拒绝文案应含「" + MSG_SENSITIVE + "」,实际=" + cause.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流:最小权限保底(不放大 / 脏数据降级) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:effectiveSharingScope 不得超过用户配置,未配置恒为 0")
|
|
|
+ void effectiveSharingScope_neverExceedsConfigured() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ // 未配置:Mapper.selectOne 默认返回 null → 请求完全公开也只能拿到最小权限 0
|
|
|
+ assertEquals(SCOPE_PRIVATE, intInvoke(service, "effectiveSharingScope", 2, USER_ID, SCOPE_ALL),
|
|
|
+ TAG + "未配置用户请求范围 2 时,effective 必须降级为 0(最小权限保底)。");
|
|
|
+
|
|
|
+ // 配置为家庭成员(1):请求 2 也只能拿到 1;请求 0 则为 0
|
|
|
+ stubMapperConfig(service, configEntity(SCOPE_FAMILY, 0, 1, 0));
|
|
|
+ assertEquals(SCOPE_FAMILY, intInvoke(service, "effectiveSharingScope", 2, USER_ID, SCOPE_ALL),
|
|
|
+ TAG + "配置范围 1 时请求 2,effective 必须被夹到 1(不得超过用户配置)。");
|
|
|
+ assertEquals(SCOPE_PRIVATE, intInvoke(service, "effectiveSharingScope", 2, USER_ID, SCOPE_PRIVATE),
|
|
|
+ TAG + "请求范围 0 时 effective 应为 0。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:脏配置范围 99 应降级为 0,不得放大共享范围")
|
|
|
+ void effectiveSharingScope_dirtyConfigDowngradesToLeastPrivilege() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ stubMapperConfig(service, configEntity(99, 0, 0, 0));
|
|
|
+ assertEquals(SCOPE_PRIVATE, intInvoke(service, "effectiveSharingScope", 2, USER_ID, SCOPE_ALL),
|
|
|
+ TAG + "脏配置范围 99 必须降级为 0(最小权限保底),不得被当作「完全公开」放大。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-3:脏配置 sensitiveFieldsExposed=1 时敏感字段仍不外露")
|
|
|
+ void isSensitiveFieldExposed_dirtyConfigStillFalse() throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ stubMapperConfig(service, configEntity(SCOPE_ALL, 1, 1, 0));
|
|
|
+ assertEquals(Boolean.FALSE, invoke(service, "isSensitiveFieldExposed", 1, USER_ID),
|
|
|
+ TAG + "即使落库脏数据 sensitiveFieldsExposed=1,仍须返回 false"
|
|
|
+ + "(敏感字段最小权限保底不外露,TC-P-66-E)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流:本人配置只能本人读写 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-1:设置端点应为 POST /api/privacy/setting/get|save 且按 userId 隔离")
|
|
|
+ void controller_scopesConfigByUserId() {
|
|
|
+ Class<?> controller = requireClass(CONTROLLER,
|
|
|
+ "配置控制器未落地:无法验证「家长只能读写本人隐私设置」的权限契约(TC-P-66-P)。");
|
|
|
+
|
|
|
+ for (String suffix : new String[]{"/get", "/save"}) {
|
|
|
+ String path = "/api/privacy/setting" + suffix;
|
|
|
+ boolean mapped = false;
|
|
|
+ boolean userIdScoped = false;
|
|
|
+ for (Method m : controller.getDeclaredMethods()) {
|
|
|
+ PostMapping post = m.getAnnotation(PostMapping.class);
|
|
|
+ if (post == null || !Arrays.asList(post.value()).contains(path)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ mapped = true;
|
|
|
+ for (Annotation[] paramAnns : m.getParameterAnnotations()) {
|
|
|
+ for (Annotation ann : paramAnns) {
|
|
|
+ if (ann instanceof RequestAttribute
|
|
|
+ && "userId".equals(((RequestAttribute) ann).value())) {
|
|
|
+ userIdScoped = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(mapped, TAG + "缺少 POST " + path + " 端点(SC-P-66 隐私设置读写)。");
|
|
|
+ assertTrue(userIdScoped, TAG + "POST " + path
|
|
|
+ + " 参数须带 @RequestAttribute(\"userId\"),按当前登录用户隔离配置,禁止跨用户读写(TC-P-66-P)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== DB 迁移契约 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("DB 迁移-1:schema.sql 应含 privacy_settings 表及五项配置列(默认全 0)")
|
|
|
+ void schemaSql_hasPrivacySettingsTableWithLeastPrivilegeDefaults() {
|
|
|
+ String schema = readSchemaSql();
|
|
|
+ int idx = schema.indexOf("CREATE TABLE IF NOT EXISTS " + TABLE);
|
|
|
+ if (idx < 0) {
|
|
|
+ idx = schema.indexOf("CREATE TABLE `" + TABLE + "`");
|
|
|
+ }
|
|
|
+ assertTrue(idx >= 0, TAG + "schema.sql 缺 " + TABLE + " 建表(隐私设置持久化未落地)。");
|
|
|
+ String tableDef = schema.substring(idx, Math.min(idx + 1500, schema.length()));
|
|
|
+
|
|
|
+ assertColumnDefaultZero(tableDef, "user_id");
|
|
|
+ assertColumnDefaultZero(tableDef, "data_sharing_scope");
|
|
|
+ assertColumnDefaultZero(tableDef, "sensitive_fields_exposed");
|
|
|
+ assertColumnDefaultZero(tableDef, "allow_family_view");
|
|
|
+ assertColumnDefaultZero(tableDef, "notify_on_scope_change");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 辅助 ====================
|
|
|
+
|
|
|
+ private void assertRejectsBadScope(int badScope) throws Exception {
|
|
|
+ Object service = newServiceWithMockMapper();
|
|
|
+ Object dto = newDto();
|
|
|
+ setProp(dto, "setDataSharingScope", badScope);
|
|
|
+ setProp(dto, "setSensitiveFieldsExposed", 0);
|
|
|
+ setProp(dto, "setAllowFamilyView", 0);
|
|
|
+ setProp(dto, "setNotifyOnScopeChange", 0);
|
|
|
+
|
|
|
+ try {
|
|
|
+ invoke(service, "saveConfig", 2, USER_ID, dto);
|
|
|
+ fail(TAG + "非法数据共享范围 " + badScope + " 未被拒绝:仅允许 0/1/2"
|
|
|
+ + "(SC-P-66 / TC-P-66-E)。");
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ assertTrue(cause instanceof IllegalArgumentException,
|
|
|
+ TAG + "非法范围 " + badScope + " 应抛 IllegalArgumentException,实际=" + cause);
|
|
|
+ assertEquals(MSG_BAD_SCOPE, cause.getMessage(), TAG + "非法范围报错文案");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertColumnDefaultZero(String tableDef, String col) {
|
|
|
+ for (String line : tableDef.split("\\r?\\n")) {
|
|
|
+ if (line.contains(col)) {
|
|
|
+ assertTrue(line.contains("DEFAULT 0"),
|
|
|
+ TAG + TABLE + "." + col + " 默认必须为 0(最小权限保底),实际定义=" + line.trim());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail(TAG + TABLE + " 缺列 " + col + "(SC-P-66 配置契约)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 无配置用户:service 的 Mapper 字段全部注入 mock(selectOne 默认返回 null)。 */
|
|
|
+ private Object newServiceWithMockMapper() throws Exception {
|
|
|
+ Class<?> clazz = requireClass(SERVICE, "无法构造配置服务:服务类未落地。");
|
|
|
+ Object service = clazz.getDeclaredConstructor().newInstance();
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if (f.getType().isInterface() && f.getType().getName().endsWith("Mapper")) {
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(service, mock(f.getType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return service;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 把 mock Mapper 的 selectOne 桩为返回指定配置,用于「已配置 / 脏配置」边界场景。 */
|
|
|
+ private void stubMapperConfig(Object service, Object config) throws Exception {
|
|
|
+ Field mapperField = null;
|
|
|
+ for (Field f : service.getClass().getDeclaredFields()) {
|
|
|
+ if (f.getType().isInterface() && f.getType().getName().endsWith("Mapper")) {
|
|
|
+ mapperField = f;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertNotNull(mapperField, TAG + "配置服务未注入 Mapper,无法读取用户配置。");
|
|
|
+ mapperField.setAccessible(true);
|
|
|
+ Object mapper = mapperField.get(service);
|
|
|
+ Method selectOne = null;
|
|
|
+ for (Method m : mapper.getClass().getMethods()) {
|
|
|
+ if (m.getName().equals("selectOne") && m.getParameterCount() == 1) {
|
|
|
+ selectOne = m;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertNotNull(selectOne, TAG + "Mapper 缺 selectOne(Wrapper) 查询入口。");
|
|
|
+ try {
|
|
|
+ when(selectOne.invoke(mapper, new Object[]{null})).thenReturn(config);
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail(TAG + "无法桩接 Mapper.selectOne:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object configEntity(int scope, int sensitive, int familyView, int notify) throws Exception {
|
|
|
+ Class<?> clazz = requireClass(ENTITY, "无法构造配置实体:实体未落地。");
|
|
|
+ Object entity = clazz.getDeclaredConstructor().newInstance();
|
|
|
+ setProp(entity, "setUserId", USER_ID);
|
|
|
+ setProp(entity, "setDataSharingScope", scope);
|
|
|
+ setProp(entity, "setSensitiveFieldsExposed", sensitive);
|
|
|
+ setProp(entity, "setAllowFamilyView", familyView);
|
|
|
+ setProp(entity, "setNotifyOnScopeChange", notify);
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object newDto() throws Exception {
|
|
|
+ Class<?> clazz = requireClass(DTO, "无法构造配置 DTO:DTO 未落地。");
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> requireClass(String fqcn, String reason) {
|
|
|
+ try {
|
|
|
+ return Class.forName(fqcn, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ fail(TAG + reason + " 期望类:" + fqcn);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Method findMethod(Class<?> clazz, String name, int paramCount) {
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name) && m.getParameterCount() == paramCount) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object invoke(Object target, String name, int paramCount, Object... args) throws Exception {
|
|
|
+ Method m = findMethod(target.getClass(), name, paramCount);
|
|
|
+ assertNotNull(m, TAG + target.getClass().getName() + " 缺方法 " + name + "(参数 " + paramCount + ")。");
|
|
|
+ m.setAccessible(true);
|
|
|
+ return m.invoke(target, args);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int intInvoke(Object target, String name, int paramCount, Object... args) throws Exception {
|
|
|
+ Object value = invoke(target, name, paramCount, args);
|
|
|
+ assertTrue(value instanceof Number, TAG + name + " 应返回数值,实际=" + value);
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object prop(Object target, String getter) throws Exception {
|
|
|
+ try {
|
|
|
+ Method m = target.getClass().getMethod(getter);
|
|
|
+ return m.invoke(target);
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ fail(TAG + target.getClass().getName() + " 缺访问器 " + getter + "(配置字段契约未落地)。");
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int intProp(Object target, String getter) throws Exception {
|
|
|
+ Object value = prop(target, getter);
|
|
|
+ assertTrue(value instanceof Number, TAG + getter + " 应返回数值,实际=" + value);
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setProp(Object target, String setter, Object value) throws Exception {
|
|
|
+ Method found = null;
|
|
|
+ for (Method m : target.getClass().getMethods()) {
|
|
|
+ if (m.getName().equals(setter) && m.getParameterCount() == 1) {
|
|
|
+ found = m;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertNotNull(found, TAG + target.getClass().getName() + " 缺写入器 " + setter + "。");
|
|
|
+ found.invoke(target, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String simpleName(String fqcn) {
|
|
|
+ int idx = fqcn.lastIndexOf('.');
|
|
|
+ return idx >= 0 ? fqcn.substring(idx + 1) : fqcn;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String readSchemaSql() {
|
|
|
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream("schema.sql")) {
|
|
|
+ if (is == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
|
|
|
+ scanner.useDelimiter("\\A");
|
|
|
+ return scanner.hasNext() ? scanner.next() : "";
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|