Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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.olt.impl; |
| 18 | |
| 19 | import org.junit.Assert; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.mockito.Mockito; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.cluster.DefaultControllerNode; |
| 26 | import org.onosproject.cluster.Leader; |
| 27 | import org.onosproject.cluster.Leadership; |
| 28 | import org.onosproject.cluster.LeadershipService; |
| 29 | import org.onosproject.cluster.NodeId; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.opencord.sadis.SadisService; |
| 34 | |
| 35 | import java.util.LinkedList; |
| 36 | |
| 37 | import static org.mockito.Matchers.eq; |
| 38 | import static org.mockito.Mockito.doReturn; |
| 39 | |
| 40 | public class OltDeviceServiceTest { |
| 41 | OltDeviceService component; |
| 42 | private OltDeviceService oltDeviceService; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() { |
| 46 | component = new OltDeviceService(); |
| 47 | component.mastershipService = Mockito.mock(MastershipService.class); |
| 48 | component.deviceService = Mockito.mock(DeviceService.class); |
| 49 | component.leadershipService = Mockito.mock(LeadershipService.class); |
| 50 | component.clusterService = Mockito.mock(ClusterService.class); |
| 51 | component.sadisService = Mockito.mock(SadisService.class); |
| 52 | component.activate(); |
| 53 | |
| 54 | oltDeviceService = Mockito.spy(component); |
| 55 | |
| 56 | |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testIsLocalLeader() { |
| 61 | |
| 62 | NodeId nodeId = NodeId.nodeId("node1"); |
| 63 | ControllerNode localNode = new DefaultControllerNode(nodeId, "host1"); |
| 64 | DeviceId deviceId1 = DeviceId.deviceId("availableNotLocal"); |
| 65 | DeviceId deviceId2 = DeviceId.deviceId("notAvailableButLocal"); |
| 66 | Leadership leadership = new Leadership(deviceId2.toString(), new Leader(nodeId, 0, 0), new LinkedList<>()); |
| 67 | |
| 68 | doReturn(true).when(oltDeviceService.deviceService).isAvailable(eq(deviceId1)); |
| 69 | doReturn(false).when(oltDeviceService.mastershipService).isLocalMaster(eq(deviceId1)); |
| 70 | Assert.assertFalse(oltDeviceService.isLocalLeader(deviceId1)); |
| 71 | |
| 72 | doReturn(false).when(oltDeviceService.deviceService).isAvailable(eq(deviceId1)); |
| 73 | doReturn(localNode).when(oltDeviceService.clusterService).getLocalNode(); |
| 74 | doReturn(leadership).when(oltDeviceService.leadershipService).runForLeadership(eq(deviceId2.toString())); |
| 75 | Assert.assertTrue(oltDeviceService.isLocalLeader(deviceId2)); |
| 76 | |
| 77 | } |
| 78 | } |