|
@@ -0,0 +1,224 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+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.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>与已合入主链的 {@code FoodProcessingEffectContractTest}(21 用例:20 红 / 1 对照绿)同风格,
|
|
|
|
|
+ * 本文件只补其未覆盖的 CR-04 边界/健壮性契约,不重复既有断言:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@code parseNutrientChange(null/非法 JSON)} —— 容错返回空映射,不抛异常(§4.4 nutrient_change 为 JSON);</li>
|
|
|
|
|
+ * <li>{@code isValidProcessingMethod("")} —— 空串不在 8 值域内,应拒绝(§4.4);</li>
|
|
|
|
|
+ * <li>{@code resolveScoreAdjust(null effects)} —— 规则集为空/null 时无调整 0,不抛 NPE(§8 兜底口径);</li>
|
|
|
|
|
+ * <li>{@code resolveScoreAdjust(未知加工方式)} —— 未配置方式 → 无调整 0(§8);</li>
|
|
|
|
|
+ * <li>{@code resolveScoreAdjust} 结果恒在 [-50, +50](§4.4 score_adjust 值域不变量);</li>
|
|
|
|
|
+ * <li>{@code inferProcessingMethod(未知 dish.type)} —— 不抛异常、不返回非法方式(§4.7-2);</li>
|
|
|
|
|
+ * <li>{@code processingWarning(blanched, oxalic_acid=-60%)} —— 带出降幅并提示草酸(§4.4 菠菜焯水示例)。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状</b>:{@code src/main} 中 {@code food_processing_effect} / {@code FoodProcessingEffectService}
|
|
|
|
|
+ * 零引用,schema.sql 无建表,DatabaseInitializer 无迁移。故沿用「反射宿主探测」定义契约:
|
|
|
|
|
+ * 能编译、能运行、断言失败(红),GREEN 落地后自动转绿。纯 JUnit5 + 反射,无 Spring / DB / 外部服务。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class FoodProcessingEffectEdgeCasesContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String CAP = "[food.rule.processingEffect]";
|
|
|
|
|
+ private static final String RED = "RED[food.rule.processingEffect]";
|
|
|
|
|
+ private static final String HOST_CLASS = "com.etotem.cfc.service.FoodProcessingEffectService";
|
|
|
|
|
+
|
|
|
|
|
+ private static final String[] METHODS = {
|
|
|
|
|
+ "raw", "steamed", "boiled", "stir_fried",
|
|
|
|
|
+ "deep_fried", "roasted", "fermented", "blanched"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ private static final double EPS = 1e-9;
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 边界/健壮性(§4.4 / §8,核心 RED) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-1:parseNutrientChange(null) 应容错返回空映射(§4.4 JSON 字段)")
|
|
|
|
|
+ void parseNutrientChange_nullJson_returnsEmptyMap() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ Map<String, String> parsed = callMapStringString(host, "parseNutrientChange", (Object) null);
|
|
|
|
|
+ assertNotNull(parsed, CAP + " parseNutrientChange(null) 应返回空映射而非 null(§4.4 容错)。");
|
|
|
|
|
+ assertTrue(parsed.isEmpty(),
|
|
|
|
|
+ CAP + " parseNutrientChange(null) 应返回空映射,实际=" + parsed);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-2:parseNutrientChange(非法 JSON) 不得抛异常(§4.4)")
|
|
|
|
|
+ void parseNutrientChange_malformedJson_doesNotThrow() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ Map<String, String> parsed = callMapStringString(host, "parseNutrientChange", "not-a-json");
|
|
|
|
|
+ assertNotNull(parsed, CAP + " 非法 nutrient_change JSON 应容错返回映射(§4.4)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-3:isValidProcessingMethod(\"\") 应拒绝空串(§4.4 值域仅 8 种)")
|
|
|
|
|
+ void isValidProcessingMethod_emptyString_rejected() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ assertFalse(callBoolean(host, "isValidProcessingMethod", ""),
|
|
|
|
|
+ CAP + " 空串加工方式应被拒绝,不得判为合法(§4.4)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-4:resolveScoreAdjust(null effects) 应返回 0 且不抛 NPE(§8 兜底)")
|
|
|
|
|
+ void resolveScoreAdjust_nullEffects_returnsZero() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "steamed", (Object) null);
|
|
|
|
|
+ assertEquals(0.0, adjust, EPS,
|
|
|
|
|
+ CAP + " 规则集为 null → 未配置 → 无调整 0(§8),实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-5:未知加工方式未配置 → 无调整 0(§8 兜底口径)")
|
|
|
|
|
+ void resolveScoreAdjust_unknownMethod_returnsZero() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ effects.add(effect(null, "steamed", 10.0));
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "microwave", effects);
|
|
|
|
|
+ assertEquals(0.0, adjust, EPS,
|
|
|
|
|
+ CAP + " 未知方式 microwave 无规则 → 无调整 0(§8),实际=" + adjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-6:resolveScoreAdjust 结果恒在 [-50,+50](§4.4 score_adjust 值域不变量)")
|
|
|
|
|
+ void resolveScoreAdjust_resultWithinContractRange() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ List<Map<String, Object>> effects = new ArrayList<Map<String, Object>>();
|
|
|
|
|
+ effects.add(effect(1001L, "deep_fried", 80.0));
|
|
|
|
|
+ double adjust = callDouble(host, "resolveScoreAdjust", 1001L, "deep_fried", effects);
|
|
|
|
|
+ assertTrue(adjust >= -50.0 && adjust <= 50.0,
|
|
|
|
|
+ CAP + " resolveScoreAdjust 结果应落在 [-50,+50](§4.4),实际=" + adjust);
|
|
|
|
|
+ effects.clear();
|
|
|
|
|
+ effects.add(effect(1001L, "deep_fried", -80.0));
|
|
|
|
|
+ double lower = callDouble(host, "resolveScoreAdjust", 1001L, "deep_fried", effects);
|
|
|
|
|
+ assertTrue(lower >= -50.0 && lower <= 50.0,
|
|
|
|
|
+ CAP + " resolveScoreAdjust 结果应落在 [-50,+50](§4.4),实际=" + lower);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-7:inferProcessingMethod(未知 dish.type) 不抛异常且不返回非法方式(§4.7-2)")
|
|
|
|
|
+ void inferProcessingMethod_unknownDishType_noThrow() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ Object result = callRaw(host, "inferProcessingMethod", "未知菜型");
|
|
|
|
|
+ if (result instanceof String) {
|
|
|
|
|
+ assertTrue(Arrays.asList(METHODS).contains(result),
|
|
|
|
|
+ CAP + " 未知 dish.type 若返回值,必须是 8 种合法加工方式之一(§4.4),实际=" + result);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 返回 null(无法推断)或被拒均视为合法容错;关键约束是不得抛异常。
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("边界-8:blanched + oxalic_acid=-60% 的 processingWarning 应带出降幅(§4.4 / §4.7-2)")
|
|
|
|
|
+ void processingWarning_blanchedMentionsOxalicAcidReduction() {
|
|
|
|
|
+ Object host = contractHost();
|
|
|
|
|
+ Map<String, String> nutrient = new HashMap<String, String>();
|
|
|
|
|
+ nutrient.put("oxalic_acid", "-60%");
|
|
|
|
|
+ String warning = callString(host, "processingWarning", "blanched", nutrient);
|
|
|
|
|
+ assertNotNull(warning, CAP + " blanched 应返回 processingWarning(§4.7-2)。");
|
|
|
|
|
+ assertFalse(warning.isEmpty(), CAP + " processingWarning 不应为空(§4.7-2)。");
|
|
|
|
|
+ assertTrue(warning.contains("60%"),
|
|
|
|
|
+ CAP + " 焯水加工建议应带出草酸降幅 60%(§4.4 菠菜焯水示例),实际=" + warning);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 对照(证明测试基建可用,非契约断言) ====================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("对照:测试基建自检(常量与反射工具可用)")
|
|
|
|
|
+ void contractInfrastructure_controlGreen() {
|
|
|
|
|
+ assertEquals(8, METHODS.length, "8 种加工方式常量应完整(§4.4)。");
|
|
|
|
|
+ assertNotNull(HOST_CLASS, "契约宿主类名应可读。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ 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 callRaw(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 = callRaw(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 = callRaw(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 = callRaw(host, method, args);
|
|
|
|
|
+ if (r instanceof String) {
|
|
|
|
|
+ return (String) r;
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回字符串,实际 " + r);
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private Map<String, String> callMapStringString(Object host, String method, Object... args) {
|
|
|
|
|
+ Object r = callRaw(host, method, args);
|
|
|
|
|
+ if (r instanceof Map) {
|
|
|
|
|
+ return (Map<String, String>) r;
|
|
|
|
|
+ }
|
|
|
|
|
+ fail(CAP + " 契约方法 " + method + " 应返回 Map<String,String>,实际 " + r);
|
|
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|