|
|
@@ -0,0 +1,433 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+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.Method;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Arrays;
|
|
|
+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-255):能力键 {@code butler.ai.collaborate}(真人管家域,SC-H-06 与 AI 管家协同闭环)。
|
|
|
+ *
|
|
|
+ * <p>契约溯源:
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-H-06(与 AI 管家协同闭环);
|
|
|
+ * {@code tests/user-scenarios/09-human-butler.md} SC-H-06;
|
|
|
+ * {@code tests/test-cases/human-butler.md} TC-H-06 / TC-H-06-E / TC-H-06-P 三流。</li>
|
|
|
+ * <li>设计:派发单口径 {@code docs/superpowers/api/API_REFERENCE.md} §4.12/§4.13;
|
|
|
+ * {@code docs/superpowers/specs/2026-11-07-ai-butler-contract.md}(SC-H-06 现仅 {@code /api/ai/butler/send}
|
|
|
+ * AI→管家协同载体,未覆盖闭环回写/知识反哺审核/反馈家长/隐私/审计);
|
|
|
+ * {@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md} S-cfc-255:
|
|
|
+ * 闭环标记 → 知识反哺({@code AdminUnifiedKnowledgeController} 反哺审核)→ 反馈家长;不泄露隐私 / 可审计。</li>
|
|
|
+ * </ul></p>
|
|
|
+ *
|
|
|
+ * <p><b>契约范围(当前 src/main 未落地,TDD-RED)</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>宿主服务 {@link #SERVICE_CLASS}(闭环→反哺→反馈→审计编排)—— 未实现;</li>
|
|
|
+ * <li>反哺实体 {@link #ENTITY_CLASS}(表 {@code butler_ai_feedbacks})—— 未建表 / 无实体;</li>
|
|
|
+ * <li>管家端闭环/反哺/反馈端点({@code controller.butler.ButlerController})—— 未新增;</li>
|
|
|
+ * <li>管理端反哺审核端点({@code controller.admin.AdminUnifiedKnowledgeController})—— 未新增;</li>
|
|
|
+ * <li>核心行为:闭环标记(结论/处置/时长)→ 反哺提交(待审队列 + 幂等去重)→ 运营审核(通过入库/退回修改)
|
|
|
+ * → 反馈原家长并关闭升级链路;反哺内容脱敏且仅本家庭可见;闭环与反哺全程留痕可审计。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:上述宿主类、实体、表、端点均不存在,全部用例在反射探测处失败(红),断言原文以
|
|
|
+ * {@code [butler.ai.collaborate]} 开头,便于贴红测报告。GREEN 阶段按本测试锚定的签名实现后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p>测试风格:纯反射 + schema.sql 结构锚定,无 Spring / 无 DB / 无外部服务,与同批 RED 契约测试
|
|
|
+ * (TeamProfitHook / FamilyJoinValidationRules / ButlerEscalation)一致。</p>
|
|
|
+ */
|
|
|
+class ButlerAiCollaborationContractTest {
|
|
|
+
|
|
|
+ // ==================== 契约常量 ====================
|
|
|
+
|
|
|
+ /** 契约宿主服务(尚未实现):编排「闭环标记 → 知识反哺 → 反馈家长 → 审计留痕」。 */
|
|
|
+ private static final String SERVICE_CLASS = "com.etotem.cfc.service.ButlerAiCollaborationService";
|
|
|
+ /** 契约反哺实体(尚未落地):承载一条待审/已审的知识反哺条目。 */
|
|
|
+ private static final String ENTITY_CLASS = "com.etotem.cfc.entity.ButlerAiFeedback";
|
|
|
+ /** 契约反哺表名。 */
|
|
|
+ private static final String TABLE_NAME = "butler_ai_feedbacks";
|
|
|
+
|
|
|
+ /** 反哺审核状态(TC-H-06:待审队列 / 审核通过入库 / 未通过退回修改)。 */
|
|
|
+ private static final String[] FEEDBACK_STATUSES = {"PENDING_REVIEW", "APPROVED", "REJECTED"};
|
|
|
+
|
|
|
+ /** 管家端契约端点(ButlerController /api/butler/collaborate/*)。 */
|
|
|
+ private static final String[] FAMILY_ENDPOINTS = {
|
|
|
+ "/api/butler/collaborate/close", // 闭环标记:结论/处置/时长
|
|
|
+ "/api/butler/collaborate/feedback", // 提炼可复用结论并提交反哺
|
|
|
+ "/api/butler/collaborate/notify" // 反馈原家长、关闭升级链路
|
|
|
+ };
|
|
|
+ /** 管理端契约端点(AdminUnifiedKnowledgeController 反哺审核,审核权归运营)。 */
|
|
|
+ private static final String[] ADMIN_ENDPOINTS = {
|
|
|
+ "/api/admin/knowledge-base/unified/feedback/pending", // 待审队列
|
|
|
+ "/api/admin/knowledge-base/unified/feedback/audit" // 审核:通过入库 / 退回修改
|
|
|
+ };
|
|
|
+
|
|
|
+ // ==================== 正常流:宿主服务与实体存在 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:ButlerAiCollaborationService 宿主服务应存在")
|
|
|
+ void hostService_exists() {
|
|
|
+ assertNotNull(loadClass(SERVICE_CLASS),
|
|
|
+ "[butler.ai.collaborate] ButlerAiCollaborationService 类不存在:契约要求编排"
|
|
|
+ + " 闭环标记→知识反哺→反馈家长→审计留痕(SC-H-06 / TC-H-06)。实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:ButlerAiFeedback 实体应存在并承载反哺上下文字段")
|
|
|
+ void feedbackEntity_existsWithContextFields() {
|
|
|
+ Class<?> entity = loadClass(ENTITY_CLASS);
|
|
|
+ if (entity == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiFeedback 实体不存在:契约要求承载"
|
|
|
+ + " 反哺内容 / 审核状态 / 审核人 / 来源家庭(不泄露隐私、可审计)。");
|
|
|
+ }
|
|
|
+ assertNotNull(findField(entity, "workorderId"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 workorderId 字段(关联 AI 升级工单闭环)。");
|
|
|
+ assertNotNull(findField(entity, "familyId"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 familyId 字段(反哺来源家庭,禁止跨家庭泄露)。");
|
|
|
+ assertNotNull(findField(entity, "content"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 content 字段(提炼的可复用结论/FAQ)。");
|
|
|
+ assertNotNull(findField(entity, "status"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 status 字段(待审/通过/退回流转)。");
|
|
|
+ assertNotNull(findField(entity, "reviewerId"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 reviewerId 字段(审核权归运营,可审计)。");
|
|
|
+ assertNotNull(findField(entity, "dedupeKey"),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback 应含 dedupeKey 字段(同一结论重复反哺幂等去重,TC-H-06-E)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:schema.sql 应落地 butler_ai_feedbacks 建表")
|
|
|
+ void schemaSql_hasFeedbackTable() {
|
|
|
+ String schema = readSchemaSql();
|
|
|
+ assertTrue(schema.contains(TABLE_NAME),
|
|
|
+ "[butler.ai.collaborate] schema.sql 缺少 " + TABLE_NAME + " 建表(SC-H-06 知识反哺)。");
|
|
|
+ assertTrue(schema.contains("workorder_id"),
|
|
|
+ "[butler.ai.collaborate] " + TABLE_NAME + " 应含 workorder_id 列(关联闭环工单)。");
|
|
|
+ assertTrue(schema.contains("family_id"),
|
|
|
+ "[butler.ai.collaborate] " + TABLE_NAME + " 应含 family_id 列(家庭隐私隔离)。");
|
|
|
+ assertTrue(schema.contains("status"),
|
|
|
+ "[butler.ai.collaborate] " + TABLE_NAME + " 应含 status 列(待审/通过/退回)。");
|
|
|
+ assertTrue(schema.contains("dedupe_key"),
|
|
|
+ "[butler.ai.collaborate] " + TABLE_NAME + " 应含 dedupe_key 列(幂等去重)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流:契约方法签名 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:closeOrder(...) 应存在并标记闭环(结论/处置/时长)")
|
|
|
+ void closeOrderMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 closeOrder()(闭环标记)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "closeOrder",
|
|
|
+ new Class[]{Long.class, String.class, String.class, Integer.class, Long.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 closeOrder(Long workorderId, String conclusion, String disposition,"
|
|
|
+ + " Integer durationMinutes, Long userId):结论/处置/时长完整保存并将工单置为已闭环(TC-H-06 步骤 1)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:submitFeedback(...) 应存在并提交反哺到待审队列")
|
|
|
+ void submitFeedbackMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 submitFeedback()(知识反哺)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "submitFeedback", new Class[]{Long.class, String.class, Long.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 submitFeedback(Long workorderId, String content, Long userId):"
|
|
|
+ + "提炼可复用结论并写入待审队列,不直接入库(TC-H-06 步骤 2/3、TC-H-06-E)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-6:notifyParent(...) 应存在并反馈原家长、关闭升级链路")
|
|
|
+ void notifyParentMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 notifyParent()(反馈家长)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "notifyParent", new Class[]{Long.class, Long.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 notifyParent(Long workorderId, Long feedbackId):"
|
|
|
+ + "审核通过后反馈原家长并关闭升级链路(TC-H-06 步骤 5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-7:反哺状态应覆盖 PENDING_REVIEW / APPROVED / REJECTED")
|
|
|
+ void feedbackStatus_coversAuditFlow() {
|
|
|
+ Class<?> entity = loadClass(ENTITY_CLASS);
|
|
|
+ if (entity == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiFeedback 不存在,无法验证反哺审核状态流转。");
|
|
|
+ }
|
|
|
+ Field statusField = findField(entity, "status");
|
|
|
+ if (statusField == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiFeedback 缺少 status 字段,无法承载待审/通过/退回。");
|
|
|
+ }
|
|
|
+ Class<?> statusType = statusField.getType();
|
|
|
+ assertTrue(statusType.isEnum() || String.class.equals(statusType),
|
|
|
+ "[butler.ai.collaborate] ButlerAiFeedback.status 应为枚举或 String,用于承载 "
|
|
|
+ + Arrays.toString(FEEDBACK_STATUSES) + ",实际类型=" + statusType.getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流:控制器端点(权限流载体) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-8:ButlerController 应映射管家端 collaborate 闭环端点(@PostMapping)")
|
|
|
+ void familyEndpoints_mapped() {
|
|
|
+ Class<?> ctrl = loadClass("com.etotem.cfc.controller.butler.ButlerController");
|
|
|
+ if (ctrl == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerController 不存在,无法映射管家端 collaborate 端点。");
|
|
|
+ }
|
|
|
+ for (String path : FAMILY_ENDPOINTS) {
|
|
|
+ assertTrue(endpointExists(ctrl, path),
|
|
|
+ "[butler.ai.collaborate] ButlerController 应有 @PostMapping " + path
|
|
|
+ + "(闭环标记 / 反哺提交 / 反馈家长,仅本服务家庭可操作)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-9:AdminUnifiedKnowledgeController 应映射反哺审核端点(@PostMapping)")
|
|
|
+ void adminAuditEndpoints_mapped() {
|
|
|
+ Class<?> ctrl = loadClass("com.etotem.cfc.controller.admin.AdminUnifiedKnowledgeController");
|
|
|
+ if (ctrl == null) {
|
|
|
+ fail("[butler.ai.collaborate] AdminUnifiedKnowledgeController 不存在,无法映射反哺审核端点。");
|
|
|
+ }
|
|
|
+ for (String path : ADMIN_ENDPOINTS) {
|
|
|
+ assertTrue(endpointExists(ctrl, path),
|
|
|
+ "[butler.ai.collaborate] AdminUnifiedKnowledgeController 应有 @PostMapping " + path
|
|
|
+ + "(反哺待审队列 / 审核通过入库或退回修改,审核权归运营)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流:隐私脱敏、幂等去重、家庭隔离 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1:sanitizeFeedback(...) 应存在并在入库前脱敏/拦截隐私(TC-H-06-E)")
|
|
|
+ void sanitizeFeedbackMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 sanitizeFeedback()(隐私脱敏)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "sanitizeFeedback", new Class[]{String.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 sanitizeFeedback(String content):反哺内容含该家长隐私时"
|
|
|
+ + "拦截或脱敏,不允许带隐私入库、不泄露到其他家庭(TC-H-06-E / SC-H-06 边界)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-2:duplicateGuard(...) 应存在并幂等去重(同一结论仅 1 条)")
|
|
|
+ void duplicateGuardMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 duplicateGuard()(幂等去重)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "duplicateGuard", new Class[]{Long.class, String.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 duplicateGuard(Long workorderId, String content):"
|
|
|
+ + "同一结论重复反哺时幂等去重,知识库仅保留 1 条(TC-H-06-E)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-3:canAccessFeedback(...) 应存在并拒绝跨家庭读取反哺原始对话(TC-H-06-P)")
|
|
|
+ void canAccessFeedbackMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 canAccessFeedback()(家庭隔离)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "canAccessFeedback", new Class[]{Long.class, Long.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 canAccessFeedback(Long feedbackId, Long userId):"
|
|
|
+ + "仅本服务家庭可见,管家/家长不得读取其他家庭反哺原始对话,越界返回 403(TC-H-06-P)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流:审核权归运营 + 可审计 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-1:auditFeedback(...) 应存在且审核权归运营(通过入库/退回修改)")
|
|
|
+ void auditFeedbackMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 auditFeedback()(反哺审核)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "auditFeedback",
|
|
|
+ new Class[]{Long.class, String.class, Long.class, String.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 auditFeedback(Long feedbackId, String decision, Long reviewerId, String remark):"
|
|
|
+ + "仅运营角色可审核(通过入库 / 未通过退回修改),管家仅有提交权(TC-H-06 / TC-H-06-P)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-2:auditLog(...) 应存在并返回闭环/反哺操作留痕(可审计)")
|
|
|
+ void auditLogMethod_exists() {
|
|
|
+ Class<?> svc = loadClass(SERVICE_CLASS);
|
|
|
+ if (svc == null) {
|
|
|
+ fail("[butler.ai.collaborate] ButlerAiCollaborationService 不存在,无法验证 auditLog()(可审计)。");
|
|
|
+ }
|
|
|
+ assertTrue(hasMethod(svc, "auditLog", new Class[]{Long.class}),
|
|
|
+ "[butler.ai.collaborate] 应提供 auditLog(Long workorderId):闭环状态、反哺提交、审核动作全程留痕可审计"
|
|
|
+ + "(SC-H-06 边界:闭环状态可审计)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-3:三流适用性说明(正常/异常/权限均适用 SC-H-06)")
|
|
|
+ void permissionFlow_applies() {
|
|
|
+ assertTrue(true,
|
|
|
+ "权限流适用:butler.ai.collaborate 含反哺审核权(归运营、管家仅提交)、"
|
|
|
+ + "跨家庭原始对话隔离(403)与隐私脱敏,正常/异常/权限三流均适用。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助(避免编译期依赖不存在类) ====================
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (Exception 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 hasMethod(Class<?> clazz, String name, Class<?>[] paramTypes) {
|
|
|
+ Class<?> c = clazz;
|
|
|
+ while (c != null && c != Object.class) {
|
|
|
+ for (Method m : c.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name) && Arrays.equals(m.getParameterTypes(), paramTypes)) {
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查控制器是否以 POST 映射了完整路径(类级 @RequestMapping 前缀 + 方法级 @PostMapping/@RequestMapping)。
|
|
|
+ * 项目统一 @PostMapping;此处同时兼容通用 @RequestMapping(method=POST)。
|
|
|
+ */
|
|
|
+ private boolean endpointExists(Class<?> ctrl, String expectedFullPath) {
|
|
|
+ String typePrefix = typeRequestMappingPrefix(ctrl);
|
|
|
+ for (Method m : ctrl.getDeclaredMethods()) {
|
|
|
+ Annotation post = m.getAnnotation(org.springframework.web.bind.annotation.PostMapping.class);
|
|
|
+ if (post != null && pathsMatch(typePrefix, mappingPaths(post), expectedFullPath)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Annotation rm = m.getAnnotation(org.springframework.web.bind.annotation.RequestMapping.class);
|
|
|
+ if (rm != null && isPostMapping(rm) && pathsMatch(typePrefix, mappingPaths(rm), expectedFullPath)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isPostMapping(Annotation rm) {
|
|
|
+ try {
|
|
|
+ Method methodM = rm.annotationType().getMethod("method");
|
|
|
+ Object[] methods = (Object[]) methodM.invoke(rm);
|
|
|
+ if (methods.length == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ for (Object om : methods) {
|
|
|
+ if (om instanceof org.springframework.web.bind.annotation.RequestMethod
|
|
|
+ && ((org.springframework.web.bind.annotation.RequestMethod) om).name()
|
|
|
+ .equalsIgnoreCase("POST")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 忽略注解读取异常
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean pathsMatch(String typePrefix, String[] paths, String expectedFullPath) {
|
|
|
+ for (String p : paths) {
|
|
|
+ if (p == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String full = joinPath(typePrefix, p);
|
|
|
+ if (expectedFullPath.equals(full)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String joinPath(String prefix, String path) {
|
|
|
+ if (prefix == null || prefix.isEmpty()) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ if (prefix.endsWith("/") && path.startsWith("/")) {
|
|
|
+ return prefix + path.substring(1);
|
|
|
+ }
|
|
|
+ if (!prefix.endsWith("/") && !path.startsWith("/")) {
|
|
|
+ return prefix + "/" + path;
|
|
|
+ }
|
|
|
+ return prefix + path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String[] mappingPaths(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) {
|
|
|
+ // 忽略注解读取异常
|
|
|
+ }
|
|
|
+ return new String[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 读取控制器类级 @RequestMapping 的路径前缀(不含 method 限定)。 */
|
|
|
+ private String typeRequestMappingPrefix(Class<?> ctrl) {
|
|
|
+ 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) {
|
|
|
+ // 忽略注解读取异常
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+}
|