Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.rocketmq.studio.ops.audit;

import com.rocketmq.studio.common.domain.PageResult;
import com.rocketmq.studio.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -38,6 +39,7 @@ public class AuditService {
public PageResult<AuditRecordVO> queryLogs(int page, int pageSize, String search,
String operationType, String startDate,
String endDate, String result) {
validatePagination(page, pageSize);
log.info("Querying audit logs, page={}, pageSize={}, search={}, operationType={}, result={}",
page, pageSize, search, operationType, result);

Expand All @@ -47,20 +49,33 @@ public PageResult<AuditRecordVO> queryLogs(int page, int pageSize, String search
List<AuditRecordVO> allRecords = auditRepository.findAll(search, operationType, start, end, result);
long total = allRecords.size();

int fromIndex = Math.min((page - 1) * pageSize, allRecords.size());
int toIndex = Math.min(fromIndex + pageSize, allRecords.size());
long offset = (long) (page - 1) * pageSize;
int fromIndex = (int) Math.min(offset, allRecords.size());
int toIndex = (int) Math.min((long) fromIndex + pageSize, allRecords.size());
List<AuditRecordVO> pageRecords = allRecords.subList(fromIndex, toIndex);

return PageResult.of(pageRecords, total, page, pageSize);
}


public int cleanupLogs(int beforeDays) {
if (beforeDays <= 0) {
throw new BusinessException(400, "beforeDays must be greater than 0");
}
log.info("Cleaning up audit logs older than {} days", beforeDays);
LocalDateTime cutoff = LocalDateTime.now().minusDays(beforeDays);
return auditRepository.deleteBefore(cutoff);
}

private void validatePagination(int page, int pageSize) {
if (page <= 0) {
throw new BusinessException(400, "page must be greater than 0");
}
if (pageSize <= 0) {
throw new BusinessException(400, "pageSize must be greater than 0");
}
}

private LocalDateTime parseDate(String dateStr, boolean startOfDay) {
if (dateStr == null || dateStr.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.rocketmq.studio.ops.audit;

import com.rocketmq.studio.common.domain.PageResult;
import com.rocketmq.studio.common.exception.BusinessException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand All @@ -30,6 +31,7 @@
import java.util.List;

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.ArgumentMatchers.isNull;
Expand Down Expand Up @@ -107,6 +109,35 @@ void queryLogsShouldReturnEmptyWhenPageExceedsTotal() {
assertThat(result.getTotal()).isEqualTo(1);
}

@Test
void queryLogsShouldRejectNonPositivePage() {
assertThatThrownBy(() -> auditService.queryLogs(0, 10, null, null, null, null, null))
.isInstanceOf(BusinessException.class)
.hasMessage("page must be greater than 0")
.satisfies(ex -> assertThat(((BusinessException) ex).getCode()).isEqualTo(400));
}

@Test
void queryLogsShouldRejectNonPositivePageSize() {
assertThatThrownBy(() -> auditService.queryLogs(1, 0, null, null, null, null, null))
.isInstanceOf(BusinessException.class)
.hasMessage("pageSize must be greater than 0")
.satisfies(ex -> assertThat(((BusinessException) ex).getCode()).isEqualTo(400));
}

@Test
void queryLogsShouldAvoidOffsetOverflow() {
AuditRecordVO record = AuditRecordVO.builder().operationType("CREATE").build();
when(auditRepository.findAll(isNull(), isNull(), isNull(), isNull(), isNull()))
.thenReturn(List.of(record));

PageResult<AuditRecordVO> result = auditService.queryLogs(
Integer.MAX_VALUE, Integer.MAX_VALUE, null, null, null, null, null);

assertThat(result.getItems()).isEmpty();
assertThat(result.getTotal()).isEqualTo(1);
}

@Test
void queryLogsShouldPassSearchFilterToRepository() {
when(auditRepository.findAll(eq("topic-a"), isNull(), isNull(), isNull(), isNull()))
Expand Down Expand Up @@ -191,6 +222,18 @@ void cleanupLogsShouldReturnZeroWhenNoOldRecords() {
assertThat(result).isZero();
}

@Test
void cleanupLogsShouldRejectNonPositiveRetention() {
assertThatThrownBy(() -> auditService.cleanupLogs(0))
.isInstanceOf(BusinessException.class)
.hasMessage("beforeDays must be greater than 0")
.satisfies(ex -> assertThat(((BusinessException) ex).getCode()).isEqualTo(400));

assertThatThrownBy(() -> auditService.cleanupLogs(-1))
.isInstanceOf(BusinessException.class)
.hasMessage("beforeDays must be greater than 0");
}

@Test
void queryLogsShouldHandleAllFiltersTogether() {
when(auditRepository.findAll(eq("admin"), eq("DELETE"), any(LocalDateTime.class),
Expand Down
Loading