|
@@ -0,0 +1,304 @@
|
|
|
|
|
+package com.train.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.train.common.Result;
|
|
|
|
|
+import com.train.entity.TrainPromotionRecord;
|
|
|
|
|
+import com.train.mapper.TrainPromotionRecordMapper;
|
|
|
|
|
+import com.train.service.PromotionService;
|
|
|
|
|
+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.lang.reflect.Method;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * {@code train.promo.history} 契约级单测(TDD-RED,S-train-38;TC-1038、TC-3005)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md §5.5「场景定义 §3.2 P3-S2(Should)」——晋升履历可查、时间序列完整、
|
|
|
|
|
+ * 可作为合规举证材料导出。TC-1038 Given 晋升生效;When 写档;Then 晋升履历可查、时间序列完整、
|
|
|
|
|
+ * 可作为合规举证材料导出。TC-3005 要求晋升履历沉淀档案、可作为合规举证材料。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>本轮只写测试、不改任何 src/main 实现。纯 Mockito(不启 Spring、不连 DB)。被测面为
|
|
|
|
|
+ * {@link PromotionServiceImpl#records(Long, Integer, Integer)}(晋升履历查询,含时间序列字段)
|
|
|
|
|
+ * 与晋升服务上承载「导出/幂等重放」的契约入口(当前不存在)。覆盖幂等重放 replay=true、
|
|
|
|
|
+ * 同键异载荷 C4092、乐观锁冲突 C422、不存在 C404、越权 C403 五类边界。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>当前实现现状(RED 依据):records() 仅提供分页查询(total/page/size/list),无 replay/key/payload
|
|
|
|
|
+ * 入参、无合规举证导出入口;{@link TrainPromotionRecord} 无 version 字段(无法支撑 C422);
|
|
|
|
|
+ * rejectRecord 的 updateById 影响 0 行被忽略、规则/记录不存在与非管理员越权一律走 {@code Result.error}
|
|
|
|
|
+ * (code=500),未区分 C422/C404/C403。故本类边界用例预期为 RED(至少 1 条断言失败)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>契约错误码({@code Result.code}):</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>C4092 = 4092 同幂等键但载荷不一致(导出范围/格式/操作人变化)</li>
|
|
|
|
|
+ * <li>C422 = 422 乐观锁/并发更新冲突(履历记录须有 version,并发写影响 0 行须报错)</li>
|
|
|
|
|
+ * <li>C404 = 404 晋升履历记录不存在</li>
|
|
|
|
|
+ * <li>C403 = 403 非管理员越权处置/导出晋升履历</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>说明:本仓后端模块为 train-backend(无字面量 train-impl 子模块),测试按既有契约单测惯例落在
|
|
|
|
|
+ * train-backend/src/test/java/com/train/service/impl/,与同池 S-train-35/36 的晋升契约测试同目录;
|
|
|
|
|
+ * 当前契约层尚未落地(SRS 全键「⏳未契约化·契约先行待落地」),错误码直接以 Result.code 表达。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class TrainPromoHistoryContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final int CODE_OK = 200;
|
|
|
|
|
+ private static final int CODE_SAME_KEY_DIFF_PAYLOAD = 4092;
|
|
|
|
|
+ private static final int CODE_VERSION_CONFLICT = 422;
|
|
|
|
|
+ private static final int CODE_NOT_FOUND = 404;
|
|
|
|
|
+ private static final int CODE_FORBIDDEN = 403;
|
|
|
|
|
+
|
|
|
|
|
+ private static final Long USER_ID = 5001L;
|
|
|
|
|
+ private static final Long RECORD_ID = 9001L;
|
|
|
|
|
+ private static final String IDEMPOTENCY_KEY = "PROMO-HIST-5001-2026Q4";
|
|
|
|
|
+
|
|
|
|
|
+ /** 期望的履历导出契约入口方法名关键字(GREEN 实现可任选其一) */
|
|
|
|
|
+ private static final String[] EXPORT_NAME_KEYS = {"export", "history", "archive", "evidence"};
|
|
|
|
|
+
|
|
|
|
|
+ private PromotionServiceImpl service;
|
|
|
|
|
+ private TrainPromotionRecordMapper recordMapper;
|
|
|
|
|
+ private RoleGuard roleGuard;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ service = new PromotionServiceImpl();
|
|
|
|
|
+ recordMapper = mock(TrainPromotionRecordMapper.class);
|
|
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
|
|
+
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "trainPromotionRecordMapper", recordMapper);
|
|
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", roleGuard);
|
|
|
|
|
+
|
|
|
|
|
+ // 管理员默认放行;越权用例单独 stub requireAdminRole(false)
|
|
|
|
|
+ when(roleGuard.requireAdminRole(Boolean.TRUE)).thenReturn(null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static TrainPromotionRecord record(long id, String authLevel, String status) {
|
|
|
|
|
+ TrainPromotionRecord r = new TrainPromotionRecord();
|
|
|
|
|
+ r.setId(id);
|
|
|
|
|
+ r.setUserId(USER_ID);
|
|
|
|
|
+ r.setAuthLevel(authLevel);
|
|
|
|
|
+ r.setRuleId(7001L);
|
|
|
|
|
+ r.setPass(status.equals("promoted") ? 1 : 0);
|
|
|
|
|
+ r.setStatus(status);
|
|
|
|
|
+ r.setDeleted(0);
|
|
|
|
|
+ r.setAppliedAt(new Date());
|
|
|
|
|
+ r.setPromotedAt(new Date());
|
|
|
|
|
+ r.setCreatedAt(new Date());
|
|
|
|
|
+ return r;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 对照基线(当前实现已覆盖,作为 RED 的对照) ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** TC-1038 基线:晋升履历可查,分页返回 total/page/size/list。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void recordsReturnsCompletePaginatedHistory() {
|
|
|
|
|
+ when(recordMapper.selectCount(any())).thenReturn(3L);
|
|
|
|
|
+ when(recordMapper.selectList(any())).thenReturn(Arrays.asList(
|
|
|
|
|
+ record(9003L, "T3", "promoted"),
|
|
|
|
|
+ record(9002L, "T2", "promoted"),
|
|
|
|
|
+ record(9001L, "T1", "evaluated")));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.records(USER_ID, 1, 20);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
|
|
+ assertEquals(3L, ((Number) r.getData().get("total")).longValue());
|
|
|
|
|
+ assertEquals(1, ((Number) r.getData().get("page")).intValue());
|
|
|
|
|
+ assertEquals(20, ((Number) r.getData().get("size")).intValue());
|
|
|
|
|
+ assertEquals(3, ((List<?>) r.getData().get("list")).size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 时间序列完整基线:每条履历须携带 appliedAt/promotedAt/createdAt/time/status 字段。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void recordVoCarriesFullTimeSeriesFields() {
|
|
|
|
|
+ when(recordMapper.selectCount(any())).thenReturn(1L);
|
|
|
|
|
+ when(recordMapper.selectList(any())).thenReturn(
|
|
|
|
|
+ java.util.Collections.singletonList(record(9001L, "T1", "promoted")));
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.records(USER_ID, 1, 20);
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ List<Map<String, Object>> list = (List<Map<String, Object>>) r.getData().get("list");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_OK, r.getCode());
|
|
|
|
|
+ assertEquals(1, list.size());
|
|
|
|
|
+ Map<String, Object> row = list.get(0);
|
|
|
|
|
+ assertNotNull(row.get("appliedAt"), "履历项须携带评定时间 appliedAt");
|
|
|
|
|
+ assertNotNull(row.get("promotedAt"), "履历项须携带晋升生效时间 promotedAt");
|
|
|
|
|
+ assertNotNull(row.get("createdAt"), "履历项须携带创建时间 createdAt");
|
|
|
|
|
+ assertNotNull(row.get("time"), "履历项须携带时间序列展示字段 time");
|
|
|
|
|
+ assertNotNull(row.get("status"), "履历项须携带状态 status");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 0:幂等重放 replay=true ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 契约入口:晋升履历须暴露可显式表达 replay/key/payload 的合规举证导出入口(Result 返回且至少 4 入参),
|
|
|
|
|
+ * 否则调用方无法声明「幂等重放」,也无法在同键异载荷时返回 C4092。
|
|
|
|
|
+ * 当前仅有 records(Long,Integer,Integer) 3 入参与 rejectRecord/saveRule 等入口,均无 replay/key/payload。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void historyExportEntryMustAcceptReplayKeyPayload() {
|
|
|
|
|
+ Method entry = findHistoryExportEntry();
|
|
|
|
|
+ assertNotNull(entry,
|
|
|
|
|
+ "train.promo.history 契约要求暴露带 replay/key/payload 的合规举证导出入口"
|
|
|
|
|
+ + "(Result 返回且至少 4 入参)以支持 replay=true;"
|
|
|
|
|
+ + "当前晋升服务仅有 records(userId,page,size) 等入口,无法表达幂等重放");
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(hasBooleanParam(entry),
|
|
|
|
|
+ "履历导出入口须有 replay 布尔入参以表达幂等重放;当前入口不支持 replay=true");
|
|
|
|
|
+ assertTrue(hasParamOfTypes(entry, String.class, Map.class),
|
|
|
|
|
+ "履历导出入口须同时接收幂等键 key 与载荷 payload;当前入口不支持");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 1:同键异载荷 C4092 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 同一幂等键携带不同导出载荷(导出范围/格式/操作人变化)重放必须返回 C4092,禁止静默按 200 复用。
|
|
|
|
|
+ * 当前晋升服务无幂等键入参,导出能力缺失,无法表达同键异载荷冲突。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
|
|
+ Method entry = findHistoryExportEntry();
|
|
|
|
|
+ assertNotNull(entry,
|
|
|
|
|
+ "同一幂等键 " + IDEMPOTENCY_KEY + " 携带不同履历导出载荷(范围/格式/操作人)重放"
|
|
|
|
|
+ + "必须返回 C4092(" + CODE_SAME_KEY_DIFF_PAYLOAD + "),禁止静默按 200 复用既有档案;"
|
|
|
|
|
+ + "当前晋升服务无 replay/key/payload 导出入口,无法表达同键异载荷冲突");
|
|
|
|
|
+ assertTrue(hasParamOfTypes(entry, String.class, Map.class),
|
|
|
|
|
+ "履历导出入口须同时接收幂等键 key 与载荷 payload 方可区分同键异载荷并返回 C4092("
|
|
|
|
|
+ + CODE_SAME_KEY_DIFF_PAYLOAD + ")");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 2:乐观锁冲突 C422 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 履历记录实体须有 version 字段支撑并发处置/导出的乐观锁 C422。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void promotionRecordEntityMustHaveVersionField() {
|
|
|
|
|
+ Field version = findField(TrainPromotionRecord.class, "version");
|
|
|
|
|
+ assertNotNull(version,
|
|
|
|
|
+ "train_promotion_record 履历实体必须声明 version 字段以支撑并发处置/导出冲突 C422("
|
|
|
|
|
+ + CODE_VERSION_CONFLICT + ");当前实体无 version");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 并发处置 updateById 影响 0 行(丢失更新/版本不匹配)必须返回 C422,不得静默按 200 成功。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void staleVersionRecordUpdateReturnsC422() {
|
|
|
|
|
+ when(recordMapper.selectById(RECORD_ID)).thenReturn(record(RECORD_ID, "T2", "evaluated"));
|
|
|
|
|
+ when(recordMapper.updateById(any(TrainPromotionRecord.class))).thenReturn(0);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.rejectRecord(Boolean.TRUE, RECORD_ID, "并发丢失更新");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
|
|
+ "履历处置更新影响 0 行(乐观锁冲突/并发丢失更新)必须返回 C422("
|
|
|
|
|
+ + CODE_VERSION_CONFLICT + "),不得忽略 updateById 返回行数按 200 成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 3:不存在 C404 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 处置不存在的晋升履历记录必须返回 C404,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonExistentRecordReturns404() {
|
|
|
|
|
+ when(recordMapper.selectById(9999L)).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.rejectRecord(Boolean.TRUE, 9999L, "不存在");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
|
|
+ "晋升履历记录不存在必须返回 C404(" + CODE_NOT_FOUND + "),不得降级为笼统的 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界 4:越权 C403 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 非管理员越权处置/导出他人晋升履历必须返回 C403,而非笼统的 500。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void nonAdminHistoryOperationReturns403() {
|
|
|
|
|
+ when(roleGuard.requireAdminRole(Boolean.FALSE)).thenReturn("需要管理员权限");
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> r = service.rejectRecord(Boolean.FALSE, RECORD_ID, "越权处置");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
|
|
+ "非管理员越权处置/导出晋升履历必须返回 C403(" + CODE_FORBIDDEN + "),不得降级为笼统的 500");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== helpers ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 在晋升服务接口与实现上查找合规举证导出契约入口(按方法名关键字 + Result 返回类型)。 */
|
|
|
|
|
+ private static Method findHistoryExportEntry() {
|
|
|
|
|
+ for (Class<?> type : new Class<?>[]{PromotionService.class, PromotionServiceImpl.class}) {
|
|
|
|
|
+ for (Method m : type.getMethods()) {
|
|
|
|
|
+ if (!Result.class.isAssignableFrom(m.getReturnType())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String name = m.getName().toLowerCase();
|
|
|
|
|
+ for (String key : EXPORT_NAME_KEYS) {
|
|
|
|
|
+ if (name.contains(key)) {
|
|
|
|
|
+ return m;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasBooleanParam(Method m) {
|
|
|
|
|
+ for (Class<?> t : m.getParameterTypes()) {
|
|
|
|
|
+ if (boolean.class.equals(t) || Boolean.class.equals(t)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static boolean hasParamOfTypes(Method m, Class<?>... types) {
|
|
|
|
|
+ boolean allFound = true;
|
|
|
|
|
+ for (Class<?> t : types) {
|
|
|
|
|
+ boolean found = false;
|
|
|
|
|
+ for (Class<?> pt : m.getParameterTypes()) {
|
|
|
|
|
+ if (t.equals(pt) || (t.isPrimitive() && wrap(t).equals(pt))) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!found) {
|
|
|
|
|
+ allFound = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return allFound;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Class<?> wrap(Class<?> primitive) {
|
|
|
|
|
+ if (primitive == long.class) {
|
|
|
|
|
+ return Long.class;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (primitive == int.class) {
|
|
|
|
|
+ return Integer.class;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (primitive == boolean.class) {
|
|
|
|
|
+ return Boolean.class;
|
|
|
|
|
+ }
|
|
|
|
|
+ return primitive;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|