|
|
@@ -0,0 +1,186 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainCourse;
|
|
|
+import com.train.entity.TrainCohort;
|
|
|
+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.lang.reflect.Field;
|
|
|
+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.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * `train.cohort.create` 契约级单测(TDD-RED;TC-1001 / TC-3001)。
|
|
|
+ *
|
|
|
+ * <p>本测试只描述能力契约,不触碰业务实现(src/main 零改动)。契约口径:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>请求体携带幂等键 {@code code}(唯一批次号)与可选 {@code replay} / {@code version};</li>
|
|
|
+ * <li>replay=true 命中同键 → 200 且回带原批次,禁止再次 insert;</li>
|
|
|
+ * <li>同键异载荷 → C4092(4092)冲突;</li>
|
|
|
+ * <li>过期 version → C422(422)乐观锁冲突,实体须暴露 version 字段;</li>
|
|
|
+ * <li>courseId 不存在 → C404(404);非管理员 → C403(403)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p>当前实现未覆盖上述边界,本类预期为 RED:至少 1 条断言失败。</p>
|
|
|
+ */
|
|
|
+class TrainCohortCreateContractTest {
|
|
|
+
|
|
|
+ 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 EXISTING_ID = 1001L;
|
|
|
+ private static final String KEY = "BM-2026-0001";
|
|
|
+
|
|
|
+ private CohortServiceImpl service;
|
|
|
+ private TrainCohortMapper cohortMapper;
|
|
|
+ private TrainCourseMapper courseMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new CohortServiceImpl();
|
|
|
+ cohortMapper = mock(TrainCohortMapper.class);
|
|
|
+ courseMapper = mock(TrainCourseMapper.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortMapper", cohortMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainCohortSignupMapper", mock(TrainCohortSignupMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainEnrollmentMapper", mock(TrainEnrollmentMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "trainCourseMapper", courseMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "trainUserMapper", mock(TrainUserMapper.class));
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", new RoleGuard());
|
|
|
+
|
|
|
+ TrainCourse course = new TrainCourse();
|
|
|
+ course.setId(7L);
|
|
|
+ when(courseMapper.selectById(7L)).thenReturn(course);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, Object> body(String code, String name) {
|
|
|
+ Map<String, Object> b = new HashMap<String, Object>();
|
|
|
+ b.put("courseId", 7L);
|
|
|
+ b.put("name", name);
|
|
|
+ b.put("code", code);
|
|
|
+ b.put("capacity", 50);
|
|
|
+ b.put("creditTheoryHours", 2400);
|
|
|
+ b.put("creditPracticeHours", 1200);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainCohort existing(String code, String name) {
|
|
|
+ TrainCohort c = new TrainCohort();
|
|
|
+ c.setId(EXISTING_ID);
|
|
|
+ c.setCourseId(7L);
|
|
|
+ c.setCode(code);
|
|
|
+ c.setName(name);
|
|
|
+ c.setStatus("draft");
|
|
|
+ c.setDeleted(0);
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** replay=true + 同幂等键:返回原批次且不得重复落库。 */
|
|
|
+ @Test
|
|
|
+ void replayTrueWithSameKeyReturnsExistingCohortAndSkipsInsert() {
|
|
|
+ TrainCohort old = existing(KEY, "首期家立方训练营");
|
|
|
+ when(cohortMapper.selectOne(any())).thenReturn(old);
|
|
|
+ Map<String, Object> b = body(KEY, "首期家立方训练营");
|
|
|
+ b.put("replay", Boolean.TRUE);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.create(1L, Boolean.TRUE, b);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, r.getCode(), "replay=true 命中同幂等键应返回 200");
|
|
|
+ assertNotNull(r.getData(), "replay=true 应回带已存在批次");
|
|
|
+ assertEquals(EXISTING_ID, r.getData().get("id"), "重放必须返回原批次 id,不得新建");
|
|
|
+ verify(cohortMapper, never()).insert(any(TrainCohort.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 同键异载荷:必须 C4092 冲突,禁止静默新建。 */
|
|
|
+ @Test
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
+ TrainCohort old = existing(KEY, "原名称");
|
|
|
+ when(cohortMapper.selectOne(any())).thenReturn(old);
|
|
|
+ Map<String, Object> b = body(KEY, "篡改后的名称");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.create(1L, Boolean.TRUE, b);
|
|
|
+
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, r.getCode(),
|
|
|
+ "同幂等键异载荷必须返回 C4092(4092) 冲突,禁止静默新建");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 乐观锁:实体须有 version 字段,过期版本必须 C422。 */
|
|
|
+ @Test
|
|
|
+ void staleVersionReturnsC422() {
|
|
|
+ Field versionField = findField(TrainCohort.class, "version");
|
|
|
+ assertNotNull(versionField,
|
|
|
+ "train_cohort 实体必须声明 version 字段以支撑乐观锁 C422");
|
|
|
+
|
|
|
+ TrainCohort old = existing(KEY, "原名称");
|
|
|
+ setField(old, versionField, 2L);
|
|
|
+ when(cohortMapper.selectOne(any())).thenReturn(old);
|
|
|
+ Map<String, Object> b = body(KEY, "原名称");
|
|
|
+ b.put("version", 1L); // 过期版本
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.create(1L, Boolean.TRUE, b);
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(), "过期 version 必须返回 C422(422) 乐观锁冲突");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 课程不存在:C404。 */
|
|
|
+ @Test
|
|
|
+ void missingCourseReturnsC404() {
|
|
|
+ Map<String, Object> b = body(KEY, "课程不存在");
|
|
|
+ b.put("courseId", 999L);
|
|
|
+ when(courseMapper.selectById(999L)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.create(1L, Boolean.TRUE, b);
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(), "courseId 不存在必须返回 C404(404)");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 越权:非管理员创建批次 C403。 */
|
|
|
+ @Test
|
|
|
+ void nonAdminReturnsC403() {
|
|
|
+ Map<String, Object> b = body(KEY, "越权创建");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.create(1L, Boolean.FALSE, b);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|