BUG8607 Fix checkstyle issues
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManagerTest.java
1 /**
2  * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 /**
35  * Test for {@link DeviceMastershipManager}.
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 public class DeviceMastershipManagerTest {
39     private static final NodeId NODE_ID = new NodeId("testNode");
40     private DeviceMastershipManager deviceMastershipManager;
41     @Mock
42     private ClusterSingletonServiceRegistration registration;
43     @Mock
44     private ClusterSingletonServiceProvider clusterSingletonService;
45     @Mock
46     private NotificationProviderService notificationService;
47     @Mock
48     private FlowNodeReconciliation reconciliationAgent;
49     @Mock
50     private DataBroker dataBroker;
51
52     @Before
53     public void setUp() throws Exception {
54         deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService, notificationService,
55                 reconciliationAgent, dataBroker);
56         Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
57                 .thenReturn(registration);
58     }
59
60     @Test
61     public void testOnDeviceConnectedAndDisconnected() throws Exception {
62         // no context
63         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
64         NodeUpdatedBuilder nodeUpdatedBuilder = new NodeUpdatedBuilder();
65         nodeUpdatedBuilder.setId(NODE_ID);
66         deviceMastershipManager.onNodeUpdated(nodeUpdatedBuilder.build());
67         DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
68         Assert.assertNotNull(serviceInstance);
69         // destroy context - unregister
70         Assert.assertNotNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
71         NodeRemovedBuilder nodeRemovedBuilder = new NodeRemovedBuilder();
72         InstanceIdentifier<Node> nodeIId = InstanceIdentifier.create(Nodes.class).child(Node.class,
73                 new NodeKey(NODE_ID));
74         nodeRemovedBuilder.setNodeRef(new NodeRef(nodeIId));
75         deviceMastershipManager.onNodeRemoved(nodeRemovedBuilder.build());
76         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
77     }
78
79     @Test
80     public void testIsDeviceMasteredOrSlaved() {
81         // no context
82         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
83         NodeUpdatedBuilder nodeUpdatedBuilder = new NodeUpdatedBuilder();
84         nodeUpdatedBuilder.setId(NODE_ID);
85         deviceMastershipManager.onNodeUpdated(nodeUpdatedBuilder.build());
86         // is master
87         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
88         Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
89         // is not master
90         deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
91         Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
92     }
93 }