kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.kafka.integrations; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.junit.jupiter.api.AfterEach; |
| 20 | import org.junit.jupiter.api.BeforeEach; |
| 21 | import org.junit.jupiter.api.Test; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.opencord.cordmcast.CordMcastStatisticsEventListener; |
| 24 | import org.opencord.cordmcast.CordMcastStatisticsService; |
| 25 | import org.opencord.kafka.EventBusService; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | |
| 29 | /** |
| 30 | * set of unit test cases for McastKafkaIntegration. |
| 31 | */ |
| 32 | public 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 |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 87 | public void setInnerVlanValue(VlanId vlanId) { |
| 88 | |
| 89 | } |
| 90 | |
| 91 | @Override |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 92 | public void addListener(CordMcastStatisticsEventListener listener) { |
| 93 | mcastStatsListerner = listener; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void removeListener(CordMcastStatisticsEventListener listener) { |
| 98 | mcastStatsListerner = null; |
| 99 | } |
| 100 | } |
| 101 | } |