Skip to content

Commit 36d1318

Browse files
authored
Optimized RestClient Tests using customized Retry Handler (#6359)
Signed-off-by: eatulban <eatulban@amazon.com>
1 parent 1d5c891 commit 36d1318

File tree

1 file changed

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

1 file changed

+19
-1
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.opensearch.dataprepper.plugins.source.microsoft_office365.models.AuditLogsResponse;
2121
import org.opensearch.dataprepper.plugins.source.source_crawler.exception.SaaSCrawlerException;
2222
import org.opensearch.dataprepper.plugins.source.source_crawler.metrics.VendorAPIMetricsRecorder;
23+
import org.opensearch.dataprepper.plugins.source.source_crawler.utils.retry.DefaultRetryStrategy;
24+
import org.opensearch.dataprepper.plugins.source.source_crawler.utils.retry.DefaultStatusCodeHandler;
25+
import org.opensearch.dataprepper.plugins.source.source_crawler.utils.retry.RetryHandler;
2326
import org.opensearch.dataprepper.test.helper.ReflectivelySetField;
2427
import org.springframework.core.ParameterizedTypeReference;
2528
import org.springframework.http.HttpEntity;
@@ -73,6 +76,8 @@ class Office365RestClientTest {
7376

7477
private Office365RestClient office365RestClient;
7578

79+
private static final int CUSTOM_MAX_RETRIES = 1;
80+
7681
@BeforeEach
7782
void setUp() throws NoSuchFieldException, IllegalAccessException {
7883
// Setup VendorAPIMetricsRecorder method mocks - use lenient to avoid unnecessary stubbing errors
@@ -110,6 +115,12 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
110115

111116
office365RestClient = new Office365RestClient(authConfig, metricsRecorder);
112117
ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "restTemplate", restTemplate);
118+
119+
// Optionally replace RetryHandler with custom one (1 retry) for faster test execution
120+
RetryHandler customRetryHandler = new RetryHandler(
121+
new DefaultRetryStrategy(CUSTOM_MAX_RETRIES),
122+
new DefaultStatusCodeHandler());
123+
ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "retryHandler", customRetryHandler);
113124
}
114125

115126
/**
@@ -363,11 +374,18 @@ void testGetAuditLogFailure() {
363374
* Verifies that failed authentication triggers credential renewal and retry succeeds.
364375
*/
365376
@Test
366-
void testTokenRenewal() {
377+
void testTokenRenewal() throws NoSuchFieldException, IllegalAccessException {
367378
// Setup
368379
Instant startTime = Instant.now().minus(1, ChronoUnit.HOURS);
369380
Instant endTime = Instant.now();
370381

382+
// Override retry handler to allow at least 2 attempts for token renewal test
383+
RetryHandler tokenRenewalRetryHandler = new RetryHandler(
384+
new DefaultRetryStrategy(2), // Need at least 2 attempts for token renewal scenario
385+
new DefaultStatusCodeHandler());
386+
ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "retryHandler",
387+
tokenRenewalRetryHandler);
388+
371389
List<String> tokensUsed = new ArrayList<>();
372390
List<String> requestTokens = new ArrayList<>();
373391

0 commit comments

Comments
 (0)