|
|
@@ -0,0 +1,254 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.controller.admin.AdminClassController;
|
|
|
+import com.train.entity.TrainClass;
|
|
|
+import com.train.entity.TrainEnrollment;
|
|
|
+import com.train.entity.TrainGroup;
|
|
|
+import com.train.entity.TrainGroupMember;
|
|
|
+import com.train.entity.TrainUser;
|
|
|
+import com.train.mapper.TrainClassMapper;
|
|
|
+import com.train.mapper.TrainEnrollmentMapper;
|
|
|
+import com.train.mapper.TrainGroupMapper;
|
|
|
+import com.train.mapper.TrainGroupMemberMapper;
|
|
|
+import com.train.mapper.TrainUserMapper;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+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.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * {@code train.klass.assign} 契约级单测(TDD-RED,S-train-3)。
|
|
|
+ *
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md §5.5 / TC-1003、TC-3001
|
|
|
+ * 「分班名单/座位/分组与讲师关联正确,讲师在班级管理页可见;未审核学员不入名单」。</p>
|
|
|
+ *
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB),
|
|
|
+ * 覆盖幂等重放 replay=true、同键异载荷 C4092、乐观锁冲突 C422、
|
|
|
+ * 不存在 C404、越权 C403 五类边界;另附两条核心口径基线用例。</p>
|
|
|
+ *
|
|
|
+ * <p>被测面:管理端「班级管理页」后端 {@link AdminClassController} 的分班/分组入口。
|
|
|
+ * 当前实现未覆盖上述边界(错误一律 Result.error 的 500,且不识别 replay/version/operator),
|
|
|
+ * 本类预期为 RED:至少 1 条断言失败。</p>
|
|
|
+ *
|
|
|
+ * <p>契约错误码(Result.code):
|
|
|
+ * C4092 = 4092 同幂等键但载荷不一致(冲突)
|
|
|
+ * C422 = 422 乐观锁/并发更新冲突(update 影响 0 行)
|
|
|
+ * C404 = 404 目标班级/学员不存在
|
|
|
+ * C403 = 403 非管理员或代他人指派(越权)</p>
|
|
|
+ */
|
|
|
+class TrainKlassAssignContractTest {
|
|
|
+
|
|
|
+ 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 UID = 990001L; // 合成学员(避开种子数据)
|
|
|
+ private static final Long OTHER_UID = 990002L; // 越权场景中的他人
|
|
|
+ private static final Long CLASS_ID = 990101L; // 目标班级
|
|
|
+ private static final Long MISSING_CLASS_ID = 999999L;
|
|
|
+ private static final Long GROUP_ID = 990201L; // 目标分组/座位
|
|
|
+
|
|
|
+ private AdminClassController controller;
|
|
|
+ private TrainClassMapper classMapper;
|
|
|
+ private TrainUserMapper userMapper;
|
|
|
+ private TrainEnrollmentMapper enrollmentMapper;
|
|
|
+ private TrainGroupMapper groupMapper;
|
|
|
+ private TrainGroupMemberMapper groupMemberMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ controller = new AdminClassController();
|
|
|
+ classMapper = mock(TrainClassMapper.class);
|
|
|
+ userMapper = mock(TrainUserMapper.class);
|
|
|
+ enrollmentMapper = mock(TrainEnrollmentMapper.class);
|
|
|
+ groupMapper = mock(TrainGroupMapper.class);
|
|
|
+ groupMemberMapper = mock(TrainGroupMemberMapper.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(controller, "trainClassMapper", classMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "trainUserMapper", userMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "trainEnrollmentMapper", enrollmentMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "trainGroupMapper", groupMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "trainGroupMemberMapper", groupMemberMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, Object> body(Object... kv) {
|
|
|
+ Map<String, Object> m = new HashMap<String, Object>();
|
|
|
+ for (int i = 0; i + 1 < kv.length; i += 2) {
|
|
|
+ m.put((String) kv[i], kv[i + 1]);
|
|
|
+ }
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 已审核通过、待分班的合成学员 */
|
|
|
+ private static TrainUser user() {
|
|
|
+ TrainUser u = new TrainUser();
|
|
|
+ u.setId(UID);
|
|
|
+ u.setName("合成学员(测试)");
|
|
|
+ u.setRole("student");
|
|
|
+ u.setStatus("active");
|
|
|
+ return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** active 状态的目标班级 */
|
|
|
+ private static TrainClass clazz() {
|
|
|
+ TrainClass c = new TrainClass();
|
|
|
+ c.setId(CLASS_ID);
|
|
|
+ c.setName("合成班级(测试)");
|
|
|
+ c.setStatus("active");
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 已审核通过(confirmed)的报名单——分班名单数据来源 */
|
|
|
+ private static TrainEnrollment enrollment() {
|
|
|
+ TrainEnrollment e = new TrainEnrollment();
|
|
|
+ e.setId(990301L);
|
|
|
+ e.setUid(UID);
|
|
|
+ e.setClassId(CLASS_ID);
|
|
|
+ e.setStatus("confirmed");
|
|
|
+ return e;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stubAssignableStudent() {
|
|
|
+ when(userMapper.selectById(UID)).thenReturn(user());
|
|
|
+ when(classMapper.selectById(CLASS_ID)).thenReturn(clazz());
|
|
|
+ when(enrollmentMapper.selectOne(any())).thenReturn(enrollment());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 核心口径基线(TC-1003 / TC-3001) ====================
|
|
|
+
|
|
|
+ /** 分班:学员绑定到目标班级,且班级管理页可看到该班级与分组。 */
|
|
|
+ @Test
|
|
|
+ void assignRosterLinksStudentAndLecturerSeesClassDetail() {
|
|
|
+ stubAssignableStudent();
|
|
|
+ when(groupMapper.selectList(any())).thenReturn(Collections.<TrainGroup>emptyList());
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.moveUserClass(body("uid", UID, "classId", CLASS_ID));
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ ArgumentCaptor<TrainUser> cap = ArgumentCaptor.forClass(TrainUser.class);
|
|
|
+ verify(userMapper).updateById(cap.capture());
|
|
|
+ assertEquals(CLASS_ID, cap.getValue().getClassId(), "分班后学员必须绑定到目标班级");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> detail = controller.classDetail(body("classId", CLASS_ID));
|
|
|
+ assertEquals(200, detail.getCode());
|
|
|
+ assertNotNull(detail.getData().get("clazz"), "班级管理页必须回带班级信息(讲师可见)");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 座位/分组指派:学员移入目标组且只留一条成员记录。 */
|
|
|
+ @Test
|
|
|
+ void assignSeatMovesStudentIntoTargetGroup() {
|
|
|
+ TrainGroup g = new TrainGroup();
|
|
|
+ g.setId(GROUP_ID);
|
|
|
+ g.setClassId(CLASS_ID);
|
|
|
+ g.setName("组1");
|
|
|
+ when(groupMapper.selectById(GROUP_ID)).thenReturn(g);
|
|
|
+ when(groupMemberMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.addGroupMember(body("groupId", GROUP_ID, "uid", UID));
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ verify(groupMemberMapper, times(1)).insert(any(TrainGroupMember.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同一幂等键 (uid, classId) 且 replay=true 重放:
|
|
|
+ * 必须返回同一结果且不得再次写入名单/报名单(去重)。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void replayWithSameKeyDoesNotDuplicateWrites() {
|
|
|
+ stubAssignableStudent();
|
|
|
+
|
|
|
+ Result<Boolean> first = controller.moveUserClass(body("uid", UID, "classId", CLASS_ID));
|
|
|
+ assertEquals(200, first.getCode());
|
|
|
+
|
|
|
+ Result<Boolean> replay = controller.moveUserClass(
|
|
|
+ body("uid", UID, "classId", CLASS_ID, "replay", Boolean.TRUE));
|
|
|
+ assertEquals(200, replay.getCode());
|
|
|
+
|
|
|
+ verify(userMapper, times(1)).updateById(any(TrainUser.class));
|
|
|
+ verify(enrollmentMapper, times(1)).updateById(any(TrainEnrollment.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 幂等键相同但载荷指纹(payloadHash)不同:
|
|
|
+ * 不得静默复用首次分班结果,必须返回 C4092 冲突,让调用方感知载荷不一致。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
+ stubAssignableStudent();
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.moveUserClass(body(
|
|
|
+ "uid", UID, "classId", CLASS_ID, "replay", Boolean.TRUE, "payloadHash", "hash-B"));
|
|
|
+
|
|
|
+ assertEquals(CODE_PAYLOAD_CONFLICT, r.getCode(),
|
|
|
+ "同一幂等键携带不同 payloadHash 必须返回 C4092,禁止静默复用首次分班结果");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 名单行 updateById 影响 0 行(被并发修改/版本不匹配):
|
|
|
+ * 必须返回 C422,不得把失败当成功返回。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void optimisticLockConflictReturnsC422() {
|
|
|
+ when(userMapper.selectById(UID)).thenReturn(user());
|
|
|
+ when(classMapper.selectById(CLASS_ID)).thenReturn(clazz());
|
|
|
+ when(enrollmentMapper.selectOne(any())).thenReturn(null);
|
|
|
+ when(userMapper.updateById(any(TrainUser.class))).thenReturn(0); // 影响 0 行 = 乐观锁冲突
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.moveUserClass(body("uid", UID, "classId", CLASS_ID));
|
|
|
+
|
|
|
+ assertEquals(CODE_OPTIMISTIC_LOCK, r.getCode(),
|
|
|
+ "更新影响 0 行必须返回 C422 乐观锁冲突,不得静默成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 目标班级不存在 → C404,而非笼统的 500。 */
|
|
|
+ @Test
|
|
|
+ void nonExistentClassReturnsC404() {
|
|
|
+ when(userMapper.selectById(UID)).thenReturn(user());
|
|
|
+ when(classMapper.selectById(MISSING_CLASS_ID)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.moveUserClass(body("uid", UID, "classId", MISSING_CLASS_ID));
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(), "目标班级不存在必须返回 C404");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 5:越权 C403(适用时) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 非管理员携带他人 operatorId 代学员分班:
|
|
|
+ * 服务层必须拒绝越权,返回 C403(分班只能由班级管理者本人办理)。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void nonAdminAssignOnBehalfOfOtherReturnsC403() {
|
|
|
+ stubAssignableStudent();
|
|
|
+
|
|
|
+ Result<Boolean> r = controller.moveUserClass(body(
|
|
|
+ "uid", UID, "classId", CLASS_ID,
|
|
|
+ "operatorId", OTHER_UID, "isAdmin", Boolean.FALSE));
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "非管理员代他人分班必须返回 C403 越权拒绝");
|
|
|
+ }
|
|
|
+}
|