import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
private String getResponse(String url) throws CorrelationException {
String response;
CloseableHttpClient httpClient = null;
+ HttpGet httpGet = new HttpGet(url);
try {
httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.get(url, getHeaders(), httpClient);
+ HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
response = HttpsUtils.extractResponseEntity(httpResponse);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from aai", e);
} finally {
+ httpGet.releaseConnection();
if (httpClient != null) {
try {
httpClient.close();
\r
import java.io.IOException;\r
import lombok.extern.slf4j.Slf4j;\r
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;\r
+import org.apache.http.client.methods.HttpPost;\r
import org.apache.http.impl.client.CloseableHttpClient;\r
import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
import org.onap.holmes.common.exception.CorrelationException;\r
headers.put("Accept", MediaType.APPLICATION_JSON);\r
headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
CloseableHttpClient httpClient = null;\r
+ HttpPost httpPost = new HttpPost(url);\r
try {\r
httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
- httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient);\r
+ httpResponse = HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient);\r
} catch (Exception e) {\r
throw new CorrelationException("Failed to connect to DCAE.", e);\r
} finally {\r
+ httpPost.releaseConnection();\r
if (httpClient != null) {\r
try {\r
httpClient.close();\r
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
}
}
- public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
- HttpResponse response;
- HttpPost httpPost = new HttpPost(url);
- try {
- addHeaders(header, httpPost);
- addParams(param, httpPost);
- if (entity != null) {
- httpPost.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPost);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through POST method!");
- }
- return response;
+ public static HttpResponse get(HttpGet httpGet, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpGet, header, httpClient);
}
- public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
+ public static HttpResponse post(HttpPost httpPost, Map<String, String> header, Map<String, String> param,
HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
- HttpResponse response;
- HttpPut httpPut = new HttpPut(url);
- try {
- addHeaders(header, httpPut);
- addParams(param, httpPut);
- if (entity != null) {
- httpPut.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPut);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through PUT method!");
- }
- return response;
+ return getPostAndPutResponse(httpPost, header, param, entity, httpClient);
}
- public static HttpResponse get(String url, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
- HttpResponse response;
- HttpGet httpGet = new HttpGet(url);
- try {
- addHeaders(header, httpGet);
- response = executeRequest(httpClient, httpGet);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through GET method!");
- }
- return response;
+ public static HttpResponse put(HttpPut httpPut, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPut, header, param, entity, httpClient);
}
- public static HttpResponse delete(String url, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
- HttpResponse response;
- HttpDelete httpDelete = new HttpDelete(url);
- try {
- addHeaders(header, httpDelete);
- response = executeRequest(httpClient, httpDelete);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through DELETE method!");
- }
- return response;
+ public static HttpResponse delete(HttpDelete httpDelete, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpDelete, header, httpClient);
}
private static void addParams(Map<String, String> param, HttpEntityEnclosingRequestBase requestBase) {
return httpRequestBase;
}
+ private static HttpResponse getPostAndPutResponse(HttpEntityEnclosingRequestBase requestBase,
+ Map<String, String> header, Map<String, String> param, HttpEntity entity,
+ CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ addParams(param, requestBase);
+ if (entity != null) {
+ requestBase.setEntity(entity);
+ }
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
+ private static HttpResponse getGetAndDeleteResponse(HttpRequestBase requestBase,
+ Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
public static String extractResponseEntity(HttpResponse httpResponse)
throws CorrelationException, IOException {
String result = "";
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.easymock.EasyMock;
import org.junit.Rule;
import org.powermock.reflect.Whitebox;
-@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class})
+@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
@RunWith(PowerMockRunner.class)
public class AaiQueryTest {
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
- when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
+ HttpGet httpGet = new HttpGet(url);
+ PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+ when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
PowerMockito.mockStatic(MicroServiceConfig.class);
String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
- when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException(""));
+ HttpGet httpGet = new HttpGet(url);
+ PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+ when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
PowerMockito.mockStatic(MicroServiceConfig.class);
when(MicroServiceConfig.getMsbServerAddrWithHttpPrefix()).thenReturn("http://10.96.33.33:80");
PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
- when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
+ HttpGet httpGet = new HttpGet(url);
+ PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+ when(HttpsUtils.get(httpGet, headers, httpClient)).thenReturn(httpResponse);
when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
PowerMock.expectPrivate(httpClient, "close");
EasyMock.expectLastCall();
String url = "host_url";
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
- when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException(""));
+ HttpGet httpGet = new HttpGet(url);
+ PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
+ when(HttpsUtils.get(httpGet, headers, httpClient)).thenThrow(new CorrelationException(""));
PowerMock.expectPrivate(httpClient, "close");
EasyMock.expectLastCall();
PowerMock.replayAll();
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.easymock.EasyMock;
PowerMockito.mockStatic(HttpsUtils.class);
HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
PowerMockito.when(HttpsUtils
- .post(Matchers.eq("http://localhost/dmaapTopic"), Matchers.any(HashMap.class),
+ .post(Matchers.any(HttpPost.class), Matchers.any(HashMap.class),
Matchers.any(HashMap.class), Matchers.any(StringEntity.class),
Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
StatusLine statusLine = PowerMockito.mock(StatusLine.class);
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
public void testHttpsUtil_get_excepiton() throws Exception {
PowerMock.resetAll();
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to query data from server through GET method!");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
+ HttpGet httpRequestBase = new HttpGet(url);
+ HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
+ HttpGet httpRequestBase = new HttpGet(url);
+ HttpResponse httpResponse = HttpsUtils.get(httpRequestBase, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
public void testHttpsUtil_delete_excepiton() throws Exception {
PowerMock.resetAll();
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to query data from server through DELETE method!");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
+ HttpDelete httpRequestBase = new HttpDelete(url);
CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
+ HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
+ HttpDelete httpRequestBase = new HttpDelete(url);
+ HttpResponse httpResponse = HttpsUtils.delete(httpRequestBase, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
public void testHttpsUtil_post_excepiton() throws Exception {
PowerMock.resetAll();
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to query data from server through POST method!");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
+ HttpPost httpPost = new HttpPost(url);
+ HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
+ HttpPost httpPost = new HttpPost(url);
+ HttpResponse httpResponse = HttpsUtils.post(httpPost, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@Test
public void testHttpsUtil_put_excepiton() throws Exception {
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to query data from server through PUT method!");
+ thrown.expectMessage("Failed to connect to server");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
+ HttpPut httpPut = new HttpPut(url);
+ HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
+ HttpPut httpPut = new HttpPut(url);
+ HttpResponse httpResponse = HttpsUtils.put(httpPut, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
<dependency>\r
<groupId>org.onap.msb.java-sdk</groupId>\r
<artifactId>msb-java-sdk</artifactId>\r
- <version>1.1.1-SNAPSHOT</version>\r
+ <version>1.1.1</version>\r
</dependency>\r
<dependency>\r
<groupId>org.glassfish.jersey.core</groupId>\r