diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java index eae6f253..0455a583 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditTest.java @@ -24,6 +24,9 @@ import uk.gov.hmcts.reform.pip.model.account.UserProvenances; import uk.gov.hmcts.reform.pip.model.enums.AuditAction; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.http.HttpStatus.FORBIDDEN; @@ -43,6 +46,7 @@ class AuditTest { private static final String ROOT_URL = "/audit"; private static final String EMAIL = "test_account_admin@hmcts.net"; + private static final String ADDITIONAL_USER_EMAIL = "test_account_admin_2@hmcts.net"; private static final Roles ROLES = Roles.SYSTEM_ADMIN; private static final UserProvenances USER_PROVENANCE = UserProvenances.PI_AAD; private static final String AUDIT_DETAILS = "User requested to view all third party users"; @@ -53,6 +57,8 @@ class AuditTest { private static final String FORBIDDEN_STATUS_CODE = "Status code does not match forbidden"; private static final String GET_AUDIT_LOG_FAILED = "Failed to retrieve audit log"; private static final AuditAction AUDIT_ACTION = AuditAction.MANAGE_THIRD_PARTY_USER_VIEW; + private static final AuditAction ADDITIONAL_USER_AUDIT_ACTION = AuditAction.MANAGE_USER; + private static final String ADDITIONAL_USER_AUDIT_DETAILS = "Manage user"; private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); @@ -117,6 +123,264 @@ void testGetAllAuditLogs() throws Exception { assertEquals(AUDIT_DETAILS, auditLog2.getDetails(), GET_AUDIT_LOG_FAILED); } + @Test + void testGetAllAuditLogsFilterByEmail() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + ADDITIONAL_USER_EMAIL, + ROLES, + USER_PROVENANCE, + AUDIT_ACTION, + AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?email=" + EMAIL)) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + AuditLog auditLog1 = pageResponse.getContent().get(0); + + assertEquals(EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + } + + @Test + void testGetAllAuditLogsPartialEmailFilter() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + EMAIL, + ROLES, + USER_PROVENANCE, + AUDIT_ACTION, + AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?email=test_account_admin")) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + + AuditLog auditLog1 = pageResponse.getContent().get(0); + + assertEquals(EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + + AuditLog auditLog2 = pageResponse.getContent().get(1); + + assertEquals(EMAIL, auditLog2.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(USER_ID, auditLog2.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(AUDIT_DETAILS, auditLog2.getDetails(), GET_AUDIT_LOG_FAILED); + } + + @Test + void testGetAllAuditLogsFilterByUserId() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + ADDITIONAL_USER_EMAIL, + ROLES, + USER_PROVENANCE, + AUDIT_ACTION, + AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?userId=" + ADDITIONAL_USER_ID)) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + AuditLog auditLog1 = pageResponse.getContent().get(0); + + assertEquals(ADDITIONAL_USER_EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + } + + @Test + void testGetAllAuditLogsFilterByAuditAction() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + ADDITIONAL_USER_EMAIL, + ROLES, + USER_PROVENANCE, + ADDITIONAL_USER_AUDIT_ACTION, + ADDITIONAL_USER_AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?actions=" + ADDITIONAL_USER_AUDIT_ACTION)) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + AuditLog auditLog1 = pageResponse.getContent().get(0); + + + assertEquals(pageResponse.getContent().size(), 1, GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + } + + @Test + void testGetAllAuditLogsFilterByDate() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + ADDITIONAL_USER_EMAIL, + ROLES, + USER_PROVENANCE, + ADDITIONAL_USER_AUDIT_ACTION, + ADDITIONAL_USER_AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate filterDate = LocalDate.parse(LocalDate.now().toString(), formatter); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?filterDate=" + filterDate)) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + + AuditLog auditLog1 = pageResponse.getContent().get(0); + + assertEquals(ADDITIONAL_USER_EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + + AuditLog auditLog2 = pageResponse.getContent().get(1); + + assertEquals(EMAIL, auditLog2.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(USER_ID, auditLog2.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(AUDIT_DETAILS, auditLog2.getDetails(), GET_AUDIT_LOG_FAILED); + } + + @Test + void testGetAllAuditLogsFilterByEmailAndUserIdAndAuditActionAndDate() throws Exception { + MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(createAuditLog())) + .contentType(MediaType.APPLICATION_JSON); + + mockMvc.perform(mockHttpServletRequestBuilder1).andExpect(status().isOk()); + + MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = MockMvcRequestBuilders + .post(ROOT_URL) + .content(OBJECT_MAPPER.writeValueAsString(new AuditLog( + ADDITIONAL_USER_ID, + ADDITIONAL_USER_EMAIL, + ROLES, + USER_PROVENANCE, + ADDITIONAL_USER_AUDIT_ACTION, + ADDITIONAL_USER_AUDIT_DETAILS + ))) + .contentType(MediaType.APPLICATION_JSON); + mockMvc.perform(mockHttpServletRequestBuilder2).andExpect(status().isOk()); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDate filterDate = LocalDate.parse(LocalDate.now().toString(), formatter); + + MvcResult mvcResult = mockMvc.perform(get(ROOT_URL + "?email=" + ADDITIONAL_USER_EMAIL + + "&userId=" + ADDITIONAL_USER_ID + "&actions=" + ADDITIONAL_USER_AUDIT_ACTION + + "&filterDate=" + filterDate)) + .andExpect(status().isOk()) + .andReturn(); + + CustomPageImpl pageResponse = + OBJECT_MAPPER.readValue( + mvcResult.getResponse().getContentAsString(), + new TypeReference<>() { + } + ); + AuditLog auditLog1 = pageResponse.getContent().get(0); + + assertEquals(ADDITIONAL_USER_EMAIL, auditLog1.getUserEmail(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_ID, auditLog1.getUserId(), GET_AUDIT_LOG_FAILED); + assertEquals(ADDITIONAL_USER_AUDIT_DETAILS, auditLog1.getDetails(), GET_AUDIT_LOG_FAILED); + } + @Test @WithMockUser(username = UNAUTHORIZED_USERNAME, authorities = {UNAUTHORIZED_ROLE}) void testUnauthorizedGetAllAuditLogs() throws Exception { diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditController.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditController.java index 236c6fe6..2750e959 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditController.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditController.java @@ -21,7 +21,9 @@ import uk.gov.hmcts.reform.pip.account.management.model.AuditLog; import uk.gov.hmcts.reform.pip.account.management.service.AuditService; import uk.gov.hmcts.reform.pip.model.authentication.roles.IsAdmin; +import uk.gov.hmcts.reform.pip.model.enums.AuditAction; +import java.util.List; import java.util.UUID; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -45,14 +47,19 @@ public AuditController(AuditService auditService) { this.auditService = auditService; } - @ApiResponse(responseCode = OK_ERROR_CODE, description = "All audit logs returned as a page.") + @ApiResponse(responseCode = OK_ERROR_CODE, description = "All audit logs returned as a page with filtering.") @Operation(summary = "Get all audit logs returned as a page") @GetMapping public ResponseEntity> getAllAuditLogs( @RequestParam(name = "pageNumber", defaultValue = "0") int pageNumber, - @RequestParam(name = "pageSize", defaultValue = "25") int pageSize) { + @RequestParam(name = "pageSize", defaultValue = "25") int pageSize, + @RequestParam(name = "email", defaultValue = "", required = false) String email, + @RequestParam(name = "userId", defaultValue = "", required = false) String userId, + @RequestParam(name = "actions", defaultValue = "", required = false) List auditActions, + @RequestParam(name = "filterDate", defaultValue = "", required = false) String filterDate) { Pageable pageable = PageRequest.of(pageNumber, pageSize); - return ResponseEntity.ok(auditService.getAllAuditLogs(pageable)); + return ResponseEntity.ok(auditService.getAllAuditLogs(pageable, email, userId, + auditActions, filterDate)); } @ApiResponse(responseCode = OK_ERROR_CODE, description = "Audit log with id {id} returned.") diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/AuditRepository.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/AuditRepository.java index b1cb9d9a..69ba59ac 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/AuditRepository.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/database/AuditRepository.java @@ -5,8 +5,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.transaction.annotation.Transactional; import uk.gov.hmcts.reform.pip.account.management.model.AuditLog; +import uk.gov.hmcts.reform.pip.model.enums.AuditAction; import java.time.LocalDateTime; +import java.util.List; import java.util.UUID; public interface AuditRepository extends JpaRepository { @@ -14,5 +16,17 @@ public interface AuditRepository extends JpaRepository { @Transactional void deleteAllByTimestampBefore(LocalDateTime timestamp); - Page findAllByOrderByTimestampDesc(Pageable pageable); + Page findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInOrderByTimestampDesc( + String email, + String userId, + List auditAction, + Pageable pageable); + + Page findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInAndTimestampBetweenOrderByTimestampDesc( + String email, + String userId, + List auditAction, + LocalDateTime timeStampFrom, + LocalDateTime timeStampTo, + Pageable pageable); } diff --git a/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/AuditService.java b/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/AuditService.java index b9b37eee..be70fd6a 100644 --- a/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/AuditService.java +++ b/src/main/java/uk/gov/hmcts/reform/pip/account/management/service/AuditService.java @@ -8,8 +8,15 @@ import uk.gov.hmcts.reform.pip.account.management.database.AuditRepository; import uk.gov.hmcts.reform.pip.account.management.errorhandling.exceptions.NotFoundException; import uk.gov.hmcts.reform.pip.account.management.model.AuditLog; +import uk.gov.hmcts.reform.pip.model.enums.AuditAction; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; import java.util.UUID; /** @@ -33,8 +40,42 @@ public AuditService(AuditRepository auditRepository) { * @param pageable The pageable object to query by. * @return Returns the audit logs in a page. */ - public Page getAllAuditLogs(Pageable pageable) { - return auditRepository.findAllByOrderByTimestampDesc(pageable); + public Page getAllAuditLogs(Pageable pageable, String email, String userId, + List auditActions, String filterDate) { + + // If user id is supplied then find by an exact match + String userIdToQuery = "%%"; + if (!userId.isBlank()) { + userIdToQuery = userId; + } + + // If user audit action is supplied then find by an exact match + List auditActionsToQuery = new ArrayList<>(EnumSet.allOf(AuditAction.class)); + if (!auditActions.isEmpty()) { + auditActionsToQuery = auditActions; + } + + if (filterDate.isBlank()) { + return auditRepository + .findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInOrderByTimestampDesc( + "%" + email + "%", + userIdToQuery, + auditActionsToQuery, + pageable); + } + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateTime filterStartDate = LocalDate.parse(filterDate, formatter).atTime(LocalTime.MIN); + LocalDateTime filterEndDate = LocalDate.parse(filterDate, formatter).atTime(LocalTime.MAX); + + return auditRepository + .findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInAndTimestampBetweenOrderByTimestampDesc( + "%" + email + "%", + userIdToQuery, + auditActionsToQuery, + filterStartDate, + filterEndDate, + pageable); } public AuditLog getAuditLogById(UUID id) { diff --git a/src/test/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditControllerTest.java b/src/test/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditControllerTest.java index bc1eb5ad..ba34841b 100644 --- a/src/test/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditControllerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pip/account/management/controllers/AuditControllerTest.java @@ -14,6 +14,8 @@ import uk.gov.hmcts.reform.pip.model.account.UserProvenances; import uk.gov.hmcts.reform.pip.model.enums.AuditAction; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,11 +31,15 @@ class AuditControllerTest { private AuditController auditController; private static final String STATUS_CODE_MATCH = "Status code responses should match"; + private static final String EMAIL = "a@b.com"; + private static final String USER_ID = "123"; + private static final List AUDIT_ACTIONS = new ArrayList<>(); + private static final String FILTER_DATE = "2023-11-01"; @Test void testGetAllAuditLogs() { - ResponseEntity> response = auditController.getAllAuditLogs(0, 25); - + ResponseEntity> response = auditController.getAllAuditLogs(0, 25, + EMAIL, USER_ID, AUDIT_ACTIONS, FILTER_DATE); assertEquals(HttpStatus.OK, response.getStatusCode(), STATUS_CODE_MATCH); } diff --git a/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/AuditServiceTest.java b/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/AuditServiceTest.java index 906e4687..da267176 100644 --- a/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/AuditServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pip/account/management/service/AuditServiceTest.java @@ -15,7 +15,11 @@ import uk.gov.hmcts.reform.pip.account.management.model.AuditLog; import uk.gov.hmcts.reform.pip.model.enums.AuditAction; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -38,6 +42,10 @@ class AuditServiceTest { @InjectMocks private AuditService auditService; + private static final String EMAIL = "a@b.com"; + private static final String USER_ID = "123"; + private static final List AUDIT_ACTIONS = new ArrayList<>(); + private static final String FILTER_DATE = "2024-11-01"; private final AuditLog auditLogExample = new AuditLog(); @BeforeEach @@ -48,15 +56,41 @@ void setup() { auditLogExample.setAction(AuditAction.MANAGE_USER); auditLogExample.setDetails("Test details for manage user"); auditLogExample.setTimestamp(LocalDateTime.now()); + + AUDIT_ACTIONS.add(AuditAction.ADMIN_CREATION); } @Test void testGetAllAuditLogs() { Pageable pageable = PageRequest.of(0, 25); Page page = new PageImpl<>(List.of(auditLogExample), pageable, List.of(auditLogExample).size()); - when(auditRepository.findAllByOrderByTimestampDesc(pageable)).thenReturn(page); + when(auditRepository + .findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInOrderByTimestampDesc( + "%" + EMAIL + "%", USER_ID, AUDIT_ACTIONS, pageable)) + .thenReturn(page); + + Page returnedAuditLogs = auditService.getAllAuditLogs(pageable, EMAIL, USER_ID, AUDIT_ACTIONS, + ""); + + assertEquals(auditLogExample, returnedAuditLogs.getContent().get(0), + "Returned audit log does not match the expected"); + } + + @Test + void testGetAllAuditLogsWithFilterDate() { + Pageable pageable = PageRequest.of(0, 25); + Page page = new PageImpl<>(List.of(auditLogExample), pageable, List.of(auditLogExample).size()); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateTime filterStartDate = LocalDate.parse(FILTER_DATE, formatter).atTime(LocalTime.MIN); + LocalDateTime filterEndDate = LocalDate.parse(FILTER_DATE, formatter).atTime(LocalTime.MAX); + + when(auditRepository + .findAllByUserEmailLikeIgnoreCaseAndUserIdLikeAndActionInAndTimestampBetweenOrderByTimestampDesc( + "%" + EMAIL + "%", USER_ID, AUDIT_ACTIONS, filterStartDate, filterEndDate, pageable)) + .thenReturn(page); - Page returnedAuditLogs = auditService.getAllAuditLogs(pageable); + Page returnedAuditLogs = auditService.getAllAuditLogs(pageable, EMAIL, USER_ID, AUDIT_ACTIONS, + FILTER_DATE); assertEquals(auditLogExample, returnedAuditLogs.getContent().get(0), "Returned audit log does not match the expected");