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
+69
@@ -0,0 +1,69 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.mobile.api.service.AdminAuditLogService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import com.solvway.mobile.api.useraccess.Audited;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AdminAuditLogControllerTest {
|
||||
|
||||
@Mock private AdminAuditLogService audit;
|
||||
@InjectMocks private AdminAuditLogController controller;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void classLevelModule_notOverriddenOrAudited() {
|
||||
assertThat(AdminAuditLogController.class.getAnnotation(AdminModule.class).value()).containsExactly("BO_AUDIT_LOG");
|
||||
for (Method m : AdminAuditLogController.class.getDeclaredMethods()) {
|
||||
assertThat(m.getAnnotation(AdminModule.class)).isNull();
|
||||
assertThat(m.getAnnotation(Audited.class)).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_passesEveryParam() throws Exception {
|
||||
when(audit.search("boss", "LEADS", "UPDATE", "SUCCESS", "2026-11-01", "2026-11-30", 1, 5))
|
||||
.thenReturn(Map.of("content", List.of(Map.of("id", 7))));
|
||||
mvc.perform(get("/api/admin/audit-log").param("actor", "boss").param("module", "LEADS").param("action", "UPDATE")
|
||||
.param("outcome", "SUCCESS").param("from", "2026-11-01").param("to", "2026-11-30")
|
||||
.param("page", "1").param("size", "5"))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.content[0].id").value(7));
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_defaultsToFirstPageOfTwenty() throws Exception {
|
||||
when(audit.search(null, null, null, null, null, null, 0, 20)).thenReturn(Map.of());
|
||||
mvc.perform(get("/api/admin/audit-log")).andExpect(status().isOk());
|
||||
verify(audit).search(null, null, null, null, null, null, 0, 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void detail_returnsEvent() throws Exception {
|
||||
when(audit.get(9L)).thenReturn(Map.of("id", 9));
|
||||
mvc.perform(get("/api/admin/audit-log/9")).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(9));
|
||||
}
|
||||
}
|
||||
+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