|
|
@@ -0,0 +1,296 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Food;
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+import com.etotem.cfc.entity.ReportFoodSuitability;
|
|
|
+import com.etotem.cfc.mapper.ReportFoodSuitabilityMapper;
|
|
|
+import com.etotem.cfc.service.FoodRecommendService;
|
|
|
+import com.etotem.cfc.service.FoodService;
|
|
|
+import com.etotem.cfc.service.HealthReportService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:能力键 {@code diet.recommendation.byReport}(S-cfc-269,TDD-RED 阶段)。
|
|
|
+ *
|
|
|
+ * <p><b>需求锚点</b>:{@code tests/user-scenarios/应用场景总览.md} SC-P-26(基于报告饮食推荐)——
|
|
|
+ * 「身→饮食推荐 → 按菌群生成可吃/慎吃清单;边界:无报告无推荐;随报告刷新」;
|
|
|
+ * {@code tests/user-scenarios/02-parent.md} 场景 P-26;业务域 C。
|
|
|
+ * 用例 {@code tests/test-cases/parent.md} TC-P-26-b「按菌群生成可吃/慎吃清单,无报告无推荐」。</p>
|
|
|
+ *
|
|
|
+ * <p><b>设计锚点</b>:{@code docs/superpowers/api/API_REFERENCE.md} §4.11 饮食推荐 ——
|
|
|
+ * {@code POST /api/diet/recommendation/generate}(生成推荐 selected_foods)、
|
|
|
+ * {@code POST /api/diet/ingredients/suggest}(食材推荐)、
|
|
|
+ * {@code POST /api/diet/recommendation/today}(今日推荐);
|
|
|
+ * §4.59.6 明确 SC-P-26 由「/api/food/recommendation/* + /api/meal/* + /api/diet/*」等价物承载。</p>
|
|
|
+ *
|
|
|
+ * <p><b>被校验的契约(当前 src/main <u>未落地</u>)</b>:{@link FoodRecommendService} 应提供
|
|
|
+ * {@code Map<String,Object> recommendByReport(Long memberId)} 入口,按成员最新菌群报告生成
|
|
|
+ * 「可吃 / 慎吃」两份清单:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>返回结构含 {@code hasReport / reportId / canEat / caution};</li>
|
|
|
+ * <li>正常流:非负分食材进 {@code canEat}(可吃)、负分食材进 {@code caution}(慎吃/戒备);</li>
|
|
|
+ * <li>边界流:无报告({@code getLatestReport==null})→ 无推荐,两清单为空;
|
|
|
+ * 报告无食材数据 → 两清单为空且不抛异常;报告刷新后再调用按<b>最新</b>报告重算;</li>
|
|
|
+ * <li>权限流:以传入的 {@code memberId} 为报告主体查数据(成员维度隔离,不串他人报告)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:{@code recommendByReport(Long)} 方法在 {@code src/main} 中不存在(全库 0 引用),
|
|
|
+ * 本测试通过反射锚定该契约方法以避免编译期依赖不存在签名;方法缺失即断言失败(红),
|
|
|
+ * 失败原文含能力键与 SC-P-26 条款。GREEN 阶段在 FoodRecommendService 落地该方法后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito/反射,无 Spring 上下文 / 无 DB / 无外部服务(与同批 RED 契约测试
|
|
|
+ * TeamProfitHookContractTest / TeamStarMultiplierContractTest / FamilyJoinValidationRulesContractTest 同风格)。</p>
|
|
|
+ */
|
|
|
+class DietRecommendationByReportContractTest {
|
|
|
+
|
|
|
+ /** 契约方法(SC-P-26 新增能力载体,当前未实现) */
|
|
|
+ private static final String CONTRACT_METHOD = "recommendByReport";
|
|
|
+
|
|
|
+ private static final Long MEMBER_ID = 77L;
|
|
|
+ private static final Long REPORT_ID = 200L;
|
|
|
+
|
|
|
+ private FoodRecommendService service;
|
|
|
+ private HealthReportService healthReportService;
|
|
|
+ private ReportFoodSuitabilityMapper reportFoodSuitabilityMapper;
|
|
|
+ private FoodService foodService;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new FoodRecommendService();
|
|
|
+ // 与 ReportDietRecommendContractTest 一致:把所有非静态非 final 字段替换为 mock
|
|
|
+ for (Field f : FoodRecommendService.class.getDeclaredFields()) {
|
|
|
+ if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(service, mock(f.getType()));
|
|
|
+ }
|
|
|
+ healthReportService = (HealthReportService) readField(service, "healthReportService");
|
|
|
+ reportFoodSuitabilityMapper = (ReportFoodSuitabilityMapper) readField(service, "reportFoodSuitabilityMapper");
|
|
|
+ foodService = (FoodService) readField(service, "foodService");
|
|
|
+
|
|
|
+ // 默认:foods 表无数据 → 走 report_food_suitability 回退;报告默认不存在(各用例按需覆盖)
|
|
|
+ when(foodService.listByReportId(anyLong())).thenReturn(Collections.<Food>emptyList());
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Collections.<ReportFoodSuitability>emptyList());
|
|
|
+ when(healthReportService.getLatestReport(anyLong())).thenReturn(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object readField(Object target, String name) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ return f.get(target);
|
|
|
+ }
|
|
|
+
|
|
|
+ private HealthReport report(Long id, Long memberId) {
|
|
|
+ HealthReport r = new HealthReport();
|
|
|
+ r.setId(id);
|
|
|
+ r.setSubjectId(memberId);
|
|
|
+ r.setReportType("gut_flora");
|
|
|
+ r.setReportDate(new Date());
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ReportFoodSuitability suitability(Long foodId, String name, int score) {
|
|
|
+ ReportFoodSuitability rfs = new ReportFoodSuitability();
|
|
|
+ rfs.setReportId(REPORT_ID);
|
|
|
+ rfs.setFoodId(foodId);
|
|
|
+ rfs.setFoodName(name);
|
|
|
+ rfs.setCategory("食材");
|
|
|
+ rfs.setScore(score);
|
|
|
+ return rfs;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-0:契约方法 recommendByReport(Long) 应存在(SC-P-26 新增能力)")
|
|
|
+ void contractMethod_shouldExist() {
|
|
|
+ Method m = findContractMethod();
|
|
|
+ assertTrue(m != null,
|
|
|
+ "RED[diet.recommendation.byReport] FoodRecommendService 应提供 recommendByReport(Long) 入口:"
|
|
|
+ + "SC-P-26『身→饮食推荐 → 按菌群生成可吃/慎吃清单』,当前实现未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:有报告 → 返回 hasReport/reportId 与可吃、慎吃两份清单")
|
|
|
+ void withReport_returnsHasReportReportIdAndTwoLists() {
|
|
|
+ stubReport(report(REPORT_ID, MEMBER_ID));
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ assertTrue(Boolean.TRUE.equals(prop(result, "hasReport")),
|
|
|
+ "有最新菌群报告时 hasReport 应为 true");
|
|
|
+ assertEquals(REPORT_ID, prop(result, "reportId"), "reportId 应为该成员最新报告 id");
|
|
|
+ assertTrue(result != null && result.containsKey("canEat"), "返回结构应含 canEat(可吃清单)");
|
|
|
+ assertTrue(result != null && result.containsKey("caution"), "返回结构应含 caution(慎吃清单)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:按菌群清单 —— 非负分食材进可吃、负分食材进慎吃")
|
|
|
+ void byFlora_nonNegativeGoesCanEat_negativeGoesCaution() {
|
|
|
+ stubReport(report(REPORT_ID, MEMBER_ID));
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(java.util.Arrays.asList(
|
|
|
+ suitability(1L, "苹果", 80),
|
|
|
+ suitability(2L, "油炸食品", -30)));
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ assertTrue(namesContain(prop(result, "canEat"), "苹果"),
|
|
|
+ "非负分(80)食材『苹果』应进可吃清单 canEat");
|
|
|
+ assertTrue(namesContain(prop(result, "caution"), "油炸食品"),
|
|
|
+ "负分(-30)食材『油炸食品』应进慎吃清单 caution");
|
|
|
+ assertFalse(namesContain(prop(result, "caution"), "苹果"), "可吃食材不应同时进慎吃清单");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-1:无报告 → 无推荐,两清单为空且 hasReport=false")
|
|
|
+ void noReport_noRecommendation_emptyLists() {
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(null);
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ assertFalse(Boolean.TRUE.equals(prop(result, "hasReport")), "无报告时 hasReport 应为 false(无报告无推荐)");
|
|
|
+ assertTrue(asList(prop(result, "canEat")).isEmpty(), "无报告时可吃清单应为空");
|
|
|
+ assertTrue(asList(prop(result, "caution")).isEmpty(), "无报告时慎吃清单应为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-2:报告无食材数据 → 两清单为空且不抛异常")
|
|
|
+ void reportWithoutFoods_emptyListsNoThrow() {
|
|
|
+ stubReport(report(REPORT_ID, MEMBER_ID));
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Collections.<ReportFoodSuitability>emptyList());
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ assertTrue(asList(prop(result, "canEat")).isEmpty(), "报告无食材时可吃清单应为空");
|
|
|
+ assertTrue(asList(prop(result, "caution")).isEmpty(), "报告无食材时慎吃清单应为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-3:随报告刷新 —— 再次调用按最新报告重算,不返回旧报告食材")
|
|
|
+ void refreshedReport_recomputesFromLatestReport() {
|
|
|
+ // 同一成员连续两份报告:旧(200,苹果) → 新(201,牛奶)
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(
|
|
|
+ report(REPORT_ID, MEMBER_ID), report(201L, MEMBER_ID));
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(java.util.Arrays.asList(
|
|
|
+ suitability(1L, "苹果", 80)));
|
|
|
+
|
|
|
+ Map<String, Object> oldResult = recommendByReport(MEMBER_ID);
|
|
|
+ assertTrue(namesContain(prop(oldResult, "canEat"), "苹果"), "旧报告可吃清单应含苹果");
|
|
|
+
|
|
|
+ // 第二份报告食材:牛奶(新报告 id 201)
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(java.util.Arrays.asList(
|
|
|
+ suitability(3L, "牛奶", 90)));
|
|
|
+ Map<String, Object> newResult = recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ assertEquals(Long.valueOf(201L), prop(newResult, "reportId"), "应按最新报告刷新 reportId");
|
|
|
+ assertTrue(namesContain(prop(newResult, "canEat"), "牛奶"), "刷新后可吃清单应含新报告食材『牛奶』");
|
|
|
+ assertFalse(namesContain(prop(newResult, "canEat"), "苹果"), "刷新后不应残留旧报告食材『苹果』");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:以传入 memberId 为报告主体查数据(成员维度隔离,不串他人报告)")
|
|
|
+ void permission_memberScopedLookup() {
|
|
|
+ stubReport(report(REPORT_ID, MEMBER_ID));
|
|
|
+
|
|
|
+ recommendByReport(MEMBER_ID);
|
|
|
+
|
|
|
+ // 报告主体必须严格等于请求的 memberId,防止串查他人报告数据
|
|
|
+ verify(healthReportService).getLatestReport(MEMBER_ID);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射/辅助 ====================
|
|
|
+
|
|
|
+ private void stubReport(HealthReport r) {
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(r);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 反射锚定契约方法(签名不存在于 src/main,故不能静态调用)。 */
|
|
|
+ private Method findContractMethod() {
|
|
|
+ for (Method m : FoodRecommendService.class.getMethods()) {
|
|
|
+ if (!m.getName().equals(CONTRACT_METHOD)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Class<?>[] ps = m.getParameterTypes();
|
|
|
+ if (ps.length == 1 && (ps[0].isAssignableFrom(Long.class) || ps[0] == long.class)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private Map<String, Object> recommendByReport(Long memberId) {
|
|
|
+ Method m = findContractMethod();
|
|
|
+ if (m == null) {
|
|
|
+ fail("RED[diet.recommendation.byReport] 契约方法 FoodRecommendService#" + CONTRACT_METHOD
|
|
|
+ + "(Long) 未实现(实现未落地):SC-P-26 要求按菌群生成可吃/慎吃清单、无报告无推荐、随报告刷新。");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Object r = m.invoke(service, memberId);
|
|
|
+ if (r instanceof Map) {
|
|
|
+ return (Map<String, Object>) r;
|
|
|
+ }
|
|
|
+ fail("RED[diet.recommendation.byReport] " + CONTRACT_METHOD
|
|
|
+ + " 应返回 Map(含 hasReport/reportId/canEat/caution),实际 " + r);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ fail("RED[diet.recommendation.byReport] " + CONTRACT_METHOD + " 调用失败:" + e.getCause());
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ fail("RED[diet.recommendation.byReport] " + CONTRACT_METHOD + " 不可访问:" + e.getMessage());
|
|
|
+ }
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object prop(Map<String, Object> map, String key) {
|
|
|
+ return map == null ? null : map.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private static List<Object> asList(Object value) {
|
|
|
+ return value instanceof List ? (List<Object>) value : Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 兼容字符串清单与 Map 清单({@code name}/{@code foodName} 字段)。 */
|
|
|
+ private static boolean namesContain(Object value, String name) {
|
|
|
+ for (Object o : asList(value)) {
|
|
|
+ if (o == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (name.equals(o)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (o instanceof Map) {
|
|
|
+ Map<?, ?> item = (Map<?, ?>) o;
|
|
|
+ if (name.equals(item.get("name")) || name.equals(item.get("foodName"))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|