blob: 7b4ba8fd6c60bcd916a8df562cee82202cd8c4c5 [file] [log] [blame]
kishoreefa3db52020-03-23 16:09:26 +05301/*
Joey Armstrong89c55e02023-01-09 18:09:41 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
kishoreefa3db52020-03-23 16:09:26 +05303 *
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
17package org.opencord.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import org.junit.jupiter.api.AfterEach;
21import org.junit.jupiter.api.BeforeEach;
22import org.junit.jupiter.api.Test;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.RADIUS;
25import org.opencord.aaa.AaaMachineStatisticsDelegate;
26import org.opencord.aaa.AaaMachineStatisticsEventListener;
27import org.opencord.aaa.AaaMachineStatisticsService;
28import org.opencord.aaa.AaaStatistics;
29import org.opencord.aaa.AaaStatisticsSnapshot;
30import org.opencord.aaa.AaaSupplicantMachineStats;
31import org.opencord.aaa.AuthenticationEventListener;
32import org.opencord.aaa.AuthenticationRecord;
33import org.opencord.aaa.AuthenticationService;
34import org.opencord.aaa.AuthenticationStatisticsEventListener;
35import org.opencord.aaa.AuthenticationStatisticsService;
36import org.opencord.aaa.RadiusCommunicator;
37import org.opencord.aaa.RadiusOperationalStatusEventDelegate;
38import org.opencord.aaa.RadiusOperationalStatusEventListener;
39import org.opencord.aaa.RadiusOperationalStatusService;
40import org.opencord.kafka.EventBusService;
41
42import static org.junit.Assert.assertEquals;
43
44/**
45 * set of unit test cases for AaaKafkaIntegration.
46 */
47class AaaKafkaIntegrationTest extends KafkaIntegrationTestBase {
48
49
50 private AaaKafkaIntegration aaaKafkaInt;
51 private AuthenticationEventListener authEventListener;
52 private AuthenticationStatisticsEventListener authStatisticsEventListener;
53 private RadiusOperationalStatusEventListener radiusOperStatusEventListener;
54 private AaaMachineStatisticsEventListener aaaMachineStatisticsEventListener;
55
56 @BeforeEach
57 void setUp() {
58 aaaKafkaInt = new AaaKafkaIntegration();
59
60 aaaKafkaInt.deviceService = new MockDeviceService();
61 aaaKafkaInt.eventBusService = new MockEventBusService();
62 aaaKafkaInt.ignore = new MockAuthenticationService();
63 aaaKafkaInt.ignore2 = new MockAuthenticationStatisticsService();
64 aaaKafkaInt.ignore3 = new MockRadiusOperationalStatusService();
65 aaaKafkaInt.ignore4 = new MockAaaMachineStatisticsService();
66 aaaKafkaInt.bindAuthenticationService(aaaKafkaInt.ignore);
67 aaaKafkaInt.bindAuthenticationStatService(aaaKafkaInt.ignore2);
68 aaaKafkaInt.bindRadiusOperationalStatusService(aaaKafkaInt.ignore3);
69 aaaKafkaInt.bindAaaMachineStatisticsService(aaaKafkaInt.ignore4);
70 aaaKafkaInt.activate();
71 }
72
73 @AfterEach
74 void tearDown() {
75 aaaKafkaInt.deactivate();
76 aaaKafkaInt.unbindRadiusOperationalStatusService(aaaKafkaInt.ignore3);
77 aaaKafkaInt.unbindAaaMachineStatisticsService(aaaKafkaInt.ignore4);
78 aaaKafkaInt = null;
79 }
80
81 /**
82 * testAuthenticationEvent to perform unit test for
83 * the AuthenticationEvent event.
84 */
85 @Test
86 void testAuthenticationEvent() {
87 authEventListener.event(getAuthenticationEvent());
88 assertEquals(MockEventBusService.authCounter, 1);
89 assertEquals(MockEventBusService.otherCounter, 0);
90 }
91
92 /**
93 * testAuthenticationStatisticsEvent to perform unit test for
94 * the AuthenticationStatisticsEvent event.
95 */
96 @Test
97 void testAuthenticationStatisticsEvent() {
98 authStatisticsEventListener.event(getAuthenticationStatisticsEvent());
99 assertEquals(MockEventBusService.authStatsCounter, 1);
100 assertEquals(MockEventBusService.otherCounter, 0);
101 }
102
103 /**
104 * testRadiusOperationalStatusEvent to perform unit test for
105 * the RadiusOperationalStatusEvent event.
106 */
107 @Test
108 void testRadiusOperationalStatusEvent() {
109 radiusOperStatusEventListener.event(getRadiusOperationalStatusEvent());
110 assertEquals(MockEventBusService.radiusOperstate, 1);
111 assertEquals(MockEventBusService.otherCounter, 0);
112 }
113
114 /**
115 * testAaaMachineStatisticsEvent to perform unit test for
116 * the AaaMachineStatisticsEvent event.
117 */
118 @Test
119 void testAaaMachineStatisticsEvent() {
120 aaaMachineStatisticsEventListener.event(getAaaMachineStatisticsEvent());
121 assertEquals(MockEventBusService.authStatsCounter, 1);
122 assertEquals(MockEventBusService.otherCounter, 0);
123 }
124
125 /**
126 * EventBusService mocker class.
127 */
128 private static class MockEventBusService implements EventBusService {
129 static int authCounter;
130 static int authStatsCounter;
131 static int radiusOperstate;
132 static int otherCounter;
133
134 MockEventBusService() {
135 authCounter = 0;
136 authStatsCounter = 0;
137 radiusOperstate = 0;
138 otherCounter = 0;
139 }
140
141 @Override
142 public void send(String topic, JsonNode data) {
143 switch (topic) {
144 case AaaKafkaIntegration.TOPIC:
145 authCounter++;
146 break;
147 case AaaKafkaIntegration.AUTHENTICATION_STATISTICS_TOPIC:
148 authStatsCounter++;
149 break;
150 case AaaKafkaIntegration.RADIUS_OPERATION_STATUS_TOPIC:
151 radiusOperstate++;
152 break;
153 default:
154 otherCounter++;
155 break;
156 }
157 }
158 }
159
160 /**
161 * AuthenticationService mocker class.
162 */
163 private class MockAuthenticationService implements AuthenticationService {
164 @Override
165 public Iterable<AuthenticationRecord> getAuthenticationRecords() {
166 return null;
167 }
168
169 @Override
170 public boolean removeAuthenticationStateByMac(MacAddress macAddress) {
171 return false;
172 }
173
174 @Override
175 public AaaSupplicantMachineStats getSupplicantMachineStats(String s) {
176 return null;
177 }
178
179 @Override
180 public void addListener(AuthenticationEventListener listener) {
181 authEventListener = listener;
182 }
183
184 @Override
185 public void removeListener(AuthenticationEventListener listener) {
186 authEventListener = null;
187 }
188 }
189
190 /**
191 * AuthenticationStatisticsService mocker class.
192 */
193 private class MockAuthenticationStatisticsService implements AuthenticationStatisticsService {
194 @Override
195 public AaaStatistics getAaaStats() {
196 return null;
197 }
198
199 @Override
200 public AaaStatisticsSnapshot getClusterStatistics() {
201 return null;
202 }
203
204 @Override
205 public void handleRoundtripTime(byte b) {
206
207 }
208
209 @Override
210 public void calculatePacketRoundtripTime() {
211
212 }
213
214 @Override
215 public void putOutgoingIdentifierToMap(byte b) {
216
217 }
218
219 @Override
220 public void resetAllCounters() {
221
222 }
223
224 @Override
225 public void addListener(AuthenticationStatisticsEventListener listener) {
226 authStatisticsEventListener = listener;
227 }
228
229 @Override
230 public void removeListener(AuthenticationStatisticsEventListener listener) {
231 authStatisticsEventListener = null;
232 }
233 }
234
235 /**
236 * RadiusOperationalStatusService mocker class.
237 */
238 private class MockRadiusOperationalStatusService implements RadiusOperationalStatusService {
239 @Override
240 public RadiusOperationalStatusEventDelegate getRadiusOprStDelegate() {
241 return null;
242 }
243
244 @Override
245 public String getRadiusServerOperationalStatus() {
246 return null;
247 }
248
249 @Override
250 public void setStatusServerReqSent(boolean b) {
251
252 }
253
254 @Override
255 public void setRadiusOperationalStatusEvaluationMode(
256 RadiusOperationalStatusService.RadiusOperationalStatusEvaluationMode
257 radiusOperationalStatusEvaluationMode) {
258
259 }
260
261 @Override
262 public void setOperationalStatusServerTimeoutInMillis(long l) {
263
264 }
265
266 @Override
267 public void checkServerOperationalStatus() {
268
269 }
270
271 @Override
272 public boolean isRadiusResponseForOperationalStatus(byte b) {
273 return false;
274 }
275
276 @Override
277 public void handleRadiusPacketForOperationalStatus(RADIUS radius) {
278
279 }
280
281 @Override
282 public void initialize(byte[] bytes, String s, RadiusCommunicator radiusCommunicator) {
283
284 }
285
286 @Override
287 public void setOutTimeInMillis(byte b) {
288
289 }
290
291 @Override
292 public void addListener(RadiusOperationalStatusEventListener listener) {
293 radiusOperStatusEventListener = listener;
294 }
295
296 @Override
297 public void removeListener(RadiusOperationalStatusEventListener listener) {
298 radiusOperStatusEventListener = null;
299 }
300 }
301
302 /**
303 * AaaMachineStatisticsService mocker class.
304 */
305 private class MockAaaMachineStatisticsService implements AaaMachineStatisticsService {
306 @Override
307 public AaaSupplicantMachineStats getSupplicantStats(Object o) {
308 return null;
309 }
310
311 @Override
312 public AaaMachineStatisticsDelegate getMachineStatsDelegate() {
313 return null;
314 }
315
316 @Override
317 public void logAaaSupplicantMachineStats(AaaSupplicantMachineStats aaaSupplicantMachineStats) {
318
319 }
320
321 @Override
322 public void addListener(AaaMachineStatisticsEventListener listener) {
323 aaaMachineStatisticsEventListener = listener;
324 }
325
326 @Override
327 public void removeListener(AaaMachineStatisticsEventListener listener) {
328 aaaMachineStatisticsEventListener = null;
329 }
330 }
331}