Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,8 @@ public class AuditService {
|
||||
log(action, null, null, description);
|
||||
}
|
||||
|
||||
private static final int MAX_DESCRIPTION_LENGTH = 1024;
|
||||
|
||||
public void log(AuditAction action, String entityType, Long entityId, String description) {
|
||||
try {
|
||||
Long userId = null;
|
||||
@@ -49,13 +51,18 @@ public class AuditService {
|
||||
|
||||
String countryCode = geoIpService.resolveCountryCode(ipAddress);
|
||||
|
||||
String safeDescription = description;
|
||||
if (safeDescription != null && safeDescription.length() > MAX_DESCRIPTION_LENGTH) {
|
||||
safeDescription = safeDescription.substring(0, MAX_DESCRIPTION_LENGTH - 3) + "...";
|
||||
}
|
||||
|
||||
AuditLogEntity entity = AuditLogEntity.builder()
|
||||
.userId(userId)
|
||||
.username(username)
|
||||
.action(action)
|
||||
.entityType(entityType)
|
||||
.entityId(entityId)
|
||||
.description(description)
|
||||
.description(safeDescription)
|
||||
.ipAddress(ipAddress)
|
||||
.countryCode(countryCode)
|
||||
.build();
|
||||
|
||||
@@ -41,6 +41,8 @@ public class TaskHistoryService {
|
||||
auditService.log(AuditAction.TASK_EXECUTED, "Task", null, buildTaskDescription(type, options));
|
||||
}
|
||||
|
||||
private static final int MAX_DESCRIPTION_LENGTH = 1024;
|
||||
|
||||
private String buildTaskDescription(TaskType type, Map<String, Object> options) {
|
||||
String taskName = type != null ? type.getName() : "Unknown";
|
||||
StringBuilder sb = new StringBuilder("Started task: ").append(taskName);
|
||||
@@ -50,7 +52,26 @@ public class TaskHistoryService {
|
||||
Object bookIds = options.get("bookIds");
|
||||
Object libraryId = options.get("libraryId");
|
||||
if (bookIds instanceof Collection<?> ids && !ids.isEmpty()) {
|
||||
sb.append(" (Book IDs: ").append(ids.stream().map(Object::toString).collect(Collectors.joining(", "))).append(")");
|
||||
sb.append(" (").append(ids.size()).append(" books, IDs: ");
|
||||
String truncationSuffix = "...)";
|
||||
Iterator<?> it = ids.iterator();
|
||||
boolean truncated = false;
|
||||
while (it.hasNext()) {
|
||||
String id = it.next().toString();
|
||||
boolean isLast = !it.hasNext();
|
||||
String separator = sb.charAt(sb.length() - 1) == ' ' ? "" : ", ";
|
||||
if (isLast && sb.length() + separator.length() + id.length() + 1 <= MAX_DESCRIPTION_LENGTH) {
|
||||
sb.append(separator).append(id).append(")");
|
||||
} else if (!isLast && sb.length() + separator.length() + id.length() + truncationSuffix.length() <= MAX_DESCRIPTION_LENGTH) {
|
||||
sb.append(separator).append(id);
|
||||
} else {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (truncated) {
|
||||
sb.append(truncationSuffix);
|
||||
}
|
||||
} else if (libraryId != null) {
|
||||
sb.append(" (Library ID: ").append(libraryId).append(")");
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import org.mockito.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
@@ -287,4 +289,127 @@ class TaskHistoryServiceTest {
|
||||
assertNotNull(response);
|
||||
assertTrue(response.getTaskHistories().isEmpty());
|
||||
}
|
||||
|
||||
// --- buildTaskDescription tests (via createTask audit log capture) ---
|
||||
|
||||
private String captureAuditDescription(TaskType type, Map<String, Object> options) {
|
||||
taskHistoryService.createTask("test-task", type, 1L, options);
|
||||
ArgumentCaptor<String> descCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(auditService).log(any(), any(), any(), descCaptor.capture());
|
||||
reset(auditService);
|
||||
return descCaptor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withSmallBookIdsList() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", Set.of(10L, 20L, 30L));
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertTrue(desc.startsWith("Started task:"));
|
||||
assertTrue(desc.contains("3 books, IDs:"));
|
||||
assertTrue(desc.contains("10"));
|
||||
assertTrue(desc.contains("20"));
|
||||
assertTrue(desc.contains("30"));
|
||||
assertTrue(desc.endsWith(")"));
|
||||
assertFalse(desc.contains("..."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withSingleBookId() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", Set.of(42L));
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertTrue(desc.contains("1 books, IDs: 42)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withLibraryId() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("libraryId", 7L);
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_LIBRARY_METADATA, options);
|
||||
|
||||
assertTrue(desc.contains("Library ID: 7"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withEmptyBookIds() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", Collections.emptySet());
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertFalse(desc.contains("books"));
|
||||
assertFalse(desc.contains("Library ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withNullOptions() {
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, null);
|
||||
|
||||
assertEquals("Started task: Refresh Metadata", desc);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withEmptyOptions() {
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, new HashMap<>());
|
||||
|
||||
assertEquals("Started task: Refresh Metadata", desc);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_withNullType() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", Set.of(1L));
|
||||
|
||||
String desc = captureAuditDescription(null, options);
|
||||
|
||||
assertTrue(desc.startsWith("Started task: Unknown"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_bookIdsPrefersOverLibraryId() {
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", Set.of(1L, 2L));
|
||||
options.put("libraryId", 5L);
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertTrue(desc.contains("2 books, IDs:"));
|
||||
assertFalse(desc.contains("Library ID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_largeBookIdsListTruncated() {
|
||||
Set<Long> ids = LongStream.rangeClosed(1, 500)
|
||||
.boxed()
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", ids);
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertTrue(desc.length() <= 1024, "Description length " + desc.length() + " exceeds 1024");
|
||||
assertTrue(desc.contains("500 books, IDs:"));
|
||||
assertTrue(desc.endsWith("...)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildTaskDescription_veryLargeBookIdsListStaysWithinLimit() {
|
||||
Set<Long> ids = LongStream.rangeClosed(1, 2000)
|
||||
.boxed()
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
Map<String, Object> options = new HashMap<>();
|
||||
options.put("bookIds", ids);
|
||||
|
||||
String desc = captureAuditDescription(TaskType.REFRESH_METADATA_MANUAL, options);
|
||||
|
||||
assertTrue(desc.length() <= 1024, "Description length " + desc.length() + " exceeds 1024");
|
||||
assertTrue(desc.contains("2000 books, IDs:"));
|
||||
assertTrue(desc.endsWith("...)"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user