|
|
@@ -0,0 +1,456 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED 阶段,S-cfc-252):能力键 {@code butler.oneOnOne.coach}
|
|
|
+ * (真人管家域,SC-H-02 一对一家长陪跑 / SC-H-03 多方沟通协调)。
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-H-02(建档案→定期回访→随时咨询;
|
|
|
+ * 不替代规划师 / SLA 可配 / 脱敏)、SC-H-03(收集→同步专业结论→反馈疑虑→闭环;
|
|
|
+ * 不变更方案 / 脱敏 / 争议引导直连);
|
|
|
+ * {@code tests/test-cases/human-butler.md} TC-H-02 / TC-H-03 三流(正常 / 异常 / 权限)。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.13(管理员接口 /api/admin/butler/*)
|
|
|
+ * + §4.64.8(真人管家运营管理);{@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md}
|
|
|
+ * S-cfc-252 设计口径:<b>管家陪跑(SLA / 回访 / 咨询)+ 消息协同
|
|
|
+ * (teacher.message.push 同源、监督反馈闭环)</b>。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约范围(当前 src/main 未落地,本测试为预期红)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>宿主服务 {@code com.etotem.cfc.service.ButlerOneOnOneCoachService} —— 未实现。</li>
|
|
|
+ * <li>档案/记录实体 {@code com.etotem.cfc.entity.ButlerOneOnOneCoachRecord}
|
|
|
+ * (表 {@code butler_one_on_one_coach_records})—— 未建表 / 无实体。</li>
|
|
|
+ * <li>家庭端端点({@code ButlerController} /api/butler/one-on-one/*)与管理端端点
|
|
|
+ * ({@code AdminButlerController} /api/admin/butler/one-on-one/*)—— 未新增。</li>
|
|
|
+ * <li>核心契约行为:建档案 + 服务计划(服务周期 / 回访节奏 / SLA 可配);定期回访留痕;
|
|
|
+ * 家长随时发起 1v1 咨询(请求即时到管家侧 + SLA 计时 + 响应留痕家长可见);
|
|
|
+ * 服务到期续费提示(不静默拒);暂停期不回访;敏感字段脱敏;并发咨询合并单会话;
|
|
|
+ * 仅本服务家庭可见(越界 403);多方沟通协调(收集→翻译后同步不变更方案→反馈规划师→闭环确认);
|
|
|
+ * 争议引导直连(管家不改方案);消息协同以 {@code teacher.message.push} 同源
|
|
|
+ * (关联 TeacherMessage + 复用消息推送通道 + 监督反馈闭环)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:宿主服务、实体、表、端点均未落地,所有用例在反射/结构探测处失败(红),
|
|
|
+ * 断言原文含「但ler.oneOnOne.coach 契约……未实现/缺失」便于定位。GREEN 阶段实现宿主后本测试自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p>测试风格:纯反射 / 结构契约,无 Spring / 无 DB / 无外部服务,与同批 RED 契约测试
|
|
|
+ * (ButlerEscalationContractTest / TeamProfitHookContractTest / FamilyJoinValidationRulesContractTest)一致。</p>
|
|
|
+ */
|
|
|
+class ButlerOneOnOneCoachContractTest {
|
|
|
+
|
|
|
+ // ==================== 契约常量 ====================
|
|
|
+
|
|
|
+ /** 契约宿主服务(尚未实现)。 */
|
|
|
+ private static final String SERVICE_CLASS = "com.etotem.cfc.service.ButlerOneOnOneCoachService";
|
|
|
+ /** 契约档案/记录实体(尚未落地)。 */
|
|
|
+ private static final String ENTITY_CLASS = "com.etotem.cfc.entity.ButlerOneOnOneCoachRecord";
|
|
|
+ /** 契约建表名。 */
|
|
|
+ private static final String TABLE_NAME = "butler_one_on_one_coach_records";
|
|
|
+
|
|
|
+ /** 档案/计划载体应含字段(SC-H-02 建档案 + 服务计划 + SLA 可配)。 */
|
|
|
+ private static final String[] PROFILE_FIELDS = {
|
|
|
+ "familyId", "parentUserId", "butlerUserId", "serviceCycle",
|
|
|
+ "followUpCadence", "slaMinutes", "status"
|
|
|
+ };
|
|
|
+
|
|
|
+ /** 消息协同同源字段:关联 teacher.message.push(TeacherMessage)的消息主键。 */
|
|
|
+ private static final String[] MESSAGE_SAME_SOURCE_FIELDS = {"messageId", "replyContent", "coordinationStatus"};
|
|
|
+
|
|
|
+ /** 家庭端契约端点(ButlerController /api/butler/one-on-one/*)。 */
|
|
|
+ private static final String[] FAMILY_ENDPOINTS = {
|
|
|
+ "/api/butler/one-on-one/profile/create",
|
|
|
+ "/api/butler/one-on-one/follow-up",
|
|
|
+ "/api/butler/one-on-one/consult/start",
|
|
|
+ "/api/butler/one-on-one/consult/respond",
|
|
|
+ "/api/butler/one-on-one/messages"
|
|
|
+ };
|
|
|
+ /** 管理端契约端点(AdminButlerController /api/admin/butler/one-on-one/*)。 */
|
|
|
+ private static final String[] ADMIN_ENDPOINTS = {
|
|
|
+ "/api/admin/butler/one-on-one/assign",
|
|
|
+ "/api/admin/butler/one-on-one/sla/config",
|
|
|
+ "/api/admin/butler/one-on-one/records",
|
|
|
+ "/api/admin/butler/one-on-one/supervision"
|
|
|
+ };
|
|
|
+
|
|
|
+ // ==================== 正常流:宿主服务与档案实体存在(SC-H-02) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:ButlerOneOnOneCoachService 宿主服务应存在")
|
|
|
+ void hostService_exists() {
|
|
|
+ assertNotNull(loadClass(SERVICE_CLASS),
|
|
|
+ "[butler.oneOnOne.coach] ButlerOneOnOneCoachService 类不存在:契约宿主未落地,"
|
|
|
+ + "SC-H-02 一对一陪跑 / SC-H-03 多方协调(SLA/回访/咨询 + 消息协同)能力缺失。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:ButlerOneOnOneCoachRecord 实体应存在并承载档案/服务计划/可配 SLA 字段")
|
|
|
+ void profileEntity_existsWithPlanFields() {
|
|
|
+ Class<?> entity = loadClass(ENTITY_CLASS);
|
|
|
+ if (entity == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] ButlerOneOnOneCoachRecord 实体不存在:契约要求承载"
|
|
|
+ + "家长档案 + 服务计划(服务周期 / 回访节奏 / 可配 SLA)。");
|
|
|
+ }
|
|
|
+ for (String field : PROFILE_FIELDS) {
|
|
|
+ assertNotNull(findField(entity, field),
|
|
|
+ "[butler.oneOnOne.coach] ButlerOneOnOneCoachRecord 应含 " + field
|
|
|
+ + " 字段(SC-H-02 建档案 / 服务计划 / SLA 可配)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:schema.sql 应落地 butler_one_on_one_coach_records 建表")
|
|
|
+ void schemaSql_hasCoachTable() {
|
|
|
+ String schema = readSchemaSql();
|
|
|
+ assertTrue(schema.contains(TABLE_NAME),
|
|
|
+ "[butler.oneOnOne.coach] schema.sql 缺少 " + TABLE_NAME + " 建表(SC-H-02/SC-H-03 陪跑档案)。");
|
|
|
+ assertTrue(schema.contains("sla_minutes"),
|
|
|
+ "[butler.oneOnOne.coach] " + TABLE_NAME + " 应含 sla_minutes 列(SLA 可配)。");
|
|
|
+ assertTrue(schema.contains("follow_up_cadence"),
|
|
|
+ "[butler.oneOnOne.coach] " + TABLE_NAME + " 应含 follow_up_cadence 列(定期回访节奏)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:createProfile(...) 应存在并建立家长档案与服务计划")
|
|
|
+ void createProfileMethod_exists() {
|
|
|
+ assertServiceHasMethod("createProfile",
|
|
|
+ "建档案 + 服务计划(服务周期 / 回访节奏 / 可配 SLA),TC-H-02 步骤 1");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:recordFollowUp(...) 应存在并生成回访记录(时间/内容/待办)")
|
|
|
+ void recordFollowUpMethod_exists() {
|
|
|
+ assertServiceHasMethod("recordFollowUp",
|
|
|
+ "执行一次定期回访并生成记录(时间 / 内容 / 待办),TC-H-02 步骤 2");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-6:startConsultation(...) 应存在并让家长咨询即时到管家侧 + SLA 计时")
|
|
|
+ void startConsultationMethod_exists() {
|
|
|
+ assertServiceHasMethod("startConsultation",
|
|
|
+ "家长主动发起 1v1 咨询,请求即时出现在管家侧并启动 SLA 计时,TC-H-02 步骤 3");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-7:respondConsultation(...) 应存在并响应留痕、家长可见")
|
|
|
+ void respondConsultationMethod_exists() {
|
|
|
+ assertServiceHasMethod("respondConsultation",
|
|
|
+ "管家响应并记录,家长端可见答复,TC-H-02 步骤 4");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流(TC-H-02-E):到期/暂停/脱敏/并发 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1:checkServiceEntitlement(...) 应存在,服务到期咨询给续费入口不静默拒")
|
|
|
+ void checkEntitlementMethod_exists() {
|
|
|
+ assertServiceHasMethod("checkServiceEntitlement",
|
|
|
+ "服务到期当天发起咨询 → 提示服务已到期并给出续费入口(不静默拒),TC-H-02-E");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-2:pauseFollowUp(...) 应存在,服务暂停期不发起回访")
|
|
|
+ void pauseFollowUpMethod_exists() {
|
|
|
+ assertServiceHasMethod("pauseFollowUp",
|
|
|
+ "服务暂停期内不发起回访、状态置为暂停,TC-H-02-E");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-3:sanitizeProfileSensitive(...) 应存在,档案高敏字段脱敏")
|
|
|
+ void sanitizeSensitiveMethod_exists() {
|
|
|
+ assertServiceHasMethod("sanitizeProfileSensitive",
|
|
|
+ "家长档案含高敏信息时敏感字段脱敏,未授权不可导出(复用 PII 脱敏口径),TC-H-02-E / SC-H-02 脱敏边界");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-4:mergeConcurrentConsultations(...) 应存在,并发咨询合并单会话")
|
|
|
+ void mergeConcurrentMethod_exists() {
|
|
|
+ assertServiceHasMethod("mergeConcurrentConsultations",
|
|
|
+ "同一家长并发发起多个咨询 → 合并为 1 个会话或排队,不产生多条并行工单,TC-H-02-E");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流:configureSla(...) 应存在,SLA 可配(宿主服务 + 实体 slaMinutes 双重契约)")
|
|
|
+ void slaConfigurable_exists() {
|
|
|
+ assertServiceHasMethod("configureSla", "SLA 可配:按套餐/家庭配置回访与响应 SLA,SC-H-02 边界");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流(TC-H-02-P):家庭隔离与端点 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-1:canAccessProfile(...) 应存在,仅本服务家庭可见(越界 403)")
|
|
|
+ void canAccessProfileMethod_exists() {
|
|
|
+ assertServiceHasMethod("canAccessProfile",
|
|
|
+ "仅本服务家庭可见,越界拒绝/403(管家读 F9 档案、家长读他人档案),TC-H-02-P");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-2:ButlerController 应映射家庭端 /api/butler/one-on-one/* 端点(@PostMapping)")
|
|
|
+ void familyEndpoints_mapped() {
|
|
|
+ Class<?> ctrl = loadClass("com.etotem.cfc.controller.butler.ButlerController");
|
|
|
+ if (ctrl == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] ButlerController 不存在,无法映射家庭端 one-on-one 端点。");
|
|
|
+ }
|
|
|
+ for (String path : FAMILY_ENDPOINTS) {
|
|
|
+ assertTrue(endpointExists(ctrl, path),
|
|
|
+ "[butler.oneOnOne.coach] ButlerController 缺少 @PostMapping " + path
|
|
|
+ + "(家庭端陪跑 / 咨询 / 消息协同)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-3:AdminButlerController 应映射管理端 /api/admin/butler/one-on-one/* 端点")
|
|
|
+ void adminEndpoints_mapped() {
|
|
|
+ Class<?> ctrl = loadClass("com.etotem.cfc.controller.admin.AdminButlerController");
|
|
|
+ if (ctrl == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] AdminButlerController 不存在,无法映射管理端 one-on-one 端点。");
|
|
|
+ }
|
|
|
+ for (String path : ADMIN_ENDPOINTS) {
|
|
|
+ assertTrue(endpointExists(ctrl, path),
|
|
|
+ "[butler.oneOnOne.coach] AdminButlerController 缺少 @PostMapping " + path
|
|
|
+ + "(分配 / SLA 配置 / 台账 / 监督,admin/finance 角色校验)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-H-03 多方沟通协调 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-H-03-1:collectCoordination(...) 应存在,收集家长诉求与规划师方案(三方同工单)")
|
|
|
+ void collectCoordinationMethod_exists() {
|
|
|
+ assertServiceHasMethod("collectCoordination",
|
|
|
+ "收集家长诉求与规划师方案,三方信息在同一工单内可查,TC-H-03 步骤 1");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-H-03-2:syncTranslatedConclusion(...) 应存在,翻译后同步且不变更方案结论")
|
|
|
+ void syncConclusionMethod_exists() {
|
|
|
+ assertServiceHasMethod("syncTranslatedConclusion",
|
|
|
+ "向家长同步「翻译后」结论,不擅自变更规划师方案结论,TC-H-03 步骤 2 / TC-H-03-E");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-H-03-3:feedbackToTeacher(...) 应存在,向规划师反馈执行与疑虑(反馈闭环)")
|
|
|
+ void feedbackToTeacherMethod_exists() {
|
|
|
+ assertServiceHasMethod("feedbackToTeacher",
|
|
|
+ "向规划师反馈家长执行情况与疑虑,规划师侧可回复,TC-H-03 步骤 3");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-H-03-4:closeCoordination(...) 应存在,闭环确认并置为已对齐 + 全过程留痕")
|
|
|
+ void closeCoordinationMethod_exists() {
|
|
|
+ assertServiceHasMethod("closeCoordination",
|
|
|
+ "完成节点对齐并闭环确认,工单状态置为已对齐且全过程留痕,TC-H-03 步骤 4");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-H-03-5:recordDisputeDirectConnect(...) 应存在,争议引导直连且管家不改方案")
|
|
|
+ void disputeDirectConnectMethod_exists() {
|
|
|
+ assertServiceHasMethod("recordDisputeDirectConnect",
|
|
|
+ "家长与规划师意见冲突 → 引导双方直接沟通并记录,不由管家单方裁决 / 不改方案,TC-H-03-E");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 消息协同:teacher.message.push 同源 + 监督反馈闭环 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("消息协同-1:档案实体应含同源消息字段(messageId/replyContent/coordinationStatus),关联 teacher.message.push")
|
|
|
+ void messageSameSource_teacherMessageLinkExists() {
|
|
|
+ Class<?> entity = loadClass(ENTITY_CLASS);
|
|
|
+ if (entity == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] ButlerOneOnOneCoachRecord 实体不存在,无法验证与"
|
|
|
+ + " teacher.message.push 的同源关联(关联 TeacherMessage 消息主键)。");
|
|
|
+ }
|
|
|
+ for (String field : MESSAGE_SAME_SOURCE_FIELDS) {
|
|
|
+ assertNotNull(findField(entity, field),
|
|
|
+ "[butler.oneOnOne.coach] ButlerOneOnOneCoachRecord 应含 " + field
|
|
|
+ + " 字段,用于与 teacher.message.push 同源(复用 TeacherMessage 消息通道 / 回复 / 协调状态)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("消息协同-2:宿主服务应依赖消息推送通道(同源 teacher.message.push,可审计)")
|
|
|
+ void messagePushDependency_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] ButlerOneOnOneCoachService 不存在,无法验证消息推送同源依赖。"
|
|
|
+ + " 契约要求复用 teacher.message.push 同源通道(TeacherMessage / 消息推送服务),不另起独立通道。");
|
|
|
+ }
|
|
|
+ boolean dependsOnPush = false;
|
|
|
+ for (Field f : svc.getDeclaredFields()) {
|
|
|
+ String type = f.getType().getName();
|
|
|
+ if (type.contains("Message") || type.contains("Notification")) {
|
|
|
+ dependsOnPush = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(dependsOnPush,
|
|
|
+ "[butler.oneOnOne.coach] ButlerOneOnOneCoachService 应注入 teacher.message.push 同源的"
|
|
|
+ + "消息推送依赖(类型名含 Message/Notification 的字段),保证消息协同同源、可审计。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("消息协同-3:pushCoordinationMessage(...) 应存在,复用同源消息通道推送协调消息")
|
|
|
+ void pushCoordinationMessageMethod_exists() {
|
|
|
+ assertServiceHasMethod("pushCoordinationMessage",
|
|
|
+ "复用 teacher.message.push 同源通道推送三方协调消息(家长/规划师/管家)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("消息协同-4:recordSupervisionFeedback(...) 应存在,形成监督反馈闭环")
|
|
|
+ void recordSupervisionFeedbackMethod_exists() {
|
|
|
+ assertServiceHasMethod("recordSupervisionFeedback",
|
|
|
+ "监督反馈闭环:记录家长/规划师反馈与处置结论,留痕可审计(SC-H-03 反馈→闭环)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流(说明):SC-H-02/SC-H-03 含家庭隔离与管理端角色,正常/异常/权限三流均适用")
|
|
|
+ void permissionFlow_applies() {
|
|
|
+ assertTrue(true,
|
|
|
+ "权限流适用:butler.oneOnOne.coach 含本家庭档案隔离(403)、管理端 admin/finance 角色校验,"
|
|
|
+ + "正常/异常/权限三流均适用。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助(避免编译期依赖不存在类) ====================
|
|
|
+
|
|
|
+ /** 宿主服务不存在则 fail;存在则断言其声明了指定方法名。 */
|
|
|
+ private void assertServiceHasMethod(String methodName, String contractDesc) {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.oneOnOne.coach] ButlerOneOnOneCoachService 不存在,无法验证 " + methodName + "():"
|
|
|
+ + contractDesc + "。契约宿主未落地。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethodNamed(svc, methodName),
|
|
|
+ "[butler.oneOnOne.coach] ButlerOneOnOneCoachService 应提供 " + methodName + "():" + contractDesc + "。");
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (Throwable e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Field findField(Class<?> clazz, String name) {
|
|
|
+ Class<?> c = clazz;
|
|
|
+ while (c != null && c != Object.class) {
|
|
|
+ for (Field f : c.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(name)) {
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ c = c.getSuperclass();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasMethodNamed(Class<?> clazz, String name) {
|
|
|
+ Class<?> c = clazz;
|
|
|
+ while (c != null && c != Object.class) {
|
|
|
+ for (Method m : c.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ c = c.getSuperclass();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 读取类路径下 schema.sql 全文。 */
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查控制器是否以 @PostMapping(方法级完整路径或类级前缀 + 方法级路径)映射了期望路径。
|
|
|
+ * 兼容 ButlerController/AdminButlerController 使用的「方法级全路径」与 TeacherMessageController 的「类级前缀」两种风格。
|
|
|
+ */
|
|
|
+ private boolean endpointExists(Class<?> ctrl, String expectedPath) {
|
|
|
+ String typePrefix = typeRequestMappingPrefix(ctrl);
|
|
|
+ for (Method m : ctrl.getDeclaredMethods()) {
|
|
|
+ java.lang.annotation.Annotation post =
|
|
|
+ m.getAnnotation(org.springframework.web.bind.annotation.PostMapping.class);
|
|
|
+ if (post != null) {
|
|
|
+ String[] paths = mappingPaths(post);
|
|
|
+ if (pathsContains(paths, expectedPath)
|
|
|
+ || (typePrefix != null && !typePrefix.isEmpty()
|
|
|
+ && pathsContains(paths, stripPrefix(expectedPath, typePrefix)))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String stripPrefix(String path, String prefix) {
|
|
|
+ String p = prefix.endsWith("/") ? prefix.substring(0, prefix.length() - 1) : prefix;
|
|
|
+ return path.startsWith(p) ? path.substring(p.length()) : path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String[] mappingPaths(java.lang.annotation.Annotation ann) {
|
|
|
+ try {
|
|
|
+ Method valueM = ann.annotationType().getMethod("value");
|
|
|
+ Object v = valueM.invoke(ann);
|
|
|
+ if (v instanceof String[]) {
|
|
|
+ return (String[]) v;
|
|
|
+ }
|
|
|
+ Method pathM = ann.annotationType().getMethod("path");
|
|
|
+ Object p = pathM.invoke(ann);
|
|
|
+ if (p instanceof String[]) {
|
|
|
+ return (String[]) p;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ return new String[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean pathsContains(String[] paths, String expected) {
|
|
|
+ for (String p : paths) {
|
|
|
+ if (p != null && p.equals(expected)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String typeRequestMappingPrefix(Class<?> ctrl) {
|
|
|
+ java.lang.annotation.Annotation rm =
|
|
|
+ ctrl.getAnnotation(org.springframework.web.bind.annotation.RequestMapping.class);
|
|
|
+ if (rm == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Method valueM = rm.annotationType().getMethod("value");
|
|
|
+ Object v = valueM.invoke(rm);
|
|
|
+ if (v instanceof String[] && ((String[]) v).length > 0) {
|
|
|
+ return ((String[]) v)[0];
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|