2026-08-05 16:26:10 +08:00
|
|
|
package com.baiyue.common.audit;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
class SensitiveDataSanitizerTest {
|
|
|
|
|
private final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void truncatedSnapshotRemainsValidJson() throws Exception {
|
|
|
|
|
SensitiveDataSanitizer sanitizer = new SensitiveDataSanitizer(mapper, 40);
|
|
|
|
|
|
|
|
|
|
String sanitized = sanitizer.sanitize(Map.of("description", "x".repeat(200)));
|
|
|
|
|
JsonNode parsed = mapper.readTree(sanitized);
|
|
|
|
|
|
|
|
|
|
assertThat(parsed.path("_truncated").asBoolean()).isTrue();
|
|
|
|
|
assertThat(parsed.path("_originalLength").asInt()).isGreaterThan(40);
|
|
|
|
|
}
|
2026-08-14 16:06:27 +08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void redactsIdentityAndContactAliasesInsideNestedObjects() throws Exception {
|
|
|
|
|
SensitiveDataSanitizer sanitizer = new SensitiveDataSanitizer(mapper, 1024);
|
|
|
|
|
|
|
|
|
|
String sanitized = sanitizer.sanitize(Map.of(
|
|
|
|
|
"profile", Map.of("idCard", "4401...", "contactPhone", "13800138000"),
|
|
|
|
|
"contacts", java.util.List.of(Map.of("emailAddress", "user@example.com"))));
|
|
|
|
|
JsonNode parsed = mapper.readTree(sanitized);
|
|
|
|
|
|
|
|
|
|
assertThat(parsed.at("/profile/idCard").asText()).isEqualTo("******");
|
|
|
|
|
assertThat(parsed.at("/profile/contactPhone").asText()).isEqualTo("******");
|
|
|
|
|
assertThat(parsed.at("/contacts/0/emailAddress").asText()).isEqualTo("******");
|
|
|
|
|
}
|
2026-08-05 16:26:10 +08:00
|
|
|
}
|