فهرست منبع

TDD RED S-DEV-33: 新增 sleep.config.tags 契约级单测 AdminGuideConfigController 获取/保存场景

zxyj-langgraph 2 ساعت پیش
والد
کامیت
7a2746fe60

+ 162 - 0
cfc-backend/src/test/java/com/etotem/cfc/controller/config/SleepConfigTagsAdminGuideContractTest.java

@@ -0,0 +1,162 @@
+package com.etotem.cfc.controller.config;
+
+/**
+ * S-cfc-33 · sleep.config.tags —— 后台配置读取/维护机制契约级单测(RED阶段补齐)。
+ * <p>设计溯源:{@code PublicConfigController /api/config/public/value}(小程序端拉取
+ * {@code sleep.quality.tags} 配置值)+ {@code AdminGuideConfigController} get/save 模式
+ * (管理端维护配置的参考实现,见需求变更 C「后台维护睡眠情况选项」)。契约以
+ * 「已合入 origin/cfclub 的接口契约」为准。
+ *
+ * <p>覆盖:正常路径(配置值读取 / 解析返回 / 保存落库)+ 关键边界(key 缺失、
+ * 配置未配置返回 null、configValue 空拒绝、body 空拒绝)。纯单元测试:直接调用
+ * Controller + Mockito mock {@code SysConfigService},不依赖数据库/微信/外部服务。
+ */
+public class SleepConfigTagsAdminGuideContractTest {
+
+    private PublicConfigController publicConfigController;
+    private com.etotem.cfc.controller.admin.AdminGuideConfigController adminGuideConfigController;
+    private com.etotem.cfc.service.SysConfigService sysConfigService;
+
+    @BeforeEach
+    public void setUp() {
+        publicConfigController = new PublicConfigController();
+        adminGuideConfigController = new com.etotem.cfc.controller.admin.AdminGuideConfigController();
+        sysConfigService = mock(com.etotem.cfc.service.SysConfigService.class);
+        ReflectionTestUtils.setField(publicConfigController, "sysConfigService", sysConfigService);
+        ReflectionTestUtils.setField(adminGuideConfigController, "sysConfigService", sysConfigService);
+    }
+
+    // ====================================================================
+    // POST /api/config/public/value —— 小程序端拉取 sleep.quality.tags 配置值
+    // ====================================================================
+
+    @Test
+    public void publicValue_keyPresent_returnsStoredConfigValue() {
+        when(sysConfigService.getValue("sleep.quality.tags"))
+                .thenReturn("[\"入睡困难\",\"夜醒\",\"多梦\",\"打鼾\",\"早醒\",\"赖床\",\"熬夜\",\"午睡过长\",\"正常\"]");
+        Map<String, String> params = new HashMap<>();
+        params.put("key", "sleep.quality.tags");
+
+        com.etotem.cfc.common.Result<String> res = publicConfigController.getValue(params);
+
+        assertEquals(200, res.getCode());
+        assertNotNull(res.getData());
+        assertTrue(res.getData().contains("正常"), "默认 9 项配置必须包含「正常」");
+        assertTrue(res.getData().contains("午睡过长"), "默认配置应保留原 8 项");
+    }
+
+    @Test
+    public void publicValue_unconfiguredKey_returnsNullData_contractForFrontendFallback() {
+        // 配置缺失:返回 success(null),前端据此回退默认 9 项(不报错)
+        when(sysConfigService.getValue("sleep.quality.tags")).thenReturn(null);
+        Map<String, String> params = new HashMap<>();
+        params.put("key", "sleep.quality.tags");
+
+        com.etotem.cfc.common.Result<String> res = publicConfigController.getValue(params);
+
+        assertEquals(200, res.getCode(), "配置缺失不得报错(前端回退默认)");
+        assertNull(res.getData());
+    }
+
+    @Test
+    public void publicValue_missingKey_returnsError() {
+        com.etotem.cfc.common.Result<String> res = publicConfigController.getValue(new HashMap<>());
+
+        assertEquals(500, res.getCode());
+        assertTrue(res.getMessage().contains("key"), "message 应指明缺失的 key");
+        verify(sysConfigService, never()).getValue(any());
+    }
+
+    @Test
+    public void publicValue_blankKey_returnsError() {
+        Map<String, String> params = new HashMap<>();
+        params.put("key", "   ");
+
+        com.etotem.cfc.common.Result<String> res = publicConfigController.getValue(params);
+
+        assertEquals(500, res.getCode());
+        assertTrue(res.getMessage().contains("key"));
+        verify(sysConfigService, never()).getValue(any());
+    }
+
+    // ====================================================================
+    // POST /api/admin/guide-config/get —— 管理端读取配置(未配置→null,前端用默认)
+    // ====================================================================
+
+    @Test
+    public void guideConfigGet_notConfigured_returnsNullData() {
+        when(sysConfigService.getValue("sleep.quality.tags")).thenReturn(null);
+
+        com.etotem.cfc.common.Result<Map<String, Object>> res = adminGuideConfigController.get();
+
+        assertEquals(200, res.getCode());
+        assertNull(res.getData(), "未配置时返回 null,前端使用默认值");
+    }
+
+    @Test
+    public void guideConfigGet_configured_returnsParsedMap() throws Exception {
+        when(sysConfigService.getValue("sleep.quality.tags"))
+                .thenReturn("[\"入睡困难\",\"夜醒\"]");
+
+        com.etotem.cfc.common.Result<Map<String, Object>> res = adminGuideConfigController.get();
+
+        assertEquals(200, res.getCode());
+        assertNotNull(res.getData());
+        @SuppressWarnings("unchecked")
+        java.util.List<String> tags = (java.util.List<String>) res.getData().get("tags");
+        assertNotNull(tags);
+        assertEquals(2, tags.size());
+        assertTrue(tags.contains("入睡困难"));
+    }
+
+    @Test
+    public void guideConfigGet_malformedValue_returnsParseError() {
+        when(sysConfigService.getValue("sleep.quality.tags")).thenReturn("{not-json}");
+
+        com.etotem.cfc.common.Result<Map<String, Object>> res = adminGuideConfigController.get();
+
+        assertEquals(500, res.getCode());
+        assertTrue(res.getMessage().contains("解析失败"), "非法 JSON 应给出解析失败提示");
+    }
+
+    // ====================================================================
+    // POST /api/admin/guide-config/save —— 管理端保存配置(写语义)
+    // ====================================================================
+
+    @Test
+    public void guideConfigSave_validConfigValue_persists() {
+        Map<String, String> body = new HashMap<>();
+        body.put("configValue", "[\"入睡困难\",\"夜醒\"]");
+        when(sysConfigService.update(any(), any(), any())).thenReturn(com.etotem.cfc.common.Result.success("ok"));
+
+        com.etotem.cfc.common.Result<String> res = adminGuideConfigController.save(body);
+
+        assertEquals(200, res.getCode());
+        com.etotem.cfc.common.ArgumentCaptor<String> keyCaptor = com.etotem.cfc.common.ArgumentCaptor.forClass(String.class);
+        com.etotem.cfc.common.ArgumentCaptor<String> valueCaptor = com.etotem.cfc.common.ArgumentCaptor.forClass(String.class);
+        verify(sysConfigService).update(keyCaptor.capture(), valueCaptor.capture(), eq("sleep.quality.tags"));
+        assertEquals("sleep.quality.tags", keyCaptor.getValue());
+        assertEquals("[\"入睡困难\",\"夜醒\"]", valueCaptor.getValue());
+    }
+
+    @Test
+    public void guideConfigSave_emptyConfigValue_returnsError() {
+        Map<String, String> body = new HashMap<>();
+        body.put("configValue", "");
+
+        com.etotem.cfc.common.Result<String> res = adminGuideConfigController.save(body);
+
+        assertEquals(500, res.getCode());
+        assertTrue(res.getMessage().contains("configValue"), "message 应指明缺失的 configValue");
+        verify(sysConfigService, never()).update(any(), any(), any());
+    }
+
+    @Test
+    public void guideConfigSave_nullBody_returnsError() {
+        com.etotem.cfc.common.Result<String> res = adminGuideConfigController.save(null);
+
+        assertEquals(500, res.getCode());
+        assertTrue(res.getMessage().contains("configValue"));
+        verify(sysConfigService, never()).update(any(), any(), any());
+    }
+}