blob: e05139c515a17ab0dee9ce1a6cc133becdeb7d32 [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 */
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.onosproject.net.behaviour.BngProgrammable;
24import org.onosproject.net.pi.runtime.PiCounterCellData;
25import org.opencord.bng.BngStatsEventListener;
26import org.opencord.bng.BngStatsService;
27import org.opencord.kafka.EventBusService;
28
29import java.util.Map;
30
31import static org.junit.Assert.assertEquals;
32
33/**
34 * set of unit test cases to verify BngStatsKafkaIntegration.
35 */
36class BngStatsKafkaIntegrationTest extends KafkaIntegrationTestBase {
37
38
39 private BngStatsKafkaIntegration bngStatsKafkaIntegration;
40 private BngStatsEventListener bngStatsEventListener;
41
42 @BeforeEach
43 void setUp() {
44 bngStatsKafkaIntegration = new BngStatsKafkaIntegration();
45 bngStatsKafkaIntegration.eventBusService = new MockEventBusService();
46 bngStatsKafkaIntegration.ignore = new MockBngStatsService();
47 bngStatsKafkaIntegration.bindBngStatsService(bngStatsKafkaIntegration.ignore);
48 bngStatsKafkaIntegration.activate();
49 }
50
51 @AfterEach
52 void tearDown() {
53 bngStatsKafkaIntegration.deactivate();
54 }
55
56 @Test
57 void testBngStatsEvent() {
58 bngStatsEventListener.event(getBngStatsEvent());
59 assertEquals(MockEventBusService.kafkaEvents, 1);
60 assertEquals(MockEventBusService.otherCounter, 0);
61 }
62
63 private static class MockEventBusService implements EventBusService {
64 static int kafkaEvents;
65 static int otherCounter;
66
67 MockEventBusService() {
68 kafkaEvents = 0;
69 otherCounter = 0;
70 }
71
72 @Override
73 public void send(String topic, JsonNode data) {
74 if (topic.equals(BngStatsKafkaIntegration.TOPIC_STATS)) {
75 kafkaEvents++;
76 } else {
77 otherCounter++;
78 }
79 }
80 }
81
82 private class MockBngStatsService implements BngStatsService {
83 @Override
84 public Map<BngProgrammable.BngCounterType, PiCounterCellData> getStats(String s) {
85 return null;
86 }
87
88 @Override
89 public PiCounterCellData getControlStats() {
90 return null;
91 }
92
93 @Override
94 public void addListener(BngStatsEventListener listener) {
95 bngStatsEventListener = listener;
96 }
97
98 @Override
99 public void removeListener(BngStatsEventListener listener) {
100 bngStatsEventListener = null;
101 }
102 }
103}