|
@@ -0,0 +1,535 @@
|
|
|
|
|
+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.processingEffect}(S-cfc-441,TDD-RED 阶段)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code docs/需求分析/2026-11-22-食材推荐加权规则引擎需求变更.md}
|
|
|
|
|
+ * CR-04 + §4.4(加工方式影响,按食材细化)+ §5(数据结构与迁移评估)+ §8(通用兜底口径)
|
|
|
|
|
+ * + §7.1 G-3(配伍/加工验收门禁)+ §4.7-2(加工建议 processingWarning)</li>
|
|
|
|
|
+ * <li>加工效应表(§4.4):{@code food_processing_effect},字段
|
|
|
|
|
+ * {@code food_id / processing_method / nutrient_change / score_adjust / evidence_ref};
|
|
|
|
|
+ * {@code processing_method} 枚举 8 值:
|
|
|
|
|
+ * {@code raw / steamed / boiled / stir_fried / deep_fried / roasted / fermented / blanched};
|
|
|
|
|
+ * {@code nutrient_change} 为 JSON(如 {@code {"vitamin_c": "-50%", "oxalic_acid": "-60%"}});
|
|
|
|
|
+ * {@code score_adjust} 取值 [-50, +50]。</li>
|
|
|
|
|
+ * <li>规则(用户拍板 3 / §8):具体到「食材 × 加工方式」;通用加工方式规则作为**兜底默认值**
|
|
|
|
|
+ * ({@code food_id=NULL} 行),食材级覆盖通用级;**未配置的食材×方式 → 无调整(score_adjust=0)**。</li>
|
|
|
|
|
+ * <li>下游集成(§4.7-2 / G-3):{@code dish.type} 推断加工方式 → 查 {@code food_processing_effect}
|
|
|
|
|
+ * → 返回体加 {@code processingWarning}(如「油炸降低维生素 C 50%,建议改蒸煮」)。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(cfclub 源码核实)</b>:{@code src/main} 中 {@code food_processing_effect} /
|
|
|
|
|
+ * {@code FoodProcessingEffect} / {@code processingWarning} / {@code nutrient_change} 零引用,
|
|
|
|
|
+ * schema.sql 无对应建表,DatabaseInitializer 无对应迁移。故本测试以
|
|
|
|
|
+ * 「反射宿主探测 + 结构/源码锚点」定义契约:能编译通过、能运行、断言失败(红),失败原文清晰可贴;
|
|
|
|
|
+ * GREEN 实现落地后自动转绿。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约宿主(实现方按此落地)</b>:{@code com.etotem.cfc.service.FoodProcessingEffectService}
|
|
|
|
|
+ * —— 通过反射按方法名 + 参数个数探测,避免直接 import 不存在类导致编译失败:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@code List<String> supportedProcessingMethods()} —— 8 种合法加工方式</li>
|
|
|
|
|
+ * <li>{@code boolean isValidProcessingMethod(String method)} —— 加工方式值域校验</li>
|
|
|
|
|
+ * <li>{@code double resolveScoreAdjust(Long foodId, String method, List<Map<String,Object>> effects)}
|
|
|
|
|
+ * —— 食材级优先 / 通用级(foodId=NULL)兜底 / 未配置返回 0;effects 为纯内存规则集,
|
|
|
|
|
+ * 每项含 foodId(Long 或 null)/ processingMethod / scoreAdjust(Number),使契约与 DB 解耦</li>
|
|
|
|
|
+ * <li>{@code Map<String,String> parseNutrientChange(String json)} —— 解析 nutrient_change JSON</li>
|
|
|
|
|
+ * <li>{@code double clampScoreAdjust(double value)} —— 截断到 [-50, +50]</li>
|
|
|
|
|
+ * <li>{@code String processingWarning(String method, Map<String,String> nutrientChange)}
|
|
|
|
|
+ * —— 加工建议文案(如 deep_fried + vitamin_c=-50% → 「油炸降低维生素 C 50%,建议改蒸煮」)</li>
|
|
|
|
|
+ * <li>{@code String inferProcessingMethod(String dishType)} —— 由 dish.type 推断加工方式</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯 JUnit5 + 反射 + 源码文本读取(无 Spring / 无 DB / 无外部服务),与同批已合入主链的
|
|
|
|
|
+ * RED 契约测试(TeamProfitHookContractTest / TeamStarMultiplierContractTest /
|
|
|
|
|
+ * FamilyJoinValidationRulesContractTest / FoodRuleWeightEngineContractTest /
|
|
|
|
|
+ * FoodPairingMatrixContractTest)同风格。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>权限流</b>:加工效应服务为后端内部查询/计算服务(后台 CRUD 由独立能力键 ops.foodRuleManagement
|
|
|
|
|
+ * CR-06 承载,食谱集成由 diet.menu.pairingConstraint CR-07 承载),无用户侧鉴权分支,权限流不适用。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:行为用例在 {@code contractHost()} 处失败(宿主类未实现),结构用例在实体 /
|
|
|
|
|
+ * schema.sql / Mapper / 迁移 / 集成源码锚点处失败,失败原文前缀 {@code RED[food.rule.processingEffect]}。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class FoodProcessingEffectContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CAP = "[food.rule.processingEffect]";
|
|
|
|
|
+ private static final String RED = "RED[food.rule.processingEffect]";
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约宿主类(§4.4 加工效应查询服务,当前未实现)。 */
|
|
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.FoodProcessingEffectService";
|
|
|
|
|
+
|
|
|
|
|
+ /** 加工效应实体(§4.4 food_processing_effect 表,当前未实现)。 */
|
|
|
|
|
+ private static final String ENTITY_FQCN = "com.etotem.cfc.entity.FoodProcessingEffect";
|
|
|
|
|
+ private static final String MAPPER_FQCN = "com.etotem.cfc.mapper.FoodProcessingEffectMapper";
|
|
|
|
|
+ private static final String TABLE = "food_processing_effect";
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.4 processing_method 的 8 个合法枚举值 */
|
|
|
|
|
+ private static final String[] METHODS = {
|
|
|
|
|
+ "raw", "steamed", "boiled", "stir_fried",
|
|
|
|
|
+ "deep_fried", "roasted", "fermented", "blanched"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.4 加工效应表必需字段(除主键 id 外) */
|
|
|
|
|
+ private static final String[] REQUIRED_FIELDS = {
|
|
|
|
|
+ "foodId", "processingMethod", "nutrientChange", "scoreAdjust", "evidenceRef"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.4 加工效应表必需列(除 id 外,snake_case) */
|
|
|
|
|
+ private static final String[] REQUIRED_COLUMNS = {
|
|
|
|
|
+ "food_id", "processing_method", "nutrient_change", "score_adjust", "evidence_ref"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final double EPS = 1e-9;
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 正常流:宿主 + 加工方式值域 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-1:契约宿主 FoodProcessingEffectService 应存在(§4.4 加工效应查询)")
|
|
|
|
|
+ void hostClass_exists() {
|
|
|
|
|
+ assertNotNull(contractHost(),
|
|
|
|
|
+ CAP + " 契约宿主 FoodProcessingEffectService 应存在:§4.4 要求「食材 × 加工方式」"
|
|
|
|
|
+ + "加工效应查询与推荐调整,当前 src/main 零引用,实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-2:supportedProcessingMethods() 应覆盖 8 种加工方式(§4.4)")
|
|
|
|
|
+ void supportedProcessingMethods_coverEightValues() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<String> methods = callListString(host, "supportedProcessingMethods");
|
|
|
|
|
+ assertNotNull(methods, CAP + " supportedProcessingMethods() 应返回加工方式清单(§4.4)。");
|
|
|
|
|
+ Set<String> actual = new HashSet<String>(methods);
|
|
|
|
|
+ Set<String> expected = new HashSet<String>(Arrays.asList(METHODS));
|
|
|
|
|
+ assertEquals(expected, actual,
|
|
|
|
|
+ CAP + " 加工方式应恰好覆盖 8 种(§4.4):raw/steamed/boiled/stir_fried/"
|
|
|
|
|
+ + "deep_fried/roasted/fermented/blanched,实际=" + actual);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("正常流-3:isValidProcessingMethod 应接受 8 种、拒绝未知/空值(§4.4)")
|
|
|
|
|
+ void isValidProcessingMethod_acceptsEight_rejectsUnknownAndNull() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ for (String method : METHODS) {
|
|
|
|
|
+ assertTrue(callBoolean(host, "isValidProcessingMethod", method),
|
|
|
|
|
+ CAP + " 合法加工方式 " + method + " 应被接受(§4.4)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidProcessingMethod", "microwave"),
|
|
|
|
|
+ CAP + " 未知加工方式 microwave 应被拒绝(§4.4 值域仅 8 种)。");
|
|
|
|
|
+ assertTrue(!callBoolean(host, "isValidProcessingMethod", (Object) null),
|
|
|
|
|
+ CAP + " null 加工方式应被拒绝,不得抛异常或误判为合法。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 默认值/兜底契约(§4.4 / §8,核心 RED) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-1:未配置的食材×加工方式 → 无调整 score_adjust=0(§8 已知残留)")
|
|
|
|
|
+ void resolveScoreAdjust_unconfigured_returnsZero() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ // 空规则集 + 任意食材/方式:未配置 → score_adjust=0,不得产生默认扣分或抛异常
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "blanched", effects);
|
|
|
|
|
+ assertEquals(0.0, adjust, EPS,
|
|
|
|
|
+ CAP + " 未配置的食材×方式 → 无调整(score_adjust=0,§8);当前默认值未实现,实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-2:食材级规则应覆盖通用级(food_id=NULL 兜底,§4.4)")
|
|
|
|
|
+ void resolveScoreAdjust_foodLevelOverridesGeneric() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ effects.add(effect(null, "blanched", -5.0)); // 通用级兜底
|
|
|
|
|
+ effects.add(effect(1001L, "blanched", -30.0)); // 食材级(菠菜焯水去草酸)覆盖
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "blanched", effects);
|
|
|
|
|
+ assertEquals(-30.0, adjust, EPS,
|
|
|
|
|
+ CAP + " 食材级规则应优先于通用级(food_id=NULL 兜底,§4.4),实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-3:食材无对应方式规则时回落通用级兜底(§4.4)")
|
|
|
|
|
+ void resolveScoreAdjust_genericFallbackWhenFoodMethodMissing() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ effects.add(effect(null, "deep_fried", -20.0)); // 通用级:油炸降维 C
|
|
|
|
|
+ effects.add(effect(1001L, "steamed", 5.0)); // 食材级只有蒸制规则,无油炸规则
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "deep_fried", effects);
|
|
|
|
|
+ assertEquals(-20.0, adjust, EPS,
|
|
|
|
|
+ CAP + " 食材级未命中该方式时应回落通用级(food_id=NULL,§4.4),实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("默认值-4:foodId=null 时应取通用级规则(§4.4 food_id=NULL 行)")
|
|
|
|
|
+ void resolveScoreAdjust_nullFoodIdUsesGeneric() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ effects.add(effect(null, "steamed", 8.0));
|
|
|
|
|
+ effects.add(effect(1001L, "steamed", -8.0));
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", null, "steamed", effects);
|
|
|
|
|
+ assertEquals(8.0, adjust, EPS,
|
|
|
|
|
+ CAP + " foodId=null 时应取通用级(food_id=NULL)规则(§4.4),实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界流:nutrient_change 解析 + score_adjust 截断 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-1:parseNutrientChange 应解析 JSON 营养素变化(§4.4)")
|
|
|
|
|
+ void parseNutrientChange_parsesJson() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ String json = "{\"vitamin_c\": \"-50%\", \"oxalic_acid\": \"-60%\"}";
|
|
|
|
|
+ Map<String, String> parsed = callMapStringString(host, "parseNutrientChange", json);
|
|
|
|
|
+ assertNotNull(parsed, CAP + " parseNutrientChange() 应返回营养素变化映射(§4.4)。");
|
|
|
|
|
+ assertEquals("-50%", parsed.get("vitamin_c"),
|
|
|
|
|
+ CAP + " 应解析 vitamin_c=-50%(§4.4 示例:油炸降维生素 C 50%)。");
|
|
|
|
|
+ assertEquals("-60%", parsed.get("oxalic_acid"),
|
|
|
|
|
+ CAP + " 应解析 oxalic_acid=-60%(§4.4 示例:菠菜焯水去草酸 60%)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-2:clampScoreAdjust 上界 +50(§4.4 score_adjust -50…+50)")
|
|
|
|
|
+ void clampScoreAdjust_upperBoundIsFifty() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertEquals(50.0, callDouble(host, "clampScoreAdjust", 80.0), EPS,
|
|
|
|
|
+ CAP + " clampScoreAdjust(80) 应 = 50(§4.4 上界 +50)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-3:clampScoreAdjust 下界 -50(§4.4 score_adjust -50…+50)")
|
|
|
|
|
+ void clampScoreAdjust_lowerBoundIsMinusFifty() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertEquals(-50.0, callDouble(host, "clampScoreAdjust", -80.0), EPS,
|
|
|
|
|
+ CAP + " clampScoreAdjust(-80) 应 = -50(§4.4 下界 -50)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界流-4:clampScoreAdjust 区间内值不变(§4.4)")
|
|
|
|
|
+ void clampScoreAdjust_inRangeUnchanged() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertEquals(-30.0, callDouble(host, "clampScoreAdjust", -30.0), EPS,
|
|
|
|
|
+ CAP + " clampScoreAdjust(-30) 应保持原值。");
|
|
|
|
|
+ assertEquals(12.5, callDouble(host, "clampScoreAdjust", 12.5), EPS,
|
|
|
|
|
+ CAP + " clampScoreAdjust(12.5) 应保持原值。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 加工建议 + dish.type 推断(§4.7-2 / G-3) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("集成-1:deep_fried 加工建议应含「建议改蒸煮」与降幅(§4.7-2 / G-3)")
|
|
|
|
|
+ void processingWarning_deepFried_suggestsSteaming() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ Map<String, String> nutrient = new HashMap<String, String>();
|
|
|
|
|
+ nutrient.put("vitamin_c", "-50%");
|
|
|
|
|
+ String warning = callString(host, "processingWarning", "deep_fried", nutrient);
|
|
|
|
|
+ assertNotNull(warning, CAP + " deep_fried 应返回 processingWarning(§4.7-2 / G-3)。");
|
|
|
|
|
+ assertTrue(warning.contains("建议改蒸煮"),
|
|
|
|
|
+ CAP + " 油炸建议应提示改蒸煮(§4.7-2 示例「油炸降低维生素 C 50%,建议改蒸煮」),实际=" + warning);
|
|
|
|
|
+ assertTrue(warning.contains("50%"),
|
|
|
|
|
+ CAP + " 油炸建议应带出营养素降幅 50%(§4.7-2 / G-3),实际=" + warning);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("集成-2:inferProcessingMethod 应由 dish.type 推断加工方式(§4.7-2)")
|
|
|
|
|
+ void inferProcessingMethod_fromDishType() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertEquals("deep_fried", callString(host, "inferProcessingMethod", "油炸"),
|
|
|
|
|
+ CAP + " dish.type「油炸」应推断为 deep_fried(§4.7-2)。");
|
|
|
|
|
+ assertEquals("steamed", callString(host, "inferProcessingMethod", "清蒸"),
|
|
|
|
|
+ CAP + " dish.type「清蒸」应推断为 steamed(§4.7-2)。");
|
|
|
|
|
+ assertEquals("blanched", callString(host, "inferProcessingMethod", "焯水"),
|
|
|
|
|
+ CAP + " dish.type「焯水」应推断为 blanched(§4.7-2 / §4.4 菠菜焯水)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 结构契约:实体 / Mapper / schema / 迁移(§4.4 / §5) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-1:FoodProcessingEffect 实体应存在并含 5 个契约字段(§4.4)")
|
|
|
|
|
+ void entity_hasContractFields() {
|
|
|
|
|
+ Class<?> entity = loadClass(ENTITY_FQCN);
|
|
|
|
|
+ if (entity == null) {
|
|
|
|
|
+ fail(RED + " FoodProcessingEffect 实体不存在:§4.4/§5 要求 food_processing_effect 表映射实体"
|
|
|
|
|
+ + "(food_id/processing_method/nutrient_change/score_adjust/evidence_ref)。");
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String field : REQUIRED_FIELDS) {
|
|
|
|
|
+ assertNotNull(findField(entity, field),
|
|
|
|
|
+ CAP + " FoodProcessingEffect 缺少字段 " + field + "(§4.4 加工效应表字段清单)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-2:FoodProcessingEffect 应以 @TableName 映射 food_processing_effect(§4.4)")
|
|
|
|
|
+ void entity_hasTableNameAnnotation() {
|
|
|
|
|
+ Class<?> entity = loadClass(ENTITY_FQCN);
|
|
|
|
|
+ if (entity == null) {
|
|
|
|
|
+ fail(RED + " FoodProcessingEffect 实体不存在,无法验证 @TableName 映射(§4.4)。");
|
|
|
|
|
+ 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 + " FoodProcessingEffect 应标注 @TableName(\"" + TABLE + "\")(§4.4 表映射)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-3:FoodProcessingEffectMapper 应存在(分层 Mapper,§4.4 加工效应查询)")
|
|
|
|
|
+ void mapper_exists() {
|
|
|
|
|
+ assertNotNull(loadClass(MAPPER_FQCN),
|
|
|
|
|
+ CAP + " FoodProcessingEffectMapper 不存在:契约要求加工效应查询走分层 Mapper"
|
|
|
|
|
+ + "(§4.4 加工效应查询 + 后端分层约定),实现未落地。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-4:schema.sql 应声明 food_processing_effect 建表及 5 个契约列(§4.4/§5)")
|
|
|
|
|
+ void schemaSql_declaresTableAndColumns() {
|
|
|
|
|
+ String schema = readSchemaSql();
|
|
|
|
|
+ assertTrue(schema.contains(TABLE),
|
|
|
|
|
+ CAP + " schema.sql 缺少 " + TABLE + " 建表(§4.4/§5 加工效应表)。");
|
|
|
|
|
+ String block = tableBlock(schema, TABLE);
|
|
|
|
|
+ for (String col : REQUIRED_COLUMNS) {
|
|
|
|
|
+ assertTrue(block.contains(col),
|
|
|
|
|
+ CAP + " " + TABLE + " 缺少列 " + col + "(§4.4 加工效应表字段定义)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-5:schema.sql 应标注 8 种加工方式与 score_adjust 取值域(§4.4)")
|
|
|
|
|
+ void schemaSql_declaresMethodsAndScoreRange() {
|
|
|
|
|
+ String block = tableBlock(readSchemaSql(), TABLE).toLowerCase();
|
|
|
|
|
+ for (String method : METHODS) {
|
|
|
|
|
+ assertTrue(block.contains(method),
|
|
|
|
|
+ CAP + " " + TABLE + " 未标注 processing_method 合法值 " + method + "(§4.4)。");
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(block.contains("-50") && block.contains("50"),
|
|
|
|
|
+ CAP + " " + TABLE + ".score_adjust 应标注取值域 [-50, +50](§4.4)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("结构-6:DatabaseInitializer 应登记 food_processing_effect 迁移(§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:推荐/食谱链路应接入加工效应查询或 processingWarning(§1.4/§4.7-2)")
|
|
|
|
|
+ void recommendChain_consultsProcessingEffect() {
|
|
|
|
|
+ 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("processingwarning") || src.contains("foodprocessingeffect")
|
|
|
|
|
+ || src.contains(TABLE)) {
|
|
|
|
|
+ wired = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ assertTrue(wired,
|
|
|
|
|
+ CAP + " 推荐/食谱链路未接入加工效应查询(§1.4「配伍/加工效应查询」+ §4.7-2"
|
|
|
|
|
+ + "「dish.type 推断加工方式 → 查 food_processing_effect → 加 processingWarning」):"
|
|
|
|
|
+ + "当前 FoodRecommendService / FoodService / DietRecommendationService 均无引用。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 权限流(不适用):内部查询/计算服务 ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("权限流:加工效应服务为内部查询服务,无用户侧鉴权分支,权限流不适用")
|
|
|
|
|
+ void permissionFlow_notApplicable() {
|
|
|
|
|
+ assertTrue(true,
|
|
|
|
|
+ "权限流不适用:food.rule.processingEffect 为后端内部加工效应查询/计算服务;"
|
|
|
|
|
+ + "后台 CRUD/灰度由 ops.foodRuleManagement(CR-06)承载,"
|
|
|
|
|
+ + "食谱加工建议由 diet.menu.pairingConstraint(CR-07)承载。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造一条内存规则(与 DB 解耦,供 resolveScoreAdjust 契约驱动)。 */
|
|
|
|
|
+ private Map<String, Object> effect(Long foodId, String method, double scoreAdjust) {
|
|
|
|
|
+ Map<String, Object> e = new HashMap<String, Object>();
|
|
|
|
|
+ e.put("foodId", foodId);
|
|
|
|
|
+ e.put("processingMethod", method);
|
|
|
|
|
+ e.put("scoreAdjust", scoreAdjust);
|
|
|
|
|
+ return e;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Object contractHost() {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class<?> clazz = Class.forName(HOST_CLASS, false, Thread.currentThread().getContextClassLoader());
|
|
|
|
|
+ return clazz.getDeclaredConstructor().newInstance();
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ fail(RED + " 契约宿主 FoodProcessingEffectService 未实现(实现未落地):" + e.getMessage());
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ fail(RED + " 契约宿主 FoodProcessingEffectService 实例化失败:" + 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");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String callString(Object host, String method, Object... args) {
|
|
|
|
|
+ Object r = call(host, method, args);
|
|
|
|
|
+ if (r instanceof String) {
|
|
|
|
|
+ return (String) r;
|
|
|
|
|
+ }
|
|
|
|
|
+ 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");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private Map<String, String> callMapStringString(Object host, String method, Object... args) {
|
|
|
|
|
+ Object r = call(host, method, args);
|
|
|
|
|
+ if (r instanceof Map) {
|
|
|
|
|
+ return (Map<String, String>) r;
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回 Map<String,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 "";
|
|
|
|
|
+ }
|
|
|
|
|
+}
|