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 @@ -63,11 +63,11 @@ public AuditLogsResponse searchAuditLogs(final String logType,
return office365RestClient.searchAuditLogs(logType, startTime, endTime, nextPageUri);
}

// Check and adjust start time for 7-day limit
// Adjust start time based on configured lookback hours
Instant adjustedStartTime = startTime;
Instant sevenDaysAgo = Instant.now().minus(SEVEN_DAYS);
if (startTime.isBefore(sevenDaysAgo)) {
adjustedStartTime = sevenDaysAgo;
Instant lookBackHoursAgo = Instant.now().minus(Duration.ofHours(office365SourceConfig.getLookBackHours()));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add corresponding unit tests coverage!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit test

if (startTime.isBefore(lookBackHoursAgo) && lookBackHoursAgo.isBefore(endTime)) {
adjustedStartTime = lookBackHoursAgo;
}

AuditLogsResponse response =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
Expand All @@ -20,8 +21,10 @@
import org.opensearch.dataprepper.plugins.source.source_crawler.exception.SaaSCrawlerException;
import org.opensearch.dataprepper.plugins.source.microsoft_office365.models.AuditLogsResponse;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -270,6 +273,48 @@ void testSearchAuditLogsWithNullLogType() {
assertFalse(exception.isRetryable());
}

@Test
void testSearchAuditLogs_WithRange_AdjustsStartTime() {
Clock fixedClock = Clock.fixed(Instant.parse("2025-11-09T21:30:00.00Z"), ZoneOffset.UTC);
Instant now = Instant.now(fixedClock);

// Create a time window that's beyond our configured range:
// Start time = current time - (4 days + 30 minutes)
// This simulates the crawler creating a partition that starts slightly beyond our range
Instant startTime = now.minus(Duration.ofDays(4))
.minus(Duration.ofMinutes(30));
// End time = start time + 1 hour
Instant endTime = startTime.plus(Duration.ofHours(1));

String logType = "Exchange";
int lookBackHours = 96; // Configure 4 days range limit

when(sourceConfig.getLookBackHours()).thenReturn(lookBackHours);
when(office365RestClient.searchAuditLogs(
any(String.class),
any(Instant.class),
any(Instant.class),
any()
)).thenReturn(new AuditLogsResponse(new ArrayList<>(), null));

office365Service.searchAuditLogs(logType, startTime, endTime, null);

// Capture the actual start time that was passed to the REST client
ArgumentCaptor<Instant> startTimeCaptor = ArgumentCaptor.forClass(Instant.class);
verify(office365RestClient).searchAuditLogs(
eq(logType),
startTimeCaptor.capture(),
eq(endTime),
isNull()
);

// Verify that the service adjusted the start time correctly:
Instant capturedStartTime = startTimeCaptor.getValue();
Duration actualLookback = Duration.between(capturedStartTime, now);
assertEquals(96, actualLookback.toHours(),
"Adjusted start time should be exactly 96 hours ago");
}

private Map<String, Object> createTestItem(String contentId, Instant contentCreated) {
Map<String, Object> item = new HashMap<>();
item.put("contentId", contentId);
Expand Down
Loading