|
@@ -0,0 +1,239 @@
|
|
|
|
|
+package com.train.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.train.common.Result;
|
|
|
|
|
+import com.train.entity.TrainAttendance;
|
|
|
|
|
+import com.train.entity.TrainAttendanceCode;
|
|
|
|
|
+import com.train.entity.TrainCohortSignup;
|
|
|
|
|
+import com.train.mapper.TrainAttendanceCodeMapper;
|
|
|
|
|
+import com.train.mapper.TrainAttendanceMapper;
|
|
|
|
|
+import com.train.mapper.TrainCohortMapper;
|
|
|
|
|
+import com.train.mapper.TrainCohortSignupMapper;
|
|
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
|
|
+import com.train.service.CreditService;
|
|
|
|
|
+import com.train.service.RoleGuard;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyInt;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
|
+import static org.mockito.Mockito.doThrow;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * {@code train.attend.scanSign} 契约级单测(TDD-RED,S-train-5;TC-1005 / TC-3002)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md「场景定义 §2 域一 T2-S2(Must)」——一次有效、≤30s 去重为同一条、
|
|
|
|
|
+ * 无效/过期码 100% 拒绝、不做人脸;TC-1005 契约单测 / TC-3002 现场学时端到端。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB),覆盖幂等重放 replay=true、
|
|
|
|
|
+ * 同键异载荷 C4092、乐观锁冲突 C422、不存在 C404、越权 C403 五类边界。当前实现以笼统 {@code Result.error}
|
|
|
|
|
+ * (code=500)返回全部错误语义、且幂等命中时不校验载荷一致性,故本类预期为 RED(至少 1 条断言失败)。
|
|
|
|
|
+ * 契约错误码({@code Result.code}):</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>C4092 = 4092 同幂等键(user_id + session_id)但载荷不一致(学时/分类冲突)</li>
|
|
|
|
|
+ * <li>C422 = 422 乐观锁/并发更新冲突(影响 0 行)</li>
|
|
|
|
|
+ * <li>C404 = 404 资源不存在(无效/过期签到码)</li>
|
|
|
|
|
+ * <li>C403 = 403 越权(非该批次 approved 报名人扫码)</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ */
|
|
|
|
|
+class TrainAttendScansignContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Integer CODE_OK = 200;
|
|
|
|
|
+ private static final Integer CODE_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
|
|
+ private static final Integer CODE_VERSION_CONFLICT = 422;
|
|
|
|
|
+ private static final Integer CODE_NOT_FOUND = 404;
|
|
|
|
|
+ private static final Integer CODE_FORBIDDEN = 403;
|
|
|
|
|
+
|
|
|
|
|
+ private static final Long USER_ID = 100L;
|
|
|
|
|
+ private static final Long COHORT_ID = 10L;
|
|
|
|
|
+ private static final String SESSION_ID = "10:1";
|
|
|
|
|
+ private static final String SCAN_CODE = "SCANCODE1";
|
|
|
|
|
+ private static final Long EXISTING_ATT_ID = 7001L;
|
|
|
|
|
+ private static final String ST_APPROVED = "approved";
|
|
|
|
|
+
|
|
|
|
|
+ private AttendanceServiceImpl service;
|
|
|
|
|
+ private TrainAttendanceCodeMapper codeMapper;
|
|
|
|
|
+ private TrainAttendanceMapper attendanceMapper;
|
|
|
|
|
+ private TrainCohortSignupMapper signupMapper;
|
|
|
|
|
+ private CreditService creditService;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ service = new AttendanceServiceImpl();
|
|
|
|
|
+ codeMapper = mock(TrainAttendanceCodeMapper.class);
|
|
|
|
|
+ attendanceMapper = mock(TrainAttendanceMapper.class);
|
|
|
|
|
+ signupMapper = mock(TrainCohortSignupMapper.class);
|
|
|
|
|
+ creditService = mock(CreditService.class);
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainAttendanceCodeMapper", codeMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainAttendanceMapper", attendanceMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortMapper", mock(TrainCohortMapper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortSignupMapper", signupMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", mock(TrainUserMapper.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "creditService", creditService);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", mock(RoleGuard.class));
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "codeValidMinutes", 5);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造一条未过期的 active 签到码(幂等键 = user_id + session_id) */
|
|
|
|
|
+ private TrainAttendanceCode activeCode(int hours, String category) {
|
|
|
|
|
+ return code(hours, category, new Date(System.currentTimeMillis() + 5 * 60_000L), "active");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造一条已过期但仍标 active 的签到码,用于「过期码 100% 拒绝」用例 */
|
|
|
|
|
+ private TrainAttendanceCode expiredCode() {
|
|
|
|
|
+ return code(480, "theory", new Date(System.currentTimeMillis() - 60_000L), "active");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private TrainAttendanceCode code(int hours, String category, Date validTo, String status) {
|
|
|
|
|
+ TrainAttendanceCode c = new TrainAttendanceCode();
|
|
|
|
|
+ c.setId(9001L);
|
|
|
|
|
+ c.setCohortId(COHORT_ID);
|
|
|
|
|
+ c.setSessionId(SESSION_ID);
|
|
|
|
|
+ c.setCode(SCAN_CODE);
|
|
|
|
|
+ c.setSessionHours(hours);
|
|
|
|
|
+ c.setSessionCategory(category);
|
|
|
|
|
+ c.setIssuedBy(2L);
|
|
|
|
|
+ c.setValidFrom(new Date(System.currentTimeMillis() - 120_000L));
|
|
|
|
|
+ c.setValidTo(validTo);
|
|
|
|
|
+ c.setStatus(status);
|
|
|
|
|
+ return c;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 该批次 approved 报名人 */
|
|
|
|
|
+ private TrainCohortSignup approvedSignup() {
|
|
|
|
|
+ TrainCohortSignup s = new TrainCohortSignup();
|
|
|
|
|
+ s.setId(8001L);
|
|
|
|
|
+ s.setUserId(USER_ID);
|
|
|
|
|
+ s.setCohortId(COHORT_ID);
|
|
|
|
|
+ s.setStatus(ST_APPROVED);
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 已存在的签到记录(快照 hours/category 由调用方指定) */
|
|
|
|
|
+ private TrainAttendance existingAtt(int hours, String category) {
|
|
|
|
|
+ TrainAttendance a = new TrainAttendance();
|
|
|
|
|
+ a.setId(EXISTING_ATT_ID);
|
|
|
|
|
+ a.setUserId(USER_ID);
|
|
|
|
|
+ a.setCohortId(COHORT_ID);
|
|
|
|
|
+ a.setSessionId(SESSION_ID);
|
|
|
|
|
+ a.setCode(SCAN_CODE);
|
|
|
|
|
+ a.setSessionHours(hours);
|
|
|
|
|
+ a.setSessionCategory(category);
|
|
|
|
|
+ a.setAttendedAt(new Date());
|
|
|
|
|
+ a.setSource("scan");
|
|
|
|
|
+ a.setStatus("pending");
|
|
|
|
|
+ a.setDeleted(0);
|
|
|
|
|
+ return a;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true(≤30s 去重基线) ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 同场次(同幂等键)重复扫码:返回原 attendanceId、alreadyExists=true,且不得重复写台账。
|
|
|
|
|
+ * 对应 TC-1005「≤30s 去重为同一条」。当前实现已覆盖,作为对照基线。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void replaySameCodeWithinDedupWindowReturnsSameAttendanceAndNoSecondLedger() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(480, "theory"));
|
|
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(approvedSignup());
|
|
|
|
|
+ when(attendanceMapper.selectOne(any())).thenReturn(existingAtt(480, "theory"));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
|
|
+ assertNotNull(r.getData(), "幂等重放应回带原签到记录");
|
|
|
|
|
+ assertEquals(EXISTING_ATT_ID, ((Number) r.getData().get("attendanceId")).longValue(),
|
|
|
|
|
+ "≤30s 重复扫码必须返回原 attendanceId,不得新建第二条");
|
|
|
|
|
+ assertEquals(Boolean.TRUE, r.getData().get("alreadyExists"), "重放须标记 alreadyExists=true");
|
|
|
|
|
+ verify(attendanceMapper, never()).insert(any(TrainAttendance.class));
|
|
|
|
|
+ verify(creditService, never()).ledgerPending(anyLong(), anyString(), anyString(), anyString(), anyInt(), anyString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 同一幂等键(user_id + session_id)已存在签到,但码携带的学时/分类快照与已有记录不一致
|
|
|
|
|
+ * (480/theory → 240/practice):不得静默复用旧记录,必须返回 C4092 让调用方感知冲突。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameSessionDifferentPayloadReturnsC4092() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(240, "practice"));
|
|
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(approvedSignup());
|
|
|
|
|
+ when(attendanceMapper.selectOne(any())).thenReturn(existingAtt(480, "theory"));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, r.getCode(),
|
|
|
|
|
+ "同一场次幂等键携带不同 sessionHours/sessionCategory 载荷必须返回 C4092(4092) 冲突,禁止静默复用旧签到记录");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 并发扫码写 attendance 影响 0 行(乐观锁/唯一键冲突且未查到重复记录)必须 C422,不得笼统 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void concurrentInsertLostUpdateReturnsC422() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(480, "theory"));
|
|
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(approvedSignup());
|
|
|
|
|
+ when(attendanceMapper.selectOne(any())).thenReturn(null); // 首次查无 + 冲突后回查也无
|
|
|
|
|
+ doThrow(new RuntimeException("concurrent update lost / affected 0 rows"))
|
|
|
|
|
+ .when(attendanceMapper).insert(any(TrainAttendance.class));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
|
|
+ "并发写入影响 0 行必须返回 C422(422) 乐观锁冲突,不得降级为笼统 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 无效签到码(查无 active 码)必须 C404,而非笼统 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void invalidCodeReturns404() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(), "无效签到码必须返回 C404(404)");
|
|
|
|
|
+ verify(attendanceMapper, never()).insert(any(TrainAttendance.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 过期码:100% 拒绝,不得写 attendance、不得写台账(TC-1005)。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void expiredCodeIsRejectedWithoutAttendance() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(expiredCode());
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertNotEquals(CODE_OK, r.getCode(), "过期签到码必须被拒绝,不得返回成功");
|
|
|
|
|
+ verify(attendanceMapper, never()).insert(any(TrainAttendance.class));
|
|
|
|
|
+ verify(creditService, never()).ledgerPending(anyLong(), anyString(), anyString(), anyString(), anyInt(), anyString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 非该批次 approved 报名人扫码 → C403 越权拒绝,而非笼统 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void unapprovedSignupReturns403() {
|
|
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(480, "theory"));
|
|
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(null); // 未报名 / 未审核通过
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.scan(USER_ID, SCAN_CODE);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
|
|
+ "非该批次 approved 报名人扫码必须返回 C403(403) 越权拒绝");
|
|
|
|
|
+ verify(attendanceMapper, never()).insert(any(TrainAttendance.class));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|