|
|
@@ -0,0 +1,226 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainAttendanceCode;
|
|
|
+import com.train.entity.TrainCohort;
|
|
|
+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.lang.reflect.Field;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+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.codeIssue} 契约级单测(TDD-RED,S-train-4;TC-1004 / TC-3002)。
|
|
|
+ *
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md「场景定义 §2 域一 T2-S1(Must)」——动态签到码含批次+场次+时效,
|
|
|
+ * 过期/作废后失效,可轮换。</p>
|
|
|
+ *
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB),覆盖幂等重放 replay=true、
|
|
|
+ * 同键异载荷 C4092、乐观锁冲突 C422、不存在 C404、越权 C403 五类边界。当前实现未覆盖上述错误码语义,
|
|
|
+ * 本类预期为 RED(至少 1 条断言失败)。契约错误码({@code Result.code}):</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>C4092 = 4092 同幂等键(场次)但载荷不一致(冲突)</li>
|
|
|
+ * <li>C422 = 422 乐观锁/并发更新冲突</li>
|
|
|
+ * <li>C404 = 404 资源不存在</li>
|
|
|
+ * <li>C403 = 403 越权</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class TrainAttendCodeissueContractTest {
|
|
|
+
|
|
|
+ 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 ADMIN_ID = 1L;
|
|
|
+ private static final Long LECTURER_ID = 2L;
|
|
|
+ private static final Long OUTSIDER_ID = 999L;
|
|
|
+ private static final Long COHORT_ID = 10L;
|
|
|
+ private static final String SESSION_ID = "10:1";
|
|
|
+ private static final String OLD_CODE = "OLDCODE1";
|
|
|
+ private static final Long EXISTING_CODE_ID = 7001L;
|
|
|
+
|
|
|
+ private AttendanceServiceImpl service;
|
|
|
+ private TrainAttendanceCodeMapper codeMapper;
|
|
|
+ private TrainCohortMapper cohortMapper;
|
|
|
+ private RoleGuard roleGuard;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new AttendanceServiceImpl();
|
|
|
+ codeMapper = mock(TrainAttendanceCodeMapper.class);
|
|
|
+ cohortMapper = mock(TrainCohortMapper.class);
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainAttendanceCodeMapper", codeMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainAttendanceMapper", mock(TrainAttendanceMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortMapper", cohortMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortSignupMapper", mock(TrainCohortSignupMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", mock(TrainUserMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "creditService", mock(CreditService.class));
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", roleGuard);
|
|
|
+ ReflectionTestUtils.setField(service, "codeValidMinutes", 5);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 批次存在且主讲讲师为 LECTURER_ID(与越权用例的 OUTSIDER_ID 区分) */
|
|
|
+ private void stubCohortExists() {
|
|
|
+ TrainCohort cohort = new TrainCohort();
|
|
|
+ cohort.setId(COHORT_ID);
|
|
|
+ cohort.setName("首期家立方训练营");
|
|
|
+ cohort.setLecturerId(LECTURER_ID);
|
|
|
+ cohort.setStatus("published");
|
|
|
+ when(cohortMapper.selectById(COHORT_ID)).thenReturn(cohort);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 构造一条未过期的 active 签到码(幂等键 = sessionId) */
|
|
|
+ private TrainAttendanceCode activeCode(int hours, String category) {
|
|
|
+ TrainAttendanceCode c = new TrainAttendanceCode();
|
|
|
+ c.setId(EXISTING_CODE_ID);
|
|
|
+ c.setCohortId(COHORT_ID);
|
|
|
+ c.setSessionId(SESSION_ID);
|
|
|
+ c.setCode(OLD_CODE);
|
|
|
+ c.setSessionHours(hours);
|
|
|
+ c.setSessionCategory(category);
|
|
|
+ c.setIssuedBy(LECTURER_ID);
|
|
|
+ c.setValidFrom(new Date(System.currentTimeMillis() - 60_000L));
|
|
|
+ c.setValidTo(new Date(System.currentTimeMillis() + 5 * 60_000L));
|
|
|
+ c.setStatus("active");
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true(基线) ====================
|
|
|
+
|
|
|
+ /** 同场次幂等键重放:返回原码、不得重复发码(当前实现已覆盖,作为对照基线) */
|
|
|
+ @Test
|
|
|
+ void replayTrueWithSameKeyReturnsExistingCodeAndSkipsInsert() {
|
|
|
+ stubCohortExists();
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(480, "theory"));
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(true, ADMIN_ID, SESSION_ID, 480, "theory", "replay");
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
+ assertNotNull(r.getData(), "replay=true 应回带原签到码");
|
|
|
+ assertEquals(EXISTING_CODE_ID, ((Number) r.getData().get("codeId")).longValue(),
|
|
|
+ "重放必须返回原签到码 codeId,不得新建");
|
|
|
+ assertEquals(OLD_CODE, r.getData().get("code"));
|
|
|
+ verify(codeMapper, never()).insert(any(TrainAttendanceCode.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同一场次(同幂等键 sessionId)但载荷指纹不同:学时 480→240、分类 theory→practice。
|
|
|
+ * 不得静默复用旧码,必须返回 C4092 让调用方感知载荷不一致。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
+ stubCohortExists();
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(activeCode(480, "theory"));
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(true, ADMIN_ID, SESSION_ID, 240, "practice", "altered payload");
|
|
|
+
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, r.getCode(),
|
|
|
+ "同一场次幂等键携带不同 sessionHours/sessionCategory 载荷必须返回 C4092(4092) 冲突,禁止静默复用旧码");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /** 实体须有 version 字段支撑乐观锁;过期 version 必须 C422。 */
|
|
|
+ @Test
|
|
|
+ void staleVersionReturnsC422() {
|
|
|
+ Field versionField = findField(TrainAttendanceCode.class, "version");
|
|
|
+ assertNotNull(versionField,
|
|
|
+ "train_attendance_code 实体必须声明 version 字段以支撑乐观锁 C422");
|
|
|
+
|
|
|
+ stubCohortExists();
|
|
|
+ TrainAttendanceCode old = activeCode(480, "theory");
|
|
|
+ setField(old, versionField, 2L);
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(old);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(true, ADMIN_ID, SESSION_ID, 480, "theory", "version=1");
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(), "过期 version 必须返回 C422(422) 乐观锁冲突");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 轮换旧码时 updateById 影响 0 行(并发丢失更新)必须 C422,不得静默成功。 */
|
|
|
+ @Test
|
|
|
+ void rotationLostUpdateReturnsC422() {
|
|
|
+ stubCohortExists();
|
|
|
+ // 无 active 码命中 → 进入 doIssue 轮换分支
|
|
|
+ when(codeMapper.selectOne(any())).thenReturn(null);
|
|
|
+ TrainAttendanceCode old = activeCode(480, "theory");
|
|
|
+ when(codeMapper.selectList(any())).thenReturn(Collections.singletonList(old));
|
|
|
+ when(codeMapper.updateById(any(TrainAttendanceCode.class))).thenReturn(0); // 乐观锁冲突:影响 0 行
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(true, ADMIN_ID, SESSION_ID, 480, "theory", "rotate");
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
+ "轮换影响 0 行必须返回 C422 乐观锁冲突,不得忽略失败继续发码");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 场次对应的批次不存在 → C404,而非笼统的 500。 */
|
|
|
+ @Test
|
|
|
+ void nonExistentCohortReturns404() {
|
|
|
+ when(cohortMapper.selectById(COHORT_ID)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(true, ADMIN_ID, SESSION_ID, 480, "theory", null);
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(), "批次不存在必须返回 C404(404)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
+
|
|
|
+ /** 非管理员、非机构管理者、非该批次主讲讲师发码 → C403。 */
|
|
|
+ @Test
|
|
|
+ void unauthorizedUserReturns403() {
|
|
|
+ stubCohortExists();
|
|
|
+ when(roleGuard.requireOrgManager(OUTSIDER_ID)).thenReturn("无机构管理权限");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.issueCode(false, OUTSIDER_ID, SESSION_ID, 480, "theory", null);
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "非管理员/机构管理者/主讲讲师发码必须返回 C403(403) 越权拒绝");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void setField(Object target, Field field, Object value) {
|
|
|
+ try {
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(target, value);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new IllegalStateException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|