2828import org .opensearch .dataprepper .model .record .Record ;
2929import org .opensearch .dataprepper .plugins .source .microsoft_office365 .models .AuditLogsResponse ;
3030import org .opensearch .dataprepper .plugins .source .source_crawler .coordination .state .DimensionalTimeSliceWorkerProgressState ;
31+ import org .opensearch .dataprepper .plugins .source .source_crawler .exception .SaaSCrawlerException ;
3132import org .opensearch .dataprepper .metrics .PluginMetrics ;
3233import org .opensearch .dataprepper .plugins .source .microsoft_office365 .service .Office365Service ;
3334import org .slf4j .Logger ;
4142
4243import static org .junit .jupiter .api .Assertions .assertEquals ;
4344import static org .junit .jupiter .api .Assertions .assertFalse ;
45+ import static org .junit .jupiter .api .Assertions .assertTrue ;
4446import static org .junit .jupiter .api .Assertions .assertNotNull ;
4547import static org .junit .jupiter .api .Assertions .assertThrows ;
4648
@@ -180,9 +182,14 @@ void testExecutePartitionWithJsonProcessingError() throws Exception {
180182 return null ;
181183 }).when (bufferWriteLatencyTimer ).record (any (Runnable .class ));
182184
183- client .executePartition (state , buffer , acknowledgementSet );
185+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
186+ () -> client .executePartition (state , buffer , acknowledgementSet ));
187+
188+ assertEquals ("Error processing audit log: ID1" , exception .getMessage ());
189+ assertFalse (exception .isRetryable ());
190+ assertTrue (exception .getCause () instanceof SaaSCrawlerException );
191+ assertEquals ("Failed to parse audit log: ID1" , exception .getCause ().getMessage ());
184192
185- verify (buffer ).writeAll (argThat (list -> list .isEmpty ()), anyInt ());
186193 verify (mockRequestErrorsCounter , never ()).increment ();
187194 }
188195
@@ -250,10 +257,11 @@ void testBufferWriteTimeout() throws Exception {
250257 .when (buffer )
251258 .writeAll (any (), anyInt ());
252259
253- RuntimeException exception = assertThrows (RuntimeException .class ,
260+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
254261 () -> client .executePartition (state , buffer , acknowledgementSet ));
255262
256263 assertEquals ("Error writing to buffer" , exception .getMessage ());
264+ assertTrue (exception .isRetryable ());
257265 verify (buffer ).writeAll (any (), anyInt ());
258266 }
259267
@@ -281,9 +289,15 @@ void testNonRetryableError() throws Exception {
281289 return null ;
282290 }).when (bufferWriteLatencyTimer ).record (any (Runnable .class ));
283291
284- client .executePartition (state , buffer , acknowledgementSet );
292+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
293+ () -> client .executePartition (state , buffer , acknowledgementSet ));
285294
286- verify (buffer ).writeAll (argThat (list -> list .isEmpty ()), anyInt ());
295+ assertEquals ("Error processing audit log: ID1" , exception .getMessage ());
296+ assertFalse (exception .isRetryable ());
297+ assertTrue (exception .getCause () instanceof SaaSCrawlerException );
298+ assertEquals ("Received null log content for URI: uri1" , exception .getCause ().getMessage ());
299+
300+ verify (buffer , never ()).writeAll (argThat (list -> list .isEmpty ()), anyInt ());
287301 }
288302
289303 @ Test
@@ -311,9 +325,15 @@ void testMissingWorkloadField() throws Exception {
311325 return null ;
312326 }).when (bufferWriteLatencyTimer ).record (any (Runnable .class ));
313327
314- client .executePartition (state , buffer , acknowledgementSet );
328+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
329+ () -> client .executePartition (state , buffer , acknowledgementSet ));
330+
331+ assertEquals ("Error processing audit log: ID1" , exception .getMessage ());
332+ assertFalse (exception .isRetryable ());
333+ assertTrue (exception .getCause () instanceof SaaSCrawlerException );
334+ assertEquals ("Missing Workload field in audit log: ID1" , exception .getCause ().getMessage ());
315335
316- verify (buffer ).writeAll (argThat (list -> list .isEmpty ()), anyInt ());
336+ verify (buffer , never () ).writeAll (argThat (list -> list .isEmpty ()), anyInt ());
317337 }
318338
319339 @ Test
@@ -330,14 +350,43 @@ void testExecutePartitionWithSearchAuditLogsError() throws Exception {
330350 any (Instant .class ),
331351 any (Instant .class ),
332352 isNull ()
333- )).thenThrow (new RuntimeException ("Search audit logs failed" ));
353+ )).thenThrow (new SaaSCrawlerException ("Search audit logs failed" , true ));
334354
335355 // Execute and verify exception
336- RuntimeException exception = assertThrows (RuntimeException .class ,
356+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
337357 () -> client .executePartition (state , buffer , acknowledgementSet ));
338358
339359 // Verify exception message and counter increment
340360 assertEquals ("Search audit logs failed" , exception .getMessage ());
361+ assertTrue (exception .isRetryable ());
341362 verify (mockRequestErrorsCounter ).increment ();
342363 }
364+
365+ @ Test
366+ void testExecutePartitionWithNonSaaSCrawlerException () throws Exception {
367+ // Create the counter mock before creating the client
368+ Counter mockRequestErrorsCounter = mock (Counter .class );
369+ when (pluginMetrics .counter (REQUEST_ERRORS )).thenReturn (mockRequestErrorsCounter ); // Use the constant
370+
371+ // Create client after counter is mocked
372+ Office365CrawlerClient client = new Office365CrawlerClient (service , sourceConfig , pluginMetrics );
373+
374+ // Simulate a non-SaaSCrawlerException (like RuntimeException)
375+ when (service .searchAuditLogs (
376+ anyString (),
377+ any (Instant .class ),
378+ any (Instant .class ),
379+ any ()
380+ )).thenThrow (new RuntimeException ("Unexpected error" ));
381+
382+ // Execute and verify exception
383+ SaaSCrawlerException exception = assertThrows (SaaSCrawlerException .class ,
384+ () -> client .executePartition (state , buffer , acknowledgementSet ));
385+
386+ // Verify:
387+ assertEquals ("Failed to process partition" , exception .getMessage ());
388+ assertFalse (exception .isRetryable ());
389+ assertTrue (exception .getCause () instanceof RuntimeException );
390+ verify (mockRequestErrorsCounter ).increment (); // Verify counter increment
391+ }
343392}
0 commit comments