|
|
@@ -0,0 +1,342 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.EnergyController;
|
|
|
+import com.etotem.cfc.entity.EnergyBalance;
|
|
|
+import com.etotem.cfc.entity.EnergyDimension;
|
|
|
+import com.etotem.cfc.entity.EnergyLog;
|
|
|
+import com.etotem.cfc.entity.EnergySourceConfig;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.HealthDimensionScore;
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+import com.etotem.cfc.mapper.EnergyBalanceMapper;
|
|
|
+import com.etotem.cfc.mapper.EnergyDimensionMapper;
|
|
|
+import com.etotem.cfc.mapper.EnergyLogMapper;
|
|
|
+import com.etotem.cfc.mapper.EnergySourceConfigMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.service.DimensionRegistryService;
|
|
|
+import com.etotem.cfc.service.EnergyService;
|
|
|
+import com.etotem.cfc.service.HealthDimensionScoreService;
|
|
|
+import com.etotem.cfc.service.HealthReportService;
|
|
|
+import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
+import com.etotem.cfc.service.impl.DimensionScoreServiceImpl;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+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.never;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TDD-RED 契约单测:能力键 {@code energy.admin.adjust}(任务 S-cfc-305)。
|
|
|
+ *
|
|
|
+ * <p>溯源:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-89(手动发放 grant)、
|
|
|
+ * SC-P-91(健康报告入库刷新五维评分)、SC-P-92(权重/来源比例变更后重算),业务域 C-2;</li>
|
|
|
+ * <li>设计:{@code tests/user-scenarios/07-five-dimension.md} ED-3/ED-5/ED-6 与
|
|
|
+ * {@code tests/test-cases/parent.md} TC-P-89/-E/-P、TC-P-91-E、TC-P-92-E/-P;
|
|
|
+ * {@code docs/superpowers/api/API_REFERENCE.md} §4.5(能量与积分 /api/energy/logs)、
|
|
|
+ * §4.10(/{@code dan-assessment/result/{resultId}/guidance/save})、
|
|
|
+ * §4.13(管理员接口)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>RED 基线说明</b>:{@code EnergyGrantContractTest}(既有、全绿)只覆盖「任务奖励式 grant」
|
|
|
+ * ({@code sourceType=task}、必填 taskId);而本契约要求的「家长/运营手动调整」语义尚未落地:
|
|
|
+ * ① 手动发放需以 {@code sourceType=manual} 留痕并支持备注/工单号;② 负额须由控制器返回 400;
|
|
|
+ * ③ 非本家庭成员须 403;④ 同一工单号重复发放须幂等;⑤ 同一报告重复入库不得重复刷新五维分;
|
|
|
+ * ⑥ 来源比例之和≠100% 须拒绝;⑦ 非 admin 改全局比例须 403。上述 7 条当前均不成立,
|
|
|
+ * 故本测试预期全红(纯 Mockito / 反射,无 Spring / DB / 外部服务)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>GREEN 升级指引</b>:实现侧补齐上述行为后本测试自动转绿;既有
|
|
|
+ * {@code EnergyGrantContractTest} 的「taskId 发放→sourceType=task」断言保持不变,
|
|
|
+ * 两种入口按是否带工单号/备注区分即可。</p>
|
|
|
+ */
|
|
|
+class EnergyAdminAdjustContractTest extends EnergyContractTestBase {
|
|
|
+
|
|
|
+ private static final String KEY = "[energy.admin.adjust] ";
|
|
|
+
|
|
|
+ private EnergyService energyService;
|
|
|
+ private EnergyDimensionMapper dimensionMapper;
|
|
|
+ private EnergyLogMapper logMapper;
|
|
|
+ private EnergyBalanceMapper balanceMapper;
|
|
|
+ private EnergySourceConfigMapper sourceConfigMapper;
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+ private EnergyController controller;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void initTableInfo() {
|
|
|
+ MapperBuilderAssistant assistant =
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
|
|
+ TableInfoHelper.initTableInfo(assistant, HealthReport.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ energyService = new EnergyService();
|
|
|
+ dimensionMapper = mock(EnergyDimensionMapper.class);
|
|
|
+ logMapper = mock(EnergyLogMapper.class);
|
|
|
+ balanceMapper = mock(EnergyBalanceMapper.class);
|
|
|
+ sourceConfigMapper = mock(EnergySourceConfigMapper.class);
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
+
|
|
|
+ inject(energyService, "energyDimensionMapper", dimensionMapper);
|
|
|
+ inject(energyService, "energyBehaviorConfigMapper",
|
|
|
+ mock(com.etotem.cfc.mapper.EnergyBehaviorConfigMapper.class));
|
|
|
+ inject(energyService, "energyLogMapper", logMapper);
|
|
|
+ inject(energyService, "energyBalanceMapper", balanceMapper);
|
|
|
+ inject(energyService, "familyMemberMapper", familyMemberMapper);
|
|
|
+ inject(energyService, "energySourceConfigMapper", sourceConfigMapper);
|
|
|
+ inject(energyService, "wishMapper", mock(com.etotem.cfc.mapper.WishMapper.class));
|
|
|
+ inject(energyService, "notificationDispatchService", mock(NotificationDispatchService.class));
|
|
|
+
|
|
|
+ when(dimensionMapper.selectList(any())).thenReturn(Arrays.asList(fiveDims()));
|
|
|
+ when(logMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ when(balanceMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ FamilyMember own = new FamilyMember();
|
|
|
+ own.setId(50L);
|
|
|
+ own.setUserId(12L);
|
|
|
+ own.setFamilyId(1L);
|
|
|
+ FamilyMember other = new FamilyMember();
|
|
|
+ other.setId(999L);
|
|
|
+ other.setUserId(99L);
|
|
|
+ other.setFamilyId(2L);
|
|
|
+ when(familyMemberMapper.selectById(any())).thenReturn(own);
|
|
|
+ when(familyMemberMapper.selectById(999L)).thenReturn(other);
|
|
|
+
|
|
|
+ controller = new EnergyController();
|
|
|
+ inject(controller, "energyService", energyService);
|
|
|
+ inject(controller, "fiveDimensionScoreService",
|
|
|
+ mock(com.etotem.cfc.service.FiveDimensionScoreService.class));
|
|
|
+ inject(controller, "userMapper", mock(com.etotem.cfc.mapper.UserMapper.class));
|
|
|
+ inject(controller, "unlockGateService", mock(com.etotem.cfc.service.UnlockGateService.class));
|
|
|
+
|
|
|
+ initTable(EnergyDimension.class);
|
|
|
+ initTable(EnergyLog.class);
|
|
|
+ initTable(EnergyBalance.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-89 手动发放 grant ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-89 正常流:手动 grant(无 taskId,带备注/工单号)应以 manual 来源留痕")
|
|
|
+ void 正常流_手动发放以manual留痕() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("memberId", 50L);
|
|
|
+ params.put("amount", 10L);
|
|
|
+ params.put("dimensionCode", "body");
|
|
|
+ params.put("remark", "补偿奖励");
|
|
|
+ params.put("workOrderNo", "WO-MANUAL-001");
|
|
|
+
|
|
|
+ Result<EnergyLog> result = controller.grantEnergy(params, 1L, 50L);
|
|
|
+
|
|
|
+ assertNotNull(result, KEY + "手动发放应返回结果");
|
|
|
+ assertEquals(200, result.getCode(),
|
|
|
+ KEY + "家长/运营对指定成员手动发放能量应成功;当前缺 taskId 即拒绝,手动调整入口未落地(SC-P-89)");
|
|
|
+ assertNotNull(result.getData(), KEY + "手动发放应返回 EnergyLog 留痕");
|
|
|
+ assertEquals("manual", result.getData().getSourceType(),
|
|
|
+ KEY + "手动发放流水 sourceType 必须为 manual(ED-3 / TC-P-89)");
|
|
|
+ assertNotNull(result.getData().getDescription(), KEY + "备注应写入流水留痕");
|
|
|
+ assertTrue(result.getData().getDescription().contains("补偿奖励"),
|
|
|
+ KEY + "请求 remark 应落入 EnergyLog 描述,便于审计(TC-P-89)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-89 异常流:负额 grant 应返回 400 业务错误,不得抛异常穿透")
|
|
|
+ void 异常流_负额应返回400() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("memberId", 50L);
|
|
|
+ params.put("taskId", 1L);
|
|
|
+ params.put("amount", -5L);
|
|
|
+ params.put("dimensionCode", "body");
|
|
|
+
|
|
|
+ Result<EnergyLog> result = assertDoesNotThrow(
|
|
|
+ () -> controller.grantEnergy(params, 1L, 50L),
|
|
|
+ KEY + "负额发放应被控制器校验并以 Result(400) 返回,当前异常直接穿透(TC-P-89-E)");
|
|
|
+
|
|
|
+ assertEquals(400, result.getCode(),
|
|
|
+ KEY + "负额/非法金额必须 400 且不发放(SC-P-89 / TC-P-89-E)");
|
|
|
+ verify(logMapper, never()).insert(any(EnergyLog.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-89 权限流:家长给非本家庭成员 grant 应 403")
|
|
|
+ void 权限流_跨家庭发放应403() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("memberId", 999L); // familyId=2,与当前 familyId=1 不同
|
|
|
+ params.put("taskId", 1L);
|
|
|
+ params.put("amount", 10L);
|
|
|
+ params.put("dimensionCode", "body");
|
|
|
+
|
|
|
+ Result<EnergyLog> result = controller.grantEnergy(params, 1L, 50L);
|
|
|
+
|
|
|
+ assertEquals(403, result.getCode(),
|
|
|
+ KEY + "familyId=1 的家长不得给 familyId=2 的成员发放能量,当前无归属校验(SC-P-89-P)");
|
|
|
+ verify(logMapper, never()).insert(any(EnergyLog.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-89 异常流:重复工单号 grant 应幂等(仅写一条流水)")
|
|
|
+ void 异常流_重复工单号幂等() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("memberId", 50L);
|
|
|
+ params.put("taskId", 1234L);
|
|
|
+ params.put("amount", 10L);
|
|
|
+ params.put("dimensionCode", "body");
|
|
|
+ params.put("workOrderNo", "WO-IDEMPOTENT-001");
|
|
|
+
|
|
|
+ Result<EnergyLog> first = controller.grantEnergy(params, 1L, 50L);
|
|
|
+ Result<EnergyLog> second = controller.grantEnergy(params, 1L, 50L);
|
|
|
+
|
|
|
+ assertEquals(200, first.getCode(), KEY + "首次发放应成功");
|
|
|
+ assertEquals(200, second.getCode(), KEY + "重复工单号应幂等返回成功,而非报错");
|
|
|
+ // 期望仅 1 条流水;当前实现不识别 workOrderNo,两次调用各写一条 → 红
|
|
|
+ verify(logMapper, times(1)).insert(any(EnergyLog.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-91 报告确认刷新五维评分 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-91 异常流:同一报告重复入库不得为同一维度重复写分")
|
|
|
+ void 异常流_重复入库同一报告幂等() throws Exception {
|
|
|
+ DimensionScoreServiceImpl scoreService = newScoreServiceWithBodyRegistry();
|
|
|
+ HealthReportService healthReportService = (HealthReportService) readField(scoreService, "healthReportService");
|
|
|
+ HealthDimensionScoreService healthDimScoreService =
|
|
|
+ (HealthDimensionScoreService) readField(scoreService, "scoreService");
|
|
|
+
|
|
|
+ HealthReport report = new HealthReport();
|
|
|
+ report.setId(200L);
|
|
|
+ report.setGutHealthScore(80);
|
|
|
+ report.setNutritionScore(90);
|
|
|
+ report.setGutBalanceScore(70);
|
|
|
+ report.setCoreSpeciesScore(60);
|
|
|
+ report.setReportDate(new Date());
|
|
|
+ when(healthReportService.getReportById(200L)).thenReturn(report);
|
|
|
+ when(healthReportService.getGutFloraByReportId(200L)).thenReturn(new ArrayList<>());
|
|
|
+ when(healthReportService.getReportIndicators(200L)).thenReturn(new ArrayList<>());
|
|
|
+ when(healthReportService.getDiseaseRisksByReportId(200L)).thenReturn(new ArrayList<>());
|
|
|
+
|
|
|
+ scoreService.refreshFromGutReport(55L, 200L);
|
|
|
+ scoreService.refreshFromGutReport(55L, 200L); // 重复确认入库
|
|
|
+
|
|
|
+ ArgumentCaptor<HealthDimensionScore> captor =
|
|
|
+ ArgumentCaptor.forClass(HealthDimensionScore.class);
|
|
|
+ verify(healthDimScoreService, org.mockito.Mockito.atLeastOnce()).saveScore(captor.capture());
|
|
|
+ Set<String> writtenDimensions = new HashSet<>();
|
|
|
+ for (HealthDimensionScore score : captor.getAllValues()) {
|
|
|
+ assertEquals(55L, score.getMemberId(), KEY + "报告分必须写入指定成员");
|
|
|
+ assertTrue(writtenDimensions.add(score.getDimension()),
|
|
|
+ KEY + "同一报告重复入库不得对同一维度重复写分,当前无幂等去重(SC-P-91 / TC-P-91-E)");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== SC-P-92 来源比例配置变更后重算 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-92 异常流:来源比例之和≠100% 应拒绝且不得写配置")
|
|
|
+ void 异常流_来源比例和不为100拒绝() {
|
|
|
+ List<Map<String, Object>> ratios = new ArrayList<>();
|
|
|
+ ratios.add(ratio("body", 60));
|
|
|
+ ratios.add(ratio("mind", 60)); // 合计 120% → 非法
|
|
|
+
|
|
|
+ RuntimeException e = assertThrows(RuntimeException.class,
|
|
|
+ () -> energyService.configureSourceRatios("task", 1L, "任务", ratios),
|
|
|
+ KEY + "来源比例之和必须为 100%,当前无校验直接写库(SC-P-92 / TC-P-92-E)");
|
|
|
+ assertTrue(e.getMessage() != null
|
|
|
+ && (e.getMessage().contains("比例") || e.getMessage().contains("100")),
|
|
|
+ KEY + "拒绝信息应说明比例非法,实际为:" + e.getMessage());
|
|
|
+ verify(sourceConfigMapper, never()).insert(any(EnergySourceConfig.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-92 权限流:非 admin 改全局来源比例应 403")
|
|
|
+ void 权限流_非admin改全局比例应403() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("sourceType", "task");
|
|
|
+ params.put("sourceId", 1L);
|
|
|
+ params.put("sourceName", "任务");
|
|
|
+ params.put("role", "parent"); // 非 admin
|
|
|
+ params.put("ratios", Collections.singletonList(ratio("body", 100)));
|
|
|
+
|
|
|
+ Result<String> result = controller.configureSourceRatios(params, 1L, 12L);
|
|
|
+
|
|
|
+ assertEquals(403, result.getCode(),
|
|
|
+ KEY + "非 admin 不得修改全局能量来源比例,当前未按角色拦截(SC-P-92-P / TC-P-92-P)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ private static Map<String, Object> ratio(String dimensionCode, Integer ratio) {
|
|
|
+ Map<String, Object> r = new HashMap<>();
|
|
|
+ r.put("dimensionCode", dimensionCode);
|
|
|
+ r.put("ratio", ratio);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private DimensionScoreServiceImpl newScoreServiceWithBodyRegistry() throws Exception {
|
|
|
+ DimensionScoreServiceImpl svc = new DimensionScoreServiceImpl();
|
|
|
+ for (Field f : svc.getClass().getDeclaredFields()) {
|
|
|
+ if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(svc, mock(f.getType()));
|
|
|
+ }
|
|
|
+ DimensionRegistryService registry =
|
|
|
+ (DimensionRegistryService) readField(svc, "dimensionRegistryService");
|
|
|
+ String[][] dims = {
|
|
|
+ {"body.growth", "生长发育"}, {"body.sleep", "睡眠质量"},
|
|
|
+ {"body.vision", "视力健康"}, {"body.immunity", "免疫力"},
|
|
|
+ {"body.nutrition", "营养均衡"}, {"body.gut", "肠胃健康"},
|
|
|
+ {"body.exercise", "运动活力"}
|
|
|
+ };
|
|
|
+ List<com.etotem.cfc.dto.DimensionNode> children = new ArrayList<>();
|
|
|
+ for (String[] item : dims) {
|
|
|
+ com.etotem.cfc.dto.DimensionNode node = new com.etotem.cfc.dto.DimensionNode();
|
|
|
+ node.setCode(item[0]);
|
|
|
+ node.setName(item[1]);
|
|
|
+ node.setNodeKind("sub");
|
|
|
+ children.add(node);
|
|
|
+ }
|
|
|
+ com.etotem.cfc.dto.DimensionNode body = new com.etotem.cfc.dto.DimensionNode();
|
|
|
+ body.setCode("body");
|
|
|
+ body.setName("身体健康");
|
|
|
+ body.setNodeKind("root");
|
|
|
+ body.setChildren(children);
|
|
|
+ when(registry.getNodeByCode("body")).thenReturn(body);
|
|
|
+ return svc;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object readField(Object target, String name) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ return f.get(target);
|
|
|
+ }
|
|
|
+}
|