From 8182df37f0308ad04433a47570e7ca9612fcfd84 Mon Sep 17 00:00:00 2001 From: tang peng Date: Wed, 2 Jun 2021 17:14:36 +0800 Subject: [PATCH] Removed Dependency: httpclient Issue-ID: HOLMES-414 Signed-off-by: tang peng Change-Id: I3d7771d25d4bd0f67a2e02373ceb2ec392ad4f40 --- pom.xml | 8 +- rulemgt-frontend/pom.xml | 2 +- rulemgt-standalone/pom.xml | 2 +- rulemgt/pom.xml | 2 +- .../java/org/onap/holmes/rulemgt/Initializer.java | 9 +- .../org/onap/holmes/rulemgt/RuleActiveApp.java | 3 +- .../bean/request/CorrelationDeployRule4Engine.java | 4 +- .../rulemgt/bolt/enginebolt/EngineService.java | 102 ++++-------- .../rulemgt/bolt/enginebolt/EngineWrapper.java | 38 +---- .../rulemgt/dcae/DcaeConfigurationPolling.java | 93 ++--------- .../org/onap/holmes/rulemgt/InitializerTest.java | 4 +- .../rulemgt/bolt/enginebolt/EngineServiceTest.java | 91 +++++----- .../rulemgt/bolt/enginebolt/EngineWrapperTest.java | 184 +++++++-------------- .../rulemgt/dcae/DcaeConfigurationPollingTest.java | 134 ++++----------- 14 files changed, 209 insertions(+), 467 deletions(-) diff --git a/pom.xml b/pom.xml index 21d91dc..bac7610 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ org.onap.holmes.rule-management holmes-rulemgt-parent - 1.3.3-SNAPSHOT + 1.3.4-SNAPSHOT pom holmes-rule-management @@ -111,6 +111,10 @@ com.squareup.retrofit2 retrofit + + org.apache.httpcomponents + httpclient + @@ -126,7 +130,7 @@ org.onap.holmes.common holmes-actions - 1.3.2 + 1.3.4 org.glassfish.jersey.containers diff --git a/rulemgt-frontend/pom.xml b/rulemgt-frontend/pom.xml index c2695a8..9609647 100644 --- a/rulemgt-frontend/pom.xml +++ b/rulemgt-frontend/pom.xml @@ -24,7 +24,7 @@ org.onap.holmes.rule-management holmes-rulemgt-parent - 1.3.3-SNAPSHOT + 1.3.4-SNAPSHOT holmes-rulemgt-frontend diff --git a/rulemgt-standalone/pom.xml b/rulemgt-standalone/pom.xml index 3658a22..db98945 100644 --- a/rulemgt-standalone/pom.xml +++ b/rulemgt-standalone/pom.xml @@ -23,7 +23,7 @@ org.onap.holmes.rule-management holmes-rulemgt-parent - 1.3.3-SNAPSHOT + 1.3.4-SNAPSHOT holmes-rulemgt-standalone diff --git a/rulemgt/pom.xml b/rulemgt/pom.xml index b264edc..13c568a 100644 --- a/rulemgt/pom.xml +++ b/rulemgt/pom.xml @@ -20,7 +20,7 @@ org.onap.holmes.rule-management holmes-rulemgt-parent - 1.3.3-SNAPSHOT + 1.3.4-SNAPSHOT holmes-rulemgt diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java index 92c77ab..6769d65 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java @@ -19,7 +19,7 @@ package org.onap.holmes.rulemgt; import org.jvnet.hk2.annotations.Service; 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.utils.CommonUtils; import org.onap.holmes.common.utils.MsbRegister; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; import org.onap.msb.sdk.discovery.entity.Node; @@ -31,7 +31,8 @@ import javax.inject.Inject; import java.util.HashSet; import java.util.Set; -import static org.onap.holmes.common.config.MicroServiceConfig.*; +import static org.onap.holmes.common.utils.CommonUtils.getEnv; +import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; @Service public class Initializer { @@ -62,13 +63,13 @@ public class Initializer { msinfo.setProtocol("REST"); msinfo.setVisualRange("0|1"); msinfo.setLb_policy("round-robin"); - msinfo.setEnable_ssl(HttpsUtils.isHttpsEnabled()); + msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled()); Set nodes = new HashSet<>(); Node node = new Node(); node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_RULE_MGMT_SERVICE_HOST")); node.setPort("9101"); /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now - String msbAddrTemplate = (HttpsUtils.isHttpsEnabled() ? "https" : "http") + String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http") + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck"; node.setCheckType("HTTP"); node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9101")); diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java index 0385f91..f77b909 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java @@ -19,6 +19,7 @@ package org.onap.holmes.rulemgt; import io.dropwizard.setup.Environment; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.dropwizard.ioc.bundle.IOCApplication; +import org.onap.holmes.common.utils.CommonUtils; import org.onap.holmes.common.utils.transactionid.TransactionIdFilter; import org.onap.holmes.rulemgt.dcae.DcaeConfigurationPolling; import org.slf4j.Logger; @@ -43,7 +44,7 @@ public class RuleActiveApp extends IOCApplication { if (!"1".equals(System.getenv("TESTING"))) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate( - new DcaeConfigurationPolling(MicroServiceConfig.getEnv(MicroServiceConfig.HOSTNAME)), 0, + new DcaeConfigurationPolling(CommonUtils.getEnv(MicroServiceConfig.HOSTNAME)), 0, DcaeConfigurationPolling.POLLING_PERIOD, TimeUnit.MILLISECONDS); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java index 4c578e4..714a95c 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java @@ -22,9 +22,7 @@ import lombok.Setter; @Getter @Setter -public class CorrelationDeployRule4Engine { - - private String content; +public class CorrelationDeployRule4Engine extends CorrelationCheckRule4Engine { private String engineId; diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java index 8782d7a..794ea93 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,94 +15,54 @@ */ package org.onap.holmes.rulemgt.bolt.enginebolt; -import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpPost; -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.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.CommonUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.util.HashMap; -@Slf4j +import static org.onap.holmes.rulemgt.constant.RuleMgtConstant.ENGINE_PATH; + @Service public class EngineService { - private static final String HTTPS = "https://"; - private static final String HTTP = "http://"; - private static final String PORT = ":9102"; + private static final String PORT = "9102"; + private static final String SEP = "//"; + private static final String COLON = ":"; - protected HttpResponse delete(String packageName, String ip) throws Exception { - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName; - CloseableHttpClient httpClient = null; - HttpDelete httpDelete = new HttpDelete(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.delete(httpDelete, headers, httpClient); - } finally { - httpDelete.releaseConnection(); - closeHttpClient(httpClient); - } + protected boolean delete(String packageName, String ip) { + return new JerseyClient() + .path(packageName) + .delete(getUrl(ip)) != null; } - protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) - throws Exception { - String content = GsonUtil.beanToJson(correlationCheckRule4Engine); - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH; - CloseableHttpClient httpClient = null; - HttpPost httpPost = new HttpPost(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient); - } finally { - httpPost.releaseConnection(); - closeHttpClient(httpClient); - } + protected boolean check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) { + return new JerseyClient() + .header("Accept", MediaType.APPLICATION_JSON) + .post(getUrl(ip), Entity.json(correlationCheckRule4Engine)) != null; } - protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception { - String content = GsonUtil.beanToJson(correlationDeployRule4Engine); - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH; - CloseableHttpClient httpClient = null; - HttpPut httpPut = new HttpPut(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient); - } finally { - closeHttpClient(httpClient); - } + protected String deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) { + return new JerseyClient() + .header("Accept", MediaType.APPLICATION_JSON) + .put(getUrl(ip), Entity.json(correlationDeployRule4Engine)); } - private void closeHttpClient(CloseableHttpClient httpClient) { - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } - private HashMap createHeaders() { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - headers.put("Accept", MediaType.APPLICATION_JSON); - return headers; + private String getRequestPref() { + return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP; } - private String getRequestPref() { - return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP; + private String getUrl(String ip) { + return new StringBuilder(getRequestPref()) + .append(SEP) + .append(ip) + .append(COLON) + .append(PORT) + .append(ENGINE_PATH) + .toString(); } } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java index 4fe5896..7c927c0 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,8 @@ package org.onap.holmes.rulemgt.bolt.enginebolt; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; import org.onap.holmes.rulemgt.constant.RuleMgtConstant; @@ -36,16 +34,10 @@ public class EngineWrapper { private EngineService engineService; public String deployEngine(CorrelationDeployRule4Engine correlationRule, String ip) throws CorrelationException { - HttpResponse response; - try { - response = engineService.deploy(correlationRule, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule deployment RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule deployment RESTful API from the engine management service."); + String response = engineService.deploy(correlationRule, ip); + if (response != null) { try { - JsonObject json = JsonParser.parseString(HttpsUtils.extractResponseEntity(response)).getAsJsonObject(); + JsonObject json = JsonParser.parseString(response).getAsJsonObject(); return json.get(RuleMgtConstant.PACKAGE).getAsString(); } catch (Exception e) { throw new CorrelationException("Failed to parse the value returned by the engine management service.", e); @@ -56,14 +48,7 @@ public class EngineWrapper { } public boolean deleteRuleFromEngine(String packageName, String ip) throws CorrelationException { - HttpResponse response; - try { - response = engineService.delete(packageName, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule deleting RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule deleting RESTful API from the engine management service."); + if (engineService.delete(packageName, ip)) { return true; } else { throw new CorrelationException("Failed to delete the rule!"); @@ -73,18 +58,9 @@ public class EngineWrapper { public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) throws CorrelationException { log.info("Rule Contents: " + correlationCheckRule4Engine.getContent()); - HttpResponse response; - try { - response = engineService.check(correlationCheckRule4Engine, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule verification RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule verification RESTful API from the engine management service."); - return true; - } else { - log.info(response.getStatusLine().getStatusCode() + " " + response.getEntity()); + if (!engineService.check(correlationCheckRule4Engine, ip)) { throw new CorrelationException("Failed to verify the rule. The contents of the rule are invalid."); } + return true; } } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java index e6bc790..036bd9a 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* 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 @@ -14,29 +14,19 @@ package org.onap.holmes.rulemgt.dcae; -import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -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.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.Rule; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.common.utils.Md5Util; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; import org.onap.holmes.rulemgt.bean.response.RuleResult4API; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; import java.util.List; @Slf4j @@ -77,12 +67,8 @@ public class DcaeConfigurationPolling implements Runnable { if (dcaeConfigurations != null) { try { ruleQueryListResponse = getAllCorrelationRules(); - } catch (CorrelationException e) { - log.error("Failed to get right response!" + e.getMessage(), e); - } catch (IOException e) { - log.error("Failed to extract response entity. " + e.getMessage(), e); } catch (Exception e) { - log.error("Failed to build http client. " + e.getMessage(), e); + log.error("Failed to get deployed rules from the rule management module: " + e.getMessage(), e); } } if (ruleQueryListResponse != null) { @@ -97,53 +83,17 @@ public class DcaeConfigurationPolling implements Runnable { } } - public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - CloseableHttpClient httpClient = null; - HttpGet httpGet = new HttpGet(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpResponse httpResponse = HttpsUtils.get(httpGet, headers, httpClient); - String response = HttpsUtils.extractResponseEntity(httpResponse); - return GsonUtil.jsonToBean(response, RuleQueryListResponse.class); - } finally { - httpGet.releaseConnection(); - closeHttpClient(httpClient); - } + private RuleQueryListResponse getAllCorrelationRules() { + return new JerseyClient().get(url, RuleQueryListResponse.class); } private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException { boolean suc = false; for (Rule rule : dcaeConfigurations.getDefaultRules()) { RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule); - String content = ""; - try { - content = GsonUtil.beanToJson(ruleCreateRequest); - } catch (Exception e) { - throw new CorrelationException("Failed to convert the message object to a json string.", e); - } - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - headers.put("Accept", MediaType.APPLICATION_JSON); - HttpResponse httpResponse; - CloseableHttpClient httpClient = null; - HttpPut httpPut = new HttpPut(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - httpResponse = HttpsUtils - .put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient); - } catch (UnsupportedEncodingException e) { - throw new CorrelationException("Failed to create https entity.", e); - } catch (Exception e) { - throw new CorrelationException(e.getMessage()); - } finally { - httpPut.releaseConnection(); - closeHttpClient(httpClient); - } - if (httpResponse != null) { - suc = httpResponse.getStatusLine().getStatusCode() == 200; - } + suc = new JerseyClient().header("Accept", MediaType.APPLICATION_JSON) + .put(url, Entity.json(ruleCreateRequest)) != null; + if (!suc) { break; } @@ -153,19 +103,8 @@ public class DcaeConfigurationPolling implements Runnable { private void deleteAllCorrelationRules(List ruleResult4APIs) { ruleResult4APIs.forEach(correlationRule -> { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - CloseableHttpClient httpClient = null; - HttpDelete httpDelete = new HttpDelete(url + "/" + correlationRule.getRuleId()); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpsUtils.delete(httpDelete, headers, httpClient); - } catch (Exception e) { - log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId() - + " exception messge is : " + e.getMessage(), e); - } finally { - httpDelete.releaseConnection(); - closeHttpClient(httpClient); + if (null == new JerseyClient().delete(url + "/" + correlationRule.getRuleId())) { + log.warn("Failed to delete rule, the rule id is: {}", correlationRule.getRuleId()); } }); } @@ -179,14 +118,4 @@ public class DcaeConfigurationPolling implements Runnable { ruleCreateRequest.setEnabled(1); return ruleCreateRequest; } - - private void closeHttpClient(CloseableHttpClient httpClient) { - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } } diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/InitializerTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/InitializerTest.java index 3f071b3..120aba2 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/InitializerTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/InitializerTest.java @@ -28,7 +28,7 @@ import org.powermock.reflect.internal.WhiteboxImpl; @RunWith(PowerMockRunner.class) @PrepareForTest(MicroServiceConfig.class) -@SuppressStaticInitializationFor("org.onap.holmes.common.utils.HttpsUtils") +@SuppressStaticInitializationFor("org.onap.holmes.common.utils.CommonUtils") public class InitializerTest { @Test @@ -38,7 +38,7 @@ public class InitializerTest { PowerMock.mockStaticPartial(MicroServiceConfig.class, "getMicroServiceIpAndPort", "getEnv"); EasyMock.expect(MicroServiceConfig.getMicroServiceIpAndPort()).andReturn(new String[]{"127.0.0.1", "443"}); - EasyMock.expect(MicroServiceConfig.getEnv("ENABLE_ENCRYPT")).andReturn("true"); + System.setProperty("ENABLE_ENCRYPT", "true"); mockedMsbRegister.register2Msb(EasyMock.anyObject(MicroServiceInfo.class)); EasyMock.expectLastCall(); diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java index e581699..aae62aa 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017 - 2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,67 +17,74 @@ package org.onap.holmes.rulemgt.bolt.enginebolt; - -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; +import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; - -import java.util.HashMap; -import static org.hamcrest.MatcherAssert.assertThat; +import static org.easymock.EasyMock.*; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.easymock.PowerMock.createMock; +import static org.powermock.api.easymock.PowerMock.*; -@PrepareForTest({HttpClients.class, CloseableHttpClient.class, HttpsUtils.class}) -@PowerMockIgnore("javax.net.ssl.*") @RunWith(PowerMockRunner.class) +@PrepareForTest(EngineService.class) +@SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"}) public class EngineServiceTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private EngineService engineService; - private HttpResponse httpResponseMock; - private CloseableHttpClient closeableHttpClient; - private CorrelationDeployRule4Engine correlationDeployRule4Engine; - private CloseableHttpResponse closeableHttpResponseMock; + private EngineService engineService = new EngineService(); + ; @Before - public void setUp() { - engineService = new EngineService(); - closeableHttpClient = PowerMock.createMock(CloseableHttpClient.class); - httpResponseMock = PowerMock.createMock(HttpResponse.class); - closeableHttpResponseMock = PowerMock.createMock(CloseableHttpResponse.class); - correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); - correlationDeployRule4Engine.setContent("{\"package\":\"test\"}"); - correlationDeployRule4Engine.setEngineId("engine_id"); + public void setUp() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "false"); } @Test - public void testEngineService_createHeaders_ok() throws Exception { - PowerMock.resetAll(); - HashMap headers = Whitebox.invokeMethod(engineService, "createHeaders"); - assertThat(headers.get("Content-Type"), equalTo("application/json")); - assertThat(headers.get("Accept"), equalTo("application/json")); + public void delete() throws Exception { + JerseyClient client = createMock(JerseyClient.class); + expectNew(JerseyClient.class).andReturn(client); + expect(client.path(anyString())).andReturn(client); + expect(client.delete(anyString())).andReturn("true"); + replayAll(); + assertThat(engineService.delete("test", "127.0.0.1"), is(true)); + verifyAll(); } @Test - public void testEngineService_closeHttpClient_ok() throws Exception { - PowerMock.resetAll(); - CloseableHttpClient closeableHttpClient = HttpsUtils - .getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - Whitebox.invokeMethod(engineService, "closeHttpClient", closeableHttpClient); + public void check() throws Exception { + JerseyClient client = createMock(JerseyClient.class); + expectNew(JerseyClient.class).andReturn(client); + expect(client.header(anyString(), anyString())).andReturn(client); + expect(client.post(anyString(), anyObject())).andReturn("true"); + + CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine(); + correlationCheckRule4Engine.setContent("{\"package\":\"test\"}"); + + replayAll(); + assertThat(engineService.check(correlationCheckRule4Engine, "127.0.0.1"), is(true)); + verifyAll(); } + @Test + public void deploy() throws Exception { + JerseyClient client = createMock(JerseyClient.class); + expectNew(JerseyClient.class).andReturn(client); + expect(client.header(anyString(), anyString())).andReturn(client); + expect(client.put(anyString(), anyObject())).andReturn("true"); + + CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); + correlationDeployRule4Engine.setContent("{\"package\":\"test\"}"); + + replayAll(); + assertThat(engineService.deploy(correlationDeployRule4Engine, "127.0.0.1"), equalTo("true")); + verifyAll(); + } } \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index 7296824..6bfb387 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -1,12 +1,12 @@ /** - * Copyright 2017 ZTE Corporation. - * + * Copyright 2017 - 2021 ZTE Corporation. + *

* 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* 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. @@ -17,201 +17,141 @@ package org.onap.holmes.rulemgt.bolt.enginebolt; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.easymock.EasyMock; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import static org.easymock.EasyMock.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.powermock.api.easymock.PowerMock.*; -@PrepareForTest({EngineWrapper.class, EngineService.class, HttpsUtils.class, HttpResponse.class, - StatusLine.class}) @RunWith(PowerMockRunner.class) public class EngineWrapperTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private EngineWrapper engineWrapper = new EngineWrapper(); - private EngineService engineServiceMock; - private HttpResponse httpResponse; - private StatusLine statusLineMock; + + private EngineService mockedEngineService; @Before - public void setUp() throws Exception { - engineServiceMock = PowerMock.createMock(EngineService.class); - httpResponse = PowerMock.createMock(HttpResponse.class); - statusLineMock = PowerMock.createMock(StatusLine.class); - Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock); + public void before() { + mockedEngineService = createMock(EngineService.class); + Whitebox.setInternalState(engineWrapper, "engineService", mockedEngineService); } - @Test - public void deployEngine_invoke_rule_deploy_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to call the rule deployment RESTful API."); - - EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), - EasyMock.anyObject(String.class))) - .andThrow( - new RuntimeException("")); - PowerMock.replayAll(); - - engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1"); - - PowerMock.verifyAll(); + @After + public void after() { + resetAll(); } @Test - public void deployEngine_http_status_not_ok() throws Exception { + public void deployEngine_fail() throws Exception { thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to deploy the rule!"); - EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400); - PowerMock.replayAll(); + expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class), + anyObject(String.class))).andReturn(null); + + replayAll(); - engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1"); + engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1"); - PowerMock.verifyAll(); + verifyAll(); } @Test public void deployEngine_parse_content_exception() throws Exception { - PowerMock.resetAll(); - String content = ""; - PowerMock.mockStatic(HttpsUtils.class); thrown.expect(CorrelationException.class); thrown.expectMessage( "Failed to parse the value returned by the engine management service."); - EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); - EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(content); - PowerMock.replayAll(); - - engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1"); - - PowerMock.verifyAll(); - } + expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class), + anyObject(String.class))).andReturn(""); - @Test - public void deployEngine_success() throws Exception { - PowerMock.resetAll(); - String content = "{\"packageName\":\"test\"}"; - PowerMock.mockStatic(HttpsUtils.class); - EasyMock.expect( - engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); - EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(content); - PowerMock.replayAll(); - - String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(),"127.0.0.1"); + replayAll(); - assertThat(result, equalTo("test")); + engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1"); + verifyAll(); } @Test - public void deleteRuleFromEngine_invoke_rule_delete_exception() throws Exception { - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to call the rule deleting RESTful API."); + public void deployEngine_success() throws Exception { + String content = "{\"packageName\":\"test\"}"; + expect(mockedEngineService.deploy(anyObject(CorrelationDeployRule4Engine.class), + anyObject(String.class))).andReturn(content); + + replayAll(); - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), - EasyMock.anyObject(String.class))).andThrow( - new RuntimeException("")); - PowerMock.replayAll(); + String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine(), "127.0.0.1"); - engineWrapper.deleteRuleFromEngine("","127.0.0.1"); + assertThat(result, equalTo("test")); - PowerMock.verifyAll(); } @Test - public void deleteRuleFromEngine_http_status_not_ok() throws Exception { + public void deleteRuleFromEngine_fail() throws Exception { thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to delete the rule!"); - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400); + expect(mockedEngineService.delete(anyObject(String.class), + anyObject(String.class))) + .andReturn(false); - PowerMock.replayAll(); + replayAll(); - engineWrapper.deleteRuleFromEngine("","127.0.0.1"); + engineWrapper.deleteRuleFromEngine("", "127.0.0.1"); - PowerMock.verifyAll(); + verifyAll(); } @Test public void deleteRuleFromEngine_success() throws Exception { - EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); + expect(mockedEngineService.delete(anyObject(String.class), + anyObject(String.class))) + .andReturn(true); - PowerMock.replayAll(); + replayAll(); - boolean result = engineWrapper.deleteRuleFromEngine("","127.0.0.1"); + boolean result = engineWrapper.deleteRuleFromEngine("", "127.0.0.1"); assertThat(result, equalTo(true)); } @Test - public void checkRuleFromEngine_rule_delete_exception() throws Exception { + public void checkRuleFromEngine_fail() throws Exception { thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to call the rule verification RESTful API."); + thrown.expectMessage("Failed to verify the rule. The contents of the rule are invalid."); - EasyMock.expect( - engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class), - EasyMock.anyObject(String.class))) - .andThrow( - new RuntimeException("")); - PowerMock.replayAll(); + expect( + mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class), + anyObject(String.class))).andReturn(false); + replayAll(); - engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"127.0.0.1"); + engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1"); - PowerMock.verifyAll(); + verifyAll(); } @Test public void checkRuleFromEngine_success() throws Exception { - EasyMock.expect( - engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class), - EasyMock.anyObject(String.class))) - .andReturn(httpResponse); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock); - EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200); + expect(mockedEngineService.check(anyObject(CorrelationCheckRule4Engine.class),anyString())).andReturn(true); - PowerMock.replayAll(); + replayAll(); - boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(),"127.0.0.1"); + boolean result = engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine(), "127.0.0.1"); - assertThat(result, equalTo(true)); + assertThat(result, is(true)); } } \ No newline at end of file diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java index 6b640b2..b6e7149 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,49 +15,31 @@ */ package org.onap.holmes.rulemgt.dcae; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.methods.HttpDelete; -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.junit.After; -import org.junit.Before; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.Rule; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; import org.onap.holmes.rulemgt.bean.response.RuleResult4API; +import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.easymock.EasyMock.*; import static org.powermock.api.easymock.PowerMock.*; -@PrepareForTest({HttpsUtils.class, DcaeConfigurationQuery.class}) -@SuppressStaticInitializationFor("org.onap.holmes.common.utils.HttpsUtils") @RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient") +@PrepareForTest({DcaeConfigurationPolling.class, DcaeConfigurationQuery.class}) public class DcaeConfigurationPollingTest { - @org.junit.Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test public void run() throws Exception { DcaeConfigurations dcaeConfigurations = new DcaeConfigurations(); @@ -69,7 +51,7 @@ public class DcaeConfigurationPollingTest { Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1"); RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - List ruleResult4APIList = new ArrayList(){ + List ruleResult4APIList = new ArrayList() { { add(new RuleResult4API()); } @@ -77,31 +59,16 @@ public class DcaeConfigurationPollingTest { ruleQueryListResponse.setCorrelationRules(ruleResult4APIList); ruleQueryListResponse.setTotalCount(ruleResult4APIList.size()); - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - expect(HttpsUtils.extractResponseEntity(httpResponseMock)).andReturn(GsonUtil.beanToJson(ruleQueryListResponse)); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class), - anyObject(StringEntity.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - StatusLine sl = createMock(StatusLine.class); - expect(httpResponseMock.getStatusLine()).andReturn(sl); - expect(sl.getStatusCode()).andReturn(200); + JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.get(anyString(), anyObject())).andReturn(ruleQueryListResponse); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.delete(anyString())).andReturn("true"); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.header(anyString(), anyString())).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.put(anyString(), anyObject())).andReturn("true"); replayAll(); @@ -116,38 +83,28 @@ public class DcaeConfigurationPollingTest { dcaeConfigurations.addDefaultRule(new Rule("test", "clName", "contents", 1)); mockStatic(DcaeConfigurationQuery.class); expect(DcaeConfigurationQuery.getDcaeConfigurations(anyObject(String.class))).andReturn(dcaeConfigurations).times(2); - DcaeConfigurationPolling dcaeConfigurationPolling = createPartialMock(DcaeConfigurationPolling.class, - "getAllCorrelationRules"); + DcaeConfigurationPolling dcaeConfigurationPolling = new DcaeConfigurationPolling("localhost"); + Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1"); RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - List ruleResult4APIList = new ArrayList(){ + List ruleResult4APIList = new ArrayList() { { add(new RuleResult4API()); } }; ruleQueryListResponse.setCorrelationRules(ruleResult4APIList); ruleQueryListResponse.setTotalCount(ruleResult4APIList.size()); - expect(dcaeConfigurationPolling.getAllCorrelationRules()).andReturn(ruleQueryListResponse); - - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class), - anyObject(StringEntity.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - StatusLine sl = createMock(StatusLine.class); - expect(httpResponseMock.getStatusLine()).andReturn(sl); - expect(sl.getStatusCode()).andReturn(200); + JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.get(anyString(), anyObject())).andReturn(ruleQueryListResponse); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.delete(anyString())).andReturn("true"); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.header(anyString(), anyString())).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.put(anyString(), anyObject())).andReturn("true"); replayAll(); @@ -156,35 +113,4 @@ public class DcaeConfigurationPollingTest { verifyAll(); } - - - - @Test - public void getAllCorrelationRules() throws Exception { - - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - expect(HttpsUtils.extractResponseEntity(httpResponseMock)).andReturn("{\"correlationRules\": [], \"totalCount\": 0}"); - clientMock.close(); - expectLastCall(); - - replayAll(); - DcaeConfigurationPolling daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt"); - RuleQueryListResponse response = daceConfigurationPolling.getAllCorrelationRules(); - assertThat(response.getTotalCount(), is(0)); - verifyAll(); - } - - @Before - public void setUp() { - mockStatic(HttpsUtils.class); - } - - @After - public void tearDown() { - resetAll(); - } } \ No newline at end of file -- 2.16.6