summaryrefslogtreecommitdiffstats
path: root/holmes-actions
diff options
context:
space:
mode:
Diffstat (limited to 'holmes-actions')
-rw-r--r--holmes-actions/pom.xml2
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java23
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java71
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java20
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java61
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/exception/HttpException.java27
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java198
-rw-r--r--holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java35
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java51
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java120
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java6
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java62
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java32
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java436
-rw-r--r--holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java95
15 files changed, 837 insertions, 402 deletions
diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml
index 60ba3b7..ecc4016 100644
--- a/holmes-actions/pom.xml
+++ b/holmes-actions/pom.xml
@@ -12,7 +12,7 @@
<parent>
<groupId>org.onap.holmes.common</groupId>
<artifactId>holmes-common-parent</artifactId>
- <version>1.3.2-SNAPSHOT</version>
+ <version>1.3.3-SNAPSHOT</version>
</parent>
<name>holmes-common-service</name>
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
index f409c27..4a4ffcb 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
@@ -14,18 +14,14 @@
package org.onap.holmes.common.aai;
import lombok.extern.slf4j.Slf4j;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-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;
import org.onap.holmes.common.aai.entity.VnfEntity;
import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.utils.JerseyClient;
import javax.inject.Inject;
-import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -95,26 +91,11 @@ public class AaiQuery {
}
private String getResponse(String url) throws CorrelationException {
- String response;
- CloseableHttpClient httpClient = null;
- HttpGet httpGet = new HttpGet(url);
try {
- httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
- response = HttpsUtils.extractResponseEntity(httpResponse);
+ return new JerseyClient().headers(getHeaders()).get(url);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from aai", e);
- } finally {
- httpGet.releaseConnection();
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- log.warn("Failed to close http client!");
- }
- }
}
- return response;
}
private Map getHeaders() {
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java
index e2bc357..892638b 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java
@@ -1,5 +1,5 @@
/**
- * Copyright 2020 Fujitsu Limited.
+ * Copyright 2020 - 2021 Fujitsu Limited.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
@@ -17,30 +17,22 @@ package org.onap.holmes.common.aai;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
-
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPut;
-import org.apache.http.entity.StringEntity;
-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.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.HttpsUtils;
-
-import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo;
-import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
+import org.onap.holmes.common.utils.JerseyClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
+import java.util.HashMap;
+import java.util.Map;
-import org.jvnet.hk2.annotations.Service;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import static org.onap.holmes.common.aai.AaiJsonParserUtil.getInfo;
+import static org.onap.holmes.common.aai.AaiJsonParserUtil.getPath;
@Service
public class AaiQueryMdons {
@@ -71,32 +63,17 @@ public class AaiQueryMdons {
}
return url;
}
-
+
private String getResponse(String url) throws CorrelationException {
- String response;
- CloseableHttpClient httpClient = null;
- HttpGet httpGet = new HttpGet(url);
try {
- httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
- HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
- response = HttpsUtils.extractResponseEntity(httpResponse);
+ return new JerseyClient().headers(getHeaders()).get(url);
} catch (Exception e) {
throw new CorrelationException("Failed to get data from aai", e);
- } finally {
- httpGet.releaseConnection();
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- log.warn("Failed to close http client!");
- }
- }
}
- return response;
}
- private Map<String, String> getHeaders() {
- Map<String, String> headers = new HashMap<>();
+ private Map<String, Object> getHeaders() {
+ Map<String, Object> headers = new HashMap<>();
headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
@@ -195,27 +172,11 @@ public class AaiQueryMdons {
put(url, jsonObject.toString());
}
- private HttpResponse put(String url, String content) throws CorrelationException {
- CloseableHttpClient httpClient = null;
- HttpPut httpPut = new HttpPut(url);
+ private void put(String url, String content) throws CorrelationException {
try {
- httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
- return HttpsUtils.put(httpPut, getHeaders(), new HashMap<>(), new StringEntity(content), httpClient);
+ new JerseyClient().headers(getHeaders()).put(url, Entity.json(content));
} catch (Exception e) {
throw new CorrelationException("Failed to put data in AAI", e);
- } finally {
- closeHttpClient(httpClient);
- }
- }
-
- private void closeHttpClient(CloseableHttpClient httpClient) {
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- log.warn("Failed to close http client!");
- }
}
}
-
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
index 7e33bf9..e9eb003 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
@@ -67,12 +67,14 @@ public class DmaapService {
.getTopicUrl());
publisher.publish(policyMsg);
deleteRequestIdIfNecessary(policyMsg);
+
log.info("send policyMsg: " + GsonUtil.beanToJson(policyMsg));
- } catch (CorrelationException e) {
- log.error("Failed to publish the control loop event to DMaaP", e);
} catch (NullPointerException e) {
log.error(String.format("DMaaP configurations do not exist!\n DCAE Configurations: \n %s",
DcaeConfigurationsCache.getDcaeConfigurations()), e);
+ } catch (Exception e) {
+ log.error(String.format("An error occurred when publishing a message to Policy: %s",
+ e.getMessage()), e);
}
}
@@ -83,7 +85,7 @@ public class DmaapService {
}
private PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm rootAlarm, VesAlarm childAlarm,
- String packageName) {
+ String packageName) {
PolicyMsg policyMsg = new PolicyMsg();
policyMsg.setRequestID(getUniqueRequestId(rootAlarm));
if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ONSET) {
@@ -91,7 +93,7 @@ public class DmaapService {
policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
policyMsg.getAai().put("vserver.in-maint", vmEntity.getInMaint());
policyMsg.getAai().put("vserver.is-closed-loop-disabled",
- vmEntity.getClosedLoopDisable());
+ vmEntity.getClosedLoopDisable());
policyMsg.getAai().put("vserver.prov-status", vmEntity.getProvStatus());
policyMsg.getAai().put("vserver.resource-version", vmEntity.getResourceVersion());
} else {
@@ -195,12 +197,12 @@ public class DmaapService {
return vmEntity;
}
- private void deleteRequestIdIfNecessary(PolicyMsg policyMsg){
- EVENT_STATUS status = policyMsg.getClosedLoopEventStatus();
- if(EVENT_STATUS.ABATED.equals(status)) {
+ private void deleteRequestIdIfNecessary(PolicyMsg policyMsg) {
+ EVENT_STATUS status = policyMsg.getClosedLoopEventStatus();
+ if (EVENT_STATUS.ABATED.equals(status)) {
String requestId = policyMsg.getRequestID();
- for(Entry<String, String> kv: uniqueRequestIdCache.entrySet()) {
- if(kv.getValue().equals(requestId)) {
+ for (Entry<String, String> kv : uniqueRequestIdCache.entrySet()) {
+ if (kv.getValue().equals(requestId)) {
uniqueRequestIdCache.remove(kv.getKey());
break;
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
index d95853d..a4024ea 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
@@ -15,68 +15,23 @@
*/
package org.onap.holmes.common.dmaap;
-import java.io.IOException;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.onap.holmes.common.dmaap.entity.PolicyMsg;
-import org.onap.holmes.common.exception.CorrelationException;
-import com.google.gson.Gson;
-import java.util.HashMap;
-import javax.ws.rs.core.MediaType;
import lombok.Getter;
import lombok.Setter;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.entity.StringEntity;
import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;
+import org.onap.holmes.common.utils.JerseyClient;
+
+import javax.ws.rs.client.Entity;
+import java.util.concurrent.TimeUnit;
@Getter
@Setter
@Service
-@Slf4j
public class Publisher {
-
- private String topic;
private String url;
- private String authInfo;
- private String authExpDate;
-
- public boolean publish(PolicyMsg msg) throws CorrelationException {
- String content;
- try {
- //content = JSON.toJSONString(msg);
- content = new Gson().toJson(msg);
- } catch (Exception e) {
- throw new CorrelationException("Failed to convert the message object to a json string.",
- e);
- }
- HttpResponse httpResponse;
- HashMap<String, String> headers = new HashMap<>();
- headers.put("Accept", MediaType.APPLICATION_JSON);
- headers.put("Content-Type", MediaType.APPLICATION_JSON);
- CloseableHttpClient httpClient = null;
- HttpPost httpPost = new HttpPost(url);
- try {
- httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
- httpResponse = HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient);
- } catch (Exception e) {
- throw new CorrelationException("Failed to connect to DCAE.", e);
- } finally {
- httpPost.releaseConnection();
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- log.warn("Failed to close http client!");
- }
- }
- }
- return checkStatus(httpResponse);
- }
+ private JerseyClient client = new JerseyClient(TimeUnit.SECONDS.toMillis(30));
- private boolean checkStatus(HttpResponse httpResponse) {
- return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false;
+ public void publish(PolicyMsg msg) {
+ client.post(url, Entity.json(msg));
}
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/exception/HttpException.java b/holmes-actions/src/main/java/org/onap/holmes/common/exception/HttpException.java
new file mode 100644
index 0000000..ad31dac
--- /dev/null
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/exception/HttpException.java
@@ -0,0 +1,27 @@
+/**
+ * Copyright 2021 ZTE Corporation.
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.holmes.common.exception;
+
+public class HttpException extends RuntimeException {
+ public HttpException(int statusCode, String msg) {
+ super(String.format("Status code: <%d>. Message: %s", statusCode, msg));
+ }
+
+ public HttpException(int statusCode, String msg, Throwable t) {
+ super(String.format("Status code: <%d>. Message: %s", statusCode, msg), t);
+ }
+}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
index 2d770ad..09d9f64 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java
@@ -16,33 +16,33 @@
package org.onap.holmes.common.utils;
-import org.glassfish.jersey.client.ClientConfig;
-import org.jvnet.hk2.annotations.Service;
+import org.eclipse.jetty.http.HttpStatus;
+import org.onap.holmes.common.exception.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.*;
+import javax.ws.rs.core.Response;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
-@Service
public class JerseyClient {
- private static Logger logger = LoggerFactory.getLogger(JerseyClient.class);
- public static final String PROTOCOL_HTTP = "http";
- public static final String PROTOCOL_HTTPS = "https";
- private SSLContext sslcontext = null;
+ static final public String PROTOCOL_HTTP = "http";
+ static final public String PROTOCOL_HTTPS = "https";
+ static private Logger logger = LoggerFactory.getLogger(JerseyClient.class);
+ static private long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toMillis(30);
+ static private SSLContext SSLCONTEXT;
- @PostConstruct
- private void init() {
+ static {
try {
- sslcontext = SSLContext.getInstance("TLS");
- sslcontext.init(null, new TrustManager[]{new X509TrustManager() {
+ SSLCONTEXT = SSLContext.getInstance("TLS");
+ SSLCONTEXT.init(null, new TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
}
@@ -58,18 +58,174 @@ public class JerseyClient {
}
}
- public Client httpClient() {
- return ClientBuilder.newClient(new ClientConfig());
+ private Client client;
+ private Map<String, Object> headers = new HashMap();
+ private Map<String, Object> parameters = new HashMap();
+ private List<String> paths = new ArrayList();
+
+
+ public JerseyClient() {
+ this(DEFAULT_TIMEOUT);
}
- public Client httpsClient() {
- return ClientBuilder.newBuilder()
- .sslContext(sslcontext)
+ public JerseyClient(long timeout) {
+ this.client = ClientBuilder.newBuilder()
+ .connectTimeout(timeout, TimeUnit.MILLISECONDS)
+ .readTimeout(timeout, TimeUnit.MILLISECONDS)
+ .sslContext(SSLCONTEXT)
.hostnameVerifier((s1, s2) -> true)
.build();
}
- public Client client(boolean isHttps) {
- return isHttps ? httpsClient() : httpClient();
+ public JerseyClient header(String name, Object value) {
+ headers.put(name, value);
+ return this;
+ }
+
+ public JerseyClient headers(Map<String, Object> hds) {
+ headers.putAll(hds);
+ return this;
+ }
+
+ public JerseyClient queryParam(String name, Object value) {
+ parameters.put(name, value);
+ return this;
+ }
+
+ public JerseyClient queryParams(Map<String, Object> params) {
+ parameters.putAll(params);
+ return this;
+ }
+
+ public JerseyClient path(String path) {
+ paths.add(path);
+ return this;
+ }
+
+ public String get(String url) {
+ return get(url, String.class);
+ }
+
+ public <T> T get(String url, Class<T> clazz) {
+
+ WebTarget target = appendPaths(client.target(url));
+
+ target = setParameters(target);
+
+ Invocation.Builder builder = setHeaders(target.request());
+
+ Response response = builder.get();
+
+ if (isSuccessful(response)) {
+ return response2Target(response, clazz);
+ }
+
+ return null;
+ }
+
+ public String post(String url) {
+ return post(url, null);
+ }
+
+ public String post(String url, Entity entity) {
+ return post(url, entity, String.class);
+ }
+
+ public <T> T post(String url, Entity entity, Class<T> clazz) {
+
+ WebTarget target = appendPaths(client.target(url));
+
+ setParameters(target);
+
+ Invocation.Builder builder = setHeaders(target.request());
+
+ Response response = builder.post(entity);
+
+ if (isSuccessful(response)) {
+ return response2Target(response, clazz);
+ }
+
+ return null;
+ }
+
+ public String put(String url, Entity entity) {
+ return put(url, entity, String.class);
+ }
+
+ public <T> T put(String url, Entity entity, Class<T> clazz) {
+ WebTarget target = appendPaths(client.target(url));
+
+ setParameters(target);
+
+ Invocation.Builder builder = setHeaders(target.request());
+
+ Response response = builder.put(entity);
+
+ if (isSuccessful(response)) {
+ return response2Target(response, clazz);
+ }
+
+ return null;
+ }
+
+ public String delete(String url) {
+ return delete(url, String.class);
+ }
+
+ public <T> T delete(String url, Class<T> clazz) {
+ WebTarget target = appendPaths(client.target(url));
+
+ setParameters(target);
+
+ Invocation.Builder builder = setHeaders(target.request());
+
+ Response response = builder.delete();
+
+ if (isSuccessful(response)) {
+ return response2Target(response, clazz);
+ }
+
+ return null;
+ }
+
+ private boolean isSuccessful(Response response) {
+ int status = response.getStatus();
+ if (!HttpStatus.isSuccess(status)) {
+ throw new HttpException(status, String.format("Failed to get response from the server. Info: %s",
+ response.readEntity(String.class)));
+ }
+ return true;
+ }
+
+ private WebTarget appendPaths(WebTarget target) {
+ for (String path : paths) {
+ target = target.path(path);
+ }
+ return target;
+ }
+
+ private Invocation.Builder setHeaders(Invocation.Builder builder) {
+ Set<Map.Entry<String, Object>> entries = headers.entrySet();
+ for (Map.Entry<String, Object> entry : entries) {
+ builder = builder.header(entry.getKey(), entry.getValue());
+ }
+ return builder;
+ }
+
+ private WebTarget setParameters(WebTarget target) {
+ Set<Map.Entry<String, Object>> entries = parameters.entrySet();
+ for (Map.Entry<String, Object> entry : entries) {
+ target = target.queryParam(entry.getKey(), entry.getValue());
+ }
+ return target;
+ }
+
+ private <T> T response2Target(Response response, Class<T> clazz) {
+ String responseText = response.readEntity(String.class);
+ if (clazz == null || clazz == String.class) {
+ return (T) responseText;
+ } else {
+ return GsonUtil.jsonToBean(responseText, clazz);
+ }
}
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java
index caff931..ff5b48a 100644
--- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java
+++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java
@@ -17,7 +17,6 @@
package org.onap.holmes.common.utils;
import org.apache.commons.lang3.StringUtils;
-import org.eclipse.jetty.http.HttpStatus;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
@@ -26,12 +25,8 @@ import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import javax.inject.Inject;
-import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import java.util.concurrent.TimeUnit;
import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
@@ -41,22 +36,17 @@ import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
public class MsbRegister {
private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
- private JerseyClient jerseyClient;
+ private JerseyClient client = new JerseyClient();
- @Inject
- public MsbRegister(JerseyClient jerseyClient) {
- this.jerseyClient = jerseyClient;
+ public MsbRegister() {
}
public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
+
boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
&& msbAddrInfo[1].equals("443");
- Client client = jerseyClient.client(isHttpsEnabled);
- WebTarget target = client.target(String.format("%s://%s:%s/api/microservices/v1/services",
- isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]));
-
log.info("Start to register Holmes Service to MSB...");
MicroServiceFullInfo microServiceFullInfo = null;
@@ -65,18 +55,13 @@ public class MsbRegister {
while (null == microServiceFullInfo && retry < 20) {
log.info("Holmes Service Registration. Retry: " + retry++);
- Response response = target.queryParam("createOrUpdate", true)
- .request(MediaType.APPLICATION_JSON)
- .post(Entity.entity(msinfo, MediaType.APPLICATION_JSON));
-
- if (response != null) {
- String ret = response.readEntity(String.class);
- int statusCode = response.getStatus();
- log.info(String.format("=========MSB REG=========\nStatus Code: %d\nInformation: %s", statusCode, ret));
- if (HttpStatus.isSuccess(statusCode)) {
- microServiceFullInfo = GsonUtil.jsonToBean(ret, MicroServiceFullInfo.class);
- }
- }
+ microServiceFullInfo = client
+ .header("Accept", MediaType.APPLICATION_JSON)
+ .queryParam("createOrUpdate", true)
+ .post(String.format("%s://%s:%s/api/microservices/v1/services",
+ isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]),
+ Entity.entity(msinfo, MediaType.APPLICATION_JSON),
+ MicroServiceFullInfo.class);
if (null == microServiceFullInfo) {
log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", interval));
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java
index 0fd577c..21dd44a 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java
@@ -14,44 +14,35 @@
package org.onap.holmes.common.aai;
-import static org.junit.Assert.assertEquals;
-import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
-
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import org.easymock.EasyMock;
+import org.junit.*;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.config.MicroServiceConfig;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.utils.JerseyClient;
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;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.junit.Assert.assertEquals;
+import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.net.ssl.*")
-@PrepareForTest({AaiQueryMdons.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
+@PrepareForTest({AaiQueryMdons.class, MicroServiceConfig.class})
public class AaiQueryMdonsTest {
@Rule
@@ -62,7 +53,6 @@ public class AaiQueryMdonsTest {
private static JsonObject data;
- private HttpResponse httpResponse;
private static final String AAI_ADDR = "https://aai.onap:8443/aai/v19/";
@BeforeClass
@@ -101,7 +91,6 @@ public class AaiQueryMdonsTest {
@Before
public void before() {
- httpResponse = PowerMock.createMock(HttpResponse.class);
Whitebox.setInternalState(aaiMdonsQuery, "headers", headers);
}
@@ -158,12 +147,16 @@ public class AaiQueryMdonsTest {
String response =
"{\"link-name\":\"link1\",\"in-maint\":false,\"link-type\":\"inter-domain\",\"available-capacity\":\"ODU2\",\"resource-version\":\"1584338211407\",\"operational-status\":\"down\"}";
- aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse", "put");
+ aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse");
+
+ JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
+ PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.headers(anyObject(Map.class))).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.put(anyObject(String.class), anyObject(Entity.class))).andReturn("");
PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", accessService)
.andReturn(data.get("get-access-service").toString());
PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", linkUrl).andReturn(data.get("get-inter-link").toString());
- PowerMock.expectPrivate(aaiMdonsQuery, "put", linkUrl, response).andReturn(httpResponse);
PowerMock.replayAll();
Whitebox.invokeMethod(aaiMdonsQuery, "updateLinksForAccessService", accessMap);
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java
index 0340e04..0eec082 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java
@@ -16,37 +16,34 @@
package org.onap.holmes.common.aai;
-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.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
-import org.onap.holmes.common.aai.config.AaiConfig;
import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VnfEntity;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.exception.HttpException;
+import org.onap.holmes.common.utils.JerseyClient;
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;
-import java.util.HashMap;
import java.util.Map;
import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.expect;
+import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
-@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class})
+@PrepareForTest({AaiQuery.class, MicroServiceConfig.class})
@PowerMockIgnore("javax.net.ssl.*")
@RunWith(PowerMockRunner.class)
public class AaiQueryTest {
@@ -75,7 +72,7 @@ public class AaiQueryTest {
VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2");
PowerMock.verifyAll();
- assertThat(vnfEntity == null, equalTo(true));
+ assertThat(vnfEntity, is(nullValue()));
}
@Test
@@ -100,64 +97,24 @@ public class AaiQueryTest {
aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
aaiResponseUtil = new AaiResponseUtil();
Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
- PowerMock.mockStatic(HttpsUtils.class);
- Map<String, String> headers = new HashMap<>();
- headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.put("Accept", "application/json");
+
String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
- HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
- HttpGet httpGet = new HttpGet(url);
- PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
- expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
- anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
- expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("{}");
+
+ JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
+ PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.get(url)).andReturn("{}");
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();
-
- assertThat(vmEntity == null, equalTo(true));
- }
- @Test
- public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception {
- PowerMock.resetAll();
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to get data from aai");
- aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks");
+ PowerMock.replayAll();
- aaiResponseUtil = new AaiResponseUtil();
- Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil);
+ VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2");
- PowerMock.mockStatic(HttpsUtils.class);
- Map<String, String> headers = new HashMap<>();
- headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.put("Accept", "application/json");
- String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure";
- CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- EasyMock.expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
- HttpGet httpGet = new HttpGet(url);
- PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
- EasyMock.expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
- anyObject(CloseableHttpClient.class))).andThrow(new CorrelationException(""));
- PowerMock.mockStatic(MicroServiceConfig.class);
- 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();
+
+ assertThat(vmEntity, is(nullValue()));
}
@Test
@@ -178,7 +135,6 @@ public class AaiQueryTest {
assertThat(resource, equalTo("le-vserver-id-val-51834"));
}
-
@Test
public void testAaiQuery_getResourceLinksResponse() throws Exception {
PowerMock.resetAll();
@@ -215,24 +171,13 @@ public class AaiQueryTest {
public void testAaiQuery_getResponse_ok() throws Exception {
PowerMock.resetAll();
aaiQuery = new AaiQuery();
- PowerMock.mockStatic(HttpsUtils.class);
- Map<String, String> headers = new HashMap<>();
- headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.put("Accept", "application/json");
+
String url = "host_url";
- HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
- HttpGet httpGet = new HttpGet(url);
- PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
- expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class),
- anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
- expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("");
- PowerMock.expectPrivate(httpClient, "close");
- EasyMock.expectLastCall();
+ JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
+ PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.get(url)).andReturn("");
PowerMock.replayAll();
String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
@@ -242,28 +187,21 @@ public class AaiQueryTest {
}
@Test
- public void testAaiQuery_getResponse_exceptioin() throws Exception {
+ public void testAaiQuery_getResponse_exception() throws Exception {
PowerMock.resetAll();
thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to get data from aai");
aaiQuery = new AaiQuery();
- PowerMock.mockStatic(HttpsUtils.class);
- Map<String, String> headers = new HashMap<>();
- headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
- headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
- headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
- headers.put("Accept", "application/json");
String url = "host_url";
- CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient);
- HttpGet httpGet = new HttpGet(url);
- PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet);
- expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException(""));
- PowerMock.expectPrivate(httpClient, "close");
- EasyMock.expectLastCall();
+
+ JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
+ PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient);
+ EasyMock.expect(mockedClient.get(url)).andThrow(new HttpException(404, "Not Found"));
+
PowerMock.replayAll();
- String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url");
+ String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", url);
PowerMock.verifyAll();
assertThat(resource, equalTo(""));
}
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java
index 76c4a45..c03bede 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java
@@ -263,7 +263,8 @@ public class DmaapServiceTest {
PolicyMsg policyMsg = new PolicyMsg();
policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
PowerMock.expectNew(Publisher.class).andReturn(publisher);
- EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
+ publisher.publish(policyMsg);
+ EasyMock.expectLastCall();
DcaeConfigurationsCache.setDcaeConfigurations(
DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
@@ -283,7 +284,8 @@ public class DmaapServiceTest {
policyMsg.setRequestID("testRequestid");
uniqueRequestIdCache.put("testAlarmId", "testRequestid");
PowerMock.expectNew(Publisher.class).andReturn(publisher);
- EasyMock.expect(publisher.publish(policyMsg)).andReturn(true);
+ publisher.publish(policyMsg);
+ EasyMock.expectLastCall();
DcaeConfigurationsCache.setDcaeConfigurations(
DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")));
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
index 0e7fe3d..8ad154d 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java
@@ -15,73 +15,39 @@
*/
package org.onap.holmes.common.dmaap;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.StatusLine;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.commons.lang3.StringUtils;
import org.easymock.EasyMock;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
-import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.utils.JerseyClient;
import org.powermock.api.easymock.PowerMock;
-import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.internal.WhiteboxImpl;
-import java.util.HashMap;
+import javax.ws.rs.client.Entity;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.easymock.EasyMock.anyObject;
-@PrepareForTest({HttpsUtils.class, HttpResponse.class, Publisher.class})
@RunWith(PowerMockRunner.class)
+@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
public class PublisherTest {
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- private static final String URL = "http://localhost/dmaapTopic";
-
@Test
- public void publish_exception() throws Exception {
+ public void publish_normal() {
Publisher publisher = new Publisher();
- publisher.setUrl(URL);
-
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to connect to DCAE");
+ publisher.setUrl("http://localhost/dmaapTopic");
- publisher.publish(new PolicyMsg());
- }
-
- @Test
- public void publish_normal() throws Exception {
-
- Publisher publisher = new Publisher();
- publisher.setUrl(URL);
-
- PowerMock.mockStatic(HttpsUtils.class);
- CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class);
- EasyMock.expect(HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT)).andReturn(httpClient);
- HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
- EasyMock.expect(HttpsUtils
- .post(EasyMock.anyObject(HttpPost.class), EasyMock.anyObject(HashMap.class),
- EasyMock.anyObject(HashMap.class), EasyMock.anyObject(StringEntity.class),
- EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
- StatusLine statusLine = PowerMock.createMock(StatusLine.class);
- EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLine);
- EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK);
- httpClient.close();
- EasyMock.expectLastCall();
+ JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class);
+ WhiteboxImpl.setInternalState(publisher, "client", mockedJerseyClient);
+ EasyMock.expect(mockedJerseyClient.post(anyObject(String.class), anyObject(Entity.class)))
+ .andReturn(StringUtils.EMPTY);
PowerMock.replayAll();
- assertThat(publisher.publish(new PolicyMsg()), is(true));
+ publisher.publish(new PolicyMsg());
PowerMock.verifyAll();
}
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java
new file mode 100644
index 0000000..a786c9f
--- /dev/null
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java
@@ -0,0 +1,32 @@
+/**
+ * Copyright 2021 ZTE Corporation.
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.holmes.common.exception;
+
+import org.junit.Test;
+
+public class HttpExceptionTest {
+
+ @Test
+ public void exception_without_cause() {
+ HttpException e = new HttpException(200, "OK");
+ }
+
+ @Test
+ public void exception_with_cause() {
+ HttpException e = new HttpException(404, "Not Found", new Exception());
+ }
+} \ No newline at end of file
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java
index 6c95ccb..0183740 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java
@@ -1,5 +1,5 @@
/**
- * Copyright 2020 ZTE Corporation.
+ * Copyright 2020 - 2021 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,32 +16,446 @@
package org.onap.holmes.common.utils;
+import org.easymock.EasyMock;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.onap.holmes.common.exception.HttpException;
+import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.internal.WhiteboxImpl;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
public class JerseyClientTest {
+ private final String ERROR_MSG = "Status code: <404>. Message: Failed to get response from the server. Info: Error";
private JerseyClient jerseyClient = new JerseyClient();
+ private String url = "http://www.onap.org/holmes/test";
+ @Rule
+ private ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void get_normal() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createMock(WebTarget.class);
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.path("test")).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.queryParam("ut", true)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.path("test")
+ .header("Accept", MediaType.APPLICATION_JSON)
+ .queryParam("ut", true)
+ .get(url), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void get_normal_with_class() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+ Bean bean = new Bean("name", "holmes");
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.get(url, Bean.class), equalTo(bean));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void get_normal_with_null_class_info() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.get(url, null), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void get_fail() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ exception.expect(HttpException.class);
+ exception.expectMessage(ERROR_MSG);
+
+ PowerMock.replayAll();
+
+ jerseyClient.get(url);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void post_normal_no_body() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.post(url), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void post_nobody_fail() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ exception.expect(HttpException.class);
+ exception.expectMessage(ERROR_MSG);
+
+ PowerMock.replayAll();
+
+ jerseyClient.post(url);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void post_normal_no_body_with_class() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+ Bean bean = new Bean("name", "holmes");
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.post(url, null, Bean.class), equalTo(bean));
+
+ PowerMock.verifyAll();
+ }
+
+
+ @Test
+ public void put_normal() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.put(url, null), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
@Test
- public void http() {
- jerseyClient.httpClient();
+ public void put_normal_with_class_info() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+ Bean bean = new Bean("name", "holmes");
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.put(url, null, Bean.class), equalTo(bean));
+
+ PowerMock.verifyAll();
}
@Test
- public void https() throws Exception {
- WhiteboxImpl.invokeMethod(jerseyClient, "init");
- jerseyClient.httpsClient();
+ public void put_normal_with_null_class_info() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+ Bean bean = new Bean("name", "holmes");
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.put(url, null, null), equalTo(GsonUtil.beanToJson(bean)));
+
+ PowerMock.verifyAll();
}
@Test
- public void clientHttp() {
- jerseyClient.client(false);
+ public void put_fail() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class);
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ exception.expect(HttpException.class);
+ exception.expectMessage(ERROR_MSG);
+
+ PowerMock.replayAll();
+
+ jerseyClient.put(url, null, null);
+
+ PowerMock.verifyAll();
}
+
@Test
- public void clientHttps() throws Exception {
- WhiteboxImpl.invokeMethod(jerseyClient, "init");
- jerseyClient.client(true);
+ public void delete_normal() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.delete(url), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void delete_normal_with_class_info() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+ Bean bean = new Bean("name", "holmes");
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.delete(url, Bean.class), equalTo(bean));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void delete_with_null_class_info() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ PowerMock.replayAll();
+
+ assertThat(jerseyClient.delete(url, null), equalTo("Normal"));
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void delete_fail() {
+ Client mockedClient = PowerMock.createMock(Client.class);
+ WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request");
+ Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete");
+ Response mockedResponse = PowerMock.createMock(Response.class);
+
+ EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
+ EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
+ EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
+ EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
+
+ WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
+
+ exception.expect(HttpException.class);
+ exception.expectMessage(ERROR_MSG);
+
+ PowerMock.replayAll();
+
+ jerseyClient.delete(url);
+
+ PowerMock.verifyAll();
+ }
+
+ @Test
+ public void headers() {
+ Map<String, Object> hds = new HashMap<>();
+ jerseyClient.headers(hds);
+ }
+
+ @Test
+ public void queryParams() {
+ Map<String, Object> params = new HashMap<>();
+ jerseyClient.queryParams(params);
+ }
+}
+
+class Bean {
+ private String name;
+ private String value;
+
+ public Bean(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public int hashCode() {
+ int hName = name == null ? 0 : name.hashCode();
+ int hValue = value == null ? 0 : value.hashCode();
+ return hName << 1 + hValue >> 1;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null || !(o instanceof Bean)) {
+ return false;
+ }
+
+ Bean b = (Bean) o;
+ return name.equals(b.name) && value.equals(b.value);
}
} \ No newline at end of file
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java
index 0637586..1a12f85 100644
--- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java
+++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java
@@ -16,69 +16,92 @@
package org.onap.holmes.common.utils;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
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.internal.WhiteboxImpl;
-import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
+import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
-import static org.powermock.api.easymock.PowerMock.createMock;
+import static org.powermock.api.easymock.PowerMock.createPartialMock;
@PrepareForTest({MicroServiceConfig.class})
@RunWith(PowerMockRunner.class)
+@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
public class MsbRegisterTest {
- @Test
- public void test_register2Msb_normal() {
- MicroServiceInfo msi = new MicroServiceInfo();
+
+ private MsbRegister msbRegister;
+ private JerseyClient mockedJerseyClient;
+ private MicroServiceInfo msi;
+
+ @Before
+ public void before() {
+ msi = new MicroServiceInfo();
String[] msbAddrInfo = {"127.0.0.1", "80"};
PowerMock.mockStatic(MicroServiceConfig.class);
expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo);
- JerseyClient mockedJerseyClient = createMock(JerseyClient.class);
-
- Client mockedClient = createMock(Client.class);
- expect(mockedJerseyClient.client(false)).andReturn(mockedClient);
+ mockedJerseyClient = createPartialMock(JerseyClient.class,
+ "post", new Class[]{String.class, Entity.class, Class.class});
- WebTarget mockedWebTarget = createMock(WebTarget.class);
- expect(mockedClient.target("http://127.0.0.1:80/api/microservices/v1/services")).andReturn(mockedWebTarget);
-
-
- expect(mockedWebTarget.queryParam("createOrUpdate", true)).andReturn(mockedWebTarget).times(2);
+ msbRegister = new MsbRegister();
+ WhiteboxImpl.setInternalState(msbRegister, "client", mockedJerseyClient);
+ }
- Invocation.Builder mockedBuilder = createMock(Invocation.Builder.class);
- expect(mockedWebTarget.request(MediaType.APPLICATION_JSON)).andReturn(mockedBuilder).times(2);
+ @Test
+ public void test_register2Msb_normal() {
+ expect(mockedJerseyClient.post(anyObject(String.class),
+ anyObject(Entity.class),
+ anyObject(Class.class)))
+ .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
+ "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
+ "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
+ "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
+ "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
+ "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
+ "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
+ MicroServiceFullInfo.class));
- Response mockedResponse = createMock(Response.class);
- expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON)))
- .andReturn(mockedResponse);
- expect(mockedResponse.getStatus()).andReturn(300);
+ PowerMock.replayAll();
- expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON)))
- .andReturn(mockedResponse);
- expect(mockedResponse.getStatus()).andReturn(201);
- expect(mockedResponse.readEntity(String.class)).andReturn("Error");
- expect(mockedResponse.readEntity(String.class)).andReturn("{\"serviceName\":\"holmes-engine-mgmt\"," +
- "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
- "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
- "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
- "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
- "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
- "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}");
+ try {
+ msbRegister.register2Msb(msi);
+ } catch (CorrelationException e) {
+ // Do nothing
+ }
+ PowerMock.verifyAll();
+ }
- MsbRegister msbRegister = new MsbRegister(mockedJerseyClient);
+ @Test
+ public void test_register2Msb_fail_once() {
+ expect(mockedJerseyClient.post(anyObject(String.class),
+ anyObject(Entity.class),
+ anyObject(Class.class)))
+ .andReturn(null);
+
+ expect(mockedJerseyClient.post(anyObject(String.class),
+ anyObject(Entity.class),
+ anyObject(Class.class)))
+ .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," +
+ "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," +
+ "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," +
+ "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," +
+ "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," +
+ "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," +
+ "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}",
+ MicroServiceFullInfo.class));
PowerMock.replayAll();