feat: admin audit-log read API
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
adc62ac436
commit
8a527e9ffe
6 files changed
+284
-1
No files matched your search
+91
@@ -0,0 +1,91 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminAuditLogServiceTest {
|
||||
|
||||
private static final String SEARCH = "/log/audit/search";
|
||||
private static final String DETAIL = "/log/audit/{id}";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminAuditLogService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
service = new AdminAuditLogService(rest);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
}
|
||||
|
||||
private static HttpClientErrorException clientError(HttpStatus status, String body) {
|
||||
return HttpClientErrorException.create(status, status.getReasonPhrase(), new HttpHeaders(), body.getBytes(StandardCharsets.UTF_8), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void search_forwardsOnlyGivenFiltersWithPaging() {
|
||||
when(rest.postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(Map.of("content", List.of())));
|
||||
service.search(" boss ", null, "", " ", "2026-11-01", null, 0, 20);
|
||||
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), body.capture());
|
||||
assertThat((Map<String, Object>) body.getValue()).containsOnlyKeys("actor", "from", "page", "size")
|
||||
.containsEntry("actor", "boss").containsEntry("from", "2026-11-01").containsEntry("page", 0).containsEntry("size", 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void search_upstreamBadRequest_becomesInvalidFilterWithDetail() {
|
||||
when(rest.postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(clientError(HttpStatus.BAD_REQUEST, "{\"detail\":\"from must be yyyy-MM-dd\"}"));
|
||||
assertThatThrownBy(() -> service.search(null, null, null, null, "nope", null, 0, 20))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(e.getCode()).isEqualTo("INVALID_FILTER");
|
||||
assertThat(e.getReason()).isEqualTo("from must be yyyy-MM-dd");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void search_upstreamBadRequestWithoutBody_usesGenericReason() {
|
||||
when(rest.postForEntity(eq(SEARCH), any(ParameterizedTypeReference.class), any())).thenThrow(clientError(HttpStatus.BAD_REQUEST, ""));
|
||||
assertThatThrownBy(() -> service.search(null, null, null, null, "nope", null, 0, 20))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> assertThat(e.getReason()).isEqualTo("Invalid request"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_returnsEvent() {
|
||||
when(rest.getForEntity(eq(DETAIL), any(ParameterizedTypeReference.class), eq(9L))).thenReturn(ResponseEntity.ok(Map.of("id", 9)));
|
||||
assertThat(service.get(9L)).containsEntry("id", 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_upstreamNotFound_becomesNotFound() {
|
||||
when(rest.getForEntity(eq(DETAIL), any(ParameterizedTypeReference.class), eq(99L))).thenThrow(clientError(HttpStatus.NOT_FOUND, ""));
|
||||
assertThatThrownBy(() -> service.get(99L))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
assertThat(e.getCode()).isEqualTo("NOT_FOUND");
|
||||
assertThat(e.getReason()).isEqualTo("Audit event not found");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user