*/
package org.onap.holmes.common.aai;
+import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.aai.entity.VmEntity;
private String getResponse(String url) throws CorrelationException {
String response;
+ CloseableHttpClient httpClient = null;
try {
- HttpResponse httpResponse = HttpsUtils.get(url, getHeaders());
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.get(url, getHeaders(), httpClient);
response = HttpsUtils.extractResponseEntity(httpResponse);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from aai", e);
+ } finally {
+ if (httpClient != null) {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("Failed to close http client!");
+ }
+ }
}
return response;
}
*/\r
package org.onap.holmes.common.dmaap;\r
\r
+import java.io.IOException;\r
+import lombok.extern.slf4j.Slf4j;\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
import com.alibaba.fastjson.JSON;\r
@Getter\r
@Setter\r
@Service\r
+@Slf4j\r
public class Publisher {\r
\r
private String topic;\r
HashMap<String, String> headers = new HashMap<>();\r
headers.put("Accept", MediaType.APPLICATION_JSON);\r
headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
+ CloseableHttpClient httpClient = null;\r
try {\r
- httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"));\r
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);\r
+ httpResponse = HttpsUtils.post(url, 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
+ if (httpClient != null) {\r
+ try {\r
+ httpClient.close();\r
+ } catch (IOException e) {\r
+ log.warn("Failed to close http client!");\r
+ }\r
+ }\r
}\r
return checkStatus(httpResponse);\r
}\r
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
- private static final int DEFUALT_TIMEOUT = 30000;
private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
private static PoolingHttpClientConnectionManager connectionManager = null;
private static SSLContextBuilder sslContextBuilder = null;
+ public static final int DEFUALT_TIMEOUT = 30000;
static{
try {
}
public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity) throws CorrelationException {
- return post(url, header, param, entity, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity, int timeout) throws CorrelationException {
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
HttpResponse response;
HttpPost httpPost = new HttpPost(url);
try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
addHeaders(header, httpPost);
addParams(param, httpPost);
if (entity != null) {
}
public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity) throws CorrelationException {
- return put(url, header, param, entity, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity, int timeout) throws CorrelationException {
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
HttpResponse response;
HttpPut httpPut = new HttpPut(url);
try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
addHeaders(header, httpPut);
addParams(param, httpPut);
if (entity != null) {
return response;
}
- public static HttpResponse get(String url, Map<String, String> header) throws CorrelationException {
- return get(url, header, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse get(String url, Map<String, String> header, int timeout) throws CorrelationException {
+ public static HttpResponse get(String url, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
HttpResponse response;
HttpGet httpGet = new HttpGet(url);
try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
addHeaders(header, httpGet);
response = executeRequest(httpClient, httpGet);
} catch (Exception e) {
return response;
}
- public static HttpResponse delete(String url, Map<String, String> header) throws CorrelationException {
- return delete(url, header, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse delete(String url, Map<String, String> header, int timeout) throws CorrelationException {
+ public static HttpResponse delete(String url, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
HttpResponse response;
HttpDelete httpDelete = new HttpDelete(url);
try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
addHeaders(header, httpDelete);
response = executeRequest(httpClient, httpDelete);
} catch (Exception e) {
return httpResponse;
}
- private static CloseableHttpClient getHttpClient(int timeout) throws Exception {
+ public static CloseableHttpClient getHttpClient(int timeout) {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(timeout)
.setConnectTimeout(timeout)
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.easymock.EasyMock;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
headers.put("Accept", "application/json");
String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+ when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+ when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("{}");
PowerMockito.mockStatic(MicroServiceConfig.class);
PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2")
.andReturn("/aai/v11/cloud-infrastructure");
+ PowerMock.expectPrivate(httpClient, "close");
+ EasyMock.expectLastCall();
PowerMock.replayAll();
VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
PowerMock.verifyAll();
headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
headers.put("Accept", "application/json");
String url = "http://10.96.33.33/api/aai-cloudInfrastructure/v11";
-
- when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
-
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+ when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+ when(HttpsUtils.get(url, 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")
.andReturn("/aai/v11/cloud-infrastructure");
+ PowerMock.expectPrivate(httpClient,"close");
+ EasyMock.expectLastCall();
PowerMock.replayAll();
Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
PowerMock.verifyAll();
String url = "host_url";
HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- when(HttpsUtils.get(url, headers)).thenReturn(httpResponse);
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+ when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+ when(HttpsUtils.get(url, headers, httpClient)).thenReturn(httpResponse);
when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn("");
+ PowerMock.expectPrivate(httpClient, "close");
+ EasyMock.expectLastCall();
PowerMock.replayAll();
String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
headers.put("Accept", "application/json");
String url = "host_url";
- when(HttpsUtils.get(url, headers)).thenThrow(new CorrelationException(""));
+ CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
+ when(HttpsUtils.getHttpClient(30000)).thenReturn(httpClient);
+ when(HttpsUtils.get(url, headers, httpClient)).thenThrow(new CorrelationException(""));
+ PowerMock.expectPrivate(httpClient, "close");
+ EasyMock.expectLastCall();
PowerMock.replayAll();
String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
PowerMock.verifyAll();
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.easymock.EasyMock;
import org.junit.Rule;
import org.junit.Test;
PowerMockito.mockStatic(HttpsUtils.class);
HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
- PowerMockito.when(HttpsUtils.post(Matchers.eq("http://localhost/dmaapTopic"),
- Matchers.any(HashMap.class), Matchers.any(HashMap.class), Matchers.any(StringEntity.class))).thenReturn(httpResponse);
+ PowerMockito.when(HttpsUtils
+ .post(Matchers.eq("http://localhost/dmaapTopic"), Matchers.any(HashMap.class),
+ Matchers.any(HashMap.class), Matchers.any(StringEntity.class),
+ Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
StatusLine statusLine = PowerMockito.mock(StatusLine.class);
PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
import org.junit.runner.RunWith;
import org.onap.holmes.common.exception.CorrelationException;
import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
@PrepareForTest({CloseableHttpClient.class, HttpClientBuilder.class, HttpClients.class, CloseableHttpResponse.class,
StatusLine.class})
@RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
public class HttpsUtilsTest {
@Rule
@Test
public void testHttpsUtil_get_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through GET method!");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpResponse httpResponse = HttpsUtils.get(url, header);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_get_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
header.put("accept", "application/json");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.get(url, header);
+ HttpResponse httpResponse = HttpsUtils.get(url, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@Test
public void testHttpsUtil_delete_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through DELETE method!");
String url = "host";
Map<String, String> header = new HashMap<>();
header.put("accept", "application/json");
- HttpResponse httpResponse = HttpsUtils.delete(url, header);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_delete_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
header.put("accept", "application/json");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.delete(url, header);
+ HttpResponse httpResponse = HttpsUtils.delete(url, header, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
@Test
public void testHttpsUtil_post_excepiton() throws Exception {
+ PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to query data from server through POST method!");
String url = "host";
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
-
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, null);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_post_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity);
+ HttpResponse httpResponse = HttpsUtils.post(url, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
header.put("accept", "application/json");
Map<String, String> para = new HashMap<>();
para.put("tset", "1111");
-
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, null);
+ CloseableHttpClient httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, null, httpClient);
String response = HttpsUtils.extractResponseEntity(httpResponse);
assertThat(response, equalTo(""));
}
@Test
public void testHttpsUtil_put_normal() throws Exception {
- HttpClientBuilder hcb = PowerMock.createMock(HttpClientBuilder.class);
+ PowerMock.resetAll();
CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- PowerMock.mockStatic(HttpClients.class);
- EasyMock.expect(HttpClients.custom()).andReturn(hcb);
- EasyMock.expect(hcb.setDefaultRequestConfig(EasyMock.anyObject(RequestConfig.class))).andReturn(hcb);
- EasyMock.expect(hcb.setSSLSocketFactory(EasyMock.anyObject(SSLConnectionSocketFactory.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManager(EasyMock.anyObject(PoolingHttpClientConnectionManager.class))).andReturn(hcb);
- EasyMock.expect(hcb.setConnectionManagerShared(true)).andReturn(hcb);
- EasyMock.expect(hcb.build()).andReturn(httpClient);
-
CloseableHttpResponse response = PowerMock.createMock(CloseableHttpResponse.class);
EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpRequestBase.class))).andReturn(response);
StatusLine sl = PowerMock.createMock(StatusLine.class);
para.put("tset", "1111");
HttpEntity entity = new StringEntity("Test");
- HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity);
+ HttpResponse httpResponse = HttpsUtils.put(url, header, para, entity, httpClient);
String res = HttpsUtils.extractResponseEntity(httpResponse);
PowerMock.verifyAll();
PowerMock.verifyAll();
}
+ @Test
+ public void testHttpsUtil_getHttpClient_ok() throws Exception {
+ PowerMock.resetAll();
+ HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ PowerMock.verifyAll();
+ }
+
}
\ No newline at end of file
<dependency>\r
<groupId>org.onap.msb.java-sdk</groupId>\r
<artifactId>msb-java-sdk</artifactId>\r
- <version>1.1.0-SNAPSHOT</version>\r
+ <version>1.1.1-SNAPSHOT</version>\r
</dependency>\r
<dependency>\r
<groupId>org.glassfish.jersey.core</groupId>\r