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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.junit.jupiter.api.AfterEach; |
| 22 | import org.junit.jupiter.api.BeforeEach; |
| 23 | import org.junit.jupiter.api.Test; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.opencord.kafka.EventBusService; |
| 28 | import org.opencord.olt.AccessDeviceListener; |
| 29 | import org.opencord.olt.AccessDeviceService; |
| 30 | import org.opencord.olt.AccessSubscriberId; |
| 31 | import org.opencord.sadis.UniTagInformation; |
| 32 | |
| 33 | import java.util.List; |
| 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | import static org.junit.Assert.assertEquals; |
| 38 | |
| 39 | /** |
| 40 | * Set of unit test cases for AccessDeviceKafkaIntegration. |
| 41 | */ |
| 42 | class AccessDeviceKafkaIntegrationTest extends KafkaIntegrationTestBase { |
| 43 | |
| 44 | private AccessDeviceKafkaIntegration accessDeviceKakfa; |
| 45 | private AccessDeviceListener accessDeviceListener; |
| 46 | |
| 47 | @BeforeEach |
| 48 | void setUp() { |
| 49 | accessDeviceKakfa = new AccessDeviceKafkaIntegration(); |
| 50 | |
| 51 | accessDeviceKakfa.deviceService = new MockDeviceService(); |
| 52 | accessDeviceKakfa.eventBusService = new MockEventBusService(); |
| 53 | accessDeviceKakfa.accessDeviceService = new MockAccessDeviceService(); |
| 54 | accessDeviceKakfa.bindAccessDeviceService(accessDeviceKakfa.accessDeviceService); |
| 55 | accessDeviceKakfa.activate(); |
| 56 | } |
| 57 | |
| 58 | @AfterEach |
| 59 | void tearDown() { |
| 60 | accessDeviceKakfa.deactivate(); |
| 61 | accessDeviceKakfa = null; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * testcase to perform UNI_ADDED AccessDeviceEvent. |
| 66 | */ |
| 67 | @Test |
| 68 | void testUniAdded() { |
| 69 | accessDeviceListener.event(getUniAdded()); |
| 70 | assertEquals(MockEventBusService.kafkaEvents, 1); |
| 71 | assertEquals(MockEventBusService.otherCounter, 0); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * testcase to perform UNI_REMOVED AccessDeviceEvent. |
| 76 | */ |
| 77 | @Test |
| 78 | void testUniRemoved() { |
| 79 | accessDeviceListener.event(getUniRemoved()); |
| 80 | assertEquals(MockEventBusService.kafkaEvents, 1); |
| 81 | assertEquals(MockEventBusService.otherCounter, 0); |
| 82 | } |
| 83 | |
| 84 | private static class MockEventBusService implements EventBusService { |
| 85 | static int kafkaEvents; |
| 86 | static int otherCounter; |
| 87 | |
| 88 | MockEventBusService() { |
| 89 | kafkaEvents = 0; |
| 90 | otherCounter = 0; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void send(String topic, JsonNode data) { |
| 95 | if (topic.equals(AccessDeviceKafkaIntegration.TOPIC)) { |
| 96 | kafkaEvents++; |
| 97 | } else { |
| 98 | otherCounter++; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private class MockAccessDeviceService implements AccessDeviceService { |
| 104 | @Override |
| 105 | public boolean provisionSubscriber(ConnectPoint connectPoint) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean removeSubscriber(ConnectPoint connectPoint) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean provisionSubscriber(AccessSubscriberId accessSubscriberId, |
| 116 | Optional<VlanId> optional, Optional<VlanId> optional1, |
| 117 | Optional<Integer> optional2) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean removeSubscriber(AccessSubscriberId accessSubscriberId, |
| 123 | Optional<VlanId> optional, Optional<VlanId> optional1, |
| 124 | Optional<Integer> optional2) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public List<DeviceId> fetchOlts() { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public ImmutableMap<ConnectPoint, Set<UniTagInformation>> getProgSubs() { |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void addListener(AccessDeviceListener listener) { |
| 140 | accessDeviceListener = listener; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void removeListener(AccessDeviceListener listener) { |
| 145 | accessDeviceListener = null; |
| 146 | } |
| 147 | } |
| 148 | } |