Skip to content

Commit 2980ad1

Browse files
committed
Optimized RestClient Tests using customized Retry Handler
Signed-off-by: eatulban <[email protected]>
1 parent 37d65f8 commit 2980ad1

File tree

1 file changed

+23
-5
lines changed
  • data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/test/java/org/opensearch/dataprepper/plugins/source/microsoft_office365

1 file changed

+23
-5
lines changed

data-prepper-plugins/saas-source-plugins/microsoft-office365-source/src/test/java/org/opensearch/dataprepper/plugins/source/microsoft_office365/Office365RestClientTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.opensearch.dataprepper.plugins.source.microsoft_office365.auth.Office365AuthenticationInterface;
2323
import org.opensearch.dataprepper.plugins.source.microsoft_office365.models.AuditLogsResponse;
2424
import 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;
2528
import org.opensearch.dataprepper.test.helper.ReflectivelySetField;
2629
import org.springframework.core.ParameterizedTypeReference;
2730
import 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

Comments
 (0)