|
|
@@ -0,0 +1,190 @@
|
|
|
+package com.train.service.impl;
|
|
|
+
|
|
|
+import com.train.common.Result;
|
|
|
+import com.train.entity.TrainPromotionRule;
|
|
|
+import com.train.mapper.TrainPromotionRuleMapper;
|
|
|
+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.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.promo.ruleConfig} 契约级边界单测(TDD-RED,S-train-35;TC-1035、TC-3005)。
|
|
|
+ *
|
|
|
+ * <p>需求锚点:docs/tdd/SRS.md §5.5「场景定义 §2 域六 P2-S1(Must)」——规则落库生效、可启停、
|
|
|
+ * 可版本化,阈值校验(≥0、上限);⚠️ 阈值边界与多规则冲突优先级尚未拍板,故本类不臆断具体上限数值。</p>
|
|
|
+ *
|
|
|
+ * <p>只写测试、不改任何 src/main。被测面为 {@link PromotionServiceImpl#saveRule(Boolean, Map)}
|
|
|
+ * 与 {@link PromotionServiceImpl#ruleEnable(Boolean, Long, Boolean)}。覆盖幂等重放 replay=true、
|
|
|
+ * 同键异载荷 C4092、乐观锁冲突 C422、不存在 C404、越权 C403 五类边界。当前实现全部错误语义走
|
|
|
+ * {@code Result.error}(code=500)、无 replay/幂等键入参、忽略 updateById 影响行数,故本类预期 RED。</p>
|
|
|
+ *
|
|
|
+ * <p>契约错误码({@code Result.code}):
|
|
|
+ * C4092=4092 同幂等键异载荷;C422=422 乐观锁冲突;C404=404 规则不存在;C403=403 越权。</p>
|
|
|
+ *
|
|
|
+ * <p>说明:本仓后端模块为 train-backend(无字面量 train-impl 模块),测试按既有契约单测惯例落在
|
|
|
+ * train-backend/src/test/java/com/train/service/impl/,与 S-train-33 等同目录契约测试保持一致。</p>
|
|
|
+ */
|
|
|
+class TrainPromoRuleconfigContractTest {
|
|
|
+
|
|
|
+ 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 RULE_ID = 7001L;
|
|
|
+
|
|
|
+ private PromotionServiceImpl service;
|
|
|
+ private TrainPromotionRuleMapper ruleMapper;
|
|
|
+ private RoleGuard roleGuard;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ service = new PromotionServiceImpl();
|
|
|
+ ruleMapper = mock(TrainPromotionRuleMapper.class);
|
|
|
+ roleGuard = mock(RoleGuard.class);
|
|
|
+ // 管理员默认放行;越权用例单独 stub requireAdminRole(false)
|
|
|
+ when(roleGuard.requireAdminRole(true)).thenReturn(null);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(service, "trainPromotionRuleMapper", ruleMapper);
|
|
|
+ ReflectionTestUtils.setField(service, "roleGuard", roleGuard);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 构造带幂等键的保存载荷;ruleKey 为契约幂等键(当前实现忽略该字段)。 */
|
|
|
+ private static Map<String, Object> ruleBody(String ruleKey, long threshold) {
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("name", "T2 讲师晋升规则");
|
|
|
+ body.put("authLevel", "T2");
|
|
|
+ body.put("ruleKey", ruleKey);
|
|
|
+ Map<String, Object> cond = new HashMap<>();
|
|
|
+ cond.put("metric", "serve_count");
|
|
|
+ cond.put("op", ">=");
|
|
|
+ cond.put("value", threshold);
|
|
|
+ body.put("conditions", Collections.singletonList(cond));
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrainPromotionRule existingRule() {
|
|
|
+ TrainPromotionRule r = new TrainPromotionRule();
|
|
|
+ r.setId(RULE_ID);
|
|
|
+ r.setName("T2 讲师晋升规则");
|
|
|
+ r.setAuthLevel("T2");
|
|
|
+ r.setConditions("[{\"metric\":\"serve_count\",\"op\":\">=\",\"value\":50}]");
|
|
|
+ r.setEnabled(1);
|
|
|
+ r.setVersion(1);
|
|
|
+ r.setDeleted(0);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 1:幂等重放 replay=true ====================
|
|
|
+
|
|
|
+ /** 同幂等键同载荷以 replay=true 重放,须显式标记 replay 且不得二次落库。 */
|
|
|
+ @Test
|
|
|
+ void replaySameKeySamePayloadFlagsReplayAndSkipsSecondInsert() {
|
|
|
+ Result<Map<String, Object>> first = service.saveRule(true, ruleBody("rule-key-A", 50L));
|
|
|
+ assertEquals(CODE_OK, first.getCode());
|
|
|
+
|
|
|
+ Map<String, Object> replayBody = ruleBody("rule-key-A", 50L);
|
|
|
+ replayBody.put("replay", true);
|
|
|
+ Result<Map<String, Object>> replay = service.saveRule(true, replayBody);
|
|
|
+
|
|
|
+ assertEquals(CODE_OK, replay.getCode());
|
|
|
+ assertNotNull(replay.getData());
|
|
|
+ assertEquals(Boolean.TRUE, replay.getData().get("replay"),
|
|
|
+ "同幂等键同载荷 replay=true 重放须在 Result.data 中显式标识 replay=true,"
|
|
|
+ + "供调用方区分首次配置与幂等回放");
|
|
|
+ // 幂等重放不得二次写库(首次配置仅 1 次 insert)
|
|
|
+ verify(ruleMapper, times(1)).insert(any(TrainPromotionRule.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 2:同键异载荷 C4092 ====================
|
|
|
+
|
|
|
+ /** 同一幂等键携带不同阈值载荷重放,必须返回 C4092,禁止静默按 200 新建另一条规则。 */
|
|
|
+ @Test
|
|
|
+ void sameKeyDifferentPayloadReturnsC4092() {
|
|
|
+ service.saveRule(true, ruleBody("rule-key-B", 50L));
|
|
|
+
|
|
|
+ Result<Map<String, Object>> conflict = service.saveRule(true, ruleBody("rule-key-B", 80L));
|
|
|
+
|
|
|
+ assertEquals(CODE_SAME_KEY_DIFF_PAYLOAD, conflict.getCode(),
|
|
|
+ "同一 ruleKey 携带不同阈值载荷(50→80)必须返回 C4092(4092) 冲突,"
|
|
|
+ + "禁止静默复用/新建规则按 200 成功返回");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 3:乐观锁冲突 C422 ====================
|
|
|
+
|
|
|
+ /** 规则实体须有 version 字段支撑并发启停的乐观锁 C422。 */
|
|
|
+ @Test
|
|
|
+ void ruleEntityDeclaresVersionForOptimisticLock() {
|
|
|
+ Field versionField = findField(TrainPromotionRule.class, "version");
|
|
|
+ assertNotNull(versionField,
|
|
|
+ "train_promotion_rule 实体必须声明 version 字段以支撑并发配置冲突 C422(422)");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 并发启停 updateById 影响 0 行(丢失更新/版本不匹配)必须返回 C422,不得静默按 200 成功。 */
|
|
|
+ @Test
|
|
|
+ void staleVersionRuleUpdateReturnsC422() {
|
|
|
+ when(ruleMapper.selectById(RULE_ID)).thenReturn(existingRule());
|
|
|
+ when(ruleMapper.updateById(any(TrainPromotionRule.class))).thenReturn(0);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.ruleEnable(true, RULE_ID, Boolean.FALSE);
|
|
|
+
|
|
|
+ assertEquals(CODE_VERSION_CONFLICT, r.getCode(),
|
|
|
+ "规则启停更新影响 0 行(乐观锁冲突/并发丢失更新)必须返回 C422(422),"
|
|
|
+ + "不得忽略 updateById 返回行数按 200 成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 4:不存在 C404 ====================
|
|
|
+
|
|
|
+ /** 启停不存在的规则必须返回 C404,而非笼统的 500。 */
|
|
|
+ @Test
|
|
|
+ void nonExistentRuleReturns404() {
|
|
|
+ when(ruleMapper.selectById(9999L)).thenReturn(null);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.ruleEnable(true, 9999L, Boolean.TRUE);
|
|
|
+
|
|
|
+ assertEquals(CODE_NOT_FOUND, r.getCode(),
|
|
|
+ "规则不存在必须返回 C404(404),不得降级为笼统的 500");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界 5:越权 C403 ====================
|
|
|
+
|
|
|
+ /** 非管理员越权配置规则必须返回 C403,而非笼统的 500。 */
|
|
|
+ @Test
|
|
|
+ void nonAdminRuleConfigReturns403() {
|
|
|
+ when(roleGuard.requireAdminRole(false)).thenReturn("需要管理员权限");
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = service.saveRule(false, ruleBody("rule-key-C", 50L));
|
|
|
+
|
|
|
+ assertEquals(CODE_FORBIDDEN, r.getCode(),
|
|
|
+ "非管理员越权配置晋升规则必须返回 C403(403),不得降级为笼统的 500");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== helpers ====================
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|