2 * Copyright 2017-2020 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.holmes.dsa.dmaappolling;
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.easymock.EasyMock;
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.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.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import static org.hamcrest.CoreMatchers.*;
41 import static org.junit.Assert.assertThat;
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class, Subscriber.class})
45 @PowerMockIgnore("javax.net.ssl.*")
46 public class SubscriberTest {
48 private DMaaPResponseUtil util = new DMaaPResponseUtil();
52 PowerMock.mockStatic(ServiceLocatorHolder.class);
53 ServiceLocator serviceLocator = PowerMock.createMock(ServiceLocator.class);
54 EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator).anyTimes();
55 EasyMock.expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util).anyTimes();
59 public void subscribe() throws Exception {
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");
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\"," +
103 "\"sourceId\": \"Source-123\"," +
104 "\"sourceName\": \"Source-123\"," +
105 "\"startEpochMicrosec\": 500," +
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\"" +
120 List<String> responseList = new ArrayList<>();
121 responseList.add(eventString);
122 String responseJson = GsonUtil.beanToJson(responseList);
124 PowerMock.mockStatic(HttpsUtils.class);
125 CloseableHttpClient mockedCloseableHttpClient = PowerMock.createMock(CloseableHttpClient.class);
126 HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class);
127 EasyMock.expect(HttpsUtils.getConditionalHttpsClient(15000)).andReturn(mockedCloseableHttpClient);
128 EasyMock.expect(HttpsUtils.get(EasyMock.anyObject(HttpGet.class),
129 EasyMock.anyObject(HashMap.class), EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse);
130 EasyMock.expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(responseJson);
131 mockedCloseableHttpClient.close();
132 EasyMock.expectLastCall();
134 PowerMock.replayAll();
136 Subscriber subscriber = new Subscriber();
137 subscriber.setUrl("https://www.onap.org");
138 subscriber.setConsumerGroup("group");
139 subscriber.setConsumer("consumer");
140 List<VesAlarm> vesAlarms = subscriber.subscribe();
142 PowerMock.verifyAll();
144 assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName()));
148 public void testSetterAndGetter() {
150 PowerMock.replayAll();
152 Subscriber subscriber = new Subscriber();
153 subscriber.setTimeout(100);
154 subscriber.setLimit(10);
155 subscriber.setPeriod(10);
156 subscriber.setSecure(false);
157 subscriber.setTopic("test");
158 subscriber.setUrl("http://localhost");
159 subscriber.setConsumerGroup("Group1");
160 subscriber.setConsumer("Consumer1");
161 subscriber.setAuthInfo(null);
162 subscriber.setAuthExpDate(null);
164 assertThat(subscriber.getTimeout(), is(100));
165 assertThat(subscriber.getLimit(), is(10));
166 assertThat(subscriber.getPeriod(), is(10));
167 assertThat(subscriber.isSecure(), is(false));
168 assertThat(subscriber.getTopic(), equalTo("test"));
169 assertThat(subscriber.getUrl(), equalTo("http://localhost"));
170 assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
171 assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
172 assertThat(subscriber.getAuthInfo(), nullValue());
173 assertThat(subscriber.getAuthExpDate(), nullValue());
175 PowerMock.verifyAll();