diff options
author | tang peng <tang.peng5@zte.com.cn> | 2021-06-02 17:17:34 +0800 |
---|---|---|
committer | tang peng <tang.peng5@zte.com.cn> | 2021-06-04 08:46:47 +0800 |
commit | ff774a309d82c6d71064d32505492d7d0d8544cf (patch) | |
tree | 8ad5fa0e63a81155dc8aba330bd84c882ac54b45 /engine-d/src | |
parent | 240555b805b6b2f3195cd606952c8efd77693bbe (diff) |
Removed Dependency: httpclient
Issue-ID: HOLMES-414
Signed-off-by: tang peng <tang.peng5@zte.com.cn>
Change-Id: I30ce6a5ecbb6fa881d46ee78bc7fc2fb198b46db
Diffstat (limited to 'engine-d/src')
5 files changed, 119 insertions, 58 deletions
diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java index 245647d..2c9d88b 100644 --- a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java @@ -19,18 +19,13 @@ package org.onap.holmes.dsa.dmaappolling; import lombok.Getter; import lombok.Setter; 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.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; 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 java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -83,27 +78,11 @@ public class Subscriber { } private List<String> getDMaaPData() throws Exception { - String response; - CloseableHttpClient closeableHttpClient = null; - HttpGet httpGet = new HttpGet(url + "/" + consumerGroup + "/" + consumer + "?timeout=" + period); - try { - closeableHttpClient = HttpsUtils.getConditionalHttpsClient(timeout); - HttpResponse httpResponse = HttpsUtils - .get(httpGet, new HashMap<>(), closeableHttpClient); - response = HttpsUtils.extractResponseEntity(httpResponse); - } catch (Exception e) { - throw e; - } finally { - httpGet.releaseConnection(); - if (closeableHttpClient != null) { - try { - closeableHttpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } - return GsonUtil.jsonToBean(response, List.class); + return new JerseyClient() + .path(consumerGroup) + .path(consumer) + .queryParam("timeout", period) + .get(url, List.class); } private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException { diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java index 135334a..37b5c07 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2018 ZTE Corporation. + * Copyright 2017-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. @@ -19,6 +19,7 @@ import io.dropwizard.setup.Environment; import lombok.extern.slf4j.Slf4j; 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.engine.dcae.DcaeConfigurationPolling; @@ -42,7 +43,7 @@ public class EngineDActiveApp extends IOCApplication<EngineDAppConfig> { 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/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java index 2e13be3..9eca2a6 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/Initializer.java @@ -18,7 +18,7 @@ package org.onap.holmes.engine; import org.jvnet.hk2.annotations.Service; 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; @@ -30,7 +30,9 @@ 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.config.MicroServiceConfig.getMicroServiceIpAndPort; +import static org.onap.holmes.common.utils.CommonUtils.getEnv; +import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; @Service public class Initializer { @@ -61,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<Node> nodes = new HashSet<>(); Node node = new Node(); node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_ENGINE_MGMT_SERVICE_HOST")); node.setPort("9102"); /* 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-engine-mgmt/v1/healthcheck"; node.setCheckType("HTTP"); node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9102")); diff --git a/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java b/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java index 3301b89..4cf1caf 100644 --- a/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java +++ b/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java @@ -15,48 +15,49 @@ */ package org.onap.holmes.dsa.dmaappolling; -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.glassfish.hk2.api.ServiceLocator; 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.api.stat.AlarmAdditionalField; import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.exception.CorrelationException; +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 java.util.ArrayList; -import java.util.HashMap; import java.util.List; +import static org.easymock.EasyMock.*; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) -@PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class, Subscriber.class}) +@PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, Subscriber.class}) @PowerMockIgnore("javax.net.ssl.*") public class SubscriberTest { private DMaaPResponseUtil util = new DMaaPResponseUtil(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void init() { PowerMock.mockStatic(ServiceLocatorHolder.class); ServiceLocator serviceLocator = PowerMock.createMock(ServiceLocator.class); - EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator).anyTimes(); - EasyMock.expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util).anyTimes(); + expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator).anyTimes(); + expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util).anyTimes(); } @Test - public void subscribe() throws Exception { + public void subscribe_normal() throws Exception { VesAlarm vesAlarm = new VesAlarm(); vesAlarm.setDomain("ONAP"); vesAlarm.setEventId("123"); @@ -119,17 +120,12 @@ public class SubscriberTest { List<String> responseList = new ArrayList<>(); responseList.add(eventString); - String responseJson = GsonUtil.beanToJson(responseList); - - PowerMock.mockStatic(HttpsUtils.class); - CloseableHttpClient mockedCloseableHttpClient = PowerMock.createMock(CloseableHttpClient.class); - HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - EasyMock.expect(HttpsUtils.getConditionalHttpsClient(15000)).andReturn(mockedCloseableHttpClient); - EasyMock.expect(HttpsUtils.get(EasyMock.anyObject(HttpGet.class), - EasyMock.anyObject(HashMap.class), EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse); - EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(responseJson); - mockedCloseableHttpClient.close(); - EasyMock.expectLastCall(); + + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + expect(mockedClient.path(anyString())).andReturn(mockedClient).times(2); + expect(mockedClient.queryParam(anyString(), anyInt())).andReturn(mockedClient); + expect(mockedClient.get(anyString(), anyObject())).andReturn(responseList); PowerMock.replayAll(); @@ -145,6 +141,91 @@ public class SubscriberTest { } @Test + public void subscribe_retrieve_data_exception() throws Exception { + thrown.expect(CorrelationException.class); + thrown.expectMessage("Failed to get data from DMaaP."); + + VesAlarm vesAlarm = new VesAlarm(); + vesAlarm.setDomain("ONAP"); + vesAlarm.setEventId("123"); + vesAlarm.setEventName("Event-123"); + vesAlarm.setEventType("EventType"); + vesAlarm.setLastEpochMicrosec(1000L); + vesAlarm.setNfcNamingCode("123"); + vesAlarm.setNfNamingCode("123"); + vesAlarm.setPriority("high"); + vesAlarm.setReportingEntityId("ID-123"); + vesAlarm.setReportingEntityName("Name-123"); + vesAlarm.setSequence(1); + vesAlarm.setSourceId("Source-123"); + vesAlarm.setSourceName("Source-123"); + vesAlarm.setStartEpochMicrosec(500L); + vesAlarm.setVersion(1L); + List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>(); + AlarmAdditionalField field = new AlarmAdditionalField(); + field.setName("addInfo"); + field.setValue("addInfo"); + alarmAdditionalFields.add(field); + vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields); + vesAlarm.setAlarmCondition("alarmCondition"); + vesAlarm.setAlarmInterfaceA("alarmInterfaceA"); + vesAlarm.setEventCategory("eventCategory"); + vesAlarm.setEventSeverity("eventSeverity"); + vesAlarm.setEventSourceType("eventSourceType"); + vesAlarm.setFaultFieldsVersion(1L); + vesAlarm.setSpecificProblem("specificProblem"); + vesAlarm.setVfStatus("vfStatus"); + + String eventString = "{\"event\": {\"commonEventHeader\": {" + + "\"domain\": \"ONAP\"," + + "\"eventId\": \"123\"," + + "\"eventName\": \"Event-123\"," + + "\"eventType\": \"EventType\"," + + "\"lastEpochMicrosec\": 1000," + + "\"nfcNamingCode\": \"123\"," + + "\"nfNamingCode\": \"123\"," + + "\"priority\": \"high\"," + + "\"reportingEntityId\": \"ID-123\"," + + "\"reportingEntityName\": \"Name-123\"," + + "\"sequence\": 1," + + "\"sourceId\": \"Source-123\"," + + "\"sourceName\": \"Source-123\"," + + "\"startEpochMicrosec\": 500," + + "\"version\": 1" + + "}," + + " \"faultFields\" : {" + + "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," + + "\"alarmCondition\": \"alarmCondition\"," + + "\"alarmInterfaceA\": \"alarmInterfaceA\"," + + "\"eventCategory\": \"eventCategory\"," + + "\"eventSeverity\": \"eventSeverity\"," + + "\"eventSourceType\": \"eventSourceType\"," + + "\"faultFieldsVersion\": 1," + + "\"specificProblem\": \"specificProblem\"," + + "\"vfStatus\": \"vfStatus\"" + + "}}}"; + + List<String> responseList = new ArrayList<>(); + responseList.add(eventString); + + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + expect(mockedClient.path(anyString())).andReturn(mockedClient).times(2); + expect(mockedClient.queryParam(anyString(), anyInt())).andReturn(mockedClient); + expect(mockedClient.get(anyString(), anyObject())).andThrow(new RuntimeException()); + + PowerMock.replayAll(); + + Subscriber subscriber = new Subscriber(); + subscriber.setUrl("https://www.onap.org"); + subscriber.setConsumerGroup("group"); + subscriber.setConsumer("consumer"); + List<VesAlarm> vesAlarms = subscriber.subscribe(); + + PowerMock.verifyAll(); + } + + @Test public void testSetterAndGetter() { PowerMock.replayAll(); diff --git a/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java b/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java index e1915e6..d6d0b67 100644 --- a/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java +++ b/engine-d/src/test/java/org/onap/holmes/engine/InitializerTest.java @@ -22,13 +22,11 @@ import org.onap.holmes.common.utils.MsbRegister; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; 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.internal.WhiteboxImpl; @RunWith(PowerMockRunner.class) @PrepareForTest(MicroServiceConfig.class) -@SuppressStaticInitializationFor("org.onap.holmes.common.utils.HttpsUtils") public class InitializerTest { @Test @@ -38,7 +36,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(); |