2 * Copyright 2017 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 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;
36 import java.util.ArrayList;
37 import java.util.List;
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.*;
44 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, HttpsUtils.class})
45 @RunWith(PowerMockRunner.class)
46 public class SubscriberTest {
48 private DMaaPResponseUtil util = new DMaaPResponseUtil();
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);
59 public void subscribe() throws Exception {
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");
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\"," +
104 "\"sourceId\": \"Source-123\"," +
105 "\"sourceName\": \"Source-123\"," +
106 "\"startEpochMicrosec\": 500," +
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\"" +
121 List<String> responseList = new ArrayList<>();
122 responseList.add(eventString);
123 String responseJson = GsonUtil.beanToJson(responseList);
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);
131 PowerMock.replayAll();
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();
140 assertThat(vesAlarm.getEventName(), equalTo(vesAlarms.get(0).getEventName()));
144 public void testSetterAndGetter() {
146 PowerMock.replayAll();
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);
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());
171 PowerMock.verifyAll();