|
@@ -0,0 +1,306 @@
|
|
|
|
|
+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.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+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.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测补充:能力键 {@code diet.recommendation.byReport}(S-cfc-269,TDD-RED 阶段)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>与已有两份同键 RED 测试的分工</b>:{@code DietRecommendationByReportContractTest}(7 例)与
|
|
|
|
|
+ * {@code DietByReportFoodsPathContractTest}(3 例)已锚定 {@code recommendByReport(Long)} 方法的存在性、
|
|
|
|
|
+ * 返回结构(hasReport/reportId/canEat/caution)、单一食材的正负分归类、无报告/报告无食材空态与
|
|
|
|
|
+ * 成员维度隔离。本类<b>不重复上述断言</b>,专测尚未覆盖的「清单数据质量 / 默认值」契约:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>正常流-默认值:推荐指数为 {@code null} 时须按 0 分兜底归入可吃清单(≥0 口径),
|
|
|
|
|
+ * 不得抛异常、不得静默丢弃该食材(对应「默认值未实现」的 RED 诉求);</li>
|
|
|
|
|
+ * <li>正常流-完整性/互斥:报告内每个食材恰好落入可吃或慎吃之一,两清单互斥且不重复
|
|
|
|
|
+ * (「按菌群生成可吃/慎吃清单」是一次完整划分,不是抽样);</li>
|
|
|
|
|
+ * <li>正常流-跨数据源去重:同一 foodId 同时存在于 foods 主表与 report_food_suitability 回退表时,
|
|
|
|
|
+ * 结果中只应出现一次;</li>
|
|
|
|
|
+ * <li>边界流-随报告刷新:新报告无任何食材数据时,须清空上一期报告的清单,不残留旧食材。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>溯源</b>:需求 {@code tests/user-scenarios/应用场景总览.md} SC-P-26(基于报告饮食推荐:
|
|
|
|
|
+ * 身→饮食推荐 → 按菌群生成可吃/慎吃清单;边界:无报告无推荐;随报告刷新);业务域 C。
|
|
|
|
|
+ * 设计 {@code docs/superpowers/api/API_REFERENCE.md} §4.11(/api/diet/recommendation/generate
|
|
|
|
|
+ * selected_foods、/api/diet/ingredients/suggest、/api/diet/recommendation/today)与 §4.59.6
|
|
|
|
|
+ * (SC-P-26 由 /api/food/recommendation/* + /api/meal/* + /api/diet/* 等价物承载)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:{@link FoodRecommendService}#{@code recommendByReport(Long)} 在 src/main 中不存在,
|
|
|
|
|
+ * 以反射锚定避免编译期依赖不存在签名;方法缺失即断言失败(红),失败原文含能力键与 SC-P-26 条款。
|
|
|
|
|
+ * GREEN 阶段落地该方法后本测试自动转绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>纯 Mockito/反射,无 Spring 上下文 / 无 DB / 无外部服务,与同批 RED 契约测试同风格。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class DietRecommendationByReportDefaultsContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CONTRACT_METHOD = "recommendByReport";
|
|
|
|
|
+ private static final Long MEMBER_ID = 99L;
|
|
|
|
|
+ private static final Long REPORT_ID = 400L;
|
|
|
|
|
+
|
|
|
|
|
+ private FoodRecommendService service;
|
|
|
|
|
+ private HealthReportService healthReportService;
|
|
|
|
|
+ private FoodService foodService;
|
|
|
|
|
+ private ReportFoodSuitabilityMapper reportFoodSuitabilityMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ service = new FoodRecommendService();
|
|
|
|
|
+ // 与同键已有测试一致:所有非静态非 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");
|
|
|
|
|
+ foodService = (FoodService) readField(service, "foodService");
|
|
|
|
|
+ reportFoodSuitabilityMapper = (ReportFoodSuitabilityMapper) readField(service, "reportFoodSuitabilityMapper");
|
|
|
|
|
+
|
|
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(report(REPORT_ID, MEMBER_ID));
|
|
|
|
|
+ when(foodService.listByReportId(anyLong())).thenReturn(Collections.<Food>emptyList());
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Collections.<ReportFoodSuitability>emptyList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Object readField(Object target, String name) throws Exception {
|
|
|
|
|
+ Field f = target.getClass().getDeclaredField(name);
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ return f.get(target);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static 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 static Food food(Long id, String name, Integer score) {
|
|
|
|
|
+ Food f = new Food();
|
|
|
|
|
+ f.setId(id);
|
|
|
|
|
+ f.setName(name);
|
|
|
|
|
+ f.setCategory("食材");
|
|
|
|
|
+ f.setScore(score);
|
|
|
|
|
+ return f;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static ReportFoodSuitability suitability(Long foodId, String name, Integer score) {
|
|
|
|
|
+ ReportFoodSuitability rfs = new ReportFoodSuitability();
|
|
|
|
|
+ rfs.setReportId(REPORT_ID);
|
|
|
|
|
+ rfs.setFoodId(foodId);
|
|
|
|
|
+ rfs.setFoodName(name);
|
|
|
|
|
+ rfs.setCategory("食材");
|
|
|
|
|
+ rfs.setScore(score);
|
|
|
|
|
+ return rfs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:默认值(score=null → 0 → 可吃) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-默认值:回退表 score=null 按 0 分归入可吃清单,不抛异常不丢弃")
|
|
|
|
|
+ void nullScore_fallback_defaultsToZeroCanEat() {
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Arrays.asList(
|
|
|
|
|
+ suitability(1L, "牛奶", null),
|
|
|
|
|
+ suitability(2L, "油炸食品", -20)));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = recommend();
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(namesOf(listOf(result, "canEat")).contains("牛奶"),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 回退表 score=null 应默认 0 分归入可吃清单 canEat(≥0 口径),不得丢弃");
|
|
|
|
|
+ assertTrue(namesOf(listOf(result, "caution")).contains("油炸食品"),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 负分食材仍应归入慎吃清单 caution");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-默认值:foods 主表 score=null 按 0 分归入可吃清单")
|
|
|
|
|
+ void nullScore_foodsPath_defaultsToZeroCanEat() {
|
|
|
|
|
+ when(foodService.listByReportId(REPORT_ID)).thenReturn(Arrays.asList(
|
|
|
|
|
+ food(3L, "米饭", null),
|
|
|
|
|
+ food(4L, "辣椒", -10)));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = recommend();
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(namesOf(listOf(result, "canEat")).contains("米饭"),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] foods 主表 score=null 应默认 0 分归入可吃清单 canEat");
|
|
|
|
|
+ assertTrue(namesOf(listOf(result, "caution")).contains("辣椒"),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] foods 主表负分食材应归入慎吃清单 caution");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:完整划分 + 跨数据源去重 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-完整划分:报告内每个食材恰好落入可吃或慎吃之一,两清单互斥不重复")
|
|
|
|
|
+ void allReportFoods_partitionedExactlyOnceAndDisjoint() {
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Arrays.asList(
|
|
|
|
|
+ suitability(1L, "苹果", 80),
|
|
|
|
|
+ suitability(2L, "燕麦", 10),
|
|
|
|
|
+ suitability(3L, "油炸食品", -5),
|
|
|
|
|
+ suitability(4L, "含糖饮料", -30)));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = recommend();
|
|
|
|
|
+
|
|
|
|
|
+ List<String> canEat = namesOf(listOf(result, "canEat"));
|
|
|
|
|
+ List<String> caution = namesOf(listOf(result, "caution"));
|
|
|
|
|
+ Set<String> expected = new HashSet<>(Arrays.asList("苹果", "燕麦", "油炸食品", "含糖饮料"));
|
|
|
|
|
+ assertEquals(4, canEat.size() + caution.size(),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 4 个报告食材应全部落入两份清单(完整划分),实际可吃=" + canEat + " 慎吃=" + caution);
|
|
|
|
|
+ assertEquals(expected, union(canEat, caution),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 可吃∪慎吃应恰好等于报告全部食材,不得遗漏或混入其它来源");
|
|
|
|
|
+ assertTrue(Collections.disjoint(canEat, caution),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 同一食材不得同时出现在可吃与慎吃清单(互斥)");
|
|
|
|
|
+ assertEquals(4, new HashSet<>(canEat).size() + new HashSet<>(caution).size(),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 清单内不得出现重复食材");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-跨数据源去重:foodId 同时存在于 foods 主表与回退表时只出现一次")
|
|
|
|
|
+ void sameFoodInBothSources_notDuplicated() {
|
|
|
|
|
+ when(foodService.listByReportId(REPORT_ID)).thenReturn(
|
|
|
|
|
+ Collections.singletonList(food(7L, "三文鱼", 60)));
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(
|
|
|
|
|
+ Collections.singletonList(suitability(7L, "三文鱼", 60)));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = recommend();
|
|
|
|
|
+
|
|
|
|
|
+ int occurrences = countOccurrences(namesOf(listOf(result, "canEat")), "三文鱼")
|
|
|
|
|
+ + countOccurrences(namesOf(listOf(result, "caution")), "三文鱼");
|
|
|
|
|
+ assertEquals(1, occurrences,
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 同一 foodId=7『三文鱼』同时命中主表与回退表时,结果中只应出现一次,实际=" + occurrences);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:随报告刷新(空新报告清空旧清单) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-随报告刷新:新报告无食材数据时清空上一期清单,不残留旧食材")
|
|
|
|
|
+ void refreshedReportWithNoFoods_clearsPreviousLists() {
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(
|
|
|
|
|
+ Collections.singletonList(suitability(1L, "苹果", 80)));
|
|
|
|
|
+ Map<String, Object> first = recommend();
|
|
|
|
|
+ assertTrue(namesOf(listOf(first, "canEat")).contains("苹果"), "第一期报告可吃清单应含苹果");
|
|
|
|
|
+
|
|
|
|
|
+ // 刷新为第二期报告(无食材数据)
|
|
|
|
|
+ when(healthReportService.getLatestReport(MEMBER_ID)).thenReturn(report(401L, MEMBER_ID));
|
|
|
|
|
+ when(reportFoodSuitabilityMapper.selectList(any())).thenReturn(Collections.<ReportFoodSuitability>emptyList());
|
|
|
|
|
+ Map<String, Object> second = recommend();
|
|
|
|
|
+
|
|
|
|
|
+ assertTrue(listOf(second, "canEat").isEmpty(),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 随报告刷新:新报告无食材数据时可吃清单应清空,不得残留旧报告『苹果』");
|
|
|
|
|
+ assertTrue(listOf(second, "caution").isEmpty(),
|
|
|
|
|
+ "RED[diet.recommendation.byReport] 随报告刷新:新报告无食材数据时慎吃清单应清空");
|
|
|
|
|
+ assertFalse(namesOf(listOf(second, "canEat")).contains("苹果"), "刷新后不应残留旧报告食材");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 反射 / 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 反射锚定契约方法(签名不存在于 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> recommend() {
|
|
|
|
|
+ Method m = findContractMethod();
|
|
|
|
|
+ if (m == null) {
|
|
|
|
|
+ fail("RED[diet.recommendation.byReport] 契约方法 FoodRecommendService#" + CONTRACT_METHOD
|
|
|
|
|
+ + "(Long) 未实现(实现未落地):SC-P-26 要求按菌群生成可吃/慎吃清单、"
|
|
|
|
|
+ + "空 score 默认值兜底、清单完整互斥去重、随报告刷新。");
|
|
|
|
|
+ }
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 兼容字符串清单与 Map 清单(name / foodName 字段)。 */
|
|
|
|
|
+ private static List<String> namesOf(List<Object> items) {
|
|
|
|
|
+ List<String> names = new ArrayList<String>();
|
|
|
|
|
+ for (Object o : items) {
|
|
|
|
|
+ if (o == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (o instanceof Map) {
|
|
|
|
|
+ Map<?, ?> item = (Map<?, ?>) o;
|
|
|
|
|
+ Object n = item.get("name") != null ? item.get("name") : item.get("foodName");
|
|
|
|
|
+ if (n != null) {
|
|
|
|
|
+ names.add(String.valueOf(n));
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ names.add(String.valueOf(o));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return names;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Set<String> union(List<String> a, List<String> b) {
|
|
|
|
|
+ Set<String> s = new HashSet<String>(a);
|
|
|
|
|
+ s.addAll(b);
|
|
|
|
|
+ return s;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static int countOccurrences(List<String> names, String name) {
|
|
|
|
|
+ int n = 0;
|
|
|
|
|
+ for (String s : names) {
|
|
|
|
|
+ if (name.equals(s)) {
|
|
|
|
|
+ n++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return n;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|