|
|
@@ -0,0 +1,206 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Food;
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+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.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+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.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测补充:能力键 {@code diet.recommendation.byReport}(S-cfc-269,TDD-RED 阶段)。
|
|
|
+ *
|
|
|
+ * <p><b>与已有 {@code DietRecommendationByReportContractTest} 的分工</b>:那份测试 7 例全部通过
|
|
|
+ * {@code report_food_suitability} 回退表驱动({@code foodService.listByReportId} 默认返回空),
|
|
|
+ * 本类专测<b>正常报告确认流程写入 {@code foods} 表</b>的主路径——实现侧
|
|
|
+ * {@code FoodRecommendService#recalculateForUser} / {@code #getCautionFoodsByReport} 均「优先 foods 表、
|
|
|
+ * 无数据才回退 report_food_suitability」。若 GREEN 实现只覆盖回退表,用户确认入库后的真实报告将拿不到
|
|
|
+ * 可吃/慎吃清单,故本路径必须单独锚定。</p>
|
|
|
+ *
|
|
|
+ * <p><b>溯源</b>:SC-P-26「身→饮食推荐 → 按菌群生成可吃/慎吃清单;无报告无推荐;随报告刷新」;
|
|
|
+ * 设计 {@code docs/superpowers/api/API_REFERENCE.md} §4.11(/api/diet/recommendation/generate
|
|
|
+ * 生成推荐 selected_foods、/api/diet/ingredients/suggest 食材推荐、/api/diet/recommendation/today)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>契约方法</b>:{@code FoodRecommendService#recommendByReport(Long memberId)} →
|
|
|
+ * {@code Map} 含 {@code canEat}(可吃,推荐指数 ≥ 0)/ {@code caution}(慎吃,推荐指数 < 0)。
|
|
|
+ * 当前 src/main 未落地,以反射锚定避免编译期依赖不存在签名;方法缺失即失败(红)。</p>
|
|
|
+ *
|
|
|
+ * <p>纯 Mockito/反射,无 Spring / 无 DB / 无外部服务,与同批 RED 契约测试同风格。</p>
|
|
|
+ */
|
|
|
+class DietByReportFoodsPathContractTest {
|
|
|
+
|
|
|
+ private static final String CONTRACT_METHOD = "recommendByReport";
|
|
|
+ private static final Long MEMBER_ID = 88L;
|
|
|
+ private static final Long REPORT_ID = 300L;
|
|
|
+
|
|
|
+ private FoodRecommendService service;
|
|
|
+ private HealthReportService healthReportService;
|
|
|
+ private FoodService foodService;
|
|
|
+ private ReportFoodSuitabilityMapper reportFoodSuitabilityMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new FoodRecommendService();
|
|
|
+ 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");
|
|
|
+ foodService = (FoodService) readField(service, "foodService");
|
|
|
+ reportFoodSuitabilityMapper = (ReportFoodSuitabilityMapper) readField(service, "reportFoodSuitabilityMapper");
|
|
|
+
|
|
|
+ HealthReport report = new HealthReport();
|
|
|
+ report.setId(REPORT_ID);
|
|
|
+ report.setSubjectId(MEMBER_ID);
|
|
|
+ report.setReportType("gut_flora");
|
|
|
+ report.setReportDate(new Date());
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(report);
|
|
|
+ // 主路径:foods 表有数据;回退表保持空,确保断言命中的是 foods 表数据
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-foods表报告食材:非负分进可吃、负分进慎吃(按菌群生成可吃/慎吃清单)")
|
|
|
+ void foodsPath_nonNegativeToCanEat_negativeToCaution() {
|
|
|
+ when(foodService.listByReportId(REPORT_ID)).thenReturn(Arrays.asList(
|
|
|
+ food(1L, "燕麦", 80),
|
|
|
+ food(2L, "油炸食品", -30)));
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport();
|
|
|
+
|
|
|
+ assertTrue(namesContain(listOf(result, "canEat"), "燕麦"),
|
|
|
+ "RED[diet.recommendation.byReport] foods 表非负分食材『燕麦』(80)应进可吃清单 canEat");
|
|
|
+ assertTrue(namesContain(listOf(result, "caution"), "油炸食品"),
|
|
|
+ "RED[diet.recommendation.byReport] foods 表负分食材『油炸食品』(-30)应进慎吃清单 caution");
|
|
|
+ assertFalse(namesContain(listOf(result, "caution"), "燕麦"),
|
|
|
+ "RED[diet.recommendation.byReport] 可吃食材不应同时出现在慎吃清单");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-0分食材归入可吃而非慎吃(推荐指数 ≥ 0 为可吃)")
|
|
|
+ void foodsPath_zeroScoreGoesToCanEat() {
|
|
|
+ when(foodService.listByReportId(REPORT_ID)).thenReturn(Collections.singletonList(
|
|
|
+ food(3L, "米饭", 0)));
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport();
|
|
|
+
|
|
|
+ assertTrue(namesContain(listOf(result, "canEat"), "米饭"),
|
|
|
+ "RED[diet.recommendation.byReport] 0 分食材『米饭』应归入可吃清单 canEat(≥0)");
|
|
|
+ assertFalse(namesContain(listOf(result, "caution"), "米饭"),
|
|
|
+ "RED[diet.recommendation.byReport] 0 分食材不应归入慎吃清单 caution(<0 才慎吃)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("边界流-报告全为负分食材:可吃清单为空、慎吃清单含全部(不回退通用推荐)")
|
|
|
+ void foodsPath_allNegative_canEatEmpty() {
|
|
|
+ when(foodService.listByReportId(REPORT_ID)).thenReturn(Arrays.asList(
|
|
|
+ food(4L, "油炸食品", -30),
|
|
|
+ food(5L, "含糖饮料", -50)));
|
|
|
+
|
|
|
+ Map<String, Object> result = recommendByReport();
|
|
|
+
|
|
|
+ assertTrue(listOf(result, "canEat").isEmpty(),
|
|
|
+ "RED[diet.recommendation.byReport] 报告食材全为负分时,可吃清单应为空(无通用推荐兜底)");
|
|
|
+ assertTrue(namesContain(listOf(result, "caution"), "油炸食品")
|
|
|
+ && namesContain(listOf(result, "caution"), "含糖饮料"),
|
|
|
+ "RED[diet.recommendation.byReport] 全部负分食材都应进慎吃清单 caution");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射 / 辅助 ====================
|
|
|
+
|
|
|
+ private static Object readField(Object target, String name) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ return f.get(target);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Food food(Long id, String name, int score) {
|
|
|
+ Food f = new Food();
|
|
|
+ f.setId(id);
|
|
|
+ f.setName(name);
|
|
|
+ f.setCategory("食材");
|
|
|
+ f.setScore(score);
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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() {
|
|
|
+ Method m = findContractMethod();
|
|
|
+ if (m == null) {
|
|
|
+ fail("RED[diet.recommendation.byReport] 契约方法 FoodRecommendService#" + CONTRACT_METHOD
|
|
|
+ + "(Long) 未实现(实现未落地):SC-P-26 要求按菌群报告(含 foods 表主路径)"
|
|
|
+ + "生成可吃/慎吃清单。");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Object r = m.invoke(service, MEMBER_ID);
|
|
|
+ if (r instanceof Map) {
|
|
|
+ return (Map<String, Object>) r;
|
|
|
+ }
|
|
|
+ fail("RED[diet.recommendation.byReport] " + CONTRACT_METHOD
|
|
|
+ + " 应返回含 canEat/caution 的 Map,实际 " + 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");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private static List<Object> listOf(Map<String, Object> map, String key) {
|
|
|
+ Object value = map == null ? null : map.get(key);
|
|
|
+ return value instanceof List ? (List<Object>) value : Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean namesContain(List<Object> list, String name) {
|
|
|
+ for (Object o : list) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|