|
@@ -0,0 +1,529 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Scanner;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测:能力键 {@code food.rule.pairingMatrix}(S-cfc-440,TDD-RED 阶段)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code docs/需求分析/2026-11-22-食材推荐加权规则引擎需求变更.md}
|
|
|
|
|
+ * CR-03 + §4.3(食材配伍矩阵:现代营养学协同/拮抗 + 中医药食同源相生相克)
|
|
|
|
|
+ * + §5(数据结构与迁移评估)+ §7.1 G-3(配伍效应正确写入指数)+ 第六节(能力键 food.rule.pairingMatrix)。</li>
|
|
|
|
|
+ * <li>配伍表 {@code food_pairing_matrix}(§4.3):字段
|
|
|
|
|
+ * {@code food_a_id / food_b_id / relation_type / intensity / effect_score / source / evidence_ref / enabled};
|
|
|
|
|
+ * {@code relation_type} 枚举 7 值:
|
|
|
|
|
+ * {@code synergy / antagonism / xiangsheng / xiangke / xiangwei / xiangsha / xiangwu};
|
|
|
|
|
+ * {@code intensity} 1-5;{@code effect_score} 为推荐指数调整值 [-50, +50];
|
|
|
|
|
+ * {@code source} 仅 {@code modern_nutrition / tcm_shi_liao},禁止混用无标注。</li>
|
|
|
|
|
+ * <li>规则(§4.3):维生素 C 促进铁吸收(协同)、草酸抑制钙吸收(拮抗)、高嘌呤×高嘌呤(避让);
|
|
|
|
|
+ * 菠菜+豆腐、柿子+螃蟹 → 忌配伍;若同时含 A+B 且 {@code relation_type ∈ {xiangke, antagonism}}
|
|
|
|
|
+ * 且 {@code intensity ≥ 3} → 硬避让(CR-07 消费此判定)。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(origin/cfclub 源码核实)</b>:{@code FoodPairingMatrix} 实体、{@code food_pairing_matrix}
|
|
|
|
|
+ * 建表与迁移已由后台规则管理({@code ops.foodRuleManagement} / CR-06)顺带落地,但<b>本能力键的配伍查询/效应计算/
|
|
|
|
|
+ * 避让判定服务未实现</b>:{@code FoodPairingMatrixMapper} 不存在,
|
|
|
|
|
+ * {@code FoodRecommendService} / {@code FoodService} / {@code DietRecommendationService} 均未引用配伍矩阵,
|
|
|
|
|
+ * 结构探测因此能编译能运行、行为断言失败(红)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约宿主(实现方按此落地)</b>:{@code com.etotem.cfc.service.FoodPairingMatrixService}
|
|
|
|
|
+ * —— 通过反射按方法名 + 参数个数探测,避免直接 import 不存在类导致编译失败:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@code List<String> supportedRelationTypes()} —— 7 种合法关系类型</li>
|
|
|
|
|
+ * <li>{@code boolean isValidRelationType(String type)} —— 关系类型值域校验</li>
|
|
|
|
|
+ * <li>{@code List<String> supportedSources()} —— 2 种权威来源</li>
|
|
|
|
|
+ * <li>{@code boolean isValidSource(String source)} —— 来源值域校验(禁止无标注混用)</li>
|
|
|
|
|
+ * <li>{@code double clampEffectScore(double value)} —— 截断到 [-50, +50]</li>
|
|
|
|
|
+ * <li>{@code double resolvePairingEffect(Long foodAId, Long foodBId, List<Map<String,Object>> rules)}
|
|
|
|
|
+ * —— 对称匹配 A×B、仅取 enabled 规则、无配置/无命中 → 0,越界截断;rules 为纯内存规则集,
|
|
|
|
|
+ * 每项含 foodAId / foodBId / relationType / intensity / effectScore / source / evidenceRef / enabled,
|
|
|
|
|
+ * 使契约与 DB 解耦</li>
|
|
|
|
|
+ * <li>{@code boolean requiresAvoidance(Long foodAId, Long foodBId, List<Map<String,Object>> rules)}
|
|
|
|
|
+ * —— 忌配伍判定:enabled 且 relationType∈{xiangke,antagonism} 且 intensity≥3 → true,否则 false</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯 JUnit5 + 反射 + 源码文本读取(无 Spring / 无 DB / 无外部服务),与同批已合入主链的
|
|
|
|
|
+ * RED 契约测试(TeamProfitHookContractTest / FoodProcessingEffectContractTest /
|
|
|
|
|
+ * FoodOpsRuleManagementCrudBehaviorContractTest)同风格。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>权限流</b>:配伍查询/避让判定为后端内部服务(后台 CRUD 由独立能力键 ops.foodRuleManagement / CR-06
|
|
|
|
|
+ * 承载,食谱硬避让由 diet.menu.pairingConstraint / CR-07 承载),无用户侧鉴权分支,权限流不适用。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:行为用例在 {@code contractHost()} 处失败(宿主类未实现),结构用例在 Mapper /
|
|
|
|
|
+ * 推荐链路源码锚点处失败;失败原文前缀 {@code RED[food.rule.pairingMatrix]}。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class FoodPairingMatrixContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CAP = "[food.rule.pairingMatrix]";
|
|
|
|
|
+ private static final String RED = "RED[food.rule.pairingMatrix]";
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约宿主类(§4.3 配伍查询/效应计算/避让判定服务,当前未实现)。 */
|
|
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.FoodPairingMatrixService";
|
|
|
|
|
+
|
|
|
|
|
+ /** 配伍矩阵实体与表(§4.3 food_pairing_matrix 表)。 */
|
|
|
|
|
+ private static final String ENTITY_FQCN = "com.etotem.cfc.entity.FoodPairingMatrix";
|
|
|
|
|
+ private static final String MAPPER_FQCN = "com.etotem.cfc.mapper.FoodPairingMatrixMapper";
|
|
|
|
|
+ private static final String TABLE = "food_pairing_matrix";
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.3 relation_type 的 7 个合法枚举值。 */
|
|
|
|
|
+ private static final String[] RELATION_TYPES = {
|
|
|
|
|
+ "synergy", "antagonism", "xiangsheng", "xiangke", "xiangwei", "xiangsha", "xiangwu"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.3 source 的 2 个合法枚举值。 */
|
|
|
|
|
+ private static final String[] SOURCES = {"modern_nutrition", "tcm_shi_liao"};
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.3 配伍表必需字段(除主键 id 外)。 */
|
|
|
|
|
+ private static final String[] REQUIRED_FIELDS = {
|
|
|
|
|
+ "foodAId", "foodBId", "relationType", "intensity",
|
|
|
|
|
+ "effectScore", "source", "evidenceRef", "enabled"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.3 配伍表必需列(除 id 外,snake_case)。 */
|
|
|
|
|
+ private static final String[] REQUIRED_COLUMNS = {
|
|
|
|
|
+ "food_a_id", "food_b_id", "relation_type", "intensity",
|
|
|
|
|
+ "effect_score", "source", "evidence_ref", "enabled"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final double EPS = 1e-9;
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:宿主 + 关系类型 / 来源值域 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:契约宿主 FoodPairingMatrixService 应存在(§4.3 配伍查询/效应计算)")
|
|
|
|
|
+ void hostClass_exists() {
|
|
|
|
|
+ assertNotNull(contractHost(),
|
|
|
|
|
+ CAP + " 契约宿主 FoodPairingMatrixService 应存在:§4.3 要求 A×B 配伍查询、"
|
|
|
|
|
+ + "effect_score 计算与忌配伍判定,当前 src/main 无该服务,实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:supportedRelationTypes() 应覆盖 7 种关系类型(§4.3)")
|
|
|
|
|
+ void supportedRelationTypes_coverSevenValues() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<String> types = callListString(host, "supportedRelationTypes");
|
|
|
|
|
+ assertNotNull(types, CAP + " supportedRelationTypes() 应返回关系类型清单(§4.3)。");
|
|
|
|
|
+ Set<String> actual = new HashSet<String>(types);
|
|
|
|
|
+ Set<String> expected = new HashSet<String>(Arrays.asList(RELATION_TYPES));
|
|
|
|
|
+ assertEquals(expected, actual,
|
|
|
|
|
+ CAP + " 关系类型应恰好覆盖 7 种(§4.3):synergy/antagonism/xiangsheng/xiangke/"
|
|
|
|
|
+ + "xiangwei/xiangsha/xiangwu,实际=" + actual);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:isValidRelationType 应接受 7 种、拒绝未知/空值(§4.3)")
|
|
|
|
|
+ void isValidRelationType_acceptsSeven_rejectsUnknownAndNull() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ for (String type : RELATION_TYPES) {
|
|
|
|
|
+ assertTrue(callBoolean(host, "isValidRelationType", type),
|
|
|
|
|
+ CAP + " 合法关系类型 " + type + " 应被接受(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidRelationType", "unknown"),
|
|
|
|
|
+ CAP + " 未知关系类型 unknown 应被拒绝(§4.3 值域仅 7 种)。");
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidRelationType", (Object) null),
|
|
|
|
|
+ CAP + " null 关系类型应被拒绝,不得抛异常或误判为合法(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-4:supportedSources() 应覆盖 2 种权威来源(§4.3)")
|
|
|
|
|
+ void supportedSources_coverTwoValues() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<String> sources = callListString(host, "supportedSources");
|
|
|
|
|
+ assertNotNull(sources, CAP + " supportedSources() 应返回来源清单(§4.3)。");
|
|
|
|
|
+ Set<String> actual = new HashSet<String>(sources);
|
|
|
|
|
+ Set<String> expected = new HashSet<String>(Arrays.asList(SOURCES));
|
|
|
|
|
+ assertEquals(expected, actual,
|
|
|
|
|
+ CAP + " 来源应恰好覆盖 2 种(§4.3):modern_nutrition / tcm_shi_liao,实际=" + actual);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-5:isValidSource 应接受 2 种来源、拒绝未知/空值(§4.3 禁止无标注混用)")
|
|
|
|
|
+ void isValidSource_acceptsTwo_rejectsUnknownAndNull() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ for (String source : SOURCES) {
|
|
|
|
|
+ assertTrue(callBoolean(host, "isValidSource", source),
|
|
|
|
|
+ CAP + " 合法来源 " + source + " 应被接受(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidSource", "unknown"),
|
|
|
|
|
+ CAP + " 未知来源 unknown 应被拒绝,禁止混用无标注数据(§4.3)。");
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidSource", (Object) null),
|
|
|
|
|
+ CAP + " null 来源应被拒绝(§4.3 source 必须标注现代营养学或中医药食同源)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 默认值/边界流(核心 RED) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-1:未配置配伍规则 → 无调整 effect_score=0(§4.3 / G-3)")
|
|
|
|
|
+ void resolvePairingEffect_unconfigured_returnsZero() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ double effect = callDouble(host, "resolvePairingEffect", 1001L, 1002L, rules);
|
|
|
|
|
+ assertEquals(0.0, effect, EPS,
|
|
|
|
|
+ CAP + " 未配置的食材 A×B → 无调整(effect_score=0,§4.3);当前默认值未实现,实际=" + effect);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-2:A×B 与 B×A 应命中同一条配伍规则(§4.3 食材 A×B 关系)")
|
|
|
|
|
+ void resolvePairingEffect_isSymmetric() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ rules.add(rule(1001L, 1002L, "antagonism", 4, -25.0, "modern_nutrition", 1));
|
|
|
|
|
+ assertEquals(-25.0, callDouble(host, "resolvePairingEffect", 1001L, 1002L, rules), EPS,
|
|
|
|
|
+ CAP + " A×B 应命中配伍效应 -25(§4.3 草酸×钙拮抗示例)。");
|
|
|
|
|
+ assertEquals(-25.0, callDouble(host, "resolvePairingEffect", 1002L, 1001L, rules), EPS,
|
|
|
|
|
+ CAP + " B×A 应命中同一条配伍规则(配伍关系对称,§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-3:enabled=0 的配伍规则不应参与效应计算(§4.3 启用开关)")
|
|
|
|
|
+ void resolvePairingEffect_ignoresDisabledRule() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ rules.add(rule(1001L, 1002L, "xiangke", 5, -50.0, "tcm_shi_liao", 0));
|
|
|
|
|
+ assertEquals(0.0, callDouble(host, "resolvePairingEffect", 1001L, 1002L, rules), EPS,
|
|
|
|
|
+ CAP + " enabled=0 的规则必须被忽略,未命中时返回 0(§4.3 enabled 启用开关)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:clampEffectScore 应截断到 [-50, +50](§4.3 effect_score 取值域)")
|
|
|
|
|
+ void clampEffectScore_boundsAreFifty() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertEquals(50.0, callDouble(host, "clampEffectScore", 80.0), EPS,
|
|
|
|
|
+ CAP + " clampEffectScore(80) 应 = 50(§4.3 上界 +50)。");
|
|
|
|
|
+ assertEquals(-50.0, callDouble(host, "clampEffectScore", -80.0), EPS,
|
|
|
|
|
+ CAP + " clampEffectScore(-80) 应 = -50(§4.3 下界 -50)。");
|
|
|
|
|
+ assertEquals(12.5, callDouble(host, "clampEffectScore", 12.5), EPS,
|
|
|
|
|
+ CAP + " 区间内值应保持原值(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("忌配伍-1:xiangke/antagonism 且 intensity≥3 → requiresAvoidance=true(§4.3 / G-3)")
|
|
|
|
|
+ void requiresAvoidance_xiangkeIntensityThree_returnsTrue() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ rules.add(rule(2001L, 2002L, "xiangke", 3, -30.0, "tcm_shi_liao", 1));
|
|
|
|
|
+ assertTrue(callBoolean(host, "requiresAvoidance", 2001L, 2002L, rules),
|
|
|
|
|
+ CAP + " 柿子+螃蟹(xiangke, intensity=3)应判定为忌配伍、需硬避让(§4.3 / CR-07)。");
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> nutrition = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ nutrition.add(rule(3001L, 3002L, "antagonism", 4, -25.0, "modern_nutrition", 1));
|
|
|
|
|
+ assertTrue(callBoolean(host, "requiresAvoidance", 3001L, 3002L, nutrition),
|
|
|
|
|
+ CAP + " 菠菜+豆腐(antagonism, intensity≥3)应判定为忌配伍、需硬避让(§4.3 / G-3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("忌配伍-2:协同/低强度/停用规则不应触发硬避让(§4.3)")
|
|
|
|
|
+ void requiresAvoidance_nonAvoidableCases_returnFalse() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> cases = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ cases.add(rule(11L, 12L, "synergy", 5, 20.0, "modern_nutrition", 1)); // 协同,高强度也不避让
|
|
|
|
|
+ cases.add(rule(21L, 22L, "antagonism", 2, -10.0, "modern_nutrition", 1)); // 拮抗但强度 < 3
|
|
|
|
|
+ cases.add(rule(31L, 32L, "xiangke", 5, -50.0, "tcm_shi_liao", 0)); // 忌配伍但已停用
|
|
|
|
|
+ for (Map<String, Object> r : cases) {
|
|
|
|
|
+ Long a = (Long) r.get("foodAId");
|
|
|
|
|
+ Long b = (Long) r.get("foodBId");
|
|
|
|
|
+ List<Map<String, Object>> one = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ one.add(r);
|
|
|
|
|
+ assertTrue(!callBoolean(host, "requiresAvoidance", a, b, one),
|
|
|
|
|
+ CAP + " relationType=" + r.get("relationType") + "/intensity=" + r.get("intensity")
|
|
|
|
|
+ + "/enabled=" + r.get("enabled") + " 不应触发硬避让(§4.3 仅忌配伍且强度≥3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("忌配伍-3:避让判定应对称(§4.3 A×B 关系双向有效)")
|
|
|
|
|
+ void requiresAvoidance_isSymmetric() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ rules.add(rule(4001L, 4002L, "antagonism", 3, -20.0, "modern_nutrition", 1));
|
|
|
|
|
+ assertTrue(callBoolean(host, "requiresAvoidance", 4001L, 4002L, rules),
|
|
|
|
|
+ CAP + " A×B 应命中忌配伍判定(§4.3)。");
|
|
|
|
|
+ assertTrue(callBoolean(host, "requiresAvoidance", 4002L, 4001L, rules),
|
|
|
|
|
+ CAP + " B×A 应命中同一条忌配伍判定(§4.3 配伍关系对称)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 结构契约:实体 / Mapper / schema / 迁移 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-1:FoodPairingMatrix 实体应含 8 个契约字段(§4.3)")
|
|
|
|
|
+ void entity_hasContractFields() {
|
|
|
|
|
+ Class<?> entity = loadClass(ENTITY_FQCN);
|
|
|
|
|
+ if (entity == null) {
|
|
|
|
|
+ fail(RED + " FoodPairingMatrix 实体不存在:§4.3/§5 要求 food_pairing_matrix 表映射实体。");
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String field : REQUIRED_FIELDS) {
|
|
|
|
|
+ assertNotNull(findField(entity, field),
|
|
|
|
|
+ CAP + " FoodPairingMatrix 缺少字段 " + field + "(§4.3 配伍表字段清单)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-2:FoodPairingMatrix 应以 @TableName 映射 food_pairing_matrix(§4.3)")
|
|
|
|
|
+ void entity_hasTableNameAnnotation() {
|
|
|
|
|
+ Class<?> entity = loadClass(ENTITY_FQCN);
|
|
|
|
|
+ if (entity == null) {
|
|
|
|
|
+ fail(RED + " FoodPairingMatrix 实体不存在,无法验证 @TableName 映射(§4.3)。");
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ boolean mapped = false;
|
|
|
|
|
+ for (Annotation a : entity.getDeclaredAnnotations()) {
|
|
|
|
|
+ if ("TableName".equals(a.annotationType().getSimpleName())) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Object value = a.annotationType().getMethod("value").invoke(a);
|
|
|
|
|
+ if (TABLE.equals(value)) {
|
|
|
|
|
+ mapped = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 反射取值失败即视为未满足契约
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(mapped, CAP + " FoodPairingMatrix 应标注 @TableName(\"" + TABLE + "\")(§4.3 表映射)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-3:FoodPairingMatrixMapper 应存在(分层 Mapper,§4.3 配伍查询)")
|
|
|
|
|
+ void mapper_exists() {
|
|
|
|
|
+ assertNotNull(loadClass(MAPPER_FQCN),
|
|
|
|
|
+ RED + " FoodPairingMatrixMapper 不存在:契约要求配伍查询走分层 Mapper"
|
|
|
|
|
+ + "(§4.3 配伍查询 + 后端分层约定);当前仅有后台 CRUD 内存服务,实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-4:schema.sql 应声明 food_pairing_matrix 建表及 8 个契约列(§4.3/§5)")
|
|
|
|
|
+ void schemaSql_declaresTableAndColumns() {
|
|
|
|
|
+ String schema = readSchemaSql();
|
|
|
|
|
+ assertTrue(schema.contains(TABLE),
|
|
|
|
|
+ CAP + " schema.sql 缺少 " + TABLE + " 建表(§4.3/§5 配伍表)。");
|
|
|
|
|
+ String block = tableBlock(schema, TABLE);
|
|
|
|
|
+ for (String col : REQUIRED_COLUMNS) {
|
|
|
|
|
+ assertTrue(block.contains(col),
|
|
|
|
|
+ CAP + " " + TABLE + " 缺少列 " + col + "(§4.3 配伍表字段定义)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-5:schema.sql 应标注 7 种关系类型、2 种来源与强度/效应取值域(§4.3)")
|
|
|
|
|
+ void schemaSql_declaresRelationTypesSourcesAndRanges() {
|
|
|
|
|
+ String block = tableBlock(readSchemaSql(), TABLE).toLowerCase();
|
|
|
|
|
+ for (String type : RELATION_TYPES) {
|
|
|
|
|
+ assertTrue(block.contains(type),
|
|
|
|
|
+ CAP + " " + TABLE + " 未标注 relation_type 合法值 " + type + "(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String source : SOURCES) {
|
|
|
|
|
+ assertTrue(block.contains(source),
|
|
|
|
|
+ CAP + " " + TABLE + " 未标注 source 合法值 " + source + "(§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(block.contains("1-5"), CAP + " " + TABLE + ".intensity 应标注取值域 1-5(§4.3)。");
|
|
|
|
|
+ assertTrue(block.contains("-50") && block.contains("50"),
|
|
|
|
|
+ CAP + " " + TABLE + ".effect_score 应标注取值域 [-50, +50](§4.3)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-6:DatabaseInitializer 应登记 food_pairing_matrix 迁移(§5)")
|
|
|
|
|
+ void migration_registeredInDatabaseInitializer() {
|
|
|
|
|
+ String src = readMainSource("config/DatabaseInitializer.java");
|
|
|
|
|
+ assertTrue(src.contains(TABLE),
|
|
|
|
|
+ CAP + " DatabaseInitializer.runMigrations() 未登记 " + TABLE + " 迁移(§5 迁移要求):"
|
|
|
|
|
+ + "契约要求 DDL 唯一入口并同步 schema.sql。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-7:推荐/食谱链路应接入配伍矩阵查询或配伍效应计算(§1.4/§4.3)")
|
|
|
|
|
+ void recommendChain_consultsPairingMatrix() {
|
|
|
|
|
+ String[] services = {
|
|
|
|
|
+ "service/FoodRecommendService.java",
|
|
|
|
|
+ "service/FoodService.java",
|
|
|
|
|
+ "service/DietRecommendationService.java"
|
|
|
|
|
+ };
|
|
|
|
|
+ boolean wired = false;
|
|
|
|
|
+ for (String path : services) {
|
|
|
|
|
+ String src = readMainSource(path).toLowerCase();
|
|
|
|
|
+ if (src.contains("foodpairingmatrix") || src.contains(TABLE)
|
|
|
|
|
+ || src.contains("pairingeffect") || src.contains("pairingmatrix")) {
|
|
|
|
|
+ wired = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(wired,
|
|
|
|
|
+ CAP + " 推荐/食谱链路未接入配伍矩阵查询(§1.4「配伍效应查询」+ §4.3"
|
|
|
|
|
+ + "「pairing_effect × w4」):当前 FoodRecommendService / FoodService /"
|
|
|
|
|
+ + " DietRecommendationService 均无引用,实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 权限流(不适用):内部查询/判定服务 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:配伍查询/避让判定为内部服务,无用户侧鉴权分支,权限流不适用")
|
|
|
|
|
+ void permissionFlow_notApplicable() {
|
|
|
|
|
+ assertTrue(true,
|
|
|
|
|
+ "权限流不适用:food.rule.pairingMatrix 为后端内部配伍查询/效应计算/避让判定服务;"
|
|
|
|
|
+ + "后台 CRUD/灰度由 ops.foodRuleManagement(CR-06)承载,"
|
|
|
|
|
+ + "食谱硬避让由 diet.menu.pairingConstraint(CR-07)承载。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造一条内存配伍规则(与 DB 解耦,供 resolvePairingEffect / requiresAvoidance 契约驱动)。 */
|
|
|
|
|
+ private Map<String, Object> rule(Long foodAId, Long foodBId, String relationType,
|
|
|
|
|
+ int intensity, double effectScore, String source, int enabled) {
|
|
|
|
|
+ Map<String, Object> r = new HashMap<String, Object>();
|
|
|
|
|
+ r.put("foodAId", foodAId);
|
|
|
|
|
+ r.put("foodBId", foodBId);
|
|
|
|
|
+ r.put("relationType", relationType);
|
|
|
|
|
+ r.put("intensity", Integer.valueOf(intensity));
|
|
|
|
|
+ r.put("effectScore", Double.valueOf(effectScore));
|
|
|
|
|
+ r.put("source", source);
|
|
|
|
|
+ r.put("evidenceRef", "ref-" + relationType);
|
|
|
|
|
+ r.put("enabled", Integer.valueOf(enabled));
|
|
|
|
|
+ return r;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Object contractHost() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class<?> clazz = Class.forName(HOST_CLASS, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ fail(RED + " 契约宿主 FoodPairingMatrixService 未实现(实现未落地):" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(RED + " 契约宿主 FoodPairingMatrixService 实例化失败:" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 按方法名 + 参数个数匹配并调用(宽松适配基本/包装类型),契约方法缺失即断言失败。 */
|
|
|
|
|
+ private Object call(Object host, String method, Object... args) {
|
|
|
|
|
+ for (Method m : host.getClass().getMethods()) {
|
|
|
|
|
+ if (m.getName().equals(method) && m.getParameterCount() == args.length) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return m.invoke(host, args);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(RED + " 契约方法 " + HOST_CLASS + "#" + method + " 调用失败:" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(RED + " 契约方法 " + HOST_CLASS + "#" + method + " 未实现");
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private double callDouble(Object host, String method, Object... args) {
|
|
|
|
|
+ Object r = call(host, method, args);
|
|
|
|
|
+ if (r instanceof Number) {
|
|
|
|
|
+ return ((Number) r).doubleValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回数值,实际 " + r);
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean callBoolean(Object host, String method, Object... args) {
|
|
|
|
|
+ Object r = call(host, method, args);
|
|
|
|
|
+ if (r instanceof Boolean) {
|
|
|
|
|
+ return ((Boolean) r).booleanValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回布尔,实际 " + r);
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private List<String> callListString(Object host, String method) {
|
|
|
|
|
+ Object r = call(host, method);
|
|
|
|
|
+ if (r instanceof List) {
|
|
|
|
|
+ return (List<String>) r;
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回 List<String>,实际 " + r);
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Field findField(Class<?> clazz, String name) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return clazz.getDeclaredField(name);
|
|
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String readSchemaSql() {
|
|
|
|
|
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream("schema.sql")) {
|
|
|
|
|
+ if (is == null) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
|
|
|
|
|
+ scanner.useDelimiter("\\A");
|
|
|
|
|
+ return scanner.hasNext() ? scanner.next() : "";
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 截取 CREATE TABLE 片段:从表名首次出现处到 ENGINE= 或结尾;表不存在返回空串 */
|
|
|
|
|
+ private String tableBlock(String schema, String table) {
|
|
|
|
|
+ int start = schema.indexOf(table);
|
|
|
|
|
+ if (start < 0) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ int end = schema.indexOf("ENGINE=", start);
|
|
|
|
|
+ if (end < 0) {
|
|
|
|
|
+ end = Math.min(schema.length(), start + 2000);
|
|
|
|
|
+ }
|
|
|
|
|
+ return schema.substring(start, end);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 读取 src/main 源码文件(surefire 工作目录 = cfc-backend 模块目录),找不到返回空串。 */
|
|
|
|
|
+ private String readMainSource(String relative) {
|
|
|
|
|
+ String[] candidates = {
|
|
|
|
|
+ "src/main/java/com/etotem/cfc/" + relative,
|
|
|
|
|
+ "src/main/resources/" + relative.replace("resources/", ""),
|
|
|
|
|
+ "cfc-backend/src/main/java/com/etotem/cfc/" + relative,
|
|
|
|
|
+ "cfc-backend/src/main/resources/" + relative.replace("resources/", ""),
|
|
|
|
|
+ "../cfc-backend/src/main/java/com/etotem/cfc/" + relative,
|
|
|
|
|
+ "../cfc-backend/src/main/resources/" + relative.replace("resources/", "")
|
|
|
|
|
+ };
|
|
|
|
|
+ for (String candidate : candidates) {
|
|
|
|
|
+ File file = new File(candidate);
|
|
|
|
|
+ if (file.isFile()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+}
|