Skip to content

Commit 6394456

Browse files
committed
PLUGIN-1928: Add ConnectTimeOutException handling and unit test
1 parent bff0498 commit 6394456

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/io/cdap/plugin/servicenow/restapi/RestAPIClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.http.client.methods.CloseableHttpResponse;
2929
import org.apache.http.client.methods.HttpGet;
3030
import org.apache.http.client.methods.HttpPost;
31+
import org.apache.http.conn.ConnectTimeoutException;
3132
import org.apache.http.impl.client.CloseableHttpClient;
3233
import org.apache.http.impl.client.HttpClientBuilder;
3334
import org.apache.oltu.oauth2.client.OAuthClient;
@@ -42,6 +43,7 @@
4243
import org.slf4j.LoggerFactory;
4344

4445
import java.io.IOException;
46+
import java.util.Collections;
4547
import java.util.concurrent.Callable;
4648
import java.util.concurrent.ExecutionException;
4749
import java.util.concurrent.TimeUnit;
@@ -77,6 +79,9 @@ public RestAPIResponse executeGet(RestAPIRequest request) throws IOException {
7779
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
7880
return RestAPIResponse.parse(httpResponse, request.getResponseHeaders());
7981
}
82+
} catch (ConnectTimeoutException e) {
83+
ServiceNowAPIException exception = new ServiceNowAPIException(e, null);
84+
return new RestAPIResponse(Collections.emptyMap(), null, exception);
8085
}
8186
}
8287

src/test/java/io/cdap/plugin/servicenow/restapi/RestAPIClientTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.http.HttpStatus;
77
import org.apache.http.StatusLine;
88
import org.apache.http.client.methods.CloseableHttpResponse;
9+
import org.apache.http.conn.ConnectTimeoutException;
910
import org.apache.http.impl.client.CloseableHttpClient;
1011
import org.apache.http.impl.client.HttpClientBuilder;
1112
import org.apache.http.message.BasicStatusLine;
@@ -102,4 +103,28 @@ public void testExecuteGet_StatusOk() throws IOException {
102103
ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config, true);
103104
client.executeGet(request);
104105
}
106+
107+
@Test
108+
public void testExecuteGet_throwConnectTimeoutException_markAsRetryable() throws IOException {
109+
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
110+
HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
111+
PowerMockito.mockStatic(HttpClientBuilder.class);
112+
PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
113+
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
114+
Mockito.when(httpClient.execute(Mockito.any()))
115+
.thenThrow(new ConnectTimeoutException("Connection timed out"));
116+
117+
118+
ServiceNowTableAPIRequestBuilder builder = new ServiceNowTableAPIRequestBuilder("url");
119+
RestAPIRequest request = builder.build();
120+
121+
ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class);
122+
ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config, true);
123+
RestAPIResponse actualResponse = client.executeGet(request);
124+
Assert.assertNotNull(actualResponse.getException());
125+
Assert.assertTrue(actualResponse.getException().isErrorRetryable());
126+
Throwable ex = actualResponse.getException().getCause();
127+
Assert.assertTrue("Expected ConnectTimeoutException or similar, got: " + ex,
128+
ex instanceof ConnectTimeoutException || ex.getMessage().toLowerCase().contains("Connection timed out"));
129+
}
105130
}

0 commit comments

Comments
 (0)