2222import org .opensearch .dataprepper .plugins .source .microsoft_office365 .auth .Office365AuthenticationInterface ;
2323import org .opensearch .dataprepper .plugins .source .microsoft_office365 .models .AuditLogsResponse ;
2424import org .opensearch .dataprepper .plugins .source .source_crawler .exception .SaaSCrawlerException ;
25+ import org .opensearch .dataprepper .plugins .source .source_crawler .utils .retry .DefaultRetryStrategy ;
26+ import org .opensearch .dataprepper .plugins .source .source_crawler .utils .retry .DefaultStatusCodeHandler ;
27+ import org .opensearch .dataprepper .plugins .source .source_crawler .utils .retry .RetryHandler ;
2528import org .opensearch .dataprepper .test .helper .ReflectivelySetField ;
2629import org .springframework .core .ParameterizedTypeReference ;
2730import org .springframework .http .HttpEntity ;
@@ -64,10 +67,18 @@ class Office365RestClientTest {
6467
6568 private Office365RestClient office365RestClient ;
6669
70+ private static final int CUSTOM_MAX_RETRIES = 1 ;
71+
6772 @ BeforeEach
6873 void setUp () throws NoSuchFieldException , IllegalAccessException {
6974 office365RestClient = new Office365RestClient (authConfig , pluginMetrics );
7075 ReflectivelySetField .setField (Office365RestClient .class , office365RestClient , "restTemplate" , restTemplate );
76+
77+ // Optionally replace RetryHandler with custom one (1 retry) for faster test execution
78+ RetryHandler customRetryHandler = new RetryHandler (
79+ new DefaultRetryStrategy (CUSTOM_MAX_RETRIES ),
80+ new DefaultStatusCodeHandler ());
81+ ReflectivelySetField .setField (Office365RestClient .class , office365RestClient , "retryHandler" , customRetryHandler );
7182 }
7283
7384 @ Test
@@ -255,11 +266,18 @@ void testGetAuditLogFailure() {
255266 }
256267
257268 @ Test
258- void testTokenRenewal () {
269+ void testTokenRenewal () throws NoSuchFieldException , IllegalAccessException {
259270 // Setup
260271 Instant startTime = Instant .now ().minus (1 , ChronoUnit .HOURS );
261272 Instant endTime = Instant .now ();
262273
274+ // Override retry handler to allow at least 2 attempts for token renewal test
275+ RetryHandler tokenRenewalRetryHandler = new RetryHandler (
276+ new DefaultRetryStrategy (2 ), // Need at least 2 attempts for token renewal scenario
277+ new DefaultStatusCodeHandler ());
278+ ReflectivelySetField .setField (Office365RestClient .class , office365RestClient , "retryHandler" ,
279+ tokenRenewalRetryHandler );
280+
263281 List <String > tokensUsed = new ArrayList <>();
264282 List <String > requestTokens = new ArrayList <>();
265283
@@ -523,8 +541,8 @@ void testApiCallsCounterIncrementOnRetries() throws NoSuchFieldException, Illega
523541 // Execute and expect exception
524542 assertThrows (RuntimeException .class , () -> office365RestClient .getAuditLog (contentUri ));
525543
526- // Verify apiCallsCounter was incremented 6 times (once for each retry attempt)
527- verify (mockApiCallsCounter , times (6 )).increment ();
544+ // Verify apiCallsCounter was incremented CUSTOM_MAX_RETRIES times
545+ verify (mockApiCallsCounter , times (CUSTOM_MAX_RETRIES )).increment ();
528546 }
529547
530548 @ Test
@@ -549,7 +567,7 @@ void testApiCallsCounterIncrementForSearchAuditLogsWithRetries() throws NoSuchFi
549567 null
550568 ));
551569
552- // Verify apiCallsCounter was incremented 6 times (once for each retry attempt)
553- verify (mockApiCallsCounter , times (6 )).increment ();
570+ // Verify apiCallsCounter was incremented CUSTOM_MAX_RETRIES times
571+ verify (mockApiCallsCounter , times (CUSTOM_MAX_RETRIES )).increment ();
554572 }
555573}
0 commit comments