-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRestAPIClientTest.java
More file actions
113 lines (98 loc) · 5.14 KB
/
Copy pathRestAPIClientTest.java
File metadata and controls
113 lines (98 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io.cdap.plugin.servicenow.restapi;
import io.cdap.plugin.servicenow.apiclient.NonRetryableException;
import io.cdap.plugin.servicenow.apiclient.RetryableException;
import io.cdap.plugin.servicenow.apiclient.ServiceNowTableAPIClientImpl;
import io.cdap.plugin.servicenow.apiclient.ServiceNowTableAPIRequestBuilder;
import io.cdap.plugin.servicenow.connector.ServiceNowConnectorConfig;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicStatusLine;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
@RunWith(PowerMockRunner.class)
@PrepareForTest({
RestAPIClient.class,
HttpClientBuilder.class,
RestAPIResponse.class,
ServiceNowTableAPIClientImpl.class,
EntityUtils.class
})
public class RestAPIClientTest {
@Test(expected = RetryableException.class)
public void testExecuteGet_throwRetryableException() throws IOException {
CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
StatusLine statusLine = Mockito.mock(BasicStatusLine.class);
Mockito.when(statusLine.getStatusCode()).thenReturn(429);
Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
PowerMockito.mockStatic(HttpClientBuilder.class);
PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
Mockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
ServiceNowTableAPIRequestBuilder builder = new ServiceNowTableAPIRequestBuilder("url");
RestAPIRequest request = builder.build();
ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class);
ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config);
try {
client.executeGet(request);
} catch (RetryableException e) {
Assert.assertEquals(Integer.valueOf(429), e.getHttpStatusCode());
throw e;
}
}
@Test(expected = NonRetryableException.class)
public void testExecuteGet_throwIOException() throws IOException {
CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
StatusLine statusLine = Mockito.mock(BasicStatusLine.class);
Mockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR);
Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
PowerMockito.mockStatic(HttpClientBuilder.class);
PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
Mockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
ServiceNowTableAPIRequestBuilder builder = new ServiceNowTableAPIRequestBuilder("url");
RestAPIRequest request = builder.build();
ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class);
ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config);
try {
client.executeGet(request);
} catch (NonRetryableException e) {
Assert.assertEquals(
Integer.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR), e.getHttpStatusCode());
throw e;
}
}
@Test
public void testExecuteGet_StatusOk() throws IOException {
CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
StatusLine statusLine = Mockito.mock(BasicStatusLine.class);
Mockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
PowerMockito.mockStatic(HttpClientBuilder.class);
PowerMockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
Mockito.when(httpClient.execute(Mockito.any())).thenReturn(httpResponse);
PowerMockito.mockStatic(EntityUtils.class);
PowerMockito.when(EntityUtils.toString(Mockito.any())).thenReturn("{}");
ServiceNowTableAPIRequestBuilder builder = new ServiceNowTableAPIRequestBuilder("url");
RestAPIRequest request = builder.build();
ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class);
ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config);
client.executeGet(request);
}
}