97 lines
3.7 KiB
Java
97 lines
3.7 KiB
Java
package com.nanri.aiimage.testutil;
|
||
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||
|
||
import java.io.IOException;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Path;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 快照对比工具(task-217,module 05 各 Service 拆分前后响应一致性回归用)。
|
||
*
|
||
* 录制:把"输入→输出"对象序列化为规范 JSON(Map 按 key 排序)存 golden 文件;
|
||
* 对比:对重构后的实际输出做同一规范化并与 golden 逐行对比,输出可读 diff。
|
||
* 纯只读工具,无副作用;供拆分回归测试使用。
|
||
*/
|
||
public final class ResponseSnapshotTool {
|
||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||
|
||
static {
|
||
MAPPER.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
|
||
}
|
||
|
||
private ResponseSnapshotTool() {
|
||
}
|
||
|
||
/** 规范 JSON:Map key 排序、pretty print,用于稳定快照。 */
|
||
public static String canonical(Object value) {
|
||
if (value == null) {
|
||
return "null";
|
||
}
|
||
try {
|
||
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(value);
|
||
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
|
||
throw new IllegalArgumentException("快照序列化失败: " + e.getMessage(), e);
|
||
}
|
||
}
|
||
|
||
/** 两个对象规范化后是否一致(忽略字段顺序)。 */
|
||
public static boolean equalsJson(Object expected, Object actual) {
|
||
return canonical(expected).equals(canonical(actual));
|
||
}
|
||
|
||
/** 录制 golden 到文件(父目录自动创建)。 */
|
||
public static void saveGolden(Path dir, String name, Object output) throws IOException {
|
||
Path file = dir.resolve(name + ".json");
|
||
Files.createDirectories(dir);
|
||
Files.writeString(file, canonical(output));
|
||
}
|
||
|
||
public static String loadGolden(Path dir, String name) throws IOException {
|
||
return Files.readString(dir.resolve(name + ".json"));
|
||
}
|
||
|
||
/**
|
||
* 生成对比报告:一致返回空串;不一致输出 EXPECTED/ACTUAL 及首个差异行上下文。
|
||
*/
|
||
public static String diffReport(String expectedJson, String actualJson) {
|
||
if (expectedJson == null || actualJson == null) {
|
||
throw new IllegalArgumentException("diffReport 入参不能为 null");
|
||
}
|
||
if (expectedJson.equals(actualJson)) {
|
||
return "";
|
||
}
|
||
List<String> expectedLines = expectedJson.split("\n", -1).length == 0
|
||
? List.of() : List.of(expectedJson.split("\n", -1));
|
||
List<String> actualLines = List.of(actualJson.split("\n", -1));
|
||
int first = firstDiff(expectedLines, actualLines);
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.append("快照不一致:首个差异在第 ").append(Math.max(1, first + 1)).append(" 行\n");
|
||
sb.append("--- EXPECTED ---\n").append(snippet(expectedLines, first));
|
||
sb.append("\n--- ACTUAL ---\n").append(snippet(actualLines, first));
|
||
return sb.toString();
|
||
}
|
||
|
||
private static int firstDiff(List<String> a, List<String> b) {
|
||
int n = Math.min(a.size(), b.size());
|
||
for (int i = 0; i < n; i++) {
|
||
if (!a.get(i).equals(b.get(i))) {
|
||
return i;
|
||
}
|
||
}
|
||
return n;
|
||
}
|
||
|
||
private static String snippet(List<String> lines, int around) {
|
||
List<String> out = new ArrayList<>();
|
||
for (int i = Math.max(0, around - 1); i <= Math.min(lines.size() - 1, around + 1); i++) {
|
||
out.add((i == around ? ">> " : " ") + lines.get(i));
|
||
}
|
||
return String.join("\n", out);
|
||
}
|
||
}
|