|
|
@@ -0,0 +1,421 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.GrowthPlan;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.GrowthPlanMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.GrowthPlanService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+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.Collection;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+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.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED):方案编辑调整 / 查看 / 版本历史(能力键 {@code plan.edit.adjust},业务域 D)。
|
|
|
+ *
|
|
|
+ * <p><b>需求/设计锚点</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md}
|
|
|
+ * SC-P-42(方案编辑/调整:编辑方案文本/任务项,编辑后状态标记已改)、
|
|
|
+ * SC-P-43(查看生成的方案:方案详情看板块/任务,空方案不白屏)、
|
|
|
+ * SC-P-48(方案版本/历史:版本列表对比,版本不可篡改)。</li>
|
|
|
+ * <li>用例:{@code tests/test-cases/parent.md} TC-P-42 / TC-P-43 / TC-P-48(正常)、
|
|
|
+ * TC-P-42-E / TC-P-43-E / TC-P-48-E(异常)、TC-P-42-P / TC-P-43-P / TC-P-48-P(权限)。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.6 成长档案
|
|
|
+ * {@code POST /api/growth/plan/{id}}(计划详情)、{@code /review}(审核计划);模块「方案管理 / 成长计划」。</li>
|
|
|
+ * <li>派发:{@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md} S-cfc-280(能力键契约新增)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>被校验的契约行为(三类流 × 三 SC)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>SC-P-42 正常:编辑方案文本/任务项 → 状态标记「已改」;异常:超长文本(>10000 字)400 提示;
|
|
|
+ * 权限:越权编辑他人方案 403 拒绝。</li>
|
|
|
+ * <li>SC-P-43 正常:方案详情返回板块/任务结构;异常:空方案返回占位(不 null/空白,不白屏)、
|
|
|
+ * 方案不存在 404;权限:跨家庭查看 403 拒绝。</li>
|
|
|
+ * <li>SC-P-48 正常:版本列表 + 版本对比;异常:历史版本只读(修改返回 403『版本不可篡改』)、
|
|
|
+ * 版本不存在 404;权限:跨家庭版本访问 403 拒绝。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>宿主契约(TDD-RED 锚点)</b>:{@code com.etotem.cfc.service.GrowthPlanService} ——
|
|
|
+ * 当前类<b>已存在</b>但仅具备 {@code createPlan/getById/updatePlan/reviewPlan} 等基础能力,
|
|
|
+ * <b>缺少「编辑标记已改」「版本历史」「空方案占位」契约方法</b>;{@code GrowthPlan} 实体无版本号/已改标记字段,
|
|
|
+ * schema.sql 无版本历史表,控制器无编辑/版本端点。本测试通过反射探测宿主契约方法(避免直接 import 不存在方法导致编译失败),
|
|
|
+ * 方法缺失即断言失败(红),暴露「方案编辑调整/版本历史未落地」这一真实缺陷;GREEN 阶段实现落地后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>契约方法签名(GREEN 实现方按此落地,宿主 {@code GrowthPlanService})</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>{@code GrowthPlan editPlanText(Long planId, String content, Long userId)}
|
|
|
+ * —— 编辑方案文本:持久化并标记 status="edited"(已改),版本号递增。</li>
|
|
|
+ * <li>{@code Object editPlanTasks(Long planId, List<Map<String,Object>> tasks, Long userId)}
|
|
|
+ * —— 编辑任务项:更新任务并标记方案已改。</li>
|
|
|
+ * <li>{@code Map<String,Object> getPlanDetailForView(Long planId, Long userId)}
|
|
|
+ * —— 方案详情视图:含 sections/tasks;空方案返回占位(empty=true / sections=[]),不返回 null;
|
|
|
+ * 不存在/越权以异常或错误态表达(404/403)。</li>
|
|
|
+ * <li>{@code List<Map<String,Object>> listVersions(Long planId, Long userId)}
|
|
|
+ * —— 版本列表(供对比),每项含版本号/时间。</li>
|
|
|
+ * <li>{@code Map<String,Object> compareVersions(Long planId, Long fromVersion, Long toVersion, Long userId)}
|
|
|
+ * —— 版本对比差异。</li>
|
|
|
+ * <li>{@code Map<String,Object> getVersion(Long versionId, Long userId)}
|
|
|
+ * —— 读取历史版本(只读)。</li>
|
|
|
+ * <li>{@code Object modifyVersion(Long versionId, Map<String,Object> changes, Long userId)}
|
|
|
+ * —— 历史版本不可篡改:必须拒绝(403『版本不可篡改』),不允许改写。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>配套结构契约</b>:{@code GrowthPlan} 新增 {@code version}(版本号)、{@code edited}/{@code editedStatus}
|
|
|
+ * (已改标记)字段;schema.sql 新增方案版本历史表(如 {@code growth_plan_version});
|
|
|
+ * {@code GrowthPlanController}({@code /api/growth/plan})新增编辑/版本端点。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯契约级单测,无 Spring 上下文 / 无 DB / 无外部服务;仅 Mockito mock 三个 Mapper 注入真实
|
|
|
+ * {@code GrowthPlanService}(仿 {@code PlanDraftSaveContractTest})。与同批 RED 契约测试
|
|
|
+ * (TeamProfitHookContractTest / TeamStarMultiplierContractTest / TeamProfitPipelineContractTest /
|
|
|
+ * FamilyJoinValidationRulesContractTest)同风格。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:上述契约方法/字段/端点缺失,全部用例失败(红),失败断言原文前缀 {@code RED[plan.edit.adjust]}。</p>
|
|
|
+ */
|
|
|
+class PlanEditAdjustContractTest extends AssessmentPlanContractTestBase {
|
|
|
+
|
|
|
+ /** 规格设计宿主类(已存在,缺编辑/版本契约方法)。 */
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.GrowthPlanService";
|
|
|
+
|
|
|
+ /** 控制器宿主类(已存在,缺编辑/版本端点)。 */
|
|
|
+ private static final String CTRL_CLASS = "com.etotem.cfc.controller.growth.GrowthPlanController";
|
|
|
+
|
|
|
+ private GrowthPlanService host;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ host = new GrowthPlanService();
|
|
|
+ inject(host, "growthPlanMapper", mock(GrowthPlanMapper.class));
|
|
|
+ inject(host, "familyMemberMapper", mock(FamilyMemberMapper.class));
|
|
|
+ inject(host, "userMapper", mock(UserMapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-42 正常:编辑文本/任务项 → 状态标记已改 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-42 正常-1:编辑方案文本 → status 标记已改(edited)")
|
|
|
+ void editPlanText_marksStatusEdited() {
|
|
|
+ Outcome o = invoke("editPlanText", 7L, "调整后的方案文本", 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] editPlanText 不应抛异常:" + o.error);
|
|
|
+ String status = statusOf(o.value);
|
|
|
+ assertTrue("edited".equals(status) || "modified".equals(status) || "已改".equals(status),
|
|
|
+ "[plan.edit.adjust] 编辑方案文本后状态应标记『已改』(status=edited/modified,TC-P-42 边界),实际:" + status);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-42 正常-2:编辑方案任务项 → 返回更新后的任务且方案标记已改")
|
|
|
+ void editPlanTasks_returnsUpdatedTasksAndMarksEdited() {
|
|
|
+ Map<String, Object> task = new HashMap<>();
|
|
|
+ task.put("id", 100L);
|
|
|
+ task.put("title", "每日阅读 20 分钟");
|
|
|
+ java.util.List<Map<String, Object>> tasks = new java.util.ArrayList<>();
|
|
|
+ tasks.add(task);
|
|
|
+
|
|
|
+ Outcome o = invoke("editPlanTasks", 7L, tasks, 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] editPlanTasks 不应抛异常:" + o.error);
|
|
|
+ assertNotNull(o.value, "[plan.edit.adjust] 编辑任务项应返回更新后的任务(TC-P-42 流程),不得为空");
|
|
|
+ assertTrue(isCollectionLike(o.value),
|
|
|
+ "[plan.edit.adjust] 编辑任务项返回值应承载任务列表/方案(可迭代或 Map),实际:" + typeOf(o.value));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-42 异常/权限:超长 400、越权 403 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-42-E:超长文本(>10000 字)→ 400 提示,不静默成功")
|
|
|
+ void editPlanText_oversizeText_rejected400() {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < 10001; i++) {
|
|
|
+ sb.append('字');
|
|
|
+ }
|
|
|
+ Outcome o = invoke("editPlanText", 7L, sb.toString(), 1L);
|
|
|
+ assertRejected(o, "超长", "[plan.edit.adjust] 超长文本(>10000 字)应以 400 提示拒绝,不得静默成功(TC-P-42-E)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-42-P:越权编辑他人方案 → 403 拒绝,不泄露/不改写")
|
|
|
+ void editPlanText_crossFamily_rejected403() {
|
|
|
+ Outcome o = invoke("editPlanText", 7L, "越权改写内容", 999L);
|
|
|
+ assertRejected(o, "无权限", "[plan.edit.adjust] 越权编辑他人/非绑定家庭方案应 403 拒绝(TC-P-42-P),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-43 正常:方案详情看板块/任务 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-43 正常:方案详情返回板块(sections)/任务(tasks)结构")
|
|
|
+ void getPlanDetailForView_containsSectionsAndTasks() {
|
|
|
+ Outcome o = invoke("getPlanDetailForView", 7L, 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] getPlanDetailForView 不应抛异常:" + o.error);
|
|
|
+ assertTrue(o.value instanceof Map,
|
|
|
+ "[plan.edit.adjust] 方案详情应返回结构化视图 Map(含 sections/tasks),实际:" + typeOf(o.value));
|
|
|
+ Map<?, ?> view = (Map<?, ?>) o.value;
|
|
|
+ assertTrue(view.containsKey("sections") || view.containsKey("tasks"),
|
|
|
+ "[plan.edit.adjust] 方案详情视图应含 sections(板块)或 tasks(任务)键(SC-P-43 流程:看板块/任务)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-43 异常/权限:空方案不白屏、不存在 404、越权 403 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-43-E:空方案详情 → 返回占位(empty/sections/tasks),不 null 不白屏")
|
|
|
+ void getPlanDetailForView_emptyPlan_placeholderNotBlank() {
|
|
|
+ Outcome o = invoke("getPlanDetailForView", 7L, 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] 空方案详情不应抛异常导致白屏:" + o.error);
|
|
|
+ assertNotNull(o.value, "[plan.edit.adjust] 空方案详情不得返回 null(SC-P-43 边界:空方案不白屏)");
|
|
|
+ assertTrue(o.value instanceof Map,
|
|
|
+ "[plan.edit.adjust] 空方案应返回结构化占位 Map,实际:" + typeOf(o.value));
|
|
|
+ Map<?, ?> view = (Map<?, ?>) o.value;
|
|
|
+ assertTrue(view.containsKey("empty") || view.containsKey("sections") || view.containsKey("tasks")
|
|
|
+ || view.containsKey("placeholder"),
|
|
|
+ "[plan.edit.adjust] 空方案详情应含 empty/sections/tasks/placeholder 占位键,禁止返回空 Map 导致前端白屏(TC-P-43-E)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-43-E:方案 id 不存在 → 404 提示")
|
|
|
+ void getPlanDetailForView_notExist_rejected404() {
|
|
|
+ Outcome o = invoke("getPlanDetailForView", 88888L, 1L);
|
|
|
+ assertRejected(o, "不存在", "[plan.edit.adjust] 方案不存在应以 404/『不存在』提示(TC-P-43-E),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-43-P:跨家庭查看方案详情 → 403 拒绝")
|
|
|
+ void getPlanDetailForView_crossFamily_rejected403() {
|
|
|
+ Outcome o = invoke("getPlanDetailForView", 7L, 999L);
|
|
|
+ assertRejected(o, "无权限", "[plan.edit.adjust] 跨家庭查看方案详情应 403 拒绝(TC-P-43-P),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-48 正常:版本列表 + 版本对比 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-48 正常-1:版本列表 → 返回可对比的版本序列")
|
|
|
+ void listVersions_returnsVersionList() {
|
|
|
+ Outcome o = invoke("listVersions", 7L, 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] listVersions 不应抛异常:" + o.error);
|
|
|
+ assertTrue(isCollectionLike(o.value),
|
|
|
+ "[plan.edit.adjust] 版本列表应返回可迭代的版本序列(供 SC-P-48 看版本列表→对比),实际:" + typeOf(o.value));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-48 正常-2:版本对比 → 返回差异 Map")
|
|
|
+ void compareVersions_returnsDiffMap() {
|
|
|
+ Outcome o = invoke("compareVersions", 7L, 1L, 2L, 1L);
|
|
|
+ assertNull(o.error, "[plan.edit.adjust] compareVersions 不应抛异常:" + o.error);
|
|
|
+ assertTrue(o.value instanceof Map,
|
|
|
+ "[plan.edit.adjust] 版本对比应返回差异 Map(SC-P-48 对比流程),实际:" + typeOf(o.value));
|
|
|
+ assertNotNull(o.value, "[plan.edit.adjust] 版本对比结果不得为 null");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-48 异常/权限:版本不可篡改、版本不存在、越权 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-48-E:历史版本只读 → 修改被拒 403『版本不可篡改』")
|
|
|
+ void modifyVersion_immutable_rejected403() {
|
|
|
+ Map<String, Object> changes = new HashMap<>();
|
|
|
+ changes.put("content", "试图篡改的历史内容");
|
|
|
+ Outcome o = invoke("modifyVersion", 5001L, changes, 1L);
|
|
|
+ assertRejected(o, "不可篡改",
|
|
|
+ "[plan.edit.adjust] 历史版本只读,修改应返回 403『版本不可篡改』(TC-P-48-E 硬性边界),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-48-E:版本 id 不存在 → 404 提示")
|
|
|
+ void getVersion_notExist_rejected404() {
|
|
|
+ Outcome o = invoke("getVersion", 99999L, 1L);
|
|
|
+ assertRejected(o, "不存在", "[plan.edit.adjust] 版本不存在应以 404/『不存在』提示(TC-P-48-E),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-48-P:跨家庭访问版本历史 → 403 拒绝")
|
|
|
+ void listVersions_crossFamily_rejected403() {
|
|
|
+ Outcome o = invoke("listVersions", 7L, 999L);
|
|
|
+ assertRejected(o, "无权限", "[plan.edit.adjust] 跨家庭访问方案版本历史应 403 拒绝(TC-P-48-P),实际失败信息:" + o.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 结构契约:实体字段 / schema 版本表 / 控制器端点 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构-1:GrowthPlan 实体应含版本号与已改标记字段")
|
|
|
+ void entity_growthPlan_hasVersionAndEditedFields() {
|
|
|
+ assertTrue(hasField(GrowthPlan.class, "version"),
|
|
|
+ "[plan.edit.adjust] GrowthPlan 应含 version 版本号字段(SC-P-48 版本历史/对比的持久化基础)");
|
|
|
+ assertTrue(hasField(GrowthPlan.class, "edited") || hasField(GrowthPlan.class, "isEdited")
|
|
|
+ || hasField(GrowthPlan.class, "editedStatus"),
|
|
|
+ "[plan.edit.adjust] GrowthPlan 应含已改标记字段(edited/isEdited/editedStatus,SC-P-42 编辑后状态标记已改)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构-2:schema.sql 应含方案版本历史表")
|
|
|
+ void schema_hasPlanVersionTable() {
|
|
|
+ String schema = readSchemaSql();
|
|
|
+ assertTrue(schema.contains("growth_plan_version") || schema.contains("growth_plans_version")
|
|
|
+ || schema.contains("plan_version"),
|
|
|
+ "[plan.edit.adjust] schema.sql 应含方案版本历史表(如 growth_plan_version),"
|
|
|
+ + "承载 SC-P-48 版本列表/对比且保证历史版本只读不可篡改(TC-P-48-E)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构-3:GrowthPlanController 应提供编辑/版本端点")
|
|
|
+ void controller_hasEditAndVersionEndpoints() {
|
|
|
+ Class<?> ctrl = loadClass(CTRL_CLASS);
|
|
|
+ assertNotNull(ctrl, "[plan.edit.adjust] 控制器 " + CTRL_CLASS + " 不存在,无法提供编辑/版本端点");
|
|
|
+ assertTrue(hasMapping(ctrl, "edit"),
|
|
|
+ "[plan.edit.adjust] GrowthPlanController(/api/growth/plan)应提供方案编辑端点"
|
|
|
+ + "(如 POST /api/growth/plan/edit,SC-P-42)");
|
|
|
+ assertTrue(hasMapping(ctrl, "version") || hasMapping(ctrl, "history"),
|
|
|
+ "[plan.edit.adjust] GrowthPlanController 应提供版本历史端点"
|
|
|
+ + "(如 POST /api/growth/plan/versions、/compare,SC-P-48)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助(锚定宿主契约,避免编译期依赖不存在方法) ====================
|
|
|
+
|
|
|
+ /** 按方法名 + 参数个数查找契约方法;缺失即断言失败(红)。 */
|
|
|
+ private Method requireMethod(String name, int argc) {
|
|
|
+ for (Method m : host.getClass().getMethods()) {
|
|
|
+ if (m.getName().equals(name) && m.getParameterCount() == argc) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("RED[plan.edit.adjust] 契约方法 " + HOST_CLASS + "#" + name + "(" + argc + "参) 未实现"
|
|
|
+ + "(方案编辑调整/查看/版本历史未落地,SC-P-42/43/48)");
|
|
|
+ return null; // unreachable
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 调用契约方法,捕获领域异常/拒绝为 Outcome.error,不因异常中断用例。 */
|
|
|
+ private Outcome invoke(String method, Object... args) {
|
|
|
+ Method m = requireMethod(method, args.length);
|
|
|
+ try {
|
|
|
+ return new Outcome(m.invoke(host, args), null);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ return new Outcome(null, cause == null ? "调用异常" : String.valueOf(cause.getMessage()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new Outcome(null, String.valueOf(e.getMessage()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 断言被拒绝:异常提示或返回式错误态须含语义关键词或 4xx。 */
|
|
|
+ private void assertRejected(Outcome o, String keyword, String message) {
|
|
|
+ if (o.error != null && !o.error.isEmpty()) {
|
|
|
+ String e = o.error;
|
|
|
+ assertTrue(e.contains(keyword) || e.contains("403") || e.contains("401") || e.contains("400")
|
|
|
+ || e.contains("404") || e.contains("无权限") || e.contains("不可篡改")
|
|
|
+ || e.contains("不存在") || e.contains("超长"),
|
|
|
+ message + "(实际异常提示:" + e + ")");
|
|
|
+ } else {
|
|
|
+ String desc = String.valueOf(o.value);
|
|
|
+ assertTrue(desc.contains(keyword) || desc.contains("403") || desc.contains("401")
|
|
|
+ || desc.contains("400") || desc.contains("404") || desc.contains("false")
|
|
|
+ || desc.contains("无权限") || desc.contains("不可篡改") || desc.contains("不存在")
|
|
|
+ || desc.contains("超长") || o.value == Boolean.FALSE,
|
|
|
+ message + "(实际返回:" + desc + ")");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String statusOf(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof GrowthPlan) {
|
|
|
+ return ((GrowthPlan) value).getStatus();
|
|
|
+ }
|
|
|
+ if (value instanceof Map) {
|
|
|
+ Object s = ((Map<?, ?>) value).get("status");
|
|
|
+ return s == null ? null : String.valueOf(s);
|
|
|
+ }
|
|
|
+ return String.valueOf(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isCollectionLike(Object value) {
|
|
|
+ return value instanceof Collection || value instanceof Map || (value != null && value.getClass().isArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean hasField(Class<?> clazz, String fieldName) {
|
|
|
+ Class<?> c = clazz;
|
|
|
+ while (c != null) {
|
|
|
+ try {
|
|
|
+ c.getDeclaredField(fieldName);
|
|
|
+ return true;
|
|
|
+ } catch (NoSuchFieldException ignored) {
|
|
|
+ c = c.getSuperclass();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 探测控制器是否存在含关键字的映射路径(@PostMapping/@RequestMapping 的 value)。 */
|
|
|
+ private boolean hasMapping(Class<?> ctrl, String keyword) {
|
|
|
+ for (Method m : ctrl.getDeclaredMethods()) {
|
|
|
+ String methodName = m.getName().toLowerCase();
|
|
|
+ if (methodName.contains(keyword)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ for (Annotation a : m.getAnnotations()) {
|
|
|
+ try {
|
|
|
+ Object raw = a.annotationType().getMethod("value").invoke(a);
|
|
|
+ String[] paths = raw instanceof String[]
|
|
|
+ ? (String[]) raw : new String[]{String.valueOf(raw)};
|
|
|
+ for (String p : paths) {
|
|
|
+ if (p.toLowerCase().contains(keyword)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // 非映射注解(无 value 属性)忽略
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String typeOf(Object o) {
|
|
|
+ return o == null ? "null" : o.getClass().getSimpleName();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 调用结果:value=正常返回,error=领域异常/拒绝提示。 */
|
|
|
+ private static final class Outcome {
|
|
|
+ final Object value;
|
|
|
+ final String error;
|
|
|
+
|
|
|
+ Outcome(Object value, String error) {
|
|
|
+ this.value = value;
|
|
|
+ this.error = error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|