diff options
6 files changed, 743 insertions, 35 deletions
diff --git a/engine-d/pom.xml b/engine-d/pom.xml index 42af369..490d2e0 100644 --- a/engine-d/pom.xml +++ b/engine-d/pom.xml @@ -92,28 +92,6 @@ <artifactId>reflections</artifactId> </dependency> <dependency> - <groupId>org.onap.holmes.dsa</groupId> - <artifactId>dmaap-dsa</artifactId> - <exclusions> - <exclusion> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - </exclusion> - <exclusion> - <groupId>org.onap.holmes.common</groupId> - <artifactId>holmes-actions</artifactId> - </exclusion> - <exclusion> - <groupId>org.onap.msb.java-sdk</groupId> - <artifactId>msb-java-sdk</artifactId> - </exclusion> - <exclusion> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> <groupId>org.onap.holmes.common</groupId> <artifactId>holmes-actions</artifactId> <exclusions> @@ -356,14 +334,6 @@ <exclude>META-INF/*.RSA</exclude> </excludes> </filter> - <filter> - <artifact>org.onap.holmes.dsa:dmaap-dsa</artifact> - <excludes> - <exclude>org/onap/holmes/common/**</exclude> - <exclude>com/fasterxml/jackson/core/jackson-databind/**</exclude> - <exclude>org/slf4j/**</exclude> - </excludes> - </filter> </filters> </configuration> <executions> diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java new file mode 100644 index 0000000..be30153 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017 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 + * + * 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.dsa.dmaappolling; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.stat.AlarmAdditionalField; +import org.onap.holmes.common.api.stat.VesAlarm; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Service +public class DMaaPResponseUtil { + + public VesAlarm convertJsonToVesAlarm(String responseJson) throws IOException { + JSONObject jsonNode = JSON.parseObject(responseJson); + + VesAlarm vesAlarm = new VesAlarm(); + + JSONObject eventJson = JSON.parseObject(jsonNode.get("event") +""); + JSONObject commonEventHeaderJson = JSON.parseObject(eventJson.get("commonEventHeader") +""); + convertCommonEventHeaderJsonToEvent(commonEventHeaderJson, vesAlarm); + + JSONObject faultFieldsJson = JSON.parseObject(eventJson.get("faultFields") +""); + convertFaultFieldsJsonToEvent(faultFieldsJson, vesAlarm); + return vesAlarm; + } + + private void convertCommonEventHeaderJsonToEvent(JSONObject commonEventHeaderJson, + VesAlarm vesAlarm) { + vesAlarm.setDomain((String) commonEventHeaderJson.get("domain")); + vesAlarm.setEventId((String) commonEventHeaderJson.get("eventId")); + vesAlarm.setEventName((String) commonEventHeaderJson.get("eventName")); + vesAlarm.setAlarmIsCleared(vesAlarm.getEventName().endsWith("Cleared") ? 1 : 0); + vesAlarm.setEventType(getTextElementByNode(commonEventHeaderJson, "eventType")); + vesAlarm.setInternalHeaderFields( + getTextElementByNode(commonEventHeaderJson, "internalHeaderFields")); + vesAlarm.setLastEpochMicrosec(commonEventHeaderJson.getLong("lastEpochMicrosec")); + vesAlarm.setNfcNamingCode(getTextElementByNode(commonEventHeaderJson, "nfcNamingCode")); + vesAlarm.setNfNamingCode(getTextElementByNode(commonEventHeaderJson, "nfNamingCode")); + vesAlarm.setPriority((String) commonEventHeaderJson.get("priority")); + vesAlarm.setReportingEntityId( + getTextElementByNode(commonEventHeaderJson, "reportingEntityId")); + vesAlarm.setReportingEntityName( (String) commonEventHeaderJson.get("reportingEntityName")); + vesAlarm.setSequence((Integer) commonEventHeaderJson.get("sequence")); + vesAlarm.setSourceId(getTextElementByNode(commonEventHeaderJson, "sourceId")); + vesAlarm.setSourceName( (String) commonEventHeaderJson.get("sourceName")); + vesAlarm.setStartEpochMicrosec(commonEventHeaderJson.getLong("startEpochMicrosec")); + vesAlarm.setVersion(commonEventHeaderJson.getLong("version")); + } + + private void convertFaultFieldsJsonToEvent(JSONObject faultFieldsJson, VesAlarm vesAlarm) { + vesAlarm.setAlarmAdditionalInformation(getListElementByNode(faultFieldsJson, "alarmAdditionalInformation")); + vesAlarm.setAlarmCondition(faultFieldsJson.getString("alarmCondition")); + vesAlarm.setAlarmInterfaceA(getTextElementByNode(faultFieldsJson, "alarmInterfaceA")); + vesAlarm.setEventCategory(getTextElementByNode(faultFieldsJson,"eventCategory")); + vesAlarm.setEventSeverity(faultFieldsJson.getString("eventSeverity")); + vesAlarm.setEventSourceType(faultFieldsJson.getString("eventSourceType")); + vesAlarm.setFaultFieldsVersion(faultFieldsJson.getLong("faultFieldsVersion")); + vesAlarm.setSpecificProblem(faultFieldsJson.getString("specificProblem")); + vesAlarm.setVfStatus(faultFieldsJson.getString("vfStatus")); + } + + private String getTextElementByNode(JSONObject jsonNode,String name){ + if(jsonNode.get(name) != null){ + return jsonNode.getString(name); + } + return null; + } + + private Long getLongElementByNode(JSONObject jsonNode, String name) { + if(jsonNode.get(name) != null){ + return jsonNode.getLong(name); + } + return null; + } + + private List<AlarmAdditionalField> getListElementByNode(JSONObject jsonNode, String name){ + List<AlarmAdditionalField> alarms = new ArrayList<AlarmAdditionalField>(); + if (jsonNode.get(name) != null) { + JSONArray alarmAdditionalInformations = jsonNode.getJSONArray(name); + for (int i = 0; i < alarmAdditionalInformations.size(); i++) { + JSONObject jsonObject = alarmAdditionalInformations.getJSONObject(i); + if (jsonObject.get("name") != null + && jsonObject.get("value") != null) { + AlarmAdditionalField field = new AlarmAdditionalField(); + field.setName(getTextElementByNode(jsonObject, "name")); + field.setValue(getTextElementByNode(jsonObject, "value")); + alarms.add(field); + } + } + } + return alarms; + } +} 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 new file mode 100644 index 0000000..160fc38 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java @@ -0,0 +1,116 @@ +/* + * Copyright 2017 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 + * + * 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.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 java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +@Getter +@Setter +@Slf4j +public class Subscriber { + + private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator() + .getService(DMaaPResponseUtil.class); + + /** + * The number of milliseconds to wait for messages if none are immediately available. This + * should normally be used, and set at 15000 or higher. + */ + private int timeout = 15000; + + /** + * The maximum number of messages to return + */ + private int limit = 100; + + /** + * The number of milliseconds to poll interval time. This should normally be used, and set at + * 15000 or higher. + */ + private int period = timeout; + + private boolean secure; + private String topic; + private String url; + private String uuid = UUID.randomUUID() + ""; + private String consumerGroup = "homlesGroup" + uuid; + private String consumer = "homles" + uuid; + private String authInfo; + private String authExpDate; + + public List<VesAlarm> subscribe() throws CorrelationException { + List<String> response; + try { + response = getDMaaPData(); + } catch (Exception e) { + throw new CorrelationException("Failed to get data from DMaaP.", e); + } + try { + return extractVesAlarm(response); + } catch (Exception e) { + throw new CorrelationException("Failed to convert the response data to VES alarms.", e); + } + } + + private List<String> getDMaaPData() throws Exception { + String response; + CloseableHttpClient closeableHttpClient = null; + HttpGet httpGet = new HttpGet(url + "/" + consumerGroup + "/" + consumer + "?timeout=" + period); + try { + closeableHttpClient = HttpsUtils.getHttpClient(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); + } + + private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException { + List<VesAlarm> vesAlarmList = new ArrayList<>(); + for (String entity : responseEntity) { + vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity)); + } + return vesAlarmList; + } +} diff --git a/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtilTest.java b/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtilTest.java new file mode 100644 index 0000000..85e3083 --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtilTest.java @@ -0,0 +1,341 @@ +/* + * Copyright 2017 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 + * + * 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.dsa.dmaappolling; + +import com.alibaba.fastjson.JSONException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.holmes.common.api.stat.AlarmAdditionalField; +import org.onap.holmes.common.api.stat.VesAlarm; +import org.onap.holmes.dsa.dmaappolling.DMaaPResponseUtil; +import org.powermock.core.classloader.annotations.PrepareForTest; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + + +@PrepareForTest(DMaaPResponseUtil.class) +public class DMaaPResponseUtilTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private DMaaPResponseUtil dMaaPResponseUtil; + + @Before + public void setUp() { + dMaaPResponseUtil = new DMaaPResponseUtil(); + } + + @Test + public void testDMaaPResponseUtil_input_normal() throws IOException { + String json = "{\"event\":{" + + "\"commonEventHeader\":{" + + "\"domain\":\"fault\"," + + "\"eventId\":\"112355255-24345\"," + + "\"eventName\":\"4333454333\"," + + "\"eventType\":\"remote\"," + + "\"lastEpochMicrosec\":333434333," + + "\"nfcNamingCode\":\"567422\"," + + "\"nfNamingCode\":\"qweertrr\"," + + "\"priority\":\"High\"," + + "\"reportingEntityId\":\"99888888888\"," + + "\"reportingEntityName\":\"tianshisdlsdjf\"," + + "\"sequence\":34566," + + "\"sourceId\":\"3345556444\"," + + "\"sourceName\":\"nihoahh\"," + + "\"startEpochMicrosec\":54545454," + + "\"version\":998989879" + + "}," + + "\"faultFields\":{" + + "\"alarmAdditionalInformation\":[" + + "{" + + "\"name\":\"niaho\"," + + "\"value\":\"1111\"" + + "}," + + "{" + + "\"name\":\"tian\"," + + "\"value\":\"22222\"" + + "}" + + "]," + + "\"alarmCondition\":\"fadilel\"," + + "\"alarmInterfaceA\":\"generating the alarm\"," + + "\"eventCategory\":\"Event category\"," + + "\"eventSeverity\":\"CRITICAL\"," + + "\"eventSourceType\":\"type of event source\"," + + "\"faultFieldsVersion\":567567567," + + "\"specificProblem\":\"short description\"," + + "\"vfStatus\":\"Preparing to terminate\"" + + "}" + + "}" + + "}"; + VesAlarm expected = new VesAlarm(); + expected.setDomain("fault"); + expected.setEventId("112355255-24345"); + expected.setEventName("4333454333"); + expected.setEventType("remote"); + expected.setLastEpochMicrosec((long) 333434333); + expected.setNfcNamingCode("567422"); + expected.setNfNamingCode("qweertrr"); + expected.setPriority("High"); + expected.setReportingEntityId("99888888888"); + expected.setReportingEntityName("tianshisdlsdjf"); + expected.setSequence(34566); + expected.setSourceId("3345556444"); + expected.setSourceName("nihoahh"); + expected.setStartEpochMicrosec((long) 54545454); + expected.setVersion((long) 998989879); + + List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>(); + AlarmAdditionalField alarmAdditionalField1 = new AlarmAdditionalField(); + alarmAdditionalField1.setName("niaho"); + alarmAdditionalField1.setName("1111"); + alarmAdditionalFields.add(alarmAdditionalField1); + AlarmAdditionalField alarmAdditionalField2 = new AlarmAdditionalField(); + alarmAdditionalField2.setName("tian"); + alarmAdditionalField2.setName("22222"); + alarmAdditionalFields.add(alarmAdditionalField2); + expected.setAlarmAdditionalInformation(alarmAdditionalFields); + + expected.setAlarmCondition("fadilel"); + expected.setAlarmInterfaceA("generating the alarm"); + expected.setEventCategory("Event category"); + expected.setEventSeverity("CRITICAL"); + expected.setEventSourceType("type of event source"); + expected.setFaultFieldsVersion((long) 567567567); + expected.setSpecificProblem("short description"); + expected.setVfStatus("Preparing to terminate"); + + VesAlarm result = dMaaPResponseUtil.convertJsonToVesAlarm(json); + + assertThat(expected.getDomain(), equalTo(result.getDomain())); + assertThat(expected.getEventId(), equalTo(result.getEventId())); + assertThat(expected.getEventName(), equalTo(result.getEventName())); + assertThat(expected.getEventType(), equalTo(result.getEventType())); + assertThat(expected.getLastEpochMicrosec(), equalTo(result.getLastEpochMicrosec())); + assertThat(expected.getNfcNamingCode(), equalTo(result.getNfcNamingCode())); + assertThat(expected.getNfNamingCode(), equalTo(result.getNfNamingCode())); + assertThat(expected.getPriority(), equalTo(result.getPriority())); + assertThat(expected.getReportingEntityId(), equalTo(result.getReportingEntityId())); + assertThat(expected.getReportingEntityName(), equalTo(result.getReportingEntityName())); + assertThat(expected.getSequence(), equalTo(result.getSequence())); + assertThat(expected.getSourceId(), equalTo(result.getSourceId())); + assertThat(expected.getSourceName(), equalTo(result.getSourceName())); + assertThat(expected.getStartEpochMicrosec(), equalTo(result.getStartEpochMicrosec())); + assertThat(expected.getVersion(), equalTo(result.getVersion())); + + assertThat(expected.getAlarmCondition(), equalTo(result.getAlarmCondition())); + assertThat(expected.getAlarmInterfaceA(), equalTo(result.getAlarmInterfaceA())); + assertThat(expected.getEventCategory(), equalTo(result.getEventCategory())); + assertThat(expected.getEventSeverity(), equalTo(result.getEventSeverity())); + assertThat(expected.getEventSourceType(), equalTo(result.getEventSourceType())); + assertThat(expected.getFaultFieldsVersion(), equalTo(result.getFaultFieldsVersion())); + assertThat(expected.getSpecificProblem(), equalTo(result.getSpecificProblem())); + assertThat(expected.getVfStatus(), equalTo(result.getVfStatus())); + + } + + @Test + public void testDMaaPResponseUtil_throws_nullPointerException() throws Exception { + String json = "{}"; + thrown.expect(NullPointerException.class); + dMaaPResponseUtil.convertJsonToVesAlarm(json); + } + + @Test + public void testDMaaPResponseUtil_input_illegal() throws Exception { + String json = "***"; + thrown.expect(JSONException.class); + dMaaPResponseUtil.convertJsonToVesAlarm(json); + } + + @Test + public void testDMaaPResponseUtil_only_necessary_information() throws IOException { + String json = "{\"event\":{" + + "\"commonEventHeader\":{" + + "\"domain\":\"fault\"," + + "\"eventId\":\"112355255-24345\"," + + "\"eventName\":\"4333454333\"," + + "\"eventType\":\"remote\"," + + "\"lastEpochMicrosec\":333434333," + + "\"priority\":\"High\"," + + "\"reportingEntityName\":\"tianshisdlsdjf\"," + + "\"sequence\":34566," + + "\"sourceName\":\"nihoahh\"," + + "\"startEpochMicrosec\":54545454," + + "\"version\":998989879" + + "}," + + "\"faultFields\":{" + + "\"alarmAdditionalInformation\":[" + + "{" + + "\"name\":\"niaho\"," + + "\"value\":\"1111\"" + + "}," + + "{" + + "\"name\":\"tian\"," + + "\"value\":\"22222\"" + + "}" + + "]," + + "\"alarmCondition\":\"fadilel\"," + + "\"eventSeverity\":\"CRITICAL\"," + + "\"eventSourceType\":\"type of event source\"," + + "\"faultFieldsVersion\":567567567," + + "\"specificProblem\":\"short description\"," + + "\"vfStatus\":\"Preparing to terminate\"" + + "}" + + "}" + + "}"; + VesAlarm expected = new VesAlarm(); + expected.setDomain("fault"); + expected.setEventId("112355255-24345"); + expected.setEventName("4333454333"); + expected.setPriority("High"); + expected.setReportingEntityName("tianshisdlsdjf"); + expected.setSequence(34566); + expected.setSourceName("nihoahh"); + expected.setStartEpochMicrosec((long) 54545454); + expected.setVersion((long) 998989879); + + List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>(); + AlarmAdditionalField alarmAdditionalField1 = new AlarmAdditionalField(); + alarmAdditionalField1.setName("niaho"); + alarmAdditionalField1.setName("1111"); + alarmAdditionalFields.add(alarmAdditionalField1); + AlarmAdditionalField alarmAdditionalField2 = new AlarmAdditionalField(); + alarmAdditionalField2.setName("tian"); + alarmAdditionalField2.setName("22222"); + alarmAdditionalFields.add(alarmAdditionalField2); + expected.setAlarmAdditionalInformation(alarmAdditionalFields); + + expected.setAlarmCondition("fadilel"); + expected.setEventSeverity("CRITICAL"); + expected.setEventSourceType("type of event source"); + expected.setFaultFieldsVersion((long) 567567567); + expected.setSpecificProblem("short description"); + expected.setVfStatus("Preparing to terminate"); + + VesAlarm result = dMaaPResponseUtil.convertJsonToVesAlarm(json); + + assertThat(expected.getDomain(), equalTo(result.getDomain())); + assertThat(expected.getEventId(), equalTo(result.getEventId())); + assertThat(expected.getEventName(), equalTo(result.getEventName())); + assertThat(expected.getPriority(), equalTo(result.getPriority())); + assertThat(expected.getReportingEntityName(), equalTo(result.getReportingEntityName())); + assertThat(expected.getSequence(), equalTo(result.getSequence())); + assertThat(expected.getSourceName(), equalTo(result.getSourceName())); + assertThat(expected.getStartEpochMicrosec(), equalTo(result.getStartEpochMicrosec())); + assertThat(expected.getVersion(), equalTo(result.getVersion())); + + assertThat(expected.getAlarmCondition(), equalTo(result.getAlarmCondition())); + assertThat(expected.getEventSeverity(), equalTo(result.getEventSeverity())); + assertThat(expected.getEventSourceType(), equalTo(result.getEventSourceType())); + assertThat(expected.getFaultFieldsVersion(), equalTo(result.getFaultFieldsVersion())); + assertThat(expected.getSpecificProblem(), equalTo(result.getSpecificProblem())); + assertThat(expected.getVfStatus(), equalTo(result.getVfStatus())); + } + + @Test + public void testDMaaPResponseUtil_input_array_illegal() throws IOException { + String json = "{\"event\":{" + + "\"commonEventHeader\":{" + + "\"domain\":\"fault\"," + + "\"eventId\":\"112355255-24345\"," + + "\"eventName\":\"4333454333\"," + + "\"eventType\":\"remote\"," + + "\"lastEpochMicrosec\":333434333," + + "\"priority\":\"High\"," + + "\"reportingEntityName\":\"tianshisdlsdjf\"," + + "\"sequence\":34566," + + "\"sourceName\":\"nihoahh\"," + + "\"startEpochMicrosec\":54545454," + + "\"version\":998989879" + + "}," + + "\"faultFields\":{" + + "\"alarmAdditionalInformation\":[" + + "{" + + "\"nam\":\"niaho\"," + + "\"value\":\"1111\"" + + "}," + + "{" + + "\"name\":\"tian\"," + + "\"value\":\"22222\"" + + "}" + + "]," + + "\"alarmCondition\":\"fadilel\"," + + "\"eventSeverity\":\"CRITICAL\"," + + "\"eventSourceType\":\"type of event source\"," + + "\"faultFieldsVersion\":567567567," + + "\"specificProblem\":\"short description\"," + + "\"vfStatus\":\"Preparing to terminate\"" + + "}" + + "}" + + "}"; + VesAlarm expected = new VesAlarm(); + expected.setDomain("fault"); + expected.setEventId("112355255-24345"); + expected.setEventName("4333454333"); + expected.setPriority("High"); + expected.setReportingEntityName("tianshisdlsdjf"); + expected.setSequence(34566); + expected.setSourceName("nihoahh"); + expected.setStartEpochMicrosec((long) 54545454); + expected.setVersion((long) 998989879); + + List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>(); + AlarmAdditionalField alarmAdditionalField = new AlarmAdditionalField(); + alarmAdditionalField.setName("tian"); + alarmAdditionalField.setValue("22222"); + alarmAdditionalFields.add(alarmAdditionalField); + expected.setAlarmAdditionalInformation(alarmAdditionalFields); + + expected.setAlarmCondition("fadilel"); + expected.setEventSeverity("CRITICAL"); + expected.setEventSourceType("type of event source"); + expected.setFaultFieldsVersion((long) 567567567); + expected.setSpecificProblem("short description"); + expected.setVfStatus("Preparing to terminate"); + + VesAlarm result = dMaaPResponseUtil.convertJsonToVesAlarm(json); + + assertThat(expected.getDomain(), equalTo(result.getDomain())); + assertThat(expected.getEventId(), equalTo(result.getEventId())); + assertThat(expected.getEventName(), equalTo(result.getEventName())); + assertThat(expected.getPriority(), equalTo(result.getPriority())); + assertThat(expected.getReportingEntityName(), equalTo(result.getReportingEntityName())); + assertThat(expected.getSequence(), equalTo(result.getSequence())); + assertThat(expected.getSourceName(), equalTo(result.getSourceName())); + assertThat(expected.getStartEpochMicrosec(), equalTo(result.getStartEpochMicrosec())); + assertThat(expected.getVersion(), equalTo(result.getVersion())); + + assertThat(expected.getAlarmAdditionalInformation().get(0).getName(), + equalTo(result.getAlarmAdditionalInformation().get(0).getName())); + assertThat(expected.getAlarmAdditionalInformation().get(0).getValue(), + equalTo(result.getAlarmAdditionalInformation().get(0).getValue())); + + assertThat(expected.getAlarmCondition(), equalTo(result.getAlarmCondition())); + assertThat(expected.getEventSeverity(), equalTo(result.getEventSeverity())); + assertThat(expected.getEventSourceType(), equalTo(result.getEventSourceType())); + assertThat(expected.getFaultFieldsVersion(), equalTo(result.getFaultFieldsVersion())); + assertThat(expected.getSpecificProblem(), equalTo(result.getSpecificProblem())); + assertThat(expected.getVfStatus(), equalTo(result.getVfStatus())); + } +} 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 new file mode 100644 index 0000000..4b64e6a --- /dev/null +++ b/engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java @@ -0,0 +1,174 @@ +/* + * Copyright 2017 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 + * + * 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.dsa.dmaappolling; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.glassfish.hk2.api.ServiceLocator; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +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.dsa.dmaappolling.DMaaPResponseUtil; +import org.onap.holmes.dsa.dmaappolling.Subscriber; +import org.powermock.api.easymock.PowerMock; +import org.powermock.api.mockito.PowerMockito; +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.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +@PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class}) +@RunWith(PowerMockRunner.class) +public class SubscriberTest { + + private DMaaPResponseUtil util = new DMaaPResponseUtil(); + + @Before + public void init() { + PowerMockito.mockStatic(ServiceLocatorHolder.class); + ServiceLocator serviceLocator = PowerMockito.mock(ServiceLocator.class); + PowerMockito.when(ServiceLocatorHolder.getLocator()).thenReturn(serviceLocator); + PowerMockito.when(serviceLocator.getService(DMaaPResponseUtil.class)).thenReturn(util); + } + + @Test + public void subscribe() throws Exception { + PowerMock.resetAll(); + 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\"" + + "}}}"; + Subscriber subscriber = new Subscriber(); + subscriber.setUrl("https://www.onap.org"); + subscriber.setConsumerGroup("group"); + subscriber.setConsumer("consumer"); + List<String> responseList = new ArrayList<>(); + responseList.add(eventString); + String responseJson = GsonUtil.beanToJson(responseList); + + PowerMockito.mockStatic(HttpsUtils.class); + HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class); + PowerMockito.when(HttpsUtils.get(Matchers.any(HttpGet.class), + Matchers.any(HashMap.class), Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse); + PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(responseJson); + + PowerMock.replayAll(); + + List<VesAlarm> vesAlarms = subscriber.subscribe(); + PowerMock.verifyAll(); + + assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName())); + } + + @Test + public void testSetterAndGetter() { + + PowerMock.replayAll(); + + Subscriber subscriber = new Subscriber(); + subscriber.setTimeout(100); + subscriber.setLimit(10); + subscriber.setPeriod(10); + subscriber.setSecure(false); + subscriber.setTopic("test"); + subscriber.setUrl("http://localhost"); + subscriber.setConsumerGroup("Group1"); + subscriber.setConsumer("Consumer1"); + subscriber.setAuthInfo(null); + subscriber.setAuthExpDate(null); + + assertThat(subscriber.getTimeout(), is(100)); + assertThat(subscriber.getLimit(), is(10)); + assertThat(subscriber.getPeriod(), is(10)); + assertThat(subscriber.isSecure(), is(false)); + assertThat(subscriber.getTopic(), equalTo("test")); + assertThat(subscriber.getUrl(), equalTo("http://localhost")); + assertThat(subscriber.getConsumerGroup(), equalTo("Group1")); + assertThat(subscriber.getConsumer(), equalTo("Consumer1")); + assertThat(subscriber.getAuthInfo(), nullValue()); + assertThat(subscriber.getAuthExpDate(), nullValue()); + + PowerMock.verifyAll(); + } + +}
\ No newline at end of file @@ -97,11 +97,6 @@ <version>1.2.6</version> </dependency> <dependency> - <groupId>org.onap.holmes.dsa</groupId> - <artifactId>dmaap-dsa</artifactId> - <version>1.2.1</version> - </dependency> - <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-core</artifactId> <version>${dropwizard.version}</version> |