|
@@ -0,0 +1,305 @@
|
|
|
|
|
+package com.train.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.train.common.Result;
|
|
|
|
|
+import com.train.entity.TrainAuditLog;
|
|
|
|
|
+import com.train.mapper.TrainOrderDispatchMapper;
|
|
|
|
|
+import com.train.mapper.TrainServiceOrderMapper;
|
|
|
|
|
+import com.train.service.ServiceOrderService;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+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.assertNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * {@code train.global.redline} 契约级单测(TDD-RED,S-train-42;TC-2004 / TC-2010)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md §5.5「需求纪要 §5(合规红线,任何版本不可突破)」——红线 1 无健康评估 /
|
|
|
|
|
+ * 诊疗结论;红线 2 不向服务人员收费 + 不生物识别;红线 3 脱敏 / 最小必要 / 审计;§3.4 Won't 清单
|
|
|
|
|
+ * 6 项均不可实现。TC-2004 为 P0 合规回归(检索健康评估 / 诊疗结论输出、向服务人员收费、生物识别、
|
|
|
|
|
+ * 家政评估解读、train 直写 cfc 表);TC-2010 为 P0 合规回归(worker 单次操作 ≤30s、≤3 步,
|
|
|
|
|
+ * 不提供服务人员完整工作台)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB)。被测面为合规红线校验面:
|
|
|
|
|
+ * 期望存在「红线合规校验」契约入口({@code RedlineService} / {@code ComplianceService} 等)接受
|
|
|
|
|
+ * replay/key/payload 入参,并以 C4092 / C422 / C404 / C403 区分冲突码;当前 main 中无任何该类入口、
|
|
|
|
|
+ * 合规载体无 version 字段、既有 {@link ServiceOrderService#detail(Long, Long)} 对不存在 / 越权一律
|
|
|
|
|
+ * {@code Result.error}(500)。故本类边界用例预期为 RED(至少 1 条断言失败)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>契约错误码({@code Result.code}):C4092=4092 同幂等键异载荷;C422=422 乐观锁冲突;
|
|
|
|
|
+ * C404=404 目标不存在;C403=403 越权。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>说明:本仓后端模块为 train-backend(无字面量 train-impl 子模块),测试按既有契约单测惯例落在
|
|
|
|
|
+ * train-backend/src/test/java/com/train/service/impl/,与 S-train-39/40/41 同目录。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class TrainGlobalRedlineContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
|
|
+ private static final int CODE_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
|
|
+ private static final int CODE_VERSION_CONFLICT = 422;
|
|
|
|
|
+ private static final int CODE_NOT_FOUND = 404;
|
|
|
|
|
+ private static final int CODE_FORBIDDEN = 403;
|
|
|
|
|
+
|
|
|
|
|
+ private static final Long WORKER_ID = 1001L;
|
|
|
|
|
+ private static final Long OTHER_WORKER_ID = 2002L;
|
|
|
|
|
+ private static final Long ORDER_ID = 5001L;
|
|
|
|
|
+ private static final String IDEMPOTENCY_KEY = "REDLINE-1001-COMPLIANCE";
|
|
|
|
|
+
|
|
|
|
|
+ /** 红线校验契约入口的方法名候选(GREEN 实现可任选其一) */
|
|
|
|
|
+ private static final List<String> ENTRY_METHOD_NAMES = Arrays.asList(
|
|
|
|
|
+ "redline", "redlineCheck", "checkRedline", "verifyRedline", "validateRedline",
|
|
|
|
|
+ "evaluateRedline", "scanRedline", "compliance", "complianceCheck",
|
|
|
|
|
+ "checkCompliance", "assertRedline", "guard");
|
|
|
|
|
+
|
|
|
|
|
+ /** 红线校验入口可能落地的类型(全部按类名动态探测;当前 main 中均不存在) */
|
|
|
|
|
+ private static final String[] CANDIDATE_TYPE_NAMES = {
|
|
|
|
|
+ "com.train.service.RedlineService",
|
|
|
|
|
+ "com.train.service.impl.RedlineServiceImpl",
|
|
|
|
|
+ "com.train.service.ComplianceService",
|
|
|
|
|
+ "com.train.service.impl.ComplianceServiceImpl",
|
|
|
|
|
+ "com.train.service.RedlineCheckService",
|
|
|
|
|
+ "com.train.service.impl.RedlineCheckServiceImpl",
|
|
|
|
|
+ "com.train.common.RedlineCheck",
|
|
|
|
|
+ "com.train.common.ComplianceGuard"};
|
|
|
|
|
+
|
|
|
|
|
+ /** 合规载体(审计 / 校验记录)候选类型:前 2 个编译期存在,其余按类名动态探测 */
|
|
|
|
|
+ private static final String[] CANDIDATE_CARRIER_TYPE_NAMES = {
|
|
|
|
|
+ "com.train.entity.TrainAuditLog",
|
|
|
|
|
+ "com.train.entity.TrainServiceOrder",
|
|
|
|
|
+ "com.train.entity.TrainRedlineCheck",
|
|
|
|
|
+ "com.train.entity.TrainComplianceCheck"};
|
|
|
|
|
+
|
|
|
|
|
+ /** 红线 1/2 明令禁止的实现入口(当前均不存在,作为合规基线) */
|
|
|
|
|
+ private static final String[] FORBIDDEN_TYPE_NAMES = {
|
|
|
|
|
+ "com.train.service.FaceRecognitionService",
|
|
|
|
|
+ "com.train.service.impl.FaceRecognitionServiceImpl",
|
|
|
|
|
+ "com.train.service.BiometricService",
|
|
|
|
|
+ "com.train.service.impl.BiometricServiceImpl",
|
|
|
|
|
+ "com.train.service.HealthAssessmentService",
|
|
|
|
|
+ "com.train.service.impl.DiagnosisService",
|
|
|
|
|
+ "com.train.service.ChargeStaffService"};
|
|
|
|
|
+
|
|
|
|
|
+ private ServiceOrderService detailService;
|
|
|
|
|
+ private TrainServiceOrderMapper orderMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ detailService = new ServiceOrderService();
|
|
|
|
|
+ orderMapper = mock(TrainServiceOrderMapper.class);
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(detailService, "orderMapper", orderMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(detailService, "dispatchMapper", mock(TrainOrderDispatchMapper.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 对照基线(当前实现已覆盖,作为对照) ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** Result 已支持分层错误码表达,契约冲突码可据此落地(对照基线,当前通过)。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void resultSupportsTieredContractCodes() {
|
|
|
|
|
+ assertEquals(CODE_OK, Result.success().getCode().intValue());
|
|
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, Result.error(CODE_SAME_KEY_DIFF_PAYLOAD, "同键异载荷").getCode().intValue());
|
|
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, Result.error(CODE_VERSION_CONFLICT, "乐观锁冲突").getCode().intValue());
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, Result.error(CODE_NOT_FOUND, "不存在").getCode().intValue());
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, Result.error(CODE_FORBIDDEN, "越权").getCode().intValue());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 红线 3(审计 / 最小必要)载体须含审计六要素(对照基线,当前实体已具备)。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void auditLogCarriesComplianceEvidence() {
|
|
|
|
|
+ for (String field : Arrays.asList("action", "actorUid", "targetType", "targetId", "detail", "ts")) {
|
|
|
|
|
+ assertNotNull(findField(TrainAuditLog.class, field),
|
|
|
|
|
+ "审计载体 train_audit_log 须承载合规要素 " + field + ",否则红线 3 审计不可举证");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 红线 1/2:代码面不得存在生物识别 / 健康诊疗结论入口(对照基线,当前不存在,通过)。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void noBiometricOrDiagnosisEntryPointExists() {
|
|
|
|
|
+ for (String forbidden : FORBIDDEN_TYPE_NAMES) {
|
|
|
|
|
+ assertNull(loadClass(forbidden),
|
|
|
|
|
+ "红线 1/2:不得存在 " + forbidden + " 生物识别 / 健康诊疗结论 / 向服务人员收费入口");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 同幂等键 replay=true 重放红线校验必须复用首次结果,不得重复落审计 / 重复扫描。
|
|
|
|
|
+ * 当前 main 无红线校验契约入口(不接受 replay/key 入参),故 RED。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replayTrueSameKeyIsIdempotent() {
|
|
|
|
|
+ Method entry = findRedlineEntry();
|
|
|
|
|
+ assertNotNull(entry,
|
|
|
|
|
+ "train.global.redline 契约要求红线校验入口存在并接受 replay/key 入参以支持幂等重放"
|
|
|
|
|
+ + "(TC-2004/TC-2010);当前 main 无任何 RedlineService/ComplianceService 契约入口");
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(hasBooleanParam(entry),
|
|
|
|
|
+ "红线校验入口须有 replay 布尔入参以表达幂等重放;当前入口不支持 replay=true");
|
|
|
|
|
+ assertTrue(hasStringParam(entry),
|
|
|
|
|
+ "红线校验入口须有幂等键 key 入参,同键重放才可被去重(" + IDEMPOTENCY_KEY + ");当前入口不支持幂等键");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 同一幂等键携带不同载荷(不同红线项集合)须返回 C4092,禁止静默重算或重复落审计。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
|
|
+ Method entry = findRedlineEntry();
|
|
|
|
|
+ assertNotNull(entry,
|
|
|
|
|
+ "同键异载荷须返回 C4092(" + CODE_SAME_KEY_DIFF_PAYLOAD + ") 让调用方感知冲突;"
|
|
|
|
|
+ + "但 train.global.redline 契约入口尚未落地,无法表达冲突码");
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(hasPayloadParam(entry),
|
|
|
|
|
+ "红线校验入口须接收载荷(Map / Long 目标 ID)与幂等键 key 方可区分同键异载荷,"
|
|
|
|
|
+ + "并返回 C4092(" + CODE_SAME_KEY_DIFF_PAYLOAD + ")");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 合规载体须有 version 字段支撑并发校验;并发更新影响 0 行时须返回 C422 而非静默成功 / 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void complianceCarrierMustHaveVersionField() {
|
|
|
|
|
+ boolean hasVersion = false;
|
|
|
|
|
+ for (Class<?> type : candidateCarrierTypes()) {
|
|
|
|
|
+ if (findField(type, "version") != null) {
|
|
|
|
|
+ hasVersion = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(hasVersion,
|
|
|
|
|
+ "红线合规载体(审计 / 校验记录)须声明 version 字段,使并发补录 / 复核更新影响 0 行时"
|
|
|
|
|
+ + "可返回 C422(" + CODE_VERSION_CONFLICT + ");当前合规载体无 version");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 校验不存在的红线目标 → C404,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonExistentRedlineTargetReturns404() {
|
|
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = detailService.detail(WORKER_ID, ORDER_ID);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
|
|
+ "校验不存在的红线目标必须返回 C404(" + CODE_NOT_FOUND + "),"
|
|
|
|
|
+ + "当前 detail() 返回 500(工单不存在或不属于您)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 非目标所属人校验其红线合规结果 → C403,而非与「不存在」混同为 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unauthorizedRedlineTargetReturns403() {
|
|
|
|
|
+ // mustOwn 按 workerId 过滤;他人工单查不到 → 当前实现与「不存在」共用同一条 500 分支。
|
|
|
|
|
+ when(orderMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = detailService.detail(OTHER_WORKER_ID, ORDER_ID);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
|
|
+ "非目标所属人校验其红线合规结果必须返回 C403(" + CODE_FORBIDDEN + ") 越权拒绝,"
|
|
|
|
|
+ + "当前 detail() 返回 500(工单不存在或不属于您),无法定位越权");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== helpers ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 在红线校验相关服务 / 工具类上查找契约入口(按候选方法名 + 契约结果返回类型)。 */
|
|
|
|
|
+ private static Method findRedlineEntry() {
|
|
|
|
|
+ for (Class<?> type : candidateTypes()) {
|
|
|
|
|
+ for (String name : ENTRY_METHOD_NAMES) {
|
|
|
|
|
+ for (Method m : type.getDeclaredMethods()) {
|
|
|
|
|
+ if (name.equalsIgnoreCase(m.getName()) && isContractResult(m.getReturnType())) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static List<Class<?>> candidateTypes() {
|
|
|
|
|
+ return loadTypes(CANDIDATE_TYPE_NAMES);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static List<Class<?>> candidateCarrierTypes() {
|
|
|
|
|
+ return loadTypes(CANDIDATE_CARRIER_TYPE_NAMES);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static List<Class<?>> loadTypes(String[] names) {
|
|
|
|
|
+ List<Class<?>> types = new ArrayList<Class<?>>();
|
|
|
|
|
+ for (String name : names) {
|
|
|
|
|
+ Class<?> type = loadClass(name);
|
|
|
|
|
+ if (type != null) {
|
|
|
|
|
+ types.add(type);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return types;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name);
|
|
|
|
|
+ } catch (Throwable ignored) {
|
|
|
|
|
+ // GREEN 实现尚未落地该类,跳过
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean isContractResult(Class<?> t) {
|
|
|
|
|
+ return Result.class.equals(t) || Map.class.equals(t) || List.class.equals(t)
|
|
|
|
|
+ || boolean.class.equals(t) || Boolean.class.equals(t)
|
|
|
|
|
+ || int.class.equals(t) || Integer.class.equals(t);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasBooleanParam(Method m) {
|
|
|
|
|
+ for (Class<?> t : m.getParameterTypes()) {
|
|
|
|
|
+ if (boolean.class.equals(t) || Boolean.class.equals(t)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasStringParam(Method m) {
|
|
|
|
|
+ for (Class<?> t : m.getParameterTypes()) {
|
|
|
|
|
+ if (String.class.equals(t)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasPayloadParam(Method m) {
|
|
|
|
|
+ for (Class<?> t : m.getParameterTypes()) {
|
|
|
|
|
+ if (Map.class.equals(t) || Long.class.equals(t) || long.class.equals(t)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Field findField(Class<?> type, String name) {
|
|
|
|
|
+ Class<?> c = type;
|
|
|
|
|
+ while (c != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return c.getDeclaredField(name);
|
|
|
|
|
+ } catch (NoSuchFieldException ignored) {
|
|
|
|
|
+ c = c.getSuperclass();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|