|
|
@@ -0,0 +1,579 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+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.Mockito.mock;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED):爱云医 AI 检测照片路径编排(能力键 {@code ai.aiyunyi.detect},任务 S-cfc-450)。
|
|
|
+ *
|
|
|
+ * <p><b>溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code docs/2026-11-24-爱云医AI检测接入方案.md} §5 时序(照片路径)
|
|
|
+ * —— POST /api/aiyunyi/detect → identify → spu/page + get-detail → order/create →
|
|
|
+ * detection-user/create → enter/detection_user → package/page → get-items → 上传 OSS →
|
|
|
+ * enter/detection_data → 轮询 → report/get;</li>
|
|
|
+ * <li>需求:{@code docs/爱云医接入/爱云医接口契约-完整版.md} §A.4 录入检测数据
|
|
|
+ * —— basicUrlList 标准 12 张 type=0、reportUrlList type=3、语音 familyHistoryUrlList type=1 /
|
|
|
+ * diseaseUrlList type=2、attachmentUrlList type=4、type 0 普通 / 3 癌症、
|
|
|
+ * isQualified 上传时空值;</li>
|
|
|
+ * <li>设计:接入方案 §3.4(照片 vs 视频分流)、§3.5(单租户账户 + aiyunyi_mapping 幂等映射)、
|
|
|
+ * §4.5(无商品/无子项归一提示)、§8(mock gateway 单测)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约 AC(GREEN 阶段实现方按此落地)</b></p>
|
|
|
+ * <ol>
|
|
|
+ * <li>AC1 宿主编排服务 {@code com.etotem.cfc.service.AicdsDetectionService} 存在。</li>
|
|
|
+ * <li>AC2 提供 {@code detect(...)} 编排入口(照片路径主流程)。</li>
|
|
|
+ * <li>AC3 持有可注入的 {@code AicdsGatewayService} 字段,可用 Mockito mock(mock gateway 单测)。</li>
|
|
|
+ * <li>AC4 提供 {@code buildEnterDetectionDataRequest(long detectionUserId, long detectionReportUserId,
|
|
|
+ * long detectionPackageId, long detectionPackageItemId, List<String> basicUrls, List<String> reportUrls,
|
|
|
+ * List<String> familyHistoryVoiceUrls, List<String> diseaseVoiceUrls, List<String> attachmentUrls, int type)}
|
|
|
+ * 返回 {@code Map<String,Object>},含全部必填键。</li>
|
|
|
+ * <li>AC5 basicUrlList 恰好 12 项:id=index(0..11)、url 原样、type=0、isQualified 留空。</li>
|
|
|
+ * <li>AC6 reportUrlList type=3;familyHistoryUrlList type=1;diseaseUrlList type=2;
|
|
|
+ * attachmentUrlList type=4。</li>
|
|
|
+ * <li>AC7 request.type 为 0(普通)或 3(癌症);非法检测类型快速失败。</li>
|
|
|
+ * <li>AC8 {@code resolveRoute(int inspectionType)}:0/3 → 走录入检测数据(含 data),
|
|
|
+ * 1/2 → 走 getPath(含 path)。</li>
|
|
|
+ * <li>AC9 提供 {@code findOrCreateMapping(...)} 保证同一成员重复识别复用映射(幂等,§3.5)。</li>
|
|
|
+ * <li>AC10 编排依次调用 identify → spu/page → get-detail → order/create → detection-user/create →
|
|
|
+ * enter/detection_user → package/page → get-items → enter/detection_data → report/get,
|
|
|
+ * 视频路径另调 getPath;无商品提示「该服务尚未开通」,无可用子项提示「次数已用完」(§4.5)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:本分支 {@code src/main} 中 {@code Aicds/AiYunyi/aiyunyi} 0 引用,
|
|
|
+ * 宿主服务、编排源码与映射表均未落地。测试通过反射锚定宿主契约 + 源码结构探测
|
|
|
+ * (避免 import 不存在类导致整批测试编译失败):可编译、可运行,断言失败(红)且失败原文清晰可贴;
|
|
|
+ * GREEN 阶段补齐实现后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito + 反射 + 源码读取,无 Spring 上下文 / 无 DB / 无真实网络与密钥。</p>
|
|
|
+ *
|
|
|
+ * <p><b>权限流</b>:本能力由后端代理统一收口鉴权(私钥不下发前端,§3.1),
|
|
|
+ * 编排内部无独立用户角色门禁分支,权限流不适用,以说明性用例标注。</p>
|
|
|
+ */
|
|
|
+class AiYunyiDetectContractTest {
|
|
|
+
|
|
|
+ /** 设计 §4.1/§5 指定的编排宿主与网关(均未落地)。 */
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.AicdsDetectionService";
|
|
|
+ private static final String GATEWAY_CLASS = "com.etotem.cfc.service.AicdsGatewayService";
|
|
|
+ private static final String DETECTION_SOURCE =
|
|
|
+ "src/main/java/com/etotem/cfc/service/AicdsDetectionService.java";
|
|
|
+
|
|
|
+ /** §A.4 请求体必填键。 */
|
|
|
+ private static final String[] REQUEST_KEYS = {
|
|
|
+ "detectionUserId", "detectionReportUserId", "detectionPackageId", "detectionPackageItemId",
|
|
|
+ "basicUrlList", "familyHistoryUrlList", "diseaseUrlList", "reportUrlList", "attachmentUrlList",
|
|
|
+ "type"
|
|
|
+ };
|
|
|
+
|
|
|
+ /** §5 编排步骤 → 允许的方法名别名(源码归一化后匹配)。 */
|
|
|
+ private static final String[][] ORCHESTRATION_STEPS = {
|
|
|
+ {"identify"},
|
|
|
+ {"spupage", "getspupage"},
|
|
|
+ {"getdetail", "spugetdetail"},
|
|
|
+ {"ordercreate", "createorder"},
|
|
|
+ {"detectionusercreate", "createdetectionuser"},
|
|
|
+ {"enterdetectionuser", "enteruser", "activatedetectionuser"},
|
|
|
+ {"packagepage", "getpackagepage"},
|
|
|
+ {"getitems"},
|
|
|
+ {"enterdetectiondata", "enterdata"},
|
|
|
+ {"reportget", "getreport"}
|
|
|
+ };
|
|
|
+
|
|
|
+ private static final String[] STEP_LABELS = {
|
|
|
+ "identify 登录", "spu/page 商品列表", "get-detail 商品明细", "order/create 下单",
|
|
|
+ "detection-user/create 建检测人", "enter/detection_user 激活", "package/page 套餐分页",
|
|
|
+ "get-items 子项", "enter/detection_data 录入数据", "report/get 报告"
|
|
|
+ };
|
|
|
+
|
|
|
+ // ==================== 正常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:编排宿主 AicdsDetectionService 应存在(§4.1/§5)")
|
|
|
+ void hostClass_exists() {
|
|
|
+ assertNotNull(loadClass(HOST_CLASS),
|
|
|
+ "[ai.aiyunyi.detect] " + HOST_CLASS + " 不存在:设计 §4.1/§5 要求照片路径编排"
|
|
|
+ + "(identify→下单→建检测人→激活→子项→录入数据→报告)。实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:应提供 detect(...) 编排入口")
|
|
|
+ void detectEntry_exists() {
|
|
|
+ Class<?> host = loadClass(HOST_CLASS);
|
|
|
+ if (host == null) {
|
|
|
+ fail("[ai.aiyunyi.detect] AicdsDetectionService 不存在,无法校验 detect 编排入口(§5)。");
|
|
|
+ }
|
|
|
+ assertNotNull(findMethodByName(host, "detect"),
|
|
|
+ "[ai.aiyunyi.detect] 应提供 detect(...) 入口承接 POST /api/aiyunyi/detect(§5 时序)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:应持有可 Mockito mock 的 AicdsGatewayService 字段(mock gateway 单测)")
|
|
|
+ void gatewayField_isMockable() {
|
|
|
+ Class<?> host = loadClass(HOST_CLASS);
|
|
|
+ if (host == null) {
|
|
|
+ fail("[ai.aiyunyi.detect] AicdsDetectionService 不存在,无法校验网关注入(§3.1/§8)。");
|
|
|
+ }
|
|
|
+ Class<?> gateway = loadClass(GATEWAY_CLASS);
|
|
|
+ assertNotNull(gateway,
|
|
|
+ "[ai.aiyunyi.detect] " + GATEWAY_CLASS + " 不存在:签名/令牌/转发须服务端网关统一收口(§3.1)。");
|
|
|
+
|
|
|
+ Field gatewayField = findGatewayField(host, gateway);
|
|
|
+ assertNotNull(gatewayField,
|
|
|
+ "[ai.aiyunyi.detect] AicdsDetectionService 应持有 AicdsGatewayService 字段,"
|
|
|
+ + "以便 §8 用 mock gateway 单测编排(无 Spring / 无真实 HTTP)。");
|
|
|
+ assertNotNull(mock(gateway),
|
|
|
+ "[ai.aiyunyi.detect] AicdsGatewayService 应可被 Mockito mock(无 Spring / 无外部服务)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:录入检测数据请求应含 §A.4 全部必填键")
|
|
|
+ void requestPayload_containsAllRequiredKeys() {
|
|
|
+ Object host = contractHost();
|
|
|
+ Map<String, Object> request = buildRequest(host, 0);
|
|
|
+ for (String key : REQUEST_KEYS) {
|
|
|
+ assertTrue(request.containsKey(key),
|
|
|
+ "[ai.aiyunyi.detect] enter/detection_data 请求缺少必填键 " + key + "(契约 §A.4)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:basicUrlList 恰好 12 张,type=0,id=index,isQualified 留空")
|
|
|
+ void basicUrlList_twelveItemsTypeZero() {
|
|
|
+ Object host = contractHost();
|
|
|
+ Map<String, Object> request = buildRequest(host, 0);
|
|
|
+ List<?> basic = requireUrlList(request, "basicUrlList");
|
|
|
+ assertEquals(12, basic.size(),
|
|
|
+ "[ai.aiyunyi.detect] basicUrlList 必须是标准 12 张基本图片(§A.4 / §3.4)。");
|
|
|
+
|
|
|
+ for (int i = 0; i < basic.size(); i++) {
|
|
|
+ Object item = basic.get(i);
|
|
|
+ assertTrue(isNumber(itemValue(item, "id")) && ((Number) itemValue(item, "id")).intValue() == i,
|
|
|
+ "[ai.aiyunyi.detect] basicUrlList[" + i + "].id 应为 index " + i + "(§A.4)。");
|
|
|
+ assertTrue(isNumber(itemValue(item, "type")) && ((Number) itemValue(item, "type")).intValue() == 0,
|
|
|
+ "[ai.aiyunyi.detect] basicUrlList[" + i + "].type 固定为 0(§A.4 基本图片)。");
|
|
|
+ assertTrue(isBlank(itemValue(item, "isQualified")),
|
|
|
+ "[ai.aiyunyi.detect] basicUrlList[" + i + "].isQualified 上传时应留空(§A.4)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-6:reportUrlList type=3,url 原样透传")
|
|
|
+ void reportUrlList_typeThree() {
|
|
|
+ Object host = contractHost();
|
|
|
+ Map<String, Object> request = buildRequest(host, 0);
|
|
|
+ List<?> reports = requireUrlList(request, "reportUrlList");
|
|
|
+ assertEquals(2, reports.size(), "§A.4 应透传 2 张病历/化验/检查报告图。");
|
|
|
+ for (int i = 0; i < reports.size(); i++) {
|
|
|
+ Object item = reports.get(i);
|
|
|
+ assertTrue(isNumber(itemValue(item, "type")) && ((Number) itemValue(item, "type")).intValue() == 3,
|
|
|
+ "[ai.aiyunyi.detect] reportUrlList[" + i + "].type 固定为 3(§A.4)。");
|
|
|
+ assertEquals("https://cfc.s3.cn-north-1.jdcloud-oss.com/report/" + i + ".jpg",
|
|
|
+ text(itemValue(item, "url")), "病历报告 url 应原样透传(OSS 公有直链,§3.3)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-7:语音 type=1/2、体表异常 type=4")
|
|
|
+ void voiceAndAttachmentUrlList_types() {
|
|
|
+ Object host = contractHost();
|
|
|
+ Map<String, Object> request = buildRequest(host, 0);
|
|
|
+ assertSingleType(request, "familyHistoryUrlList", 1, "家族病史语音");
|
|
|
+ assertSingleType(request, "diseaseUrlList", 2, "已知病症语音");
|
|
|
+ assertSingleType(request, "attachmentUrlList", 4, "体表异常部位");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-8:type 为 0(普通)/ 3(癌症)")
|
|
|
+ void requestType_normalAndCancer() {
|
|
|
+ Object host = contractHost();
|
|
|
+ assertEquals(0, number(buildRequest(host, 0).get("type")), "普通检测 type=0(§A.4)。");
|
|
|
+ assertEquals(3, number(buildRequest(host, 3).get("type")), "癌症检测 type=3(§A.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-9:inspectionType 分流——0/3 录入数据,1/2 取 getPath")
|
|
|
+ void routeSplit_photoVsVideo() {
|
|
|
+ Object host = contractHost();
|
|
|
+ String photoNormal = callString(host, "resolveRoute", 0);
|
|
|
+ String photoCancer = callString(host, "resolveRoute", 3);
|
|
|
+ String videoBmp = callString(host, "resolveRoute", 1);
|
|
|
+ String videoPro = callString(host, "resolveRoute", 2);
|
|
|
+ assertTrue(photoNormal.toLowerCase().contains("data"),
|
|
|
+ "[ai.aiyunyi.detect] inspectionType=0 应走 enter/detection_data 录入数据(§3.4)。");
|
|
|
+ assertTrue(photoCancer.toLowerCase().contains("data"),
|
|
|
+ "[ai.aiyunyi.detect] inspectionType=3(癌症)应走录入数据(§3.4)。");
|
|
|
+ assertTrue(videoBmp.toLowerCase().contains("path"),
|
|
|
+ "[ai.aiyunyi.detect] inspectionType=1(AIBMP)应先取 getPath(§3.4)。");
|
|
|
+ assertTrue(videoPro.toLowerCase().contains("path"),
|
|
|
+ "[ai.aiyunyi.detect] inspectionType=2(AIBMP-PRO)应先取 getPath(§3.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:空素材列表应优雅处理,不抛异常、列表字段存在")
|
|
|
+ void boundary_emptyLists_handledGracefully() {
|
|
|
+ Object host = contractHost();
|
|
|
+ List<String> empty = new ArrayList<String>();
|
|
|
+ Map<String, Object> request = callMap(host, "buildEnterDetectionDataRequest",
|
|
|
+ 1001L, 2002L, 3003L, 4004L, empty, empty, empty, empty, empty, 0);
|
|
|
+ assertNotNull(request, "空素材时仍应返回请求体而非 null/抛异常。");
|
|
|
+ for (String key : REQUEST_KEYS) {
|
|
|
+ assertTrue(request.containsKey(key), "空素材时字段 " + key + " 仍应存在(值可为 null/空列表)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:同一成员重复识别应复用映射(findOrCreateMapping 幂等)")
|
|
|
+ void boundary_idempotentMappingHook() {
|
|
|
+ Class<?> host = loadClass(HOST_CLASS);
|
|
|
+ if (host == null) {
|
|
|
+ fail("[ai.aiyunyi.detect] AicdsDetectionService 不存在,无法校验映射幂等(§3.5)。");
|
|
|
+ }
|
|
|
+ assertNotNull(findMethodByName(host, "findOrCreateMapping"),
|
|
|
+ "[ai.aiyunyi.detect] 应提供 findOrCreateMapping(...) 复用已建检测人/未用子项,"
|
|
|
+ + "保证同一成员重复识别幂等(§3.5 / §4.3 aiyunyi_mapping)。");
|
|
|
+ String source = readDetectionSource();
|
|
|
+ assertTrue(!source.isEmpty(),
|
|
|
+ "[ai.aiyunyi.detect] 无法读取 AicdsDetectionService.java,无法校验映射落地。");
|
|
|
+ assertTrue(source.toLowerCase().contains("aiyunyi"),
|
|
|
+ "[ai.aiyunyi.detect] 编排应落地 aiyunyi_mapping 映射关联,"
|
|
|
+ + "否则重复识别无法幂等复用(§3.5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-3:编排应覆盖 §5 十步且顺序正确,视频另调 getPath")
|
|
|
+ void boundary_orchestrationStepsPresentAndOrdered() {
|
|
|
+ String source = readDetectionSource();
|
|
|
+ assertTrue(!source.isEmpty(),
|
|
|
+ "[ai.aiyunyi.detect] 无法读取 " + DETECTION_SOURCE + ",无法校验 §5 编排步骤。");
|
|
|
+ String norm = normalize(source);
|
|
|
+
|
|
|
+ int[] positions = new int[ORCHESTRATION_STEPS.length];
|
|
|
+ for (int i = 0; i < ORCHESTRATION_STEPS.length; i++) {
|
|
|
+ int found = -1;
|
|
|
+ for (String alias : ORCHESTRATION_STEPS[i]) {
|
|
|
+ int idx = norm.indexOf(alias);
|
|
|
+ if (idx >= 0 && (found < 0 || idx < found)) {
|
|
|
+ found = idx;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ positions[i] = found;
|
|
|
+ assertTrue(found >= 0,
|
|
|
+ "[ai.aiyunyi.detect] 编排缺少步骤 " + (i + 1) + " " + STEP_LABELS[i]
|
|
|
+ + "(§5 照片路径时序)。");
|
|
|
+ }
|
|
|
+ assertTrue(positions[0] < positions[8],
|
|
|
+ "[ai.aiyunyi.detect] identify 必须在 enter/detection_data 之前(§5)。");
|
|
|
+ assertTrue(positions[8] < positions[9],
|
|
|
+ "[ai.aiyunyi.detect] 录入检测数据必须在 report/get 拉报告之前(§5 异步生成)。");
|
|
|
+ assertTrue(norm.contains("getpath"),
|
|
|
+ "[ai.aiyunyi.detect] 视频路径(inspectionType 1/2)应调 getPath 取 H5 检测页(§3.4/§5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-4:无商品/无可用子项应有明确归一提示(§4.5)")
|
|
|
+ void boundary_errorNormalizationMessages() {
|
|
|
+ String source = readDetectionSource();
|
|
|
+ assertTrue(!source.isEmpty(),
|
|
|
+ "[ai.aiyunyi.detect] 无法读取 " + DETECTION_SOURCE + ",无法校验错误归一提示。");
|
|
|
+ assertTrue(source.contains("该服务尚未开通"),
|
|
|
+ "[ai.aiyunyi.detect] spu/page 返回空应归一为「该服务尚未开通」而非报错(§4.5)。");
|
|
|
+ assertTrue(source.contains("次数已用完"),
|
|
|
+ "[ai.aiyunyi.detect] get-items 无 status=0 子项应提示「次数已用完」(§4.5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1:录入检测数据 type 仅允许 0/3,视频类型应快速失败")
|
|
|
+ void exception_invalidTypeRejected() {
|
|
|
+ Object host = contractHost();
|
|
|
+ assertThrows(RuntimeException.class,
|
|
|
+ () -> invokePropagating(host, "buildEnterDetectionDataRequest",
|
|
|
+ 1001L, 2002L, 3003L, 4004L,
|
|
|
+ basicUrls(), reportUrls(), voiceUrls(), voiceUrls(), attachmentUrls(), 1),
|
|
|
+ "[ai.aiyunyi.detect] type=1/2 属视频检测,不得传给 enter/detection_data(仅 0 普通 / 3 癌症,§A.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流(不适用) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:服务端代理统一收口鉴权,编排内部无独立角色门禁,权限流不适用")
|
|
|
+ void permissionFlow_notApplicable() {
|
|
|
+ // 契约 AC 均为后端编排/网关内部行为:RSA 私钥与令牌只在服务端(§3.1),
|
|
|
+ // 前端仅带小程序 token,无独立用户角色/权限分支断言。
|
|
|
+ assertTrue(true, "权限流不适用:ai.aiyunyi.detect 为服务端代理编排,鉴权由网关统一收口。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 请求体构造辅助 ====================
|
|
|
+
|
|
|
+ private Map<String, Object> buildRequest(Object host, int type) {
|
|
|
+ return callMap(host, "buildEnterDetectionDataRequest",
|
|
|
+ 1001L, 2002L, 3003L, 4004L,
|
|
|
+ basicUrls(), reportUrls(), voiceUrls(), voiceUrls(), attachmentUrls(), type);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> basicUrls() {
|
|
|
+ List<String> urls = new ArrayList<String>();
|
|
|
+ for (int i = 0; i < 12; i++) {
|
|
|
+ urls.add("https://cfc.s3.cn-north-1.jdcloud-oss.com/basic/" + i + ".jpg");
|
|
|
+ }
|
|
|
+ return urls;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> reportUrls() {
|
|
|
+ return Arrays.asList(
|
|
|
+ "https://cfc.s3.cn-north-1.jdcloud-oss.com/report/0.jpg",
|
|
|
+ "https://cfc.s3.cn-north-1.jdcloud-oss.com/report/1.jpg");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> voiceUrls() {
|
|
|
+ return Arrays.asList("https://cfc.s3.cn-north-1.jdcloud-oss.com/voice/a.m4a");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> attachmentUrls() {
|
|
|
+ return Arrays.asList("https://cfc.s3.cn-north-1.jdcloud-oss.com/attachment/a.jpg");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertSingleType(Map<String, Object> request, String key, int expectedType, String label) {
|
|
|
+ Object value = request.get(key);
|
|
|
+ assertTrue(value instanceof List, label + " 列表应为数组(可为 null 时此项不参与)");
|
|
|
+ List<?> items = (List<?>) value;
|
|
|
+ assertEquals(1, items.size(), label + " 应透传 1 项");
|
|
|
+ Object item = items.get(0);
|
|
|
+ assertTrue(isNumber(itemValue(item, "type")) && ((Number) itemValue(item, "type")).intValue() == expectedType,
|
|
|
+ "[ai.aiyunyi.detect] " + label + ".type 固定为 " + expectedType + "(§A.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private List<?> requireUrlList(Map<String, Object> request, String key) {
|
|
|
+ Object value = request.get(key);
|
|
|
+ assertNotNull(value, "[ai.aiyunyi.detect] 请求体缺少列表字段 " + key + "(§A.4)。");
|
|
|
+ assertTrue(value instanceof List, "[ai.aiyunyi.detect] 字段 " + key + " 应为列表。");
|
|
|
+ return (List<?>) value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 同时兼容 Map 请求项与 DTO 对象项(getter / 字段),避免约束实现方的内部载体类型。 */
|
|
|
+ private Object itemValue(Object item, String key) {
|
|
|
+ if (item == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (item instanceof Map) {
|
|
|
+ return ((Map<?, ?>) item).get(key);
|
|
|
+ }
|
|
|
+ String suffix = Character.toUpperCase(key.charAt(0)) + key.substring(1);
|
|
|
+ try {
|
|
|
+ Method getter = item.getClass().getMethod("get" + suffix);
|
|
|
+ return getter.invoke(item);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // 继续尝试字段
|
|
|
+ }
|
|
|
+ Field f = findField(item.getClass(), key);
|
|
|
+ if (f == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ f.setAccessible(true);
|
|
|
+ return f.get(item);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNumber(Object value) {
|
|
|
+ return value instanceof Number;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int number(Object value) {
|
|
|
+ assertTrue(isNumber(value), "[ai.aiyunyi.detect] type 应为数值(0/3),实际 " + value);
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isBlank(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return value instanceof CharSequence && ((CharSequence) value).toString().trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String text(Object value) {
|
|
|
+ return value == null ? null : value.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射 / 源码辅助 ====================
|
|
|
+
|
|
|
+ private static Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object contractHost() {
|
|
|
+ Class<?> clazz = loadClass(HOST_CLASS);
|
|
|
+ if (clazz == null) {
|
|
|
+ fail("RED[ai.aiyunyi.detect] 契约宿主 AicdsDetectionService 未实现(实现未落地);"
|
|
|
+ + "设计 §3.4/§5 要求照片路径编排。请 GREEN 阶段按类注释 AC4~AC9 的方法签名落地。");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("契约宿主 " + HOST_CLASS + " 实例化失败:" + e.getMessage());
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Method findMethodByName(Class<?> type, String name) {
|
|
|
+ for (Method m : type.getMethods()) {
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Method m : type.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Field findField(Class<?> type, String name) {
|
|
|
+ for (Field f : type.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(name)) {
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 字段类型与网关互相兼容(接口/父类/实现类三种声明均可)。 */
|
|
|
+ private static Field findGatewayField(Class<?> host, Class<?> gateway) {
|
|
|
+ for (Field f : host.getDeclaredFields()) {
|
|
|
+ Class<?> t = f.getType();
|
|
|
+ if (t.isAssignableFrom(gateway) || gateway.isAssignableFrom(t)) {
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Method findMethod(Class<?> type, String name, Object[] args) {
|
|
|
+ for (Method m : type.getMethods()) {
|
|
|
+ if (matches(m, name, args)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Method m : type.getDeclaredMethods()) {
|
|
|
+ if (matches(m, name, args)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean matches(Method m, String name, Object[] args) {
|
|
|
+ if (!m.getName().equals(name)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Class<?>[] types = m.getParameterTypes();
|
|
|
+ if (types.length != args.length) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
+ Object arg = args[i];
|
|
|
+ Class<?> type = types[i];
|
|
|
+ if (arg == null) {
|
|
|
+ if (type.isPrimitive()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else if (type.isAssignableFrom(arg.getClass())) {
|
|
|
+ // 兼容类型(List/Collection/Object 等)
|
|
|
+ } else if ((type == long.class || type == int.class) && arg instanceof Number) {
|
|
|
+ // 基本类型与装箱类型兼容
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object invokePropagating(Object host, String name, Object... args) {
|
|
|
+ Method m = findMethod(host.getClass(), name, args);
|
|
|
+ if (m == null) {
|
|
|
+ fail("RED[ai.aiyunyi.detect] 契约方法 " + HOST_CLASS + "#" + name + "(...) 未实现");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ m.setAccessible(true);
|
|
|
+ return m.invoke(host, args);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ Throwable cause = e.getCause() == null ? e : e.getCause();
|
|
|
+ throw new RuntimeException(cause);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object call(Object host, String name, Object... args) {
|
|
|
+ try {
|
|
|
+ return invokePropagating(host, name, args);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ fail("契约方法 " + HOST_CLASS + "#" + name + " 调用失败:" + e.getMessage());
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String callString(Object host, String name, Object... args) {
|
|
|
+ Object result = call(host, name, args);
|
|
|
+ if (result instanceof CharSequence) {
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ fail("契约方法 " + name + " 应返回字符串,实际 " + result);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private Map<String, Object> callMap(Object host, String name, Object... args) {
|
|
|
+ Object result = call(host, name, args);
|
|
|
+ if (result instanceof Map) {
|
|
|
+ return (Map<String, Object>) result;
|
|
|
+ }
|
|
|
+ fail("契约方法 " + name + " 应返回 Map,实际 " + result);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 归一化:小写化 + 去反引号/引号/空白,便于在源码中匹配方法名与字符串字面量。 */
|
|
|
+ private static String normalize(String source) {
|
|
|
+ return source.toLowerCase().replace("`", "").replace("\"", "").replaceAll("\\s+", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** mvn test 工作目录为模块根(cfc-backend),相对路径可直接读取(同 AiYunyiMappingContractTest)。 */
|
|
|
+ private static String readDetectionSource() {
|
|
|
+ try {
|
|
|
+ byte[] bytes = Files.readAllBytes(Paths.get(DETECTION_SOURCE));
|
|
|
+ return new String(bytes, StandardCharsets.UTF_8);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|