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 org.easymock.EasyMock;
19 import org.glassfish.hk2.api.ServiceLocator;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.onap.holmes.common.api.stat.AlarmAdditionalField;
24 import org.onap.holmes.common.api.stat.VesAlarm;
25 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
26 import org.powermock.api.easymock.PowerMock;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.ClientBuilder;
32 import javax.ws.rs.client.Invocation;
33 import javax.ws.rs.client.WebTarget;
34 import javax.ws.rs.core.Response;
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, ClientBuilder.class,
45 WebTarget.class, Response.class, Invocation.Builder.class, Client.class})
46 @RunWith(PowerMockRunner.class)
47 public class SubscriberTest {
49 private DMaaPResponseUtil util = new DMaaPResponseUtil();
53 PowerMock.mockStatic(ServiceLocatorHolder.class);
54 ServiceLocator serviceLocator = PowerMock.createMock(ServiceLocator.class);
55 EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator);
56 EasyMock.expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util);
60 public void subscribe() throws Exception {
62 VesAlarm vesAlarm = new VesAlarm();
63 vesAlarm.setDomain("ONAP");
64 vesAlarm.setEventId("123");
65 vesAlarm.setEventName("Event-123");
66 vesAlarm.setEventType("EventType");
67 vesAlarm.setLastEpochMicrosec(1000L);
68 vesAlarm.setNfcNamingCode("123");
69 vesAlarm.setNfNamingCode("123");
70 vesAlarm.setPriority("high");
71 vesAlarm.setReportingEntityId("ID-123");
72 vesAlarm.setReportingEntityName("Name-123");
73 vesAlarm.setSequence(1);
74 vesAlarm.setSourceId("Source-123");
75 vesAlarm.setSourceName("Source-123");
76 vesAlarm.setStartEpochMicrosec(500L);
77 vesAlarm.setVersion(1L);
78 List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>();
79 AlarmAdditionalField field = new AlarmAdditionalField();
80 field.setName("addInfo");
81 field.setValue("addInfo");
82 alarmAdditionalFields.add(field);
83 vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
84 vesAlarm.setAlarmCondition("alarmCondition");
85 vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
86 vesAlarm.setEventCategory("eventCategory");
87 vesAlarm.setEventSeverity("eventSeverity");
88 vesAlarm.setEventSourceType("eventSourceType");
89 vesAlarm.setFaultFieldsVersion(1L);
90 vesAlarm.setSpecificProblem("specificProblem");
91 vesAlarm.setVfStatus("vfStatus");
93 String eventString = "{\"event\": {\"commonEventHeader\": {" +
94 "\"domain\": \"ONAP\"," +
95 "\"eventId\": \"123\"," +
96 "\"eventName\": \"Event-123\"," +
97 "\"eventType\": \"EventType\"," +
98 "\"lastEpochMicrosec\": 1000," +
99 "\"nfcNamingCode\": \"123\"," +
100 "\"nfNamingCode\": \"123\"," +
101 "\"priority\": \"high\"," +
102 "\"reportingEntityId\": \"ID-123\"," +
103 "\"reportingEntityName\": \"Name-123\"," +
105 "\"sourceId\": \"Source-123\"," +
106 "\"sourceName\": \"Source-123\"," +
107 "\"startEpochMicrosec\": 500," +
110 " \"faultFields\" : {" +
111 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
112 "\"alarmCondition\": \"alarmCondition\"," +
113 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
114 "\"eventCategory\": \"eventCategory\"," +
115 "\"eventSeverity\": \"eventSeverity\"," +
116 "\"eventSourceType\": \"eventSourceType\"," +
117 "\"faultFieldsVersion\": 1," +
118 "\"specificProblem\": \"specificProblem\"," +
119 "\"vfStatus\": \"vfStatus\"" +
122 List<String> responseList = new ArrayList<>();
123 responseList.add(eventString);
126 PowerMock.mockStatic(ClientBuilder.class);
127 Client client = PowerMock.createMock(Client.class);
128 WebTarget webTarget = PowerMock.createMock(WebTarget.class);
129 Response response = PowerMock.createMock(Response.class);
130 Invocation.Builder builder = PowerMock.createMock(Invocation.Builder.class);
132 EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
133 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
134 EasyMock.expect(webTarget.queryParam("timeout", 15000)).andReturn(webTarget);
135 EasyMock.expect(webTarget.request()).andReturn(builder);
136 EasyMock.expect(builder.get()).andReturn(response);
137 EasyMock.expect(response.readEntity(List.class)).andReturn(responseList);
139 PowerMock.replayAll();
141 List<VesAlarm> vesAlarms = new Subscriber().subscribe();
143 assertThat(vesAlarm, equalTo(vesAlarms.get(0)));
145 PowerMock.verifyAll();
149 public void testSetterAndGetter() {
151 PowerMock.replayAll();
153 Subscriber subscriber = new Subscriber();
154 subscriber.setTimeout(100);
155 subscriber.setLimit(10);
156 subscriber.setPeriod(10);
157 subscriber.setSecure(false);
158 subscriber.setTopic("test");
159 subscriber.setUrl("http://localhost");
160 subscriber.setConsumerGroup("Group1");
161 subscriber.setConsumer("Consumer1");
162 subscriber.setAuthInfo(null);
163 subscriber.setAuthExpDate(null);
165 assertThat(subscriber.getTimeout(), is(100));
166 assertThat(subscriber.getLimit(), is(10));
167 assertThat(subscriber.getPeriod(), is(10));
168 assertThat(subscriber.isSecure(), is(false));
169 assertThat(subscriber.getTopic(), equalTo("test"));
170 assertThat(subscriber.getUrl(), equalTo("http://localhost"));
171 assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
172 assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
173 assertThat(subscriber.getAuthInfo(), nullValue());
174 assertThat(subscriber.getAuthExpDate(), nullValue());
176 PowerMock.verifyAll();