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