From 1d8c0955bc266f08559385a58b2f40a9457cd5d9 Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Wed, 20 Feb 2019 08:44:40 +0800 Subject: Merged DSA into the Engine Mgmt Module Change-Id: If6b4e8345dcda8599606989ba9f4afd3497e48f1 Issue-ID: HOLMES-199 Signed-off-by: GuangrongFu --- .../dsa/dmaappolling/DMaaPResponseUtilTest.java | 341 +++++++++++++++++++++ .../holmes/dsa/dmaappolling/SubscriberTest.java | 174 +++++++++++ 2 files changed, 515 insertions(+) create mode 100644 engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtilTest.java create mode 100644 engine-d/src/test/java/org/onap/holmes/dsa/dmaappolling/SubscriberTest.java (limited to 'engine-d/src/test/java/org/onap/holmes/dsa') 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 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 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 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 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 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 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 -- cgit 1.2.3-korg