blob: 135239ab56e00aa22a8d9064ffe78e8e0cff8794 [file] [log] [blame]
kishoreefa3db52020-03-23 16:09:26 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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 */
16package org.opencord.kafka.integrations;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.junit.jupiter.api.AfterEach;
20import org.junit.jupiter.api.BeforeEach;
21import org.junit.jupiter.api.Test;
22import org.onlab.packet.VlanId;
23import org.opencord.cordmcast.CordMcastStatisticsEventListener;
24import org.opencord.cordmcast.CordMcastStatisticsService;
25import org.opencord.kafka.EventBusService;
26
27import static org.junit.Assert.assertEquals;
28
29/**
30 * set of unit test cases for McastKafkaIntegration.
31 */
32public class McastKafkaIntegrationTest extends KafkaIntegrationTestBase {
33 private McastKafkaIntegration mcastKafkaIntegration;
34 private CordMcastStatisticsEventListener mcastStatsListerner;
35
36 @BeforeEach
37 void setup() {
38 mcastKafkaIntegration = new McastKafkaIntegration();
39 mcastKafkaIntegration.eventBusService = new MockEventBusService();
40 mcastKafkaIntegration.cordMcastStatisticsService = new MockCordMcastStatisticsService();
41 mcastKafkaIntegration.bindMcastStatisticsService(mcastKafkaIntegration.cordMcastStatisticsService);
42 mcastKafkaIntegration.activate();
43 }
44
45 @AfterEach
46 void tearDown() {
47 mcastKafkaIntegration.unbindMcastStatisticsService(mcastKafkaIntegration.cordMcastStatisticsService);
48 mcastKafkaIntegration.deactivate();
49 }
50
51 /**
52 * test to verify CordMcastStatisticsEvent event.
53 */
54 @Test
55 void testHandleEvent() {
56 mcastStatsListerner.event(getCordMcastStatisticsEvent());
57 assertEquals(MockEventBusService.mcastStats, 1);
58 assertEquals(MockEventBusService.otherCounter, 0);
59
60 }
61
62 private static class MockEventBusService implements EventBusService {
63 static int mcastStats;
64 static int otherCounter;
65
66 MockEventBusService() {
67 mcastStats = 0;
68 otherCounter = 0;
69 }
70
71 @Override
72 public void send(String topic, JsonNode data) {
73 if (topic.equals(McastKafkaIntegration.MCAST_OPERATIONAL_STATUS_TOPIC)) {
74 mcastStats++;
75 } else {
76 otherCounter++;
77 }
78 }
79 }
80
81 private class MockCordMcastStatisticsService implements CordMcastStatisticsService {
82 @Override
83 public void setVlanValue(VlanId vlanId) {
84 }
85
86 @Override
87 public void addListener(CordMcastStatisticsEventListener listener) {
88 mcastStatsListerner = listener;
89 }
90
91 @Override
92 public void removeListener(CordMcastStatisticsEventListener listener) {
93 mcastStatsListerner = null;
94 }
95 }
96}