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
+102
@@ -0,0 +1,102 @@
|
||||
package com.solvway.services.log.controller;
|
||||
|
||||
import com.solvway.services.log.model.AuditEventLog;
|
||||
import com.solvway.services.log.repo.AuditEventLogRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AuditEventControllerTest {
|
||||
|
||||
private final AuditEventLogRepository repository = mock(AuditEventLogRepository.class);
|
||||
private final AuditEventController controller = new AuditEventController(repository);
|
||||
|
||||
private ArgumentCaptor<Pageable> pageable = ArgumentCaptor.forClass(Pageable.class);
|
||||
private ArgumentCaptor<LocalDateTime> from = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
private ArgumentCaptor<LocalDateTime> to = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
|
||||
private Map<String, Object> search(AuditEventController.SearchRequest r) {
|
||||
AuditEventLog row = new AuditEventLog();
|
||||
Page<AuditEventLog> page = new PageImpl<>(List.of(row));
|
||||
when(repository.search(any(), any(), any(), any(), any(), any(), any())).thenReturn(page);
|
||||
return controller.search(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
void passesExactFiltersAndInclusiveDateRange() {
|
||||
Map<String, Object> res = search(new AuditEventController.SearchRequest(
|
||||
"a@x.com", "LEADS", "lead.update", "FAILURE", "2026-11-01", "2026-11-30", 2, 50));
|
||||
|
||||
verify(repository).search(org.mockito.ArgumentMatchers.eq("a@x.com"), org.mockito.ArgumentMatchers.eq("LEADS"),
|
||||
org.mockito.ArgumentMatchers.eq("lead.update"), org.mockito.ArgumentMatchers.eq("FAILURE"),
|
||||
from.capture(), to.capture(), pageable.capture());
|
||||
assertThat(from.getValue()).isEqualTo(LocalDateTime.of(2026, 11, 1, 0, 0));
|
||||
assertThat(to.getValue()).isEqualTo(LocalDateTime.of(2026, 12, 1, 0, 0));
|
||||
assertThat(pageable.getValue().getPageNumber()).isEqualTo(2);
|
||||
assertThat(pageable.getValue().getPageSize()).isEqualTo(50);
|
||||
assertThat(pageable.getValue().getSort())
|
||||
.isEqualTo(Sort.by(Sort.Direction.DESC, "occurredAt").and(Sort.by(Sort.Direction.DESC, "id")));
|
||||
assertThat(res).containsEntry("totalElements", 1L).containsEntry("number", 2).containsEntry("size", 50);
|
||||
assertThat((List<?>) res.get("content")).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankFiltersAreIgnoredAndPagingDefaults() {
|
||||
search(new AuditEventController.SearchRequest(" ", "", null, "", "", null, -3, 0));
|
||||
|
||||
verify(repository).search(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), pageable.capture());
|
||||
assertThat(pageable.getValue().getPageNumber()).isZero();
|
||||
assertThat(pageable.getValue().getPageSize()).isEqualTo(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sizeIsCappedAtOneHundred() {
|
||||
search(new AuditEventController.SearchRequest(null, null, null, null, null, null, null, 5000));
|
||||
|
||||
verify(repository).search(any(), any(), any(), any(), any(), any(), pageable.capture());
|
||||
assertThat(pageable.getValue().getPageSize()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedDateAnswers400() {
|
||||
assertThatThrownBy(() -> controller.search(
|
||||
new AuditEventController.SearchRequest(null, null, null, null, "30/11/2026", null, 0, 20)))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class,
|
||||
e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailUnknownIdAnswers404() {
|
||||
when(repository.findById(9L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> controller.get(9L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class,
|
||||
e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailReturnsTheRow() {
|
||||
AuditEventLog row = new AuditEventLog();
|
||||
when(repository.findById(1L)).thenReturn(Optional.of(row));
|
||||
|
||||
assertThat(controller.get(1L)).isSameAs(row);
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package com.solvway.services.log.pubsub;
|
||||
|
||||
import com.google.cloud.spring.pubsub.support.BasicAcknowledgeablePubsubMessage;
|
||||
import com.google.cloud.spring.pubsub.support.GcpPubSubHeaders;
|
||||
import com.solvway.core.model.AuditEvent;
|
||||
import com.solvway.core.utils.PubSubUtil;
|
||||
import com.solvway.services.log.model.AuditEventLog;
|
||||
import com.solvway.services.log.repo.AuditEventLogRepository;
|
||||
import com.solvway.services.log.util.RedisUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
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.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AuditEventListenerTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private final RedisUtil<String> redisUtil = mock(RedisUtil.class);
|
||||
private final PubSubUtil pubSubUtil = mock(PubSubUtil.class);
|
||||
private final AuditEventLogRepository repository = mock(AuditEventLogRepository.class);
|
||||
private final BasicAcknowledgeablePubsubMessage original = mock(BasicAcknowledgeablePubsubMessage.class);
|
||||
private final MessageHandler handler =
|
||||
new PubSubListener(redisUtil, pubSubUtil).auditEventMessageReceiver(repository);
|
||||
|
||||
private Message<byte[]> message() {
|
||||
return MessageBuilder.withPayload(new byte[0]).setHeader(GcpPubSubHeaders.ORIGINAL_MESSAGE, original).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void savesEveryFieldAndAcks() {
|
||||
AuditEvent e = new AuditEvent();
|
||||
e.setCompanyId(7L);
|
||||
e.setOccurredAt(LocalDateTime.of(2026, 11, 30, 10, 0));
|
||||
e.setActor("admin@x.com");
|
||||
e.setModule("LEADS");
|
||||
e.setAction("lead.update");
|
||||
e.setHttpMethod("PUT");
|
||||
e.setPathTemplate("/api/admin/leads/{id}");
|
||||
e.setTargetId("42");
|
||||
e.setOutcome(AuditEvent.OUTCOME_SUCCESS);
|
||||
e.setHttpStatus(200);
|
||||
e.setClientIp("10.0.0.1");
|
||||
e.setChanges("{\"fields\":[\"name\"]}");
|
||||
when(pubSubUtil.convertToOriginalObject(any(), any())).thenReturn(e);
|
||||
|
||||
handler.handleMessage(message());
|
||||
|
||||
ArgumentCaptor<AuditEventLog> c = ArgumentCaptor.forClass(AuditEventLog.class);
|
||||
verify(repository).save(c.capture());
|
||||
AuditEventLog r = c.getValue();
|
||||
assertThat(r.getCompanyId()).isEqualTo(7L);
|
||||
assertThat(r.getOccurredAt()).isEqualTo(e.getOccurredAt());
|
||||
assertThat(r.getActor()).isEqualTo("admin@x.com");
|
||||
assertThat(r.getModule()).isEqualTo("LEADS");
|
||||
assertThat(r.getAction()).isEqualTo("lead.update");
|
||||
assertThat(r.getHttpMethod()).isEqualTo("PUT");
|
||||
assertThat(r.getPathTemplate()).isEqualTo("/api/admin/leads/{id}");
|
||||
assertThat(r.getTargetId()).isEqualTo("42");
|
||||
assertThat(r.getOutcome()).isEqualTo("SUCCESS");
|
||||
assertThat(r.getHttpStatus()).isEqualTo(200);
|
||||
assertThat(r.getClientIp()).isEqualTo("10.0.0.1");
|
||||
assertThat(r.getChanges()).isEqualTo("{\"fields\":[\"name\"]}");
|
||||
verify(original).ack();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cutsOversizeStringsToColumnWidth() {
|
||||
AuditEvent e = new AuditEvent();
|
||||
e.setActor("a".repeat(300));
|
||||
e.setAction("b".repeat(300));
|
||||
e.setPathTemplate("p".repeat(300));
|
||||
e.setClientIp("i".repeat(100));
|
||||
when(pubSubUtil.convertToOriginalObject(any(), any())).thenReturn(e);
|
||||
|
||||
handler.handleMessage(message());
|
||||
|
||||
ArgumentCaptor<AuditEventLog> c = ArgumentCaptor.forClass(AuditEventLog.class);
|
||||
verify(repository).save(c.capture());
|
||||
assertThat(c.getValue().getActor()).hasSize(191);
|
||||
assertThat(c.getValue().getAction()).hasSize(128);
|
||||
assertThat(c.getValue().getPathTemplate()).hasSize(255);
|
||||
assertThat(c.getValue().getClientIp()).hasSize(64);
|
||||
}
|
||||
|
||||
@Test
|
||||
void acksEvenWhenSaveFails() {
|
||||
when(pubSubUtil.convertToOriginalObject(any(), any())).thenReturn(new AuditEvent());
|
||||
when(repository.save(any())).thenThrow(new IllegalStateException("db down"));
|
||||
|
||||
handler.handleMessage(message());
|
||||
|
||||
verify(original).ack();
|
||||
verify(original, never()).nack();
|
||||
}
|
||||
}
|
||||
+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