kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 89c55e0 | 2023-01-09 18:09:41 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 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; |
Ilayda Ozdemir | 8b96b32 | 2020-06-18 08:52:17 +0000 | [diff] [blame] | 22 | import org.opencord.igmpproxy.IgmpStatisticType; |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 23 | import org.opencord.igmpproxy.IgmpStatisticsEventListener; |
| 24 | import org.opencord.igmpproxy.IgmpStatisticsService; |
| 25 | import org.opencord.kafka.EventBusService; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | |
| 29 | /** |
| 30 | * set of unit test cases for IgmpKafkaIntegration. |
| 31 | */ |
| 32 | public class IgmpKafkaIntegrationTest extends KafkaIntegrationTestBase { |
| 33 | private IgmpKafkaIntegration igmpKafkaIntegration; |
| 34 | private IgmpStatisticsEventListener igmpStatisticsEventListener; |
| 35 | |
| 36 | @BeforeEach |
| 37 | void setup() { |
| 38 | igmpKafkaIntegration = new IgmpKafkaIntegration(); |
| 39 | igmpKafkaIntegration.eventBusService = new MockEventBusService(); |
| 40 | igmpKafkaIntegration.ignore = new MockIgmpStatisticsService(); |
| 41 | igmpKafkaIntegration.bindIgmpStatService(igmpKafkaIntegration.ignore); |
| 42 | igmpKafkaIntegration.activate(); |
| 43 | } |
| 44 | |
| 45 | @AfterEach |
| 46 | void tearDown() { |
| 47 | igmpKafkaIntegration.unbindIgmpStatService(igmpKafkaIntegration.ignore); |
| 48 | igmpKafkaIntegration.deactivate(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * test to verify IgmpStatisticsEvent event. |
| 53 | */ |
| 54 | @Test |
| 55 | void testIgmpStatisticsEvent() { |
| 56 | igmpStatisticsEventListener.event(getIgmpStatisticsEvent()); |
| 57 | assertEquals(MockEventBusService.igmpstats, 1); |
| 58 | assertEquals(MockEventBusService.otherCounter, 0); |
| 59 | } |
| 60 | |
| 61 | private static class MockEventBusService implements EventBusService { |
| 62 | static int igmpstats; |
| 63 | static int otherCounter; |
| 64 | |
| 65 | MockEventBusService() { |
| 66 | igmpstats = 0; |
| 67 | otherCounter = 0; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void send(String topic, JsonNode data) { |
| 72 | if (topic.equals(IgmpKafkaIntegration.IGMP_STATISTICS_TOPIC)) { |
| 73 | igmpstats++; |
| 74 | } else { |
| 75 | otherCounter++; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private class MockIgmpStatisticsService implements IgmpStatisticsService { |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public void addListener(IgmpStatisticsEventListener listener) { |
| 84 | igmpStatisticsEventListener = listener; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void removeListener(IgmpStatisticsEventListener listener) { |
| 89 | igmpStatisticsEventListener = null; |
| 90 | } |
Ilayda Ozdemir | 8b96b32 | 2020-06-18 08:52:17 +0000 | [diff] [blame] | 91 | |
| 92 | @Override |
| 93 | public void increaseStat(IgmpStatisticType igmpStatisticType) { |
| 94 | |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void resetAllStats() { |
| 99 | |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Long getStat(IgmpStatisticType igmpStatisticType) { |
| 104 | return 0L; |
| 105 | } |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 106 | } |
| 107 | } |