|
12 | 12 | import org.junit.jupiter.api.BeforeEach; |
13 | 13 | import org.junit.jupiter.api.Test; |
14 | 14 | import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.mockito.ArgumentCaptor; |
15 | 16 | import org.mockito.Mock; |
16 | 17 | import org.mockito.junit.jupiter.MockitoExtension; |
17 | 18 | import org.opensearch.dataprepper.metrics.PluginMetrics; |
|
20 | 21 | import org.opensearch.dataprepper.plugins.source.microsoft_office365.exception.Office365Exception; |
21 | 22 | import org.opensearch.dataprepper.plugins.source.microsoft_office365.models.AuditLogsResponse; |
22 | 23 |
|
| 24 | +import java.time.Clock; |
23 | 25 | import java.time.Duration; |
24 | 26 | import java.time.Instant; |
| 27 | +import java.time.ZoneOffset; |
25 | 28 | import java.util.ArrayList; |
26 | 29 | import java.util.HashMap; |
27 | 30 | import java.util.List; |
28 | 31 | import java.util.Map; |
29 | 32 |
|
30 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
31 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
32 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
33 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
34 | | -import static org.mockito.ArgumentMatchers.any; |
35 | | -import static org.mockito.ArgumentMatchers.anyString; |
36 | | -import static org.mockito.ArgumentMatchers.eq; |
37 | | -import static org.mockito.ArgumentMatchers.isNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.*; |
| 34 | +import static org.mockito.ArgumentMatchers.*; |
38 | 35 | import static org.mockito.Mockito.verify; |
39 | 36 | import static org.mockito.Mockito.when; |
40 | 37 |
|
@@ -208,6 +205,49 @@ void testGetAuditLog() { |
208 | 205 | verify(office365RestClient).getAuditLog("test-id"); |
209 | 206 | } |
210 | 207 |
|
| 208 | + @Test |
| 209 | + void testSearchAuditLogs_WithRange_AdjustsStartTime() { |
| 210 | + Clock fixedClock = Clock.fixed(Instant.parse("2025-11-09T21:30:00.00Z"), ZoneOffset.UTC); |
| 211 | + Instant now = Instant.now(fixedClock); |
| 212 | + |
| 213 | + // Create a time window that's beyond our configured range: |
| 214 | + // Start time = current time - (4 days + 30 minutes) |
| 215 | + // This simulates the crawler creating a partition that starts slightly beyond our range |
| 216 | + Instant startTime = now.minus(Duration.ofDays(4)) |
| 217 | + .minus(Duration.ofMinutes(30)); |
| 218 | + // End time = start time + 1 hour |
| 219 | + Instant endTime = startTime.plus(Duration.ofHours(1)); |
| 220 | + |
| 221 | + String logType = "Exchange"; |
| 222 | + int lookBackHours = 96; // Configure 4 days range limit |
| 223 | + |
| 224 | + when(sourceConfig.getLookBackHours()).thenReturn(lookBackHours); |
| 225 | + when(office365RestClient.searchAuditLogs( |
| 226 | + any(String.class), |
| 227 | + any(Instant.class), |
| 228 | + any(Instant.class), |
| 229 | + any() |
| 230 | + )).thenReturn(new AuditLogsResponse(new ArrayList<>(), null)); |
| 231 | + |
| 232 | + office365Service.searchAuditLogs(logType, startTime, endTime, null); |
| 233 | + |
| 234 | + // Capture the actual start time that was passed to the REST client |
| 235 | + ArgumentCaptor<Instant> startTimeCaptor = ArgumentCaptor.forClass(Instant.class); |
| 236 | + verify(office365RestClient).searchAuditLogs( |
| 237 | + eq(logType), |
| 238 | + startTimeCaptor.capture(), |
| 239 | + eq(endTime), |
| 240 | + isNull() |
| 241 | + ); |
| 242 | + |
| 243 | + // Verify that the service adjusted the start time correctly: |
| 244 | + Instant capturedStartTime = startTimeCaptor.getValue(); |
| 245 | + Duration actualLookback = Duration.between(capturedStartTime, now); |
| 246 | + assertEquals(96, actualLookback.toHours(), |
| 247 | + "Adjusted start time should be exactly 96 hours ago"); |
| 248 | + } |
| 249 | + |
| 250 | + |
211 | 251 | private Map<String, Object> createTestItem(String contentId, Instant contentCreated) { |
212 | 252 | Map<String, Object> item = new HashMap<>(); |
213 | 253 | item.put("contentId", contentId); |
|
0 commit comments