|
|
@@ -0,0 +1,533 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.invocation.Invocation;
|
|
|
+
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+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;
|
|
|
+import static org.mockito.Mockito.mockingDetails;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED):爱云医 AI 视频检测路径,能力键 {@code ai.aiyunyi.detectVideo}
|
|
|
+ * (任务 S-cfc-451)。
|
|
|
+ *
|
|
|
+ * <p><b>溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code docs/2026-11-24-爱云医AI检测接入方案.md} §3.4 视频路径(inspectionType 1/2)
|
|
|
+ * —— 第 7 步发现 {@code inspectionType=1/2} 后走 {@code getPath} 取 H5 URL,
|
|
|
+ * {@code <web-view>} 内嵌采集,完成后回小程序轮询 package/page 确认完成,再 {@code report/get};
|
|
|
+ * §7 R7(web-view 域名白名单);§8 后端单测要求「mock gateway」。</li>
|
|
|
+ * <li>契约:{@code docs/爱云医接入/爱云医接口契约-完整版.md} §A.5
|
|
|
+ * {@code GET /detection/package/getPath} —— 查询参数 {@code detectionPackageItemId}(必填)
|
|
|
+ * + {@code detectionReportUserId}(必填);响应 {@code data.path} 为检测页面 URL(H5)。</li>
|
|
|
+ * <li>设计:方案 §4.1 新增 {@code AicdsDetectionService}(照片/视频分支编排)+
|
|
|
+ * {@code AicdsGatewayService}(HTTP 转发,仿 AiGateway 可 mock);
|
|
|
+ * §4.5 错误归一(失败必须明确,禁止静默空 URL)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约 AC(实现方按此落地,GREEN 阶段自动转绿)</b></p>
|
|
|
+ * <ol>
|
|
|
+ * <li>AC1 编排宿主 {@code com.etotem.cfc.service.AicdsDetectionService} 存在。</li>
|
|
|
+ * <li>AC2 网关 {@code com.etotem.cfc.service.AicdsGatewayService} 暴露
|
|
|
+ * {@code getPath(detectionPackageItemId, detectionReportUserId)}(两个必填 ID),
|
|
|
+ * 对应 {@code GET /detection/package/getPath},响应取 {@code data.path}。</li>
|
|
|
+ * <li>AC3 {@code String detectVideo(long detectionPackageItemId, long detectionReportUserId)}
|
|
|
+ * 返回网关 {@code getPath} 响应中的 {@code data.path}(H5 URL),不做吞错/默认填空串。</li>
|
|
|
+ * <li>AC4 两个 ID 必须原样透传(itemId → detectionPackageItemId,reportUserId → detectionReportUserId),
|
|
|
+ * 不得交换、丢弃或用固定示例值。</li>
|
|
|
+ * <li>AC5 {@code boolean isVideoInspectionType(int inspectionType)}:{@code 1}(AIBMP)/{@code 2}(AIBMP-PRO)
|
|
|
+ * 为视频路径返回 {@code true};{@code 0}(普通)/{@code 3}(癌症) 及非法值返回 {@code false}。</li>
|
|
|
+ * <li>AC6 web-view 完成后:{@code getReportAfterVideoDetect(long packageId)} 必须先轮询
|
|
|
+ * 检测套餐分页/子项(package/page)确认完成,再调 {@code report/get} 拉报告,顺序不可颠倒。</li>
|
|
|
+ * <li>AC7 {@code data.path} 空白或网关返回错误码(code != 0)时必须快速失败,
|
|
|
+ * 禁止静默返回空/占位 URL(§4.5 错误归一)。</li>
|
|
|
+ * <li>AC8 网关可被 Mockito mock,全部行为可在纯单测环境验证(无 Spring / DB / 真实 HTTP / 网络)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:本分支 {@code src/main} 中 {@code detectVideo / getPath / Aicds /
|
|
|
+ * AiYunyi} 仅出现在同批契约测试中,业务实现 0 引用;宿主类尚未落地。测试通过反射锚定宿主契约
|
|
|
+ * (避免 import 不存在类导致编译失败),运行时宿主缺失即断言失败(红),失败信息明确「实现未落地」。</p>
|
|
|
+ *
|
|
|
+ * <p><b>GREEN 升级指引</b>:按上述 AC 创建 {@code AicdsDetectionService}(注入可 mock 的
|
|
|
+ * {@code AicdsGatewayService}),实现 {@code detectVideo / isVideoInspectionType /
|
|
|
+ * getReportAfterVideoDetect} 与网关 {@code getPath};本测试自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>权限流</b>:本能力为后端代理(私钥签名/令牌收口服务端,§3.1),无面向用户的角色门禁分支,
|
|
|
+ * 权限流不适用,以说明性用例标注。</p>
|
|
|
+ */
|
|
|
+class AiYunyiDetectVideoContractTest {
|
|
|
+
|
|
|
+ /** 设计 §4.1 的视频检测编排宿主(未实现)。 */
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.AicdsDetectionService";
|
|
|
+ /** 设计 §4.1 的 HTTP 转发网关(未实现;测试以 Mockito mock 注入)。 */
|
|
|
+ private static final String GATEWAY_CLASS = "com.etotem.cfc.service.AicdsGatewayService";
|
|
|
+
|
|
|
+ /** 契约 §A.5 示例:子项 id=852、检测报告关联用户 id=2316。 */
|
|
|
+ private static final long DETECTION_PACKAGE_ITEM_ID = 852L;
|
|
|
+ private static final long DETECTION_REPORT_USER_ID = 2316L;
|
|
|
+ private static final long PACKAGE_ID = 859L;
|
|
|
+
|
|
|
+ /** 视频检测 H5 页 URL(getPath 的 data.path)。 */
|
|
|
+ private static final String H5_PATH =
|
|
|
+ "https://devadmin.aicds.com.cn/video-detect/index.html?token=contract-test";
|
|
|
+
|
|
|
+ // ==================== 正常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:视频检测编排宿主 AicdsDetectionService 应存在")
|
|
|
+ void hostClass_exists() {
|
|
|
+ assertNotNull(loadClass(HOST_CLASS),
|
|
|
+ "[ai.aiyunyi.detectVideo] " + HOST_CLASS + " 不存在:设计 §4.1/§3.4 要求新增"
|
|
|
+ + " AicdsDetectionService 编排 getPath→web-view→轮询→report/get(视频分支)。实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:网关应暴露 getPath(子项ID, 报告用户ID) 契约方法")
|
|
|
+ void gateway_exposesGetPathWithBothIds() {
|
|
|
+ Class<?> gateway = loadClass(GATEWAY_CLASS);
|
|
|
+ if (gateway == null) {
|
|
|
+ fail("[ai.aiyunyi.detectVideo] " + GATEWAY_CLASS + " 不存在:设计 §4.1 要求新增可 mock 的"
|
|
|
+ + " 网关转发 GET /detection/package/getPath。实现未落地。");
|
|
|
+ }
|
|
|
+ assertNotNull(findGetPathMethod(gateway),
|
|
|
+ "[ai.aiyunyi.detectVideo] 网关缺少 getPath(detectionPackageItemId, detectionReportUserId)"
|
|
|
+ + " 两参数方法(§A.5 两个 ID 均必填,缺一返回 400)。实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:detectVideo 返回网关 data.path 的 H5 URL")
|
|
|
+ void detectVideo_returnsH5PathFromGateway() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ stubGetPath(gateway, 0, H5_PATH);
|
|
|
+
|
|
|
+ String actual = callString(host, "detectVideo",
|
|
|
+ DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID);
|
|
|
+ assertEquals(H5_PATH, actual,
|
|
|
+ "[ai.aiyunyi.detectVideo] detectVideo 必须返回网关 getPath 响应中的 data.path(H5 URL),"
|
|
|
+ + "不得改写、截断或用默认值替代(§A.5 / §3.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:两个必填 ID 必须原样透传给网关 getPath,不得交换/丢弃")
|
|
|
+ void detectVideo_passesBothIdsToGateway() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ stubGetPath(gateway, 0, H5_PATH);
|
|
|
+
|
|
|
+ call(host, "detectVideo", DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID);
|
|
|
+
|
|
|
+ Invocation getPathInv = firstInvocation(gateway, "getPath");
|
|
|
+ assertNotNull(getPathInv,
|
|
|
+ "[ai.aiyunyi.detectVideo] detectVideo 必须通过网关 getPath 取路径(§3.4 第 8 步)。");
|
|
|
+ Object[] args = getPathInv.getArguments();
|
|
|
+ assertEquals(2, args.length, "getPath 应为两参数(子项 ID + 报告用户 ID)");
|
|
|
+ assertTrue(args[0] instanceof Number && args[1] instanceof Number,
|
|
|
+ "两个 ID 必须以数值类型透传");
|
|
|
+ assertEquals(DETECTION_PACKAGE_ITEM_ID, ((Number) args[0]).longValue(),
|
|
|
+ "[ai.aiyunyi.detectVideo] detectionPackageItemId 必须原样透传(§A.5 必填,缺则 400)。");
|
|
|
+ assertEquals(DETECTION_REPORT_USER_ID, ((Number) args[1]).longValue(),
|
|
|
+ "[ai.aiyunyi.detectVideo] detectionReportUserId 必须原样透传(§A.5 必填)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:inspectionType 1/2 走视频路径,0/3 不走")
|
|
|
+ void isVideoInspectionType_videoTypesOnly() {
|
|
|
+ Object host = hostAndMockGateway()[0];
|
|
|
+ assertTrue(callBoolean(host, "isVideoInspectionType", 1),
|
|
|
+ "[ai.aiyunyi.detectVideo] inspectionType=1 (AIBMP) 必须走 getPath 视频路径(§3.4)。");
|
|
|
+ assertTrue(callBoolean(host, "isVideoInspectionType", 2),
|
|
|
+ "[ai.aiyunyi.detectVideo] inspectionType=2 (AIBMP-PRO) 必须走 getPath 视频路径(§3.4)。");
|
|
|
+ assertFalse(callBoolean(host, "isVideoInspectionType", 0),
|
|
|
+ "[ai.aiyunyi.detectVideo] inspectionType=0 (普通照片) 不得走视频路径,"
|
|
|
+ + "应走 enter/detection_data(§3.4)。");
|
|
|
+ assertFalse(callBoolean(host, "isVideoInspectionType", 3),
|
|
|
+ "[ai.aiyunyi.detectVideo] inspectionType=3 (癌症照片) 不得走视频路径,"
|
|
|
+ + "应走 enter/detection_data(§3.4)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-6:web-view 完成后先轮询 package/page 确认完成,再 report/get")
|
|
|
+ void getReportAfterVideoDetect_pollsBeforeReportGet() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ stubGatewayMethodByPart(gateway, "package", 4); // 轮询返回 status=4(已完成)
|
|
|
+ stubGatewayMethodByPart(gateway, "report", 0); // report/get 返回报告
|
|
|
+
|
|
|
+ call(host, "getReportAfterVideoDetect", PACKAGE_ID);
|
|
|
+
|
|
|
+ List<String> names = invokedMethodNames(gateway);
|
|
|
+ int poll = firstContaining(names, "package");
|
|
|
+ int report = firstContaining(names, "report");
|
|
|
+ assertTrue(poll >= 0,
|
|
|
+ "[ai.aiyunyi.detectVideo] web-view 完成后必须先轮询 package/page(检测套餐分页/子项)"
|
|
|
+ + "确认完成(§3.4 第 10 步)。");
|
|
|
+ assertTrue(report >= 0,
|
|
|
+ "[ai.aiyunyi.detectVideo] 确认完成后必须调 report/get 拉报告展示(§3.4 第 11 步)。");
|
|
|
+ assertTrue(poll < report,
|
|
|
+ "[ai.aiyunyi.detectVideo] 顺序不可颠倒:必须先轮询确认完成,再 report/get。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:inspectionType 非法/未知值不得误判为视频路径")
|
|
|
+ void isVideoInspectionType_unknownTypeIsFalse() {
|
|
|
+ Object host = hostAndMockGateway()[0];
|
|
|
+ assertFalse(callBoolean(host, "isVideoInspectionType", -1), "未知类型 -1 不得走视频路径");
|
|
|
+ assertFalse(callBoolean(host, "isVideoInspectionType", 99), "未知类型 99 不得走视频路径");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:data.path 空白时必须快速失败,禁止静默返回空 URL")
|
|
|
+ void detectVideo_blankPathFailsFast() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ stubGetPath(gateway, 0, " ");
|
|
|
+
|
|
|
+ assertThrows(RuntimeException.class,
|
|
|
+ () -> call(host, "detectVideo", DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID),
|
|
|
+ "[ai.aiyunyi.detectVideo] data.path 空白时必须明确失败,"
|
|
|
+ + "禁止返回空/占位 URL 导致 web-view 白屏(§4.5 错误归一)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-3:网关错误码(code != 0)必须失败,不得返回 data.path")
|
|
|
+ void detectVideo_gatewayErrorCodeFailsFast() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ stubGetPath(gateway, 400, H5_PATH);
|
|
|
+
|
|
|
+ assertThrows(RuntimeException.class,
|
|
|
+ () -> call(host, "detectVideo", DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID),
|
|
|
+ "[ai.aiyunyi.detectVideo] 网关返回错误码时必须失败归一,"
|
|
|
+ + "不得把错误响应里的 path 当作有效 H5 URL 返回(§A.5 缺参返回 400 / §4.5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1:网关不可用时异常应向上抛出,不得吞成默认路径")
|
|
|
+ void detectVideo_gatewayFailurePropagates() {
|
|
|
+ Object[] pair = hostAndMockGateway();
|
|
|
+ Object host = pair[0];
|
|
|
+ Object gateway = pair[1];
|
|
|
+ Method getPath = findGetPathMethod(gateway.getClass());
|
|
|
+ assertNotNull(getPath, "无法定位网关 getPath,无法模拟网关异常");
|
|
|
+ try {
|
|
|
+ when(getPath.invoke(gateway, coerceArgs(getPath,
|
|
|
+ DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID)))
|
|
|
+ .thenThrow(new RuntimeException("mock gateway unavailable"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("模拟网关异常失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ assertThrows(RuntimeException.class,
|
|
|
+ () -> call(host, "detectVideo", DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID),
|
|
|
+ "[ai.aiyunyi.detectVideo] 网关异常必须向上抛出,禁止吞错后返回默认/空路径(§4.5)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流(不适用) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:后端代理无用户角色门禁分支,权限流不适用")
|
|
|
+ void permissionFlow_notApplicable() {
|
|
|
+ // §3.1 私钥签名/令牌全部收口服务端,前端不接触 AppKey/私钥;能力本身无独立角色维度。
|
|
|
+ assertTrue(true, "权限流不适用:ai.aiyunyi.detectVideo 为后端代理能力,无用户权限维度。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射 + Mockito 辅助(避免编译期依赖未落地的类) ====================
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 构造宿主并注入可 mock 的网关:优先构造器注入,其次字段注入(@Resource 惯例)。 */
|
|
|
+ private Object[] hostAndMockGateway() {
|
|
|
+ Class<?> hostClass = loadClass(HOST_CLASS);
|
|
|
+ if (hostClass == null) {
|
|
|
+ fail("RED[ai.aiyunyi.detectVideo] 契约宿主 AicdsDetectionService 未实现(实现未落地);"
|
|
|
+ + "设计 §3.4/§4.1 要求编排 getPath→web-view→轮询→report/get。"
|
|
|
+ + "请 GREEN 阶段按类注释 AC1~AC8 的方法签名落地。");
|
|
|
+ }
|
|
|
+ Class<?> gatewayClass = loadClass(GATEWAY_CLASS);
|
|
|
+ if (gatewayClass == null) {
|
|
|
+ fail("RED[ai.aiyunyi.detectVideo] 契约网关 AicdsGatewayService 未实现(实现未落地);"
|
|
|
+ + "设计 §4.1 要求提供可 mock 的 HTTP 转发网关。");
|
|
|
+ }
|
|
|
+ Object gateway = mock(gatewayClass);
|
|
|
+ try {
|
|
|
+ for (Constructor<?> c : hostClass.getDeclaredConstructors()) {
|
|
|
+ Class<?>[] types = c.getParameterTypes();
|
|
|
+ Object[] args = new Object[types.length];
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
+ if (types[i].isAssignableFrom(gatewayClass) || gatewayClass.isAssignableFrom(types[i])) {
|
|
|
+ args[i] = gateway;
|
|
|
+ } else if (types[i].isInterface() || !types[i].getName().startsWith("java.")) {
|
|
|
+ args[i] = mock(types[i]);
|
|
|
+ } else {
|
|
|
+ args[i] = defaultValue(types[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ c.setAccessible(true);
|
|
|
+ return new Object[]{c.newInstance(args), gateway};
|
|
|
+ }
|
|
|
+ Object host = hostClass.getDeclaredConstructor().newInstance();
|
|
|
+ for (Field f : hostClass.getDeclaredFields()) {
|
|
|
+ if (f.getType().isAssignableFrom(gatewayClass)
|
|
|
+ || gatewayClass.isAssignableFrom(f.getType())) {
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(host, gateway);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new Object[]{host, gateway};
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("契约宿主 " + HOST_CLASS + " 实例化/网关注入失败:" + e.getMessage());
|
|
|
+ throw new IllegalStateException("unreachable", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object defaultValue(Class<?> type) {
|
|
|
+ if (type == long.class) return 0L;
|
|
|
+ if (type == int.class || type == short.class) return 0;
|
|
|
+ if (type == boolean.class) return false;
|
|
|
+ if (type == double.class) return 0D;
|
|
|
+ if (type == float.class) return 0F;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Method findGetPathMethod(Class<?> gatewayClass) {
|
|
|
+ for (Method m : gatewayClass.getMethods()) {
|
|
|
+ if (!"getPath".equals(m.getName()) || m.getParameterCount() != 2) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Class<?>[] p = m.getParameterTypes();
|
|
|
+ boolean numeric = isNumberLike(p[0]) && isNumberLike(p[1]);
|
|
|
+ if (numeric) return m;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNumberLike(Class<?> type) {
|
|
|
+ return type == long.class || type == int.class || type == Long.class
|
|
|
+ || type == Integer.class || type == Number.class || type == Object.class;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 给网关 getPath 打桩:按返回类型返回 String 或含 code/data.path 的 Map。 */
|
|
|
+ private void stubGetPath(Object gateway, int code, String path) {
|
|
|
+ Method m = findGetPathMethod(gateway.getClass());
|
|
|
+ if (m == null) {
|
|
|
+ fail("RED[ai.aiyunyi.detectVideo] 网关 getPath 方法不存在,无法打桩(§A.5)。");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ when(m.invoke(gateway, coerceArgs(m,
|
|
|
+ DETECTION_PACKAGE_ITEM_ID, DETECTION_REPORT_USER_ID)))
|
|
|
+ .thenReturn(responseValue(m.getReturnType(), code, path));
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("网关 getPath 打桩失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 对名称含关键字的网关方法打桩(用于 package/page 轮询与 report/get)。 */
|
|
|
+ private void stubGatewayMethodByPart(Object gateway, String namePart, int status) {
|
|
|
+ Method m = null;
|
|
|
+ for (Method cand : gateway.getClass().getMethods()) {
|
|
|
+ String n = cand.getName().toLowerCase();
|
|
|
+ if (n.contains(namePart.toLowerCase()) && !n.startsWith("getclass")
|
|
|
+ && !n.equals("tostring") && !n.equals("hashcode")) {
|
|
|
+ m = cand;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (m == null) return; // 由调用处的调用次数断言暴露缺失
|
|
|
+ Map<String, Object> body = new HashMap<String, Object>();
|
|
|
+ body.put("status", status);
|
|
|
+ body.put("id", 1172L);
|
|
|
+ Map<String, Object> data = new HashMap<String, Object>();
|
|
|
+ data.put("list", new ArrayList<Map<String, Object>>());
|
|
|
+ data.put("status", status);
|
|
|
+ data.put("id", 1172L);
|
|
|
+ Map<String, Object> resp = new HashMap<String, Object>();
|
|
|
+ resp.put("code", 0);
|
|
|
+ resp.put("data", data);
|
|
|
+ resp.put("msg", "");
|
|
|
+ try {
|
|
|
+ when(m.invoke(gateway, defaultArgs(m))).thenReturn(coerceReturn(m.getReturnType(), resp));
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ // 打桩失败不影响 RED 判定(宿主尚未实现时根本走不到这里)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object responseValue(Class<?> returnType, int code, String path) {
|
|
|
+ if (returnType == String.class || returnType == CharSequence.class) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<String, Object>();
|
|
|
+ data.put("path", path);
|
|
|
+ Map<String, Object> resp = new HashMap<String, Object>();
|
|
|
+ resp.put("code", code);
|
|
|
+ resp.put("data", data);
|
|
|
+ resp.put("msg", code == 0 ? "" : "mock error " + code);
|
|
|
+ return coerceReturn(returnType, resp);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object coerceReturn(Class<?> returnType, Map<String, Object> resp) {
|
|
|
+ if (returnType == String.class || returnType == CharSequence.class) {
|
|
|
+ Object data = resp.get("data");
|
|
|
+ if (data instanceof Map) {
|
|
|
+ Object path = ((Map<?, ?>) data).get("path");
|
|
|
+ return path == null ? "" : String.valueOf(path);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object[] defaultArgs(Method m) {
|
|
|
+ Class<?>[] types = m.getParameterTypes();
|
|
|
+ Object[] args = new Object[types.length];
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
+ args[i] = defaultValue(types[i]);
|
|
|
+ if (types[i] == String.class) args[i] = "";
|
|
|
+ }
|
|
|
+ return args;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object[] coerceArgs(Method m, Object... values) {
|
|
|
+ Class<?>[] types = m.getParameterTypes();
|
|
|
+ Object[] args = new Object[types.length];
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
+ Object v = i < values.length ? values[i] : null;
|
|
|
+ Class<?> t = types[i];
|
|
|
+ if (t.isPrimitive() && v instanceof Number) {
|
|
|
+ Number n = (Number) v;
|
|
|
+ if (t == long.class) args[i] = n.longValue();
|
|
|
+ else if (t == int.class) args[i] = n.intValue();
|
|
|
+ else if (t == short.class) args[i] = n.shortValue();
|
|
|
+ else if (t == double.class) args[i] = n.doubleValue();
|
|
|
+ else if (t == float.class) args[i] = n.floatValue();
|
|
|
+ else args[i] = n.intValue();
|
|
|
+ } else {
|
|
|
+ args[i] = v;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return args;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Invocation firstInvocation(Object mock, String methodName) {
|
|
|
+ for (Invocation inv : mockingDetails(mock).getInvocations()) {
|
|
|
+ if (methodName.equals(inv.getMethod().getName())) return inv;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<String> invokedMethodNames(Object mock) {
|
|
|
+ List<String> names = new ArrayList<String>();
|
|
|
+ for (Invocation inv : mockingDetails(mock).getInvocations()) {
|
|
|
+ names.add(inv.getMethod().getName().toLowerCase());
|
|
|
+ }
|
|
|
+ return names;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int firstContaining(List<String> names, String part) {
|
|
|
+ for (int i = 0; i < names.size(); i++) {
|
|
|
+ if (names.get(i).contains(part.toLowerCase())) return i;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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())) {
|
|
|
+ // 兼容类型
|
|
|
+ } 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.detectVideo] 契约方法 " + 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", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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");
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean callBoolean(Object host, String name, Object... args) {
|
|
|
+ Object result = call(host, name, args);
|
|
|
+ if (result instanceof Boolean) return ((Boolean) result).booleanValue();
|
|
|
+ fail("契约方法 " + name + " 应返回 boolean,实际 " + result);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+}
|