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 com.fasterxml.jackson.databind.ObjectMapper;
19 import com.sun.org.apache.regexp.internal.RE;
20 import io.dropwizard.cli.Cli;
21 import org.easymock.EasyMock;
22 import org.glassfish.hk2.api.ServiceLocator;
23 import org.glassfish.jersey.client.ClientConfig;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
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.powermock.api.easymock.PowerMock;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
34 import javax.ws.rs.client.Client;
35 import javax.ws.rs.client.ClientBuilder;
36 import javax.ws.rs.client.Invocation;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.Response;
40 import java.util.ArrayList;
41 import java.util.List;
43 import static org.hamcrest.CoreMatchers.equalTo;
44 import static org.hamcrest.CoreMatchers.is;
45 import static org.hamcrest.CoreMatchers.nullValue;
46 import static org.junit.Assert.*;
48 @PrepareForTest({ServiceLocatorHolder.class, ServiceLocator.class, ClientBuilder.class,
49 WebTarget.class, Response.class, Invocation.Builder.class, Client.class})
50 @RunWith(PowerMockRunner.class)
51 public class SubscriberTest {
53 private DMaaPResponseUtil util = new DMaaPResponseUtil();
57 PowerMock.mockStatic(ServiceLocatorHolder.class);
58 ServiceLocator serviceLocator = PowerMock.createMock(ServiceLocator.class);
59 EasyMock.expect(ServiceLocatorHolder.getLocator()).andReturn(serviceLocator);
60 EasyMock.expect(serviceLocator.getService(DMaaPResponseUtil.class)).andReturn(util);
64 public void subscribe() throws Exception {
66 VesAlarm vesAlarm = new VesAlarm();
67 vesAlarm.setDomain("ONAP");
68 vesAlarm.setEventId("123");
69 vesAlarm.setEventName("Event-123");
70 vesAlarm.setEventType("EventType");
71 vesAlarm.setLastEpochMicrosec(1000L);
72 vesAlarm.setNfcNamingCode("123");
73 vesAlarm.setNfNamingCode("123");
74 vesAlarm.setPriority("high");
75 vesAlarm.setReportingEntityId("ID-123");
76 vesAlarm.setReportingEntityName("Name-123");
77 vesAlarm.setSequence(1);
78 vesAlarm.setSourceId("Source-123");
79 vesAlarm.setSourceName("Source-123");
80 vesAlarm.setStartEpochMicrosec(500L);
81 vesAlarm.setVersion(1L);
82 List<AlarmAdditionalField> alarmAdditionalFields = new ArrayList<>();
83 AlarmAdditionalField field = new AlarmAdditionalField();
84 field.setName("addInfo");
85 field.setValue("addInfo");
86 alarmAdditionalFields.add(field);
87 vesAlarm.setAlarmAdditionalInformation(alarmAdditionalFields);
88 vesAlarm.setAlarmCondition("alarmCondition");
89 vesAlarm.setAlarmInterfaceA("alarmInterfaceA");
90 vesAlarm.setEventCategory("eventCategory");
91 vesAlarm.setEventSeverity("eventSeverity");
92 vesAlarm.setEventSourceType("eventSourceType");
93 vesAlarm.setFaultFieldsVersion(1L);
94 vesAlarm.setSpecificProblem("specificProblem");
95 vesAlarm.setVfStatus("vfStatus");
97 String eventString = "{\"event\": {\"commonEventHeader\": {" +
98 "\"domain\": \"ONAP\"," +
99 "\"eventId\": \"123\"," +
100 "\"eventName\": \"Event-123\"," +
101 "\"eventType\": \"EventType\"," +
102 "\"lastEpochMicrosec\": 1000," +
103 "\"nfcNamingCode\": \"123\"," +
104 "\"nfNamingCode\": \"123\"," +
105 "\"priority\": \"high\"," +
106 "\"reportingEntityId\": \"ID-123\"," +
107 "\"reportingEntityName\": \"Name-123\"," +
109 "\"sourceId\": \"Source-123\"," +
110 "\"sourceName\": \"Source-123\"," +
111 "\"startEpochMicrosec\": 500," +
114 " \"faultFields\" : {" +
115 "\"alarmAdditionalInformation\": [{\"name\":\"addInfo\", \"value\":\"addInfo\"}]," +
116 "\"alarmCondition\": \"alarmCondition\"," +
117 "\"alarmInterfaceA\": \"alarmInterfaceA\"," +
118 "\"eventCategory\": \"eventCategory\"," +
119 "\"eventSeverity\": \"eventSeverity\"," +
120 "\"eventSourceType\": \"eventSourceType\"," +
121 "\"faultFieldsVersion\": 1," +
122 "\"specificProblem\": \"specificProblem\"," +
123 "\"vfStatus\": \"vfStatus\"" +
126 List<String> responseList = new ArrayList<>();
127 responseList.add(eventString);
130 PowerMock.mockStatic(ClientBuilder.class);
131 Client client = PowerMock.createMock(Client.class);
132 WebTarget webTarget = PowerMock.createMock(WebTarget.class);
133 Response response = PowerMock.createMock(Response.class);
134 Invocation.Builder builder = PowerMock.createMock(Invocation.Builder.class);
136 EasyMock.expect(ClientBuilder.newClient(EasyMock.anyObject(ClientConfig.class))).andReturn(client);
137 EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
138 EasyMock.expect(webTarget.queryParam("timeout", 15000)).andReturn(webTarget);
139 EasyMock.expect(webTarget.request()).andReturn(builder);
140 EasyMock.expect(builder.get()).andReturn(response);
141 EasyMock.expect(response.readEntity(List.class)).andReturn(responseList);
143 PowerMock.replayAll();
145 List<VesAlarm> vesAlarms = new Subscriber().subscribe();
147 assertThat(new ObjectMapper().writeValueAsString(vesAlarm),
148 equalTo(new ObjectMapper().writeValueAsString(vesAlarms.get(0))));
150 PowerMock.verifyAll();
154 public void testSetterAndGetter() {
156 PowerMock.replayAll();
158 Subscriber subscriber = new Subscriber();
159 subscriber.setTimeout(100);
160 subscriber.setLimit(10);
161 subscriber.setPeriod(10);
162 subscriber.setSecure(false);
163 subscriber.setTopic("test");
164 subscriber.setUrl("http://localhost");
165 subscriber.setConsumerGroup("Group1");
166 subscriber.setConsumer("Consumer1");
167 subscriber.setAuthInfo(null);
168 subscriber.setAuthExpDate(null);
170 assertThat(subscriber.getTimeout(), is(100));
171 assertThat(subscriber.getLimit(), is(10));
172 assertThat(subscriber.getPeriod(), is(10));
173 assertThat(subscriber.isSecure(), is(false));
174 assertThat(subscriber.getTopic(), equalTo("test"));
175 assertThat(subscriber.getUrl(), equalTo("http://localhost"));
176 assertThat(subscriber.getConsumerGroup(), equalTo("Group1"));
177 assertThat(subscriber.getConsumer(), equalTo("Consumer1"));
178 assertThat(subscriber.getAuthInfo(), nullValue());
179 assertThat(subscriber.getAuthExpDate(), nullValue());
181 PowerMock.verifyAll();