Add support to 7.1 VES data-stream in-parallel to 5.4
[holmes/engine-management.git] / engine-d / src / test / java / org / onap / holmes / dsa / dmaappolling / SubscriberTest.java
1 /*
2  * Copyright 2017-2021 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.dsa.dmaappolling;
17
18 import org.glassfish.hk2.api.ServiceLocator;
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.junit.runner.RunWith;
24 import org.onap.holmes.common.api.stat.AlarmAdditionalField;
25 import org.onap.holmes.common.api.stat.VesAlarm;
26 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
27 import org.onap.holmes.common.exception.CorrelationException;
28 import org.onap.holmes.common.utils.JerseyClient;
29 import org.powermock.api.easymock.PowerMock;
30 import org.powermock.core.classloader.annotations.PowerMockIgnore;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.easymock.EasyMock.*;
40 import static org.hamcrest.CoreMatchers.*;
41 import static org.junit.Assert.assertThat;
42
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest({ServiceLocatorHolder.class, JerseyClient.class})
45 @PowerMockIgnore("javax.net.ssl.*")
46 public class SubscriberTest {
47
48     private DMaaPResponseUtil util = new DMaaPResponseUtil();
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53     @Before
54     public void init() {
55         PowerMock.mockStatic(ServiceLocatorHolder.class);
56         ServiceLocator serviceLocator = PowerMock.createMock(ServiceLocator.class);
57         expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator).anyTimes();
58         expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util).anyTimes();
59     }
60
61     @Test
62     public void subscribe_normal() throws Exception {
63         VesAlarm vesAlarm = new VesAlarm();
64         vesAlarm.setDomain("ONAP");
65         vesAlarm.setEventId("123");
66         vesAlarm.setEventName("Event-123");
67         vesAlarm.setEventType("EventType");
68         vesAlarm.setLastEpochMicrosec(1000L);
69         vesAlarm.setNfcNamingCode("123");
70         vesAlarm.setNfNamingCode("123");
71         vesAlarm.setPriority("high");
72         vesAlarm.setReportingEntityId("ID-123");
73         vesAlarm.setReportingEntityName("Name-123");
74         vesAlarm.setSequence(1);
75         vesAlarm.setSourceId("Source-123");
76         vesAlarm.setSourceName("Source-123");
77         vesAlarm.setStartEpochMicrosec(500L);
78         vesAlarm.setVersion("4.0");
79         Map alarmAdditionalFields = new HashMap<String, String>();
80         alarmAdditionalFields.put("addInfo", "addInfo");
81         vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
82         vesAlarm.setAlarmCondition("alarmCondition");
83         vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
84         vesAlarm.setEventCategory("eventCategory");
85         vesAlarm.setEventSeverity("eventSeverity");
86         vesAlarm.setEventSourceType("eventSourceType");
87         vesAlarm.setFaultFieldsVersion("2.0");
88         vesAlarm.setSpecificProblem("specificProblem");
89         vesAlarm.setVfStatus("vfStatus");
90
91         String eventString = "{\"event\": {\"commonEventHeader\": {" +
92                 "\"domain\": \"ONAP\"," +
93                 "\"eventId\": \"123\"," +
94                 "\"eventName\": \"Event-123\"," +
95                 "\"eventType\": \"EventType\"," +
96                 "\"lastEpochMicrosec\": 1000," +
97                 "\"nfcNamingCode\": \"123\"," +
98                 "\"nfNamingCode\": \"123\"," +
99                 "\"priority\": \"high\"," +
100                 "\"reportingEntityId\": \"ID-123\"," +
101                 "\"reportingEntityName\": \"Name-123\"," +
102                 "\"sequence\": 1," +
103                 "\"sourceId\": \"Source-123\"," +
104                 "\"sourceName\": \"Source-123\"," +
105                 "\"startEpochMicrosec\": 500," +
106                 "\"version\": \"4.0\"" +
107                 "}," +
108                 " \"faultFields\" : {" +
109                 "\"alarmAdditionalInformatiddon\":[{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
110                 "\"alarmCondition\": \"alarmCondition\"," +
111                 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
112                 "\"eventCategory\": \"eventCategory\"," +
113                 "\"eventSeverity\": \"eventSeverity\"," +
114                 "\"eventSourceType\": \"eventSourceType\"," +
115                 "\"faultFieldsVersion\": \"2.0\"," +
116                 "\"specificProblem\": \"specificProblem\"," +
117                 "\"vfStatus\": \"vfStatus\"" +
118                 "}}}";
119
120         List<String> responseList = new ArrayList<>();
121         responseList.add(eventString);
122
123         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
124         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
125         expect(mockedClient.path(anyString())).andReturn(mockedClient).times(2);
126         expect(mockedClient.queryParam(anyString(), anyInt())).andReturn(mockedClient);
127         expect(mockedClient.get(anyString(), anyObject())).andReturn(responseList);
128
129         PowerMock.replayAll();
130
131         Subscriber subscriber = new Subscriber();
132         subscriber.setUrl("https://www.onap.org");
133         subscriber.setConsumerGroup("group");
134         subscriber.setConsumer("consumer");
135         List<VesAlarm> vesAlarms = subscriber.subscribe();
136
137         PowerMock.verifyAll();
138
139         assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName()));
140     }
141
142     @Test
143     public void subscribe_retrieve_data_exception() throws Exception {
144         thrown.expect(CorrelationException.class);
145         thrown.expectMessage("Failed to get data from DMaaP.");
146
147         VesAlarm vesAlarm = new VesAlarm();
148         vesAlarm.setDomain("ONAP");
149         vesAlarm.setEventId("123");
150         vesAlarm.setEventName("Event-123");
151         vesAlarm.setEventType("EventType");
152         vesAlarm.setLastEpochMicrosec(1000L);
153         vesAlarm.setNfcNamingCode("123");
154         vesAlarm.setNfNamingCode("123");
155         vesAlarm.setPriority("high");
156         vesAlarm.setReportingEntityId("ID-123");
157         vesAlarm.setReportingEntityName("Name-123");
158         vesAlarm.setSequence(1);
159         vesAlarm.setSourceId("Source-123");
160         vesAlarm.setSourceName("Source-123");
161         vesAlarm.setStartEpochMicrosec(500L);
162         vesAlarm.setVersion("4.0");
163         Map alarmAdditionalFields = new HashMap<String, String>();
164         alarmAdditionalFields.put("addInfo", "addInfo");
165         vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
166         vesAlarm.setAlarmCondition("alarmCondition");
167         vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
168         vesAlarm.setEventCategory("eventCategory");
169         vesAlarm.setEventSeverity("eventSeverity");
170         vesAlarm.setEventSourceType("eventSourceType");
171         vesAlarm.setFaultFieldsVersion("2.0");
172         vesAlarm.setSpecificProblem("specificProblem");
173         vesAlarm.setVfStatus("vfStatus");
174
175         String eventString = "{\"event\": {\"commonEventHeader\": {" +
176                 "\"domain\": \"ONAP\"," +
177                 "\"eventId\": \"123\"," +
178                 "\"eventName\": \"Event-123\"," +
179                 "\"eventType\": \"EventType\"," +
180                 "\"lastEpochMicrosec\": 1000," +
181                 "\"nfcNamingCode\": \"123\"," +
182                 "\"nfNamingCode\": \"123\"," +
183                 "\"priority\": \"high\"," +
184                 "\"reportingEntityId\": \"ID-123\"," +
185                 "\"reportingEntityName\": \"Name-123\"," +
186                 "\"sequence\": 1," +
187                 "\"sourceId\": \"Source-123\"," +
188                 "\"sourceName\": \"Source-123\"," +
189                 "\"startEpochMicrosec\": 500," +
190                 "\"version\": \"4.0\"" +
191                 "}," +
192                 " \"faultFields\" : {" +
193                 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
194                 "\"alarmCondition\": \"alarmCondition\"," +
195                 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
196                 "\"eventCategory\": \"eventCategory\"," +
197                 "\"eventSeverity\": \"eventSeverity\"," +
198                 "\"eventSourceType\": \"eventSourceType\"," +
199                 "\"faultFieldsVersion\": \"2.0\"," +
200                 "\"specificProblem\": \"specificProblem\"," +
201                 "\"vfStatus\": \"vfStatus\"" +
202                 "}}}";
203
204         List<String> responseList = new ArrayList<>();
205         responseList.add(eventString);
206
207         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
208         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
209         expect(mockedClient.path(anyString())).andReturn(mockedClient).times(2);
210         expect(mockedClient.queryParam(anyString(), anyInt())).andReturn(mockedClient);
211         expect(mockedClient.get(anyString(), anyObject())).andThrow(new RuntimeException());
212
213         PowerMock.replayAll();
214
215         Subscriber subscriber = new Subscriber();
216         subscriber.setUrl("https://www.onap.org");
217         subscriber.setConsumerGroup("group");
218         subscriber.setConsumer("consumer");
219
220         subscriber.subscribe();
221
222         PowerMock.verifyAll();
223     }
224
225     @Test
226     public void testSetterAndGetter() {
227
228         PowerMock.replayAll();
229
230         Subscriber subscriber = new Subscriber();
231         subscriber.setTimeout(100);
232         subscriber.setLimit(10);
233         subscriber.setPeriod(10);
234         subscriber.setSecure(false);
235         subscriber.setTopic("test");
236         subscriber.setUrl("http://localhost");
237         subscriber.setConsumerGroup("Group1");
238         subscriber.setConsumer("Consumer1");
239         subscriber.setAuthInfo(null);
240         subscriber.setAuthExpDate(null);
241
242         assertThat(subscriber.getTimeout(), is(100));
243         assertThat(subscriber.getLimit(), is(10));
244         assertThat(subscriber.getPeriod(), is(10));
245         assertThat(subscriber.isSecure(), is(false));
246         assertThat(subscriber.getTopic(), equalTo("test"));
247         assertThat(subscriber.getUrl(), equalTo("http://localhost"));
248         assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
249         assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
250         assertThat(subscriber.getAuthInfo(), nullValue());
251         assertThat(subscriber.getAuthExpDate(), nullValue());
252
253         PowerMock.verifyAll();
254     }
255
256 }