feat: services-log stores, purges and serves admin audit events
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
10f59c5ca8
commit
d715073ab2
10 files changed
+505
-1
No files matched your search
+54
@@ -0,0 +1,54 @@
|
||||
package com.solvway.services.log.service;
|
||||
|
||||
import com.solvway.services.log.repo.AuditEventLogRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AuditEventLogPurgeTest {
|
||||
|
||||
private final AuditEventLogRepository repository = mock(AuditEventLogRepository.class);
|
||||
|
||||
private LocalDateTime purgeCutoff(int retentionDays) {
|
||||
when(repository.deleteBatchOlderThan(any(), org.mockito.ArgumentMatchers.anyInt())).thenReturn(0);
|
||||
new AuditEventLogPurge(repository, retentionDays).purge();
|
||||
ArgumentCaptor<LocalDateTime> c = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
verify(repository).deleteBatchOlderThan(c.capture(), org.mockito.ArgumentMatchers.anyInt());
|
||||
return c.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cutoffIsNowMinusConfiguredRetention() {
|
||||
for (int days : new int[] {730, 30}) {
|
||||
org.mockito.Mockito.reset(repository);
|
||||
LocalDateTime expected = LocalDateTime.now().minusDays(days);
|
||||
assertThat(Duration.between(purgeCutoff(days), expected).abs()).isLessThan(Duration.ofSeconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletesInBatchesUntilNoRowsLeft() {
|
||||
when(repository.deleteBatchOlderThan(any(), org.mockito.ArgumentMatchers.anyInt()))
|
||||
.thenReturn(5000, 5000, 120, 0);
|
||||
|
||||
new AuditEventLogPurge(repository, 730).purge();
|
||||
|
||||
verify(repository, times(4)).deleteBatchOlderThan(any(), org.mockito.ArgumentMatchers.anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void retentionComesFromTheAuditRetentionDaysProperty() throws Exception {
|
||||
var ctor = AuditEventLogPurge.class.getDeclaredConstructors()[0];
|
||||
var value = ctor.getParameters()[1].getAnnotation(org.springframework.beans.factory.annotation.Value.class);
|
||||
assertThat(value.value()).isEqualTo("${audit.retention-days:${AUDIT_RETENTION_DAYS:730}}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user