Change HTTP Requests into HTTPS Ones
[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.glassfish.hk2.api.ServiceLocator;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Matchers;
25 import org.onap.holmes.common.api.stat.AlarmAdditionalField;
26 import org.onap.holmes.common.api.stat.VesAlarm;
27 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
28 import org.onap.holmes.common.utils.GsonUtil;
29 import org.onap.holmes.common.utils.HttpsUtils;
30 import org.powermock.api.easymock.PowerMock;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import static org.hamcrest.CoreMatchers.equalTo;
39 import static org.hamcrest.CoreMatchers.is;
40 import static org.hamcrest.CoreMatchers.nullValue;
41 import static org.junit.Assert.*;
42
43 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class})
44 @RunWith(PowerMockRunner.class)
45 public class SubscriberTest {
46
47     private DMaaPResponseUtil util = new DMaaPResponseUtil();
48
49     @Before
50     public void init() {
51         PowerMockito.mockStatic(ServiceLocatorHolder.class);
52         ServiceLocator serviceLocator = PowerMockito.mock(ServiceLocator.class);
53         PowerMockito.when(ServiceLocatorHolder.getLocator()).thenReturn(serviceLocator);
54         PowerMockito.when(serviceLocator.getService(DMaaPResponseUtil.class)).thenReturn(util);
55     }
56
57     @Test
58     public void subscribe() throws Exception {
59         PowerMock.resetAll();
60         VesAlarm vesAlarm = new VesAlarm();
61         vesAlarm.setDomain("ONAP");
62         vesAlarm.setEventId("123");
63         vesAlarm.setEventName("Event-123");
64         vesAlarm.setEventType("EventType");
65         vesAlarm.setLastEpochMicrosec(1000L);
66         vesAlarm.setNfcNamingCode("123");
67         vesAlarm.setNfNamingCode("123");
68         vesAlarm.setPriority("high");
69         vesAlarm.setReportingEntityId("ID-123");
70         vesAlarm.setReportingEntityName("Name-123");
71         vesAlarm.setSequence(1);
72         vesAlarm.setSourceId("Source-123");
73         vesAlarm.setSourceName("Source-123");
74         vesAlarm.setStartEpochMicrosec(500L);
75         vesAlarm.setVersion(1L);
76         List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>();
77         AlarmAdditionalField field = new AlarmAdditionalField();
78         field.setName("addInfo");
79         field.setValue("addInfo");
80         alarmAdditionalFields.add(field);
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(1L);
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\": 1" +
107                 "}," +
108                 " \"faultFields\" : {" +
109                 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
110                 "\"alarmCondition\": \"alarmCondition\"," +
111                 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
112                 "\"eventCategory\": \"eventCategory\"," +
113                 "\"eventSeverity\": \"eventSeverity\"," +
114                 "\"eventSourceType\": \"eventSourceType\"," +
115                 "\"faultFieldsVersion\": 1," +
116                 "\"specificProblem\": \"specificProblem\"," +
117                 "\"vfStatus\": \"vfStatus\"" +
118                 "}}}";
119
120         List<String> responseList = new ArrayList<>();
121         responseList.add(eventString);
122         String responseJson = GsonUtil.beanToJson(responseList);
123
124         PowerMockito.mockStatic(HttpsUtils.class);
125         HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
126         PowerMockito.when(HttpsUtils.get(Matchers.eq("https://www.onap.org/group/consumer"),
127                 Matchers.any(HashMap.class), Matchers.eq(15000))).thenReturn(httpResponse);
128         PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(responseJson);
129
130         PowerMock.replayAll();
131
132         Subscriber subscriber = new Subscriber();
133         subscriber.setUrl("https://www.onap.org");
134         subscriber.setConsumerGroup("group");
135         subscriber.setConsumer("consumer");
136         List<VesAlarm> vesAlarms = subscriber.subscribe();
137         PowerMock.verifyAll();
138
139         assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName()));
140     }
141
142     @Test
143     public void testSetterAndGetter() {
144
145         PowerMock.replayAll();
146
147         Subscriber subscriber = new Subscriber();
148         subscriber.setTimeout(100);
149         subscriber.setLimit(10);
150         subscriber.setPeriod(10);
151         subscriber.setSecure(false);
152         subscriber.setTopic("test");
153         subscriber.setUrl("http://localhost");
154         subscriber.setConsumerGroup("Group1");
155         subscriber.setConsumer("Consumer1");
156         subscriber.setAuthInfo(null);
157         subscriber.setAuthExpDate(null);
158
159         assertThat(subscriber.getTimeout(), is(100));
160         assertThat(subscriber.getLimit(), is(10));
161         assertThat(subscriber.getPeriod(), is(10));
162         assertThat(subscriber.isSecure(), is(false));
163         assertThat(subscriber.getTopic(), equalTo("test"));
164         assertThat(subscriber.getUrl(), equalTo("http://localhost"));
165         assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
166         assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
167         assertThat(subscriber.getAuthInfo(), nullValue());
168         assertThat(subscriber.getAuthExpDate(), nullValue());
169
170         PowerMock.verifyAll();
171     }
172
173 }