package com.zksy.watersupply; import org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertTrue; /** * Gogs #53:旧 water_pipe_info 链路必须彻底退场。 * *

管网设施的权威数据源是统一表 {@code pipe_network_base}({@code network_type = '2'}); * 旧 WaterPipeInfo 模块、旧列名与指向旧表的测试数据都不得再出现在代码库中。 * 历史迁移脚本属于既成事实,按票面口径单列白名单,且白名单只允许这一个文件。

*/ class LegacyPipeTableRetirementTest { /** 历史迁移脚本:记录“曾经 ALTER 旧表”这一事实,不随旧模块一起删除。 */ private static final Set HISTORICAL_MIGRATION_ALLOWLIST = Set.of("sql/water_supply_facility_gis.sql"); /** 旧表与旧模块标识:代码、Mapper、SQL 中都不得再出现。 */ private static final List LEGACY_TABLE_TOKENS = List.of( "water_pipe_info", "WaterPipeInfo", "WaterPipeExportDTO"); /** 旧表列名:管网口径统一到 pipe_network_base 后不应再被引用。 */ private static final List LEGACY_COLUMN_TOKENS = List.of( "pipe_length", "pipe_diameter", "pipe_material", "laying_year", "pressure_rating"); private static final List SCANNED_ROOTS = List.of("zksy-admin", "zksy-system", "zksy-common", "zksy-framework", "sql"); @Test void codeAndTestDataNoLongerReferenceTheRetiredPipeTable() throws IOException { Path repoRoot = repoRoot(); List offenders = new ArrayList<>(); for (Path file : scannedFiles(repoRoot)) { String relative = repoRoot.relativize(file).toString().replace('\\', '/'); // 本测试自身声明了这些 token,不作为命中项。 if (relative.endsWith("LegacyPipeTableRetirementTest.java")) continue; if (HISTORICAL_MIGRATION_ALLOWLIST.contains(relative)) continue; String content = Files.readString(file, StandardCharsets.UTF_8); for (String token : LEGACY_TABLE_TOKENS) { if (content.contains(token)) offenders.add(relative + " -> " + token); } for (String token : LEGACY_COLUMN_TOKENS) { if (content.contains(token)) offenders.add(relative + " -> " + token); } } assertTrue(offenders.isEmpty(), "管网设施已统一到 pipe_network_base(network_type='2'),以下位置仍在引用旧表或旧列:\n" + String.join("\n", offenders)); } @Test void historicalMigrationAllowlistStaysMinimal() throws IOException { Path repoRoot = repoRoot(); List actual = Files.walk(repoRoot.resolve("sql")) .filter(Files::isRegularFile) .filter(path -> path.toString().endsWith(".sql")) .filter(path -> { try { return Files.readString(path, StandardCharsets.UTF_8).contains("water_pipe_info"); } catch (IOException e) { throw new IllegalStateException(e); } }) .map(path -> repoRoot.relativize(path).toString().replace('\\', '/')) .collect(Collectors.toList()); Collections.sort(actual); assertTrue(actual.equals(new ArrayList<>(HISTORICAL_MIGRATION_ALLOWLIST)), "仅允许历史迁移脚本保留 water_pipe_info,实际为:" + actual); } @Test void pipeTestDataIsSeededIntoTheUnifiedNetworkTable() throws IOException { Path seed = repoRoot().resolve("sql/water_supply_facility_test_data.sql"); String sql = Files.readString(seed, StandardCharsets.UTF_8); assertTrue(sql.contains("app_user.pipe_network_base"), "管网测试数据必须写入统一表 app_user.pipe_network_base"); assertTrue(sql.contains("'2'"), "写入 pipe_network_base 的管网设施必须显式标注 network_type = '2'(供水设施)"); } private static List scannedFiles(Path repoRoot) throws IOException { List files = new ArrayList<>(); for (String root : SCANNED_ROOTS) { Path start = repoRoot.resolve(root); if (!Files.exists(start)) continue; try (Stream walk = Files.walk(start)) { walk.filter(Files::isRegularFile) .filter(LegacyPipeTableRetirementTest::isScannedFile) .forEach(files::add); } } return files; } private static boolean isScannedFile(Path path) { String name = path.getFileName().toString(); if (name.endsWith(".java") || name.endsWith(".sql") || name.endsWith(".yml") || name.endsWith(".yaml")) return true; if (!name.endsWith(".xml")) return false; String normalized = path.toString().replace('\\', '/'); return normalized.contains("/resources/") || normalized.contains("/src/test/"); } private static Path repoRoot() { Path current = Paths.get(System.getProperty("user.dir")).toAbsolutePath(); while (current != null) { if (Files.exists(current.resolve("zksy-system")) && Files.exists(current.resolve("zksy-admin"))) { return current; } current = current.getParent(); } throw new IllegalStateException("无法定位 pipe-network-service 仓库根目录"); } }