task-217: 快照对比工具(ResponseSnapshotTool:规范 JSON/录制/对比/落盘 golden/可读 diff 报告)+ 8 条测试
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package com.nanri.aiimage.testutil;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
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.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* task-217:快照对比工具契约。
|
||||
* ResponseSnapshotTool 规范化 JSON(key 排序)、record/compare 相等、检出差异、输出可读报告、
|
||||
* golden 落盘/读取、可复用、入参非法给清晰错误。
|
||||
*/
|
||||
class ResponseSnapshotToolTest {
|
||||
|
||||
private static Map<String, Object> sample(String id, int count) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("count", count);
|
||||
m.put("items", List.of(Map.of("k", "v")));
|
||||
return m;
|
||||
}
|
||||
|
||||
@Test
|
||||
void canonicalIsDeterministicIgnoringInsertionOrder() {
|
||||
assertEquals(ResponseSnapshotTool.canonical(sample("a", 1)),
|
||||
ResponseSnapshotTool.canonical(sample("a", 1)));
|
||||
Map<String, Object> reversed = new LinkedHashMap<>();
|
||||
reversed.put("count", 1);
|
||||
reversed.put("id", "a");
|
||||
reversed.put("items", List.of(Map.of("k", "v")));
|
||||
assertEquals(ResponseSnapshotTool.canonical(sample("a", 1)),
|
||||
ResponseSnapshotTool.canonical(reversed), "规范 JSON 应忽略 key 插入顺序");
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsJsonDetectsEqualObjects() {
|
||||
assertTrue(ResponseSnapshotTool.equalsJson(sample("a", 1), sample("a", 1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsJsonDetectsDifference() {
|
||||
assertFalse(ResponseSnapshotTool.equalsJson(sample("a", 1), sample("a", 2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void diffReportEmptyWhenEqual() {
|
||||
String exp = ResponseSnapshotTool.canonical(sample("a", 1));
|
||||
assertEquals("", ResponseSnapshotTool.diffReport(exp, exp));
|
||||
}
|
||||
|
||||
@Test
|
||||
void diffReportDetectsAndLocatesDifference() {
|
||||
String exp = ResponseSnapshotTool.canonical(sample("a", 1));
|
||||
String act = ResponseSnapshotTool.canonical(sample("a", 999));
|
||||
String report = ResponseSnapshotTool.diffReport(exp, act);
|
||||
assertFalse(report.isEmpty());
|
||||
assertTrue(report.contains("EXPECTED"), report);
|
||||
assertTrue(report.contains("ACTUAL"), report);
|
||||
}
|
||||
|
||||
@Test
|
||||
void goldenCanBeSavedAndLoaded() throws IOException {
|
||||
Path dir = Files.createTempDirectory("snapshot-tool-test");
|
||||
try {
|
||||
ResponseSnapshotTool.saveGolden(dir, "case_a", sample("a", 1));
|
||||
Path file = dir.resolve("case_a.json");
|
||||
assertTrue(Files.exists(file), "golden 文件应落盘");
|
||||
assertEquals(ResponseSnapshotTool.canonical(sample("a", 1)),
|
||||
ResponseSnapshotTool.loadGolden(dir, "case_a"));
|
||||
} finally {
|
||||
deleteTree(dir);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void toolIsReusableAndStable() {
|
||||
String c1 = ResponseSnapshotTool.canonical(sample("a", 1));
|
||||
String c2 = ResponseSnapshotTool.canonical(sample("a", 1));
|
||||
assertEquals(c1, c2);
|
||||
assertFalse(ResponseSnapshotTool.equalsJson(sample("a", 1), Map.of("a", 2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullInputGivesClearError() {
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||
() -> ResponseSnapshotTool.diffReport(null, "{}"));
|
||||
assertTrue(ex.getMessage().contains("null"), "错误信息应明确: " + ex.getMessage());
|
||||
}
|
||||
|
||||
private static void deleteTree(Path dir) throws IOException {
|
||||
try (var stream = Files.walk(dir)) {
|
||||
stream.sorted(java.util.Comparator.reverseOrder()).forEach(p -> p.toFile().delete());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user