Merged DSA into the Engine Mgmt Module
[holmes/engine-management.git] / engine-d / src / test / java / org / onap / holmes / dsa / dmaappolling / SubscriberTest.java
1 /*
2  * Copyright 2017 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.apache.http.HttpResponse;
19 import org.apache.http.client.methods.HttpGet;
20 import org.apache.http.impl.client.CloseableHttpClient;
21 import org.glassfish.hk2.api.ServiceLocator;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Matchers;
26 import org.onap.holmes.common.api.stat.AlarmAdditionalField;
27 import org.onap.holmes.common.api.stat.VesAlarm;
28 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
29 import org.onap.holmes.common.utils.GsonUtil;
30 import org.onap.holmes.common.utils.HttpsUtils;
31 import org.onap.holmes.dsa.dmaappolling.DMaaPResponseUtil;
32 import org.onap.holmes.dsa.dmaappolling.Subscriber;
33 import org.powermock.api.easymock.PowerMock;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42 import static org.hamcrest.CoreMatchers.*;
43 import static org.junit.Assert.assertThat;
44
45 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class})
46 @RunWith(PowerMockRunner.class)
47 public class SubscriberTest {
48
49     private DMaaPResponseUtil util = new DMaaPResponseUtil();
50
51     @Before
52     public void init() {
53         PowerMockito.mockStatic(ServiceLocatorHolder.class);
54         ServiceLocator serviceLocator = PowerMockito.mock(ServiceLocator.class);
55         PowerMockito.when(ServiceLocatorHolder.getLocator()).thenReturn(serviceLocator);
56         PowerMockito.when(serviceLocator.getService(DMaaPResponseUtil.class)).thenReturn(util);
57     }
58
59     @Test
60     public void subscribe() throws Exception {
61         PowerMock.resetAll();
62         VesAlarm vesAlarm = new VesAlarm();
63         vesAlarm.setDomain("ONAP");
64         vesAlarm.setEventId("123");
65         vesAlarm.setEventName("Event-123");
66         vesAlarm.setEventType("EventType");
67         vesAlarm.setLastEpochMicrosec(1000L);
68         vesAlarm.setNfcNamingCode("123");
69         vesAlarm.setNfNamingCode("123");
70         vesAlarm.setPriority("high");
71         vesAlarm.setReportingEntityId("ID-123");
72         vesAlarm.setReportingEntityName("Name-123");
73         vesAlarm.setSequence(1);
74         vesAlarm.setSourceId("Source-123");
75         vesAlarm.setSourceName("Source-123");
76         vesAlarm.setStartEpochMicrosec(500L);
77         vesAlarm.setVersion(1L);
78         List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>();
79         AlarmAdditionalField field = new AlarmAdditionalField();
80         field.setName("addInfo");
81         field.setValue("addInfo");
82         alarmAdditionalFields.add(field);
83         vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
84         vesAlarm.setAlarmCondition("alarmCondition");
85         vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
86         vesAlarm.setEventCategory("eventCategory");
87         vesAlarm.setEventSeverity("eventSeverity");
88         vesAlarm.setEventSourceType("eventSourceType");
89         vesAlarm.setFaultFieldsVersion(1L);
90         vesAlarm.setSpecificProblem("specificProblem");
91         vesAlarm.setVfStatus("vfStatus");
92
93         String eventString = "{\"event\": {\"commonEventHeader\": {" +
94                 "\"domain\": \"ONAP\"," +
95                 "\"eventId\": \"123\"," +
96                 "\"eventName\": \"Event-123\"," +
97                 "\"eventType\": \"EventType\"," +
98                 "\"lastEpochMicrosec\": 1000," +
99                 "\"nfcNamingCode\": \"123\"," +
100                 "\"nfNamingCode\": \"123\"," +
101                 "\"priority\": \"high\"," +
102                 "\"reportingEntityId\": \"ID-123\"," +
103                 "\"reportingEntityName\": \"Name-123\"," +
104                 "\"sequence\": 1," +
105                 "\"sourceId\": \"Source-123\"," +
106                 "\"sourceName\": \"Source-123\"," +
107                 "\"startEpochMicrosec\": 500," +
108                 "\"version\": 1" +
109                 "}," +
110                 " \"faultFields\" : {" +
111                 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
112                 "\"alarmCondition\": \"alarmCondition\"," +
113                 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
114                 "\"eventCategory\": \"eventCategory\"," +
115                 "\"eventSeverity\": \"eventSeverity\"," +
116                 "\"eventSourceType\": \"eventSourceType\"," +
117                 "\"faultFieldsVersion\": 1," +
118                 "\"specificProblem\": \"specificProblem\"," +
119                 "\"vfStatus\": \"vfStatus\"" +
120                 "}}}";
121         Subscriber subscriber = new Subscriber();
122         subscriber.setUrl("https://www.onap.org");
123         subscriber.setConsumerGroup("group");
124         subscriber.setConsumer("consumer");
125         List<String> responseList = new ArrayList<>();
126         responseList.add(eventString);
127         String responseJson = GsonUtil.beanToJson(responseList);
128
129         PowerMockito.mockStatic(HttpsUtils.class);
130         HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
131         PowerMockito.when(HttpsUtils.get(Matchers.any(HttpGet.class),
132                 Matchers.any(HashMap.class), Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
133         PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(responseJson);
134
135         PowerMock.replayAll();
136
137         List<VesAlarm> vesAlarms = subscriber.subscribe();
138         PowerMock.verifyAll();
139
140         assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName()));
141     }
142
143     @Test
144     public void testSetterAndGetter() {
145
146         PowerMock.replayAll();
147
148         Subscriber subscriber = new Subscriber();
149         subscriber.setTimeout(100);
150         subscriber.setLimit(10);
151         subscriber.setPeriod(10);
152         subscriber.setSecure(false);
153         subscriber.setTopic("test");
154         subscriber.setUrl("http://localhost");
155         subscriber.setConsumerGroup("Group1");
156         subscriber.setConsumer("Consumer1");
157         subscriber.setAuthInfo(null);
158         subscriber.setAuthExpDate(null);
159
160         assertThat(subscriber.getTimeout(), is(100));
161         assertThat(subscriber.getLimit(), is(10));
162         assertThat(subscriber.getPeriod(), is(10));
163         assertThat(subscriber.isSecure(), is(false));
164         assertThat(subscriber.getTopic(), equalTo("test"));
165         assertThat(subscriber.getUrl(), equalTo("http://localhost"));
166         assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
167         assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
168         assertThat(subscriber.getAuthInfo(), nullValue());
169         assertThat(subscriber.getAuthExpDate(), nullValue());
170
171         PowerMock.verifyAll();
172     }
173
174 }