Merge "Drop the odlparent.netty property"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.java
1 /*
2  * Copyright (c) 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.impl.role;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.util.concurrent.Futures;
16 import io.netty.util.HashedWheelTimer;
17 import java.util.concurrent.Future;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
28 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
29 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
30 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class RoleContextImplTest {
41     @Mock
42     private SalRoleService roleService;
43     @Mock
44     private ContextChainMastershipWatcher contextChainMastershipWatcher;
45     @Mock
46     private DeviceInfo deviceInfo;
47     @Mock
48     private DeviceContext deviceContext;
49     @Mock
50     private Future<RpcResult<SetRoleOutput>> setRoleFuture;
51     private RoleContext roleContext;
52
53     @Before
54     public void setUp() throws Exception {
55         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DeviceStateUtil
56                 .createNodeInstanceIdentifier(new NodeId("openflow:1")));
57         when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
58         when(roleService.setRole(any())).thenReturn(Futures.immediateFuture(null));
59
60         roleContext = new RoleContextImpl(deviceInfo, new HashedWheelTimer(), 20000);
61         roleContext.registerMastershipWatcher(contextChainMastershipWatcher);
62         roleContext.setRoleService(roleService);
63     }
64
65     @After
66     public void tearDown() throws Exception {
67         roleContext.close();
68     }
69
70     @Test
71     public void instantiateServiceInstance() throws Exception {
72         roleContext.instantiateServiceInstance();
73         verify(roleService).setRole(new SetRoleInputBuilder()
74                 .setControllerRole(OfpRole.BECOMEMASTER)
75                 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
76                 .build());
77         verify(contextChainMastershipWatcher).onMasterRoleAcquired(
78                 deviceInfo,
79                 ContextChainMastershipState.MASTER_ON_DEVICE);
80     }
81
82     @Test
83     public void closeServiceInstance() throws Exception {
84         when(setRoleFuture.isCancelled()).thenReturn(false);
85         when(setRoleFuture.isDone()).thenReturn(false);
86         when(roleService.setRole(any())).thenReturn(setRoleFuture);
87         roleContext.instantiateServiceInstance();
88         roleContext.closeServiceInstance().get();
89         verify(setRoleFuture).cancel(true);
90     }
91
92 }