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 org.junit.jupiter.api.AfterEach; |
| 21 | import org.junit.jupiter.api.BeforeEach; |
| 22 | import org.junit.jupiter.api.Test; |
| 23 | import org.onosproject.net.device.DeviceEvent; |
| 24 | import org.opencord.kafka.EventBusService; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.opencord.kafka.integrations.MockDeviceService.DEVICE_ID_1; |
| 28 | |
| 29 | /** |
| 30 | * set of unit test cases for DeviceKafkaIntegration. |
| 31 | */ |
| 32 | class DeviceKafkaIntegrationTest extends KafkaIntegrationTestBase { |
| 33 | private DeviceKafkaIntegration deviceKafkaIntegration; |
| 34 | |
| 35 | @BeforeEach |
| 36 | void setUp() { |
| 37 | deviceKafkaIntegration = new DeviceKafkaIntegration(); |
| 38 | deviceKafkaIntegration.eventBusService = new MockEventBusService(); |
| 39 | deviceKafkaIntegration.deviceService = new MockDeviceService(); |
| 40 | deviceKafkaIntegration.activate(); |
| 41 | } |
| 42 | |
| 43 | @AfterEach |
| 44 | void tearDown() { |
| 45 | deviceKafkaIntegration.deactivate(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * testcase to verify Port updated event. |
| 50 | */ |
| 51 | @Test |
| 52 | void testPortStateUpdate() { |
| 53 | DeviceEvent event = new DeviceEvent(DeviceEvent.Type.PORT_UPDATED, |
| 54 | deviceKafkaIntegration.deviceService.getDevice(DEVICE_ID_1), |
| 55 | new MockDeviceService.MockPort()); |
| 56 | deviceKafkaIntegration.listener.event(event); |
| 57 | assertEquals(MockEventBusService.events, 1); |
| 58 | assertEquals(MockEventBusService.otherCounter, 0); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * testcase to verify Port stats update event. |
| 63 | */ |
| 64 | @Test |
| 65 | void testPortStatsUpdate() { |
| 66 | DeviceEvent event = new DeviceEvent(DeviceEvent.Type.PORT_STATS_UPDATED, |
| 67 | deviceKafkaIntegration.deviceService.getDevice(DEVICE_ID_1), |
| 68 | new MockDeviceService.MockPort()); |
| 69 | deviceKafkaIntegration.listener.event(event); |
| 70 | assertEquals(MockEventBusService.kpis, 1); |
| 71 | assertEquals(MockEventBusService.otherCounter, 0); |
| 72 | } |
| 73 | |
| 74 | private static class MockEventBusService implements EventBusService { |
| 75 | static int kpis; |
| 76 | static int events; |
| 77 | static int otherCounter; |
| 78 | |
| 79 | MockEventBusService() { |
| 80 | kpis = 0; |
| 81 | events = 0; |
| 82 | otherCounter = 0; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void send(String topic, JsonNode data) { |
| 87 | switch (topic) { |
| 88 | case DeviceKafkaIntegration.TOPIC: |
| 89 | kpis++; |
| 90 | break; |
| 91 | case DeviceKafkaIntegration.PORT_EVENT_TOPIC: |
| 92 | events++; |
| 93 | break; |
| 94 | default: |
| 95 | otherCounter++; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |