|
|
@@ -0,0 +1,243 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.common.StateGuard;
|
|
|
+import com.train.entity.TrainCohort;
|
|
|
+import com.train.entity.TrainCohortSignup;
|
|
|
+import com.train.mapper.TrainCohortMapper;
|
|
|
+import com.train.mapper.TrainCohortSignupMapper;
|
|
|
+import com.train.mapper.TrainCourseMapper;
|
|
|
+import com.train.mapper.TrainEnrollmentMapper;
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
+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.HashMap;
|
|
|
+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.Mockito.doAnswer;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * train.enroll.signup 契约级单测(TDD-RED,S-train-2)。
|
|
|
+ *
|
|
|
+ * 需求锚点:docs/tdd/SRS.md §5.5 / TC-1002、TC-3001、TC-2009
|
|
|
+ * 「报名单 status=pending;容量满拒;审核通过占位锁定」。
|
|
|
+ *
|
|
|
+ * 本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB),
|
|
|
+ * 覆盖幂等重放 replay=true、同键异载荷 C4092、乐观锁冲突 C422、
|
|
|
+ * 不存在 C404、越权 C403(适用时)五类边界;另附两条核心口径基线用例。
|
|
|
+ *
|
|
|
+ * 契约错误码(Result.code):
|
|
|
+ * C4092 = 4092 同幂等键但载荷不一致(冲突)
|
|
|
+ * C422 = 422 乐观锁/并发更新冲突
|
|
|
+ * C404 = 404 资源不存在
|
|
|
+ * C403 = 403 越权
|
|
|
+ */
|
|
|
+class TrainEnrollSignupContractTest {
|
|
|
+
|
|
|
+ private static final int CODE_PAYLOAD_CONFLICT = 4092; // C4092
|
|
|
+ private static final int CODE_OPTIMISTIC_LOCK = 422; // C422
|
|
|
+ private static final int CODE_NOT_FOUND = 404; // C404
|
|
|
+ private static final int CODE_FORBIDDEN = 403; // C403
|
|
|
+
|
|
|
+ private static final Long USER_ID = 501L;
|
|
|
+ private static final Long OTHER_USER_ID = 999L;
|
|
|
+ private static final Long COHORT_ID = 10L;
|
|
|
+ private static final Long COURSE_ID = 20L;
|
|
|
+ private static final Long SIGNUP_ID = 777L;
|
|
|
+
|
|
|
+ private CohortServiceImpl service;
|
|
|
+ private TrainCohortMapper cohortMapper;
|
|
|
+ private TrainCohortSignupMapper signupMapper;
|
|
|
+ private TrainEnrollmentMapper enrollmentMapper;
|
|
|
+ private TrainCourseMapper courseMapper;
|
|
|
+ private TrainUserMapper userMapper;
|
|
|
+ private RoleGuard roleGuard;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new CohortServiceImpl();
|
|
|
+ cohortMapper = mock(TrainCohortMapper.class);
|
|
|
+ signupMapper = mock(TrainCohortSignupMapper.class);
|
|
|
+ enrollmentMapper = mock(TrainEnrollmentMapper.class);
|
|
|
+ courseMapper = mock(TrainCourseMapper.class);
|
|
|
+ userMapper = mock(TrainUserMapper.class);
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortMapper", cohortMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortSignupMapper", signupMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainEnrollmentMapper", enrollmentMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainCourseMapper", courseMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", userMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", roleGuard);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> body(Object... kv) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ for (int i = 0; i + 1 < kv.length; i += 2) {
|
|
|
+ m.put((String) kv[i], kv[i + 1]);
|
|
|
+ }
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 已发布批次,容量 30,无报名截止限制 */
|
|
|
+ private void stubPublishedCohort() {
|
|
|
+ TrainCohort c = new TrainCohort();
|
|
|
+ c.setId(COHORT_ID);
|
|
|
+ c.setCourseId(COURSE_ID);
|
|
|
+ c.setStatus(StateGuard.C_PUBLISHED);
|
|
|
+ c.setCapacity(30);
|
|
|
+ c.setDeleted(0);
|
|
|
+ when(cohortMapper.selectById(COHORT_ID)).thenReturn(c);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 首次报名完整链路桩:无存量、容量未占用、insert 回填 signupId */
|
|
|
+ private void stubFirstSignupPath(int updateAffectedRows) {
|
|
|
+ stubPublishedCohort();
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(null);
|
|
|
+ when(signupMapper.selectCount(any())).thenReturn(0L);
|
|
|
+ when(enrollmentMapper.selectOne(any())).thenReturn(null);
|
|
|
+ when(signupMapper.updateById(any())).thenReturn(updateAffectedRows);
|
|
|
+ doAnswer(inv -> {
|
|
|
+ TrainCohortSignup sig = inv.getArgument(0);
|
|
|
+ sig.setId(SIGNUP_ID);
|
|
|
+ return 1;
|
|
|
+ }).when(signupMapper).insert(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 构造已落库的 pending 报名单(幂等键已存在) */
|
|
|
+ private TrainCohortSignup storedPendingSignup() {
|
|
|
+ TrainCohortSignup sig = new TrainCohortSignup();
|
|
|
+ sig.setId(SIGNUP_ID);
|
|
|
+ sig.setUserId(USER_ID);
|
|
|
+ sig.setCohortId(COHORT_ID);
|
|
|
+ sig.setCourseId(COURSE_ID);
|
|
|
+ sig.setStatus("pending");
|
|
|
+ sig.setDeleted(0);
|
|
|
+ return sig;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 核心口径基线(TC-1002) ====================
|
|
|
+
|
|
|
+ /** 首报生成 pending 报名单,且不重复落库 */
|
|
|
+ @Test
|
|
|
+ void firstSignupCreatesPendingRecord() {
|
|
|
+ stubFirstSignupPath(1);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID, body("cohortId", COHORT_ID));
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertEquals("pending", r.getData().get("status"));
|
|
|
+ assertNotNull(r.getData().get("signupId"));
|
|
|
+ verify(signupMapper, times(1)).insert(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 容量已满 → 拒绝报名(占位仅发生在审核通过后) */
|
|
|
+ @Test
|
|
|
+ void capacityFullRejects() {
|
|
|
+ stubPublishedCohort();
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(null);
|
|
|
+ when(signupMapper.selectCount(any())).thenReturn(30L); // approved 已满
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID, body("cohortId", COHORT_ID));
|
|
|
+
|
|
|
+ assertNotEquals(200, r.getCode(), "容量满必须拒绝,不得生成新报名单");
|
|
|
+ verify(signupMapper, times(0)).insert(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同一幂等键 (userId, cohortId) 且 replay=true 重放:
|
|
|
+ * 必须返回同一 signupId / pending 状态,且不得再次 insert(去重)。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void replayWithSameKeyReturnsSameSignupIdWithoutDuplicateInsert() {
|
|
|
+ stubFirstSignupPath(1);
|
|
|
+ Result<Map<String, Object>> first = service.signup(USER_ID, COHORT_ID, body("cohortId", COHORT_ID));
|
|
|
+ assertEquals(200, first.getCode());
|
|
|
+
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(storedPendingSignup());
|
|
|
+ Result<Map<String, Object>> replay = service.signup(
|
|
|
+ USER_ID, COHORT_ID, body("cohortId", COHORT_ID, "replay", true));
|
|
|
+
|
|
|
+ assertEquals(200, replay.getCode());
|
|
|
+ assertEquals(first.getData().get("signupId"), replay.getData().get("signupId"),
|
|
|
+ "replay 必须返回首次报名的同一 signupId");
|
|
|
+ assertEquals("pending", replay.getData().get("status"));
|
|
|
+ // 重放不得重复落库报名单
|
|
|
+ verify(signupMapper, times(1)).insert(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 幂等键相同但载荷指纹(payloadHash)不同:
|
|
|
+ * 不得静默复用首次结果,必须返回 C4092 冲突,让调用方感知载荷不一致。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void sameKeyDifferentPayloadReturns4092() {
|
|
|
+ stubPublishedCohort();
|
|
|
+ when(signupMapper.selectOne(any())).thenReturn(storedPendingSignup()); // 键已存在(首报载荷 hash-A)
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID,
|
|
|
+ body("cohortId", COHORT_ID, "replay", true, "payloadHash", "hash-B"));
|
|
|
+
|
|
|
+ assertEquals(CODE_PAYLOAD_CONFLICT, r.getCode(),
|
|
|
+ "同一幂等键携带不同 payloadHash 必须返回 C4092,不得静默返回首次报名结果");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报名行 updateById 影响 0 行(被并发修改/版本不匹配):
|
|
|
+ * 必须返回 C422,不得把失败当成功返回。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void optimisticLockConflictReturns422() {
|
|
|
+ stubFirstSignupPath(0); // updateById 影响 0 行 = 乐观锁冲突
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID, body("cohortId", COHORT_ID));
|
|
|
+
|
|
|
+ assertEquals(CODE_OPTIMISTIC_LOCK, r.getCode(),
|
|
|
+ "更新影响 0 行必须返回 C422 乐观锁冲突,不得静默成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 批次不存在 → C404,而非笼统的 500 */
|
|
|
+ @Test
|
|
|
+ void nonExistentCohortReturns404() {
|
|
|
+ when(cohortMapper.selectById(COHORT_ID)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID, body("cohortId", COHORT_ID));
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(), "批次不存在必须返回 C404");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 5:越权 C403(适用时) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * body 中夹带他人 userId 试图代报名:
|
|
|
+ * 服务层必须拒绝越权,返回 C403(报名只能为已登录本人办理)。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void bodyUserIdOverrideReturns403() {
|
|
|
+ stubFirstSignupPath(1);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.signup(USER_ID, COHORT_ID,
|
|
|
+ body("cohortId", COHORT_ID, "userId", OTHER_USER_ID));
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(), "body 夹带他人 userId 必须返回 C403 越权拒绝");
|
|
|
+ }
|
|
|
+}
|