|
6 | 6 | import org.apache.http.HttpStatus; |
7 | 7 | import org.apache.http.StatusLine; |
8 | 8 | import org.apache.http.client.methods.CloseableHttpResponse; |
| 9 | +import org.apache.http.conn.ConnectTimeoutException; |
9 | 10 | import org.apache.http.impl.client.CloseableHttpClient; |
10 | 11 | import org.apache.http.impl.client.HttpClientBuilder; |
11 | 12 | import org.apache.http.message.BasicStatusLine; |
|
19 | 20 | import org.powermock.modules.junit4.PowerMockRunner; |
20 | 21 |
|
21 | 22 | import java.io.IOException; |
| 23 | +import java.net.SocketException; |
22 | 24 |
|
23 | 25 | @RunWith(PowerMockRunner.class) |
24 | 26 | @PrepareForTest({ |
@@ -102,4 +104,49 @@ public void testExecuteGet_StatusOk() throws IOException { |
102 | 104 | ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config, true); |
103 | 105 | client.executeGet(request); |
104 | 106 | } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testExecuteGet_throwConnectTimeoutException_markAsRetryable() throws IOException { |
| 110 | + CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); |
| 111 | + HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class); |
| 112 | + PowerMockito.mockStatic(HttpClientBuilder.class); |
| 113 | + PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder); |
| 114 | + Mockito.when(httpClientBuilder.build()).thenReturn(httpClient); |
| 115 | + Mockito.when(httpClient.execute(Mockito.any())) |
| 116 | + .thenThrow(new ConnectTimeoutException("Connection timed out")); |
| 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 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testExecuteGet_throwSocketException_markAsRetryable() throws IOException { |
| 133 | + CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); |
| 134 | + HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class); |
| 135 | + PowerMockito.mockStatic(HttpClientBuilder.class); |
| 136 | + PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder); |
| 137 | + Mockito.when(httpClientBuilder.build()).thenReturn(httpClient); |
| 138 | + Mockito.when(httpClient.execute(Mockito.any())) |
| 139 | + .thenThrow(new SocketException()); |
| 140 | + |
| 141 | + ServiceNowTableAPIRequestBuilder builder = new ServiceNowTableAPIRequestBuilder("url"); |
| 142 | + RestAPIRequest request = builder.build(); |
| 143 | + |
| 144 | + ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class); |
| 145 | + ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config, true); |
| 146 | + RestAPIResponse actualResponse = client.executeGet(request); |
| 147 | + Assert.assertNotNull(actualResponse.getException()); |
| 148 | + Assert.assertTrue(actualResponse.getException().isErrorRetryable()); |
| 149 | + Throwable ex = actualResponse.getException().getCause(); |
| 150 | + Assert.assertTrue("Expected SocketException or similar, got: " + ex, ex instanceof SocketException); |
| 151 | + } |
105 | 152 | } |
0 commit comments