فهرست منبع

test(tdd-red): add global.theme.brandOrange contract test (S-cfc-509)

RED contract test for capability key global.theme.brandOrange (unit global) covering replay=true idempotency, same-key-different-payload C4092, optimistic-lock C422, not-found C404, unauthorized C403. 7/7 tests fail as expected (implementation not landed). No src/main changes.
E2E Test Bot 1 روز پیش
والد
کامیت
11209140df
1فایلهای تغییر یافته به همراه293 افزوده شده و 0 حذف شده
  1. 293 0
      cfc-backend/src/test/java/com/etotem/cfc/unit/GlobalThemeBrandorangeContractTest.java

+ 293 - 0
cfc-backend/src/test/java/com/etotem/cfc/unit/GlobalThemeBrandorangeContractTest.java

@@ -0,0 +1,293 @@
+package com.etotem.cfc.unit;
+
+import com.etotem.cfc.common.Result;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * 契约级单测(TDD-RED):能力键 {@code global.theme.brandOrange}(单元 global)。
+ *
+ * <p><b>契约来源</b></p>
+ * <ul>
+ *   <li>需求:{@code docs/tdd/SRS.md} §5.5 {@code global.theme.brandOrange}(来源页面未登记);
+ *       编排派发标注设计「⏳未契约化(契约先行待落地)」、P0 用例 0。</li>
+ *   <li>设计:编排声称「该键已在 global-contract 存在 action 常量」——但本仓库
+ *       ({@code cfclub} 主线 / {@code f20b3b471})实际不存在 {@code global-contract} /
+ *       {@code global-impl} 模块,亦不存在任何 {@code global.theme.brandOrange} 常量、
+ *       端点、服务、实体或错误码。契约尚未落地,故本测试全部断言失败(红)。</li>
+ * </ul>
+ *
+ * <p><b>能力含义</b>:全局主题配置键 {@code global.theme.brandOrange}(品牌橙主题色),
+ * 需经「配置服务 + POST 配置端点」读写,并满足以下边界:</p>
+ * <ol>
+ *   <li>幂等重放:{@code replay=true} 重放同一 key+payload 不产生第二条记录(幂等)。</li>
+ *   <li>同键异载荷冲突:同 key 不同 payload → 错误码 {@code C4092}。</li>
+ *   <li>乐观锁冲突:带 version 的并发更新冲突 → 错误码 {@code C422}。</li>
+ *   <li>不存在:读取/更新不存在的配置键 → 错误码 {@code C404}。</li>
+ *   <li>越权:未授权主体写全局配置 → 错误码 {@code C403}。</li>
+ * </ol>
+ *
+ * <p><b>测试形态</b>:与同批已合入主链的 RED 契约测试({@code GlobalSearchQueryContractTest} /
+ * {@code GlobalSearchQueryEmptyResultContractTest} 等)一致——纯反射结构探测 +
+ * 字符串 {@code Class.forName},不引用未来类名(保证可编译);无 Spring / 无 DB / 无外部服务。
+ * 当前实现未落地,故断言失败(红)。</p>
+ *
+ * <p><b>目录映射说明</b>:派发模板中的 {@code global-impl/src/test} / {@code mvn -pl global-impl}
+ * 在本仓库对应唯一 Maven 模块 {@code cfc-backend},契约测试统一落
+ * {@code cfc-backend/src/test/java/com/etotem/cfc/unit/}(与既有 {@code global.search.query} 同批同构)。</p>
+ *
+ * <p><b>GREEN 升级指引</b>:实现者新增配置服务(候选名见 {@link #CONFIG_SERVICE_CANDIDATES})
+ * + POST 配置端点(候选名见 {@link #CONFIG_CONTROLLER_CANDIDATES})、请求 DTO 含
+ * {@code replay} 布尔字段、配置实体含 {@code version} 乐观锁字段、错误码类含
+ * {@code C4092/C422/C404/C403} 常量后,本测试自动转绿;届时可把结构探测升级为行为级 mock 验证。</p>
+ */
+class GlobalThemeBrandorangeContractTest {
+
+    /** 配置服务候选类名(读写 global.theme.brandOrange 等全局配置键) */
+    private static final String[] CONFIG_SERVICE_CANDIDATES = {
+            "com.etotem.cfc.service.GlobalConfigService",
+            "com.etotem.cfc.service.ThemeConfigService",
+            "com.etotem.cfc.service.ConfigService",
+            "com.etotem.cfc.service.ThemeService",
+            "com.etotem.cfc.service.global.GlobalConfigService"
+    };
+
+    /** 配置控制器候选类名(POST 配置端点) */
+    private static final String[] CONFIG_CONTROLLER_CANDIDATES = {
+            "com.etotem.cfc.controller.ConfigController",
+            "com.etotem.cfc.controller.ThemeController",
+            "com.etotem.cfc.controller.GlobalConfigController",
+            "com.etotem.cfc.controller.ThemeConfigController",
+            "com.etotem.cfc.controller.admin.ConfigController",
+            "com.etotem.cfc.controller.admin.GlobalConfigController"
+    };
+
+    /** 配置实体候选类名(须含 version 乐观锁字段) */
+    private static final String[] CONFIG_ENTITY_CANDIDATES = {
+            "com.etotem.cfc.entity.GlobalConfig",
+            "com.etotem.cfc.entity.ThemeConfig",
+            "com.etotem.cfc.entity.Config",
+            "com.etotem.cfc.entity.SystemConfig"
+    };
+
+    /** 配置写入请求 DTO 候选类名(须含 replay 布尔字段) */
+    private static final String[] CONFIG_DTO_CANDIDATES = {
+            "com.etotem.cfc.dto.GlobalConfigRequest",
+            "com.etotem.cfc.dto.ThemeConfigRequest",
+            "com.etotem.cfc.dto.ConfigSetRequest",
+            "com.etotem.cfc.dto.ConfigUpdateRequest"
+    };
+
+    /** 错误码载体候选类名(须含 C4092/C422/C404/C403 常量) */
+    private static final String[] ERROR_CODE_CANDIDATES = {
+            "com.etotem.cfc.common.ErrorCode",
+            "com.etotem.cfc.common.ResultCode",
+            "com.etotem.cfc.common.ConfigErrorCode",
+            "com.etotem.cfc.enums.ErrorCode",
+            "com.etotem.cfc.enums.ConfigErrorCode"
+    };
+
+    // ------------------------------------------------------------------
+    // 结构-1:全局配置服务应存在(读写 global.theme.brandOrange)
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("结构-1:global.theme.brandOrange 配置服务应存在")
+    void configService_existsForGlobalThemeKey() {
+        assertNotNull(loadFirst(CONFIG_SERVICE_CANDIDATES),
+                "[global.theme.brandOrange] 全局配置服务不存在:契约要求配置服务读写 global.theme.brandOrange"
+                        + "(候选类见 CONFIG_SERVICE_CANDIDATES)。实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 结构-2:配置端点是 POST(项目统一 @PostMapping,禁止 GET/PUT/DELETE)
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("结构-2:配置端点应以 @PostMapping 暴露(统一 POST 约定)")
+    void configController_exposesPostEndpoint() {
+        Class<?> controller = loadFirst(CONFIG_CONTROLLER_CANDIDATES);
+        if (controller == null) {
+            fail("[global.theme.brandOrange] 配置控制器不存在:契约要求 POST 配置端点读写"
+                    + " global.theme.brandOrange(候选类见 CONFIG_CONTROLLER_CANDIDATES)。实现未落地。");
+        }
+        String classPath = classRequestMappingPath(controller);
+        assertTrue(classPath != null
+                        && (classPath.contains("/api/config") || classPath.contains("/api/theme")
+                            || classPath.contains("/api/global")),
+                "[global.theme.brandOrange] 配置控制器类级路由应为 /api/config 或 /api/theme 或 /api/global,实际为 "
+                        + classPath + "。");
+
+        boolean hasPost = false;
+        for (Method m : controller.getDeclaredMethods()) {
+            if (m.getAnnotation(PostMapping.class) != null) {
+                hasPost = true;
+                break;
+            }
+        }
+        assertTrue(hasPost,
+                "[global.theme.brandOrange] 配置控制器应提供 @PostMapping 处理器(统一 POST 约定,"
+                        + "禁止 @GetMapping/@PutMapping/@DeleteMapping)。");
+    }
+
+    // ------------------------------------------------------------------
+    // 边界-1:幂等重放 replay=true
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("边界-1:请求载体应含 replay 布尔字段以支持 replay=true 幂等重放")
+    void replayTrue_idempotentReplay_hasReplayFlag() {
+        Class<?> dto = loadFirst(CONFIG_DTO_CANDIDATES);
+        if (dto == null) {
+            fail("[global.theme.brandOrange] 配置写入请求 DTO 不存在:契约要求 replay=true 幂等重放"
+                    + "(同一 key+payload 重放不产生第二条记录),需请求载体含 replay 布尔字段。"
+                    + "实现未落地。");
+        }
+        boolean hasReplay = false;
+        for (Class<?> c = dto; c != null && c != Object.class; c = c.getSuperclass()) {
+            for (Field f : c.getDeclaredFields()) {
+                if ("replay".equals(f.getName())
+                        && (f.getType() == boolean.class || f.getType() == Boolean.class)) {
+                    hasReplay = true;
+                    break;
+                }
+            }
+            if (hasReplay) {
+                break;
+            }
+        }
+        assertTrue(hasReplay,
+                "[global.theme.brandOrange] 请求载体 " + dto.getSimpleName()
+                        + " 缺少 boolean replay 字段:replay=true 幂等重放要求可标记重放请求。");
+
+        assertNotNull(loadFirst(CONFIG_SERVICE_CANDIDATES),
+                "[global.theme.brandOrange] 配置服务不存在:replay=true 幂等重放需服务层按 key+payload+version"
+                        + " 去重/覆盖(同一逻辑写入仅产生一条配置)。实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 边界-2:同键异载荷 → C4092
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("边界-2:同键异载荷冲突应返回错误码 C4092")
+    void sameKeyDifferentPayload_returnsC4092() {
+        assertTrue(hasErrorCode("C4092"),
+                "[global.theme.brandOrange] 错误码 C4092 不存在:契约要求同一配置键提交不同 payload 时"
+                        + "返回 C4092(同键异载荷冲突),错误码候选类见 ERROR_CODE_CANDIDATES。"
+                        + "实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 边界-3:乐观锁冲突 → C422(实体须含 version 字段)
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("边界-3:乐观锁冲突应返回 C422 且配置实体含 version 字段")
+    void optimisticLockConflict_returnsC422_andEntityHasVersion() {
+        Class<?> entity = loadFirst(CONFIG_ENTITY_CANDIDATES);
+        if (entity == null) {
+            fail("[global.theme.brandOrange] 配置实体不存在:契约要求带 version 乐观锁字段以检测并发更新冲突"
+                    + "(C422),候选类见 CONFIG_ENTITY_CANDIDATES。实现未落地。");
+        }
+        boolean hasVersion = false;
+        for (Class<?> c = entity; c != null && c != Object.class; c = c.getSuperclass()) {
+            for (Field f : c.getDeclaredFields()) {
+                if ("version".equals(f.getName())) {
+                    hasVersion = true;
+                    break;
+                }
+            }
+            if (hasVersion) {
+                break;
+            }
+        }
+        assertTrue(hasVersion,
+                "[global.theme.brandOrange] 配置实体 " + entity.getSimpleName()
+                        + " 缺少 version 字段:乐观锁冲突(C422)要求版本号参与并发控制。");
+
+        assertTrue(hasErrorCode("C422"),
+                "[global.theme.brandOrange] 错误码 C422 不存在:契约要求乐观锁冲突返回 C422。实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 边界-4:不存在 → C404
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("边界-4:配置键不存在应返回错误码 C404")
+    void notFound_returnsC404() {
+        assertTrue(hasErrorCode("C404"),
+                "[global.theme.brandOrange] 错误码 C404 不存在:契约要求读取/更新不存在的配置键时返回"
+                        + " C404(不存在)。实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 边界-5:越权 → C403(适用时)
+    // ------------------------------------------------------------------
+
+    @Test
+    @DisplayName("边界-5:未授权主体写全局配置应返回错误码 C403")
+    void unauthorized_returnsC403() {
+        assertTrue(hasErrorCode("C403"),
+                "[global.theme.brandOrange] 错误码 C403 不存在:契约要求未授权主体写全局主题配置时返回"
+                        + " C403(越权,适用时)。实现未落地。");
+    }
+
+    // ------------------------------------------------------------------
+    // 反射辅助
+    // ------------------------------------------------------------------
+
+    private Class<?> loadClass(String name) {
+        try {
+            return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
+        } catch (ClassNotFoundException e) {
+            return null;
+        }
+    }
+
+    /** 载入候选名单中第一个存在的类;均不存在返回 null */
+    private Class<?> loadFirst(String[] names) {
+        for (String name : names) {
+            Class<?> c = loadClass(name);
+            if (c != null) {
+                return c;
+            }
+        }
+        return null;
+    }
+
+    /** 错误码类中是否存在指定常量(字段名/枚举常量名,如 C4092) */
+    private boolean hasErrorCode(String code) {
+        for (String name : ERROR_CODE_CANDIDATES) {
+            Class<?> c = loadClass(name);
+            if (c == null) {
+                continue;
+            }
+            for (Field f : c.getDeclaredFields()) {
+                if (code.equals(f.getName()) || f.getName().contains(code)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /** 取类级 @RequestMapping 的 value[0](无则 null) */
+    private static String classRequestMappingPath(Class<?> clazz) {
+        RequestMapping rm = clazz.getAnnotation(RequestMapping.class);
+        if (rm == null || rm.value().length == 0) {
+            return null;
+        }
+        return rm.value()[0];
+    }
+}