|
|
@@ -0,0 +1,297 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.controller.HealthReportController;
|
|
|
+import com.etotem.cfc.dto.ParsedReportResult;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+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 health.report.abnormalSpecies},TDD-RED 派发 S-cfc-418)。
|
|
|
+ *
|
|
|
+ * <p><b>契约锚点</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-22(报告解析预览:入库前核对菌种明细);</li>
|
|
|
+ * <li>设计:{@code docs/浠艾福程序-谷禾新版解析与五维归因升级方案-v1.0.md} §三 P0-6(异常菌清单接口 ▲ 直出)
|
|
|
+ * 与 §六 6.3(新增接口),设计契约 v1.0 已并入 origin/cfclub(ef8eb62be)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>契约正文(P0-6)</b>:</p>
|
|
|
+ * <blockquote>
|
|
|
+ * 目标:让前端能一键展示「你的报告有 12 条 ▲ 超标菌」,不用前端遍历 payload。
|
|
|
+ * 新增接口:{@code GET /api/health/report/abnormal-species?reportId=…}
|
|
|
+ * 响应:{@code [{speciesNo, cnName, latinName, abundance, refRange, direction, category, populationLevel}]},
|
|
|
+ * 按 {@code direction} 排序(▲ 优先)。
|
|
|
+ * <b>口径约束</b>:响应里附 {@code disclaimer}(关联非因果、测评 ≠ 诊断),供前端统一展示。
|
|
|
+ * </blockquote>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito/反射结构契约探测(无 Spring / 无 DB / 无外部服务),
|
|
|
+ * 与同批 RED 契约测试(TeamProfitHook / TeamStarMultiplier / TeamProfitPipeline / FamilyJoinValidationRules)风格一致。</p>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(编排侧转登记用)</b>:本分支 origin/cfclub src/main <b>未落地</b>异常菌能力——
|
|
|
+ * {@code HealthReportController} 仅有 {@code /report/body-abnormal}(身体异常指标,与异常菌无关),
|
|
|
+ * 无 {@code abnormal-species} 端点;{@code ParsedReportResult} 无 {@code abnormalSpecies}/{@code speciesLibrary} 字段;
|
|
|
+ * {@code ParsedAbnormalSpecies} DTO 类不存在;schema.sql 无 {@code health_species_library}/{@code health_abnormal_species} 建表。
|
|
|
+ * 因此本测试以「反射结构契约」探测实现缺失:能编译通过、能运行、断言失败(红),失败断言原文清晰可贴。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 五处缺口(GREEN 升级指引,实现者根据设计契约落地后自动转绿)</b>:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li><b>RED-1(端点未落地)</b>:{@code HealthReportController} 应暴露路径含 {@code abnormal-species}
|
|
|
+ * 的端点方法(设计契约动词为 GET,本仓统一 POST 约定可在实现时权衡,测试仅锚定路径契约)。</li>
|
|
|
+ * <li><b>RED-2(条目模型缺失)</b>:设计 P0-4 DTO 扩展明确 {@code ParsedAbnormalSpecies}(含
|
|
|
+ * speciesNo/cnName/latinName/abundance/refRange/direction/category/populationLevel 八字段,P0-6 响应字段)。</li>
|
|
|
+ * <li><b>RED-3(解析结果扩展缺失)</b>:P0-4 要求 {@code ParsedReportResult} 新增
|
|
|
+ * {@code List<ParsedSpeciesLibrary> speciesLibrary} 与 {@code List<ParsedAbnormalSpecies> abnormalSpecies}。</li>
|
|
|
+ * <li><b>RED-4(按 direction 排序缺失)</b>:P0-6 要求响应按 direction 排序(▲ 优先);服务层应提供
|
|
|
+ * 异常菌清单查询/排序能力(方法名含 abnormal + species 语义,返回 {@code List})。</li>
|
|
|
+ * <li><b>RED-5(disclaimer 口径缺失)</b>:P0-6 口径约束要求响应附 {@code disclaimer}(关联非因果、测评 ≠ 诊断)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p>RED 预期:RED-1 ~ RED-5 五个用例在实现补齐前应失败(红);
|
|
|
+ * {@code control}(类/现有字段探针)预期绿,用于证明测试探针本身接通、区分「实现缺失」与「探针未接通」。</p>
|
|
|
+ */
|
|
|
+class HealthReportAbnormalSpeciesContractTest {
|
|
|
+
|
|
|
+ /** P0-6 契约端点路径片段(注解 value 需含此片段) */
|
|
|
+ private static final String ENDPOINT_PATH_FRAGMENT = "abnormal-species";
|
|
|
+
|
|
|
+ /** P0-6 响应条目模型(P0-4 DTO 扩展) */
|
|
|
+ private static final String PARSED_ABNORMAL_SPECIES = "com.etotem.cfc.dto.ParsedAbnormalSpecies";
|
|
|
+
|
|
|
+ /** P0-4 DTO 扩展:物种功能库条目模型 */
|
|
|
+ private static final String PARSED_SPECIES_LIBRARY = "com.etotem.cfc.dto.ParsedSpeciesLibrary";
|
|
|
+
|
|
|
+ /** P0-6 契约条目字段(speciesNo/cnName/latinName/abundance/refRange/direction/category/populationLevel) */
|
|
|
+ private static final String[] ABNORMAL_SPECIES_FIELDS = {
|
|
|
+ "speciesNo", "cnName", "latinName", "abundance", "refRange", "direction", "category", "populationLevel"
|
|
|
+ };
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // RED-1:P0-6 异常菌清单端点未落地
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-1 (P0-6):HealthReportController 应暴露路径含 abnormal-species 的端点")
|
|
|
+ void controller_exposesAbnormalSpeciesEndpoint() {
|
|
|
+ Method endpoint = findEndpointMethod(HealthReportController.class, ENDPOINT_PATH_FRAGMENT);
|
|
|
+ assertNotNull(endpoint,
|
|
|
+ "[health.report.abnormalSpecies] RED:HealthReportController 未暴露 /api/health/report/abnormal-species 端点"
|
|
|
+ + "(P0-6:让前端一键展示「▲ 超标菌」清单,设计契约动词 GET,实现未落地)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // RED-2:P0-4/P0-6 条目模型 ParsedAbnormalSpecies 缺失
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-2 (P0-4/P0-6):ParsedAbnormalSpecies 模型应存在并含八字段(含 populationLevel)")
|
|
|
+ void parsedAbnormalSpecies_modelExistsWithContractFields() {
|
|
|
+ Class<?> clazz = loadClass(PARSED_ABNORMAL_SPECIES);
|
|
|
+ assertNotNull(clazz,
|
|
|
+ "[health.report.abnormalSpecies] RED:ParsedAbnormalSpecies DTO 类不存在(P0-4 DTO 扩展),"
|
|
|
+ + "异常菌清单接口的条目模型未落地。");
|
|
|
+
|
|
|
+ for (String field : ABNORMAL_SPECIES_FIELDS) {
|
|
|
+ boolean found = false;
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(field)) {
|
|
|
+ found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(found,
|
|
|
+ "[health.report.abnormalSpecies] RED:ParsedAbnormalSpecies 缺契约字段 " + field
|
|
|
+ + "(P0-6 响应字段 speciesNo/cnName/latinName/abundance/refRange/direction/"
|
|
|
+ + "category/populationLevel)。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // RED-3:P0-4 ParsedReportResult 扩展缺失
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-3 (P0-4):ParsedReportResult 应新增 speciesLibrary 与 abnormalSpecies 两个 List 字段")
|
|
|
+ void parsedReportResult_exposesAbnormalSpeciesAndSpeciesLibrary() {
|
|
|
+ assertTrue(hasField(ParsedReportResult.class, "abnormalSpecies"),
|
|
|
+ "[health.report.abnormalSpecies] RED:ParsedReportResult 缺 List<ParsedAbnormalSpecies> abnormalSpecies"
|
|
|
+ + " 字段(P0-4:由 Python 解析结果反序列化)。");
|
|
|
+ assertTrue(hasField(ParsedReportResult.class, "speciesLibrary"),
|
|
|
+ "[health.report.abnormalSpecies] RED:ParsedReportResult 缺 List<ParsedSpeciesLibrary> speciesLibrary"
|
|
|
+ + " 字段(P0-4:物种功能库 76 条全量)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // RED-4:P0-6 按 direction 排序(▲ 优先)能力缺失
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-4 (P0-6):服务层应提供异常菌清单查询/排序能力(返回 List,语义含 abnormal+species)")
|
|
|
+ void service_listsAbnormalSpeciesSortedByDirection() {
|
|
|
+ Method query = findServiceAbnormalSpeciesQueryMethod(
|
|
|
+ "com.etotem.cfc.service.HealthReportService");
|
|
|
+ assertNotNull(query,
|
|
|
+ "[health.report.abnormalSpecies] RED:未找到异常菌清单查询/排序能力"
|
|
|
+ + "(返回 List 且方法名语义含 abnormal + species):P0-6 要求响应按 direction 排序(▲ 优先),"
|
|
|
+ + "当前实现未落地,缺排序载体。注意:getBodyAbnormalIndicators(身体异常指标)不含 species 语义,不满足契约。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // RED-5:P0-6 disclaimer 口径约束缺失
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-5 (P0-6):异常菌清单端点响应应附 disclaimer(关联非因果、测评 ≠ 诊断)")
|
|
|
+ void response_carriesDisclaimerRedline() {
|
|
|
+ Method endpoint = findEndpointMethod(HealthReportController.class, ENDPOINT_PATH_FRAGMENT);
|
|
|
+ assertNotNull(endpoint,
|
|
|
+ "[health.report.abnormalSpecies] RED:abnormal-species 端点未落地,契约要求响应附 disclaimer"
|
|
|
+ + "(关联非因果、测评 ≠ 诊断,P0-6 口径约束),实现未落地该口径无从挂载。");
|
|
|
+ // GREEN 阶段:端点落地后,本用例需进一步验证响应结构含 disclaimer(实现者按 P0-6 口径落地)。
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // control:探针接通对照(预期绿,区分「实现缺失」与「探针未接通」)
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("control-1:HealthReportController 类 + 类级 /api/health 前缀应存在(探针接通)")
|
|
|
+ void controller_classAndBasePathExists() {
|
|
|
+ Class<?> clazz = HealthReportController.class;
|
|
|
+ assertNotNull(clazz, "control:HealthReportController 类应存在于编译类路径(探针接通)");
|
|
|
+ RequestMapping rm = clazz.getAnnotation(RequestMapping.class);
|
|
|
+ assertNotNull(rm, "control:HealthReportController 应声明类级 @RequestMapping 前缀");
|
|
|
+ boolean hasHealthPrefix = false;
|
|
|
+ for (String v : rm.value()) {
|
|
|
+ if (v.contains("/api/health")) {
|
|
|
+ hasHealthPrefix = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(hasHealthPrefix,
|
|
|
+ "control:类级 @RequestMapping 前缀应含 /api/health(异常菌端点挂载于此域,abc-normal-species 全路径)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("control-2:ParsedReportResult 类 + 现有字段探针应可反射(区分 RED-3 与探针故障)")
|
|
|
+ void parsedReportResult_classAndExistingFieldReflectable() {
|
|
|
+ Class<?> clazz = ParsedReportResult.class;
|
|
|
+ assertNotNull(clazz, "control:ParsedReportResult 类应存在于编译类路径(探针接通)");
|
|
|
+ assertTrue(hasField(clazz, "gutFlora"),
|
|
|
+ "control:既有字段 gutFlora 应可反射命中(证明字段探测探针接通,RED-3 缺失的是 abnormalSpecies/speciesLibrary 而非探针)");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // schema.sql 契约:P0-4 表结构来源(附属于接口契约的数据层)
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("RED-6 (P0-4):schema.sql 应声明 health_species_library 与 health_abnormal_species 数据来源")
|
|
|
+ void schemaSql_declaresAbnormalSpeciesSources() {
|
|
|
+ String schema = readSchemaSql();
|
|
|
+ assertTrue(schema.contains("health_species_library"),
|
|
|
+ "[health.report.abnormalSpecies] RED:schema.sql 缺 health_species_library 建表"
|
|
|
+ + "(P0-4:物种功能库 76 条/份)。");
|
|
|
+ assertTrue(schema.contains("health_abnormal_species"),
|
|
|
+ "[health.report.abnormalSpecies] RED:schema.sql 缺 health_abnormal_species"
|
|
|
+ + "(P0-4:视图,direction IS NOT NULL 派生 ▲▼ 条目)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // 反射辅助
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ /** 在目标类方法注解(GetMapping/PostMapping/Mapping)中查找路径含指定片段的方法。 */
|
|
|
+ private Method findEndpointMethod(Class<?> clazz, String pathFragment) {
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ GetMapping gm = m.getAnnotation(GetMapping.class);
|
|
|
+ if (gm != null && containsPath(gm.value(), pathFragment)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ PostMapping pm = m.getAnnotation(PostMapping.class);
|
|
|
+ if (pm != null && containsPath(pm.value(), pathFragment)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ RequestMapping rm = m.getAnnotation(RequestMapping.class);
|
|
|
+ if (rm != null && containsPath(rm.value(), pathFragment)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean containsPath(String[] values, String pathFragment) {
|
|
|
+ if (values == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (String v : values) {
|
|
|
+ if (v != null && v.contains(pathFragment)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 服务层异常菌查询/排序方法(宽松语义匹配,避免硬编码方法名:方法名含 abnormal+species 语义且返回 List)。 */
|
|
|
+ private Method findServiceAbnormalSpeciesQueryMethod(String fqcn) {
|
|
|
+ Class<?> clazz = loadClass(fqcn);
|
|
|
+ if (clazz == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ String name = m.getName().toLowerCase();
|
|
|
+ if ((name.contains("abnormal") && name.contains("species"))
|
|
|
+ && List.class.isAssignableFrom(m.getReturnType())) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasField(Class<?> clazz, String fieldName) {
|
|
|
+ if (clazz == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(fieldName)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException 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 "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|