Эх сурвалжийг харах

test(S-cfc-277): assessment.appointment.create 预约时间冲突校验 RED 契约测试(SC-P-36 边界,预期红)

Sisyphus Agent 3 өдөр өмнө
parent
commit
224cf05a73

+ 128 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/AssessmentAppointmentCreateContractTest.java

@@ -0,0 +1,128 @@
+package com.etotem.cfc.unit;
+
+import com.etotem.cfc.entity.AssessmentAppointment;
+import com.etotem.cfc.mapper.AssessmentAppointmentMapper;
+import com.etotem.cfc.service.AssessmentAppointmentService;
+import com.etotem.cfc.service.EnergyService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * 契约级单测(TDD-RED):能力键 {@code assessment.appointment.create}——
+ * 测评预约创建的「预约时间冲突校验」边界(SC-P-36)。
+ *
+ * <p>契约来源(需求 SC-P-36 + 设计 §4.10):
+ * <blockquote>
+ *   主流程:{@code POST /api/assessment/appointment/create} → 先消耗一条额度 → 落预约。<br>
+ *   异常/边界:额度不足/过期直接报错不落预约;<b>预约时间冲突校验</b>。
+ * </blockquote></p>
+ *
+ * <p><b>实现现状(本分支 src/main 核实)</b>:预约创建主体已落地——
+ * {@link AssessmentAppointmentService#createAppointment} 直接 {@code this.save()} 落库,
+ * {@code AssessmentAppointmentController} 已含额度消耗({@code consumeQuota} + {@code <=0} 报错不落预约)。
+ * 但 SC-P-36 明确要求的<b>「预约时间冲突校验」边界未实现</b>:同会员同日期/时间已有非终态预约时,
+ * 再次创建会被直接放行(无条件 {@code save}),无任何冲突检查。</p>
+ *
+ * <p><b>RED 预期</b>:以下用例断言「冲突应拒绝创建且不落库」,当前实现一律放行 → 断言失败(红)。</p>
+ *
+ * <p><b>GREEN 升级指引</b>:在 {@code AssessmentAppointmentService.createAppointment} 落库前增加时间冲突校验——
+ * 查询该 {@code memberId} 在 {@code appointmentDate + appointmentTime} 是否已有非终态预约
+ * (status ∈ {0 待确认, 1 已确认};2 已完成/3 已取消/4 未到 为终态不算冲突),
+ * 命中则抛业务异常(消息含「时间冲突」)并<b>不调用 save</b>。实现后本测试自动转绿。</p>
+ *
+ * <p>纯 Mockito / 反射(无 Spring / 无 DB),风格对齐同批 RED 契约测试
+ * (TeamProfitHookContractTest / FamilyJoinValidationRulesContractTest)。</p>
+ */
+class AssessmentAppointmentCreateContractTest {
+
+    private static final String MSG_CONFLICT = "时间冲突";
+
+    private AssessmentAppointmentService service;
+    private AssessmentAppointmentMapper mapper;
+    private EnergyService energyService;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        mapper = mock(AssessmentAppointmentMapper.class);
+        energyService = mock(EnergyService.class);
+        // spy 真实 service:拦截 save(避免 ServiceImpl.baseMapper 未注入导致 NPE 干扰断言)
+        service = spy(new AssessmentAppointmentService());
+        inject(service, "assessmentAppointmentMapper", mapper);
+        inject(service, "energyService", energyService);
+        // stub 掉真实 save:冲突时应「不落库」,非冲突路径由 GREEN 实现走 save
+        doReturn(true).when(service).save(any(AssessmentAppointment.class));
+    }
+
+    // ==================== 边界:时间冲突应拒绝(RED)====================
+
+    @Test
+    @DisplayName("SC-P-36 边界:同会员同日期时间已有待确认(0)预约 → 创建应拒绝且不落库")
+    void 时间冲突_待确认预约_应拒绝不落库() {
+        stubConflictingAppointment(0);
+
+        RuntimeException ex = assertThrows(RuntimeException.class, () ->
+                        service.createAppointment(1L, 10L, 7L, null, null, "2026-11-10", "10:00", "首测"),
+                "SC-P-36 边界要求预约时间冲突校验:同会员同日期时间已有非终态预约应拒绝创建(当前实现无校验,放行)");
+
+        assertTrue(ex.getMessage().contains(MSG_CONFLICT),
+                "拒绝时业务提示应含「时间冲突」;实际实现未做冲突校验,未拒绝。");
+        verify(service, never()).save(any(AssessmentAppointment.class));
+    }
+
+    @Test
+    @DisplayName("SC-P-36 边界:同会员同日期时间已有已确认(1)预约 → 创建应拒绝且不落库")
+    void 时间冲突_已确认预约_应拒绝不落库() {
+        stubConflictingAppointment(1);
+
+        RuntimeException ex = assertThrows(RuntimeException.class, () ->
+                service.createAppointment(1L, 10L, 7L, null, null, "2026-11-10", "10:00", "首测"));
+
+        assertTrue(ex.getMessage().contains(MSG_CONFLICT),
+                "拒绝时业务提示应含「时间冲突」;已确认(1)同属非终态,应计入冲突。");
+        verify(service, never()).save(any(AssessmentAppointment.class));
+    }
+
+    // ==================== helpers ====================
+
+    /** 预置「同 memberId + 同 date + 同 time + 指定状态」的既有预约,供冲突查询命中 */
+    private void stubConflictingAppointment(int status) {
+        AssessmentAppointment existing = new AssessmentAppointment();
+        existing.setId(999L);
+        existing.setUserId(1L);
+        existing.setMemberId(10L);
+        existing.setAppointmentDate("2026-11-10");
+        existing.setAppointmentTime("10:00");
+        existing.setStatus(status);
+        when(mapper.selectList(any())).thenReturn(Collections.singletonList(existing));
+    }
+
+    private static void inject(Object target, String fieldName, Object value) throws Exception {
+        Class<?> clazz = target.getClass();
+        while (clazz != null) {
+            try {
+                Field f = clazz.getDeclaredField(fieldName);
+                f.setAccessible(true);
+                f.set(target, value);
+                return;
+            } catch (NoSuchFieldException e) {
+                clazz = clazz.getSuperclass();
+            }
+        }
+        throw new IllegalStateException("字段不存在: " + fieldName);
+    }
+}