fix https bug
[holmes/dsa.git] / dmaap-dsa / 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 java.util.HashMap;
19 import org.apache.http.HttpResponse;
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.powermock.api.easymock.PowerMock;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import static org.hamcrest.CoreMatchers.equalTo;
40 import static org.hamcrest.CoreMatchers.is;
41 import static org.hamcrest.CoreMatchers.nullValue;
42 import static org.junit.Assert.*;
43
44 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class})
45 @RunWith(PowerMockRunner.class)
46 public class SubscriberTest {
47
48     private DMaaPResponseUtil util = new DMaaPResponseUtil();
49
50     @Before
51     public void init() {
52         PowerMockito.mockStatic(ServiceLocatorHolder.class);
53         ServiceLocator serviceLocator = PowerMockito.mock(ServiceLocator.class);
54         PowerMockito.when(ServiceLocatorHolder.getLocator()).thenReturn(serviceLocator);
55         PowerMockito.when(serviceLocator.getService(DMaaPResponseUtil.class)).thenReturn(util);
56     }
57
58     @Test
59     public void subscribe() throws Exception {
60         PowerMock.resetAll();
61         VesAlarm vesAlarm = new VesAlarm();
62         vesAlarm.setDomain("ONAP");
63         vesAlarm.setEventId("123");
64         vesAlarm.setEventName("Event-123");
65         vesAlarm.setEventType("EventType");
66         vesAlarm.setLastEpochMicrosec(1000L);
67         vesAlarm.setNfcNamingCode("123");
68         vesAlarm.setNfNamingCode("123");
69         vesAlarm.setPriority("high");
70         vesAlarm.setReportingEntityId("ID-123");
71         vesAlarm.setReportingEntityName("Name-123");
72         vesAlarm.setSequence(1);
73         vesAlarm.setSourceId("Source-123");
74         vesAlarm.setSourceName("Source-123");
75         vesAlarm.setStartEpochMicrosec(500L);
76         vesAlarm.setVersion(1L);
77         List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>();
78         AlarmAdditionalField field = new AlarmAdditionalField();
79         field.setName("addInfo");
80         field.setValue("addInfo");
81         alarmAdditionalFields.add(field);
82         vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
83         vesAlarm.setAlarmCondition("alarmCondition");
84         vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
85         vesAlarm.setEventCategory("eventCategory");
86         vesAlarm.setEventSeverity("eventSeverity");
87         vesAlarm.setEventSourceType("eventSourceType");
88         vesAlarm.setFaultFieldsVersion(1L);
89         vesAlarm.setSpecificProblem("specificProblem");
90         vesAlarm.setVfStatus("vfStatus");
91
92         String eventString = "{\"event\": {\"commonEventHeader\": {" +
93                 "\"domain\": \"ONAP\"," +
94                 "\"eventId\": \"123\"," +
95                 "\"eventName\": \"Event-123\"," +
96                 "\"eventType\": \"EventType\"," +
97                 "\"lastEpochMicrosec\": 1000," +
98                 "\"nfcNamingCode\": \"123\"," +
99                 "\"nfNamingCode\": \"123\"," +
100                 "\"priority\": \"high\"," +
101                 "\"reportingEntityId\": \"ID-123\"," +
102                 "\"reportingEntityName\": \"Name-123\"," +
103                 "\"sequence\": 1," +
104                 "\"sourceId\": \"Source-123\"," +
105                 "\"sourceName\": \"Source-123\"," +
106                 "\"startEpochMicrosec\": 500," +
107                 "\"version\": 1" +
108                 "}," +
109                 " \"faultFields\" : {" +
110                 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
111                 "\"alarmCondition\": \"alarmCondition\"," +
112                 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
113                 "\"eventCategory\": \"eventCategory\"," +
114                 "\"eventSeverity\": \"eventSeverity\"," +
115                 "\"eventSourceType\": \"eventSourceType\"," +
116                 "\"faultFieldsVersion\": 1," +
117                 "\"specificProblem\": \"specificProblem\"," +
118                 "\"vfStatus\": \"vfStatus\"" +
119                 "}}}";
120
121         List<String> responseList = new ArrayList<>();
122         responseList.add(eventString);
123         String responseJson = GsonUtil.beanToJson(responseList);
124
125         PowerMockito.mockStatic(HttpsUtils.class);
126         HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
127         PowerMockito.when(HttpsUtils.get(Matchers.eq("https://www.onap.org/group/consumer"),
128                 Matchers.any(HashMap.class), Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
129         PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(responseJson);
130
131         PowerMock.replayAll();
132
133         Subscriber subscriber = new Subscriber();
134         subscriber.setUrl("https://www.onap.org");
135         subscriber.setConsumerGroup("group");
136         subscriber.setConsumer("consumer");
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 }