OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImplTest.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 package org.opendaylight.openflowplugin.impl.lifecycle;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
28 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
30
31 @RunWith(MockitoJUnitRunner.class)
32 public class ContextChainImplTest {
33
34     private static final String TEST_NODE = "test node";
35     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
36
37     @Mock
38     private StatisticsContext statisticsContext;
39     @Mock
40     private RpcContext rpcContext;
41     @Mock
42     private DeviceContext deviceContext;
43     @Mock
44     private DeviceInfo deviceInfo;
45     @Mock
46     private ConnectionContext connectionContext;
47     @Mock
48     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
49     @Mock
50     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
51     @Mock
52     private ContextChainMastershipWatcher contextChainMastershipWatcher;
53     @Mock
54     private DeviceRemovedHandler deviceRemovedHandler;
55
56     private ContextChain contextChain;
57
58     @Before
59     public void setUp() throws Exception {
60         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
61         Mockito.when(deviceInfo.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
62         Mockito.when(deviceContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
63         Mockito.when(rpcContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
64         Mockito.when(statisticsContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
65         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
66         Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
67                 .thenReturn(clusterSingletonServiceRegistration);
68
69         contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
70                 MoreExecutors.newDirectExecutorService());
71         contextChain.addContext(statisticsContext);
72         contextChain.addContext(rpcContext);
73         contextChain.addContext(deviceContext);
74         contextChain.registerServices(clusterSingletonServiceProvider);
75     }
76
77     @Test
78     public void closeServiceInstance() throws Exception {
79         contextChain.closeServiceInstance();
80         Mockito.verify(deviceContext).closeServiceInstance();
81         Mockito.verify(rpcContext).closeServiceInstance();
82         Mockito.verify(statisticsContext).closeServiceInstance();
83     }
84
85     @Test
86     public void close() throws Exception {
87         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
88         contextChain.close();
89         Mockito.verify(statisticsContext).close();
90         Mockito.verify(deviceContext).close();
91         Mockito.verify(rpcContext).close();
92         Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
93     }
94
95     @Test
96     public void closeTwoTimes() throws Exception {
97         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
98         contextChain.close();
99         contextChain.close();
100         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
101                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
102     }
103
104     @Test
105     public void closeThreeTimes() throws Exception {
106         contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
107         contextChain.close();
108         contextChain.close();
109         Mockito.verify(deviceRemovedHandler, Mockito.times(1))
110                 .onDeviceRemoved(Mockito.any(DeviceInfo.class));
111     }
112
113     @Test
114     public void getIdentifier() throws Exception {
115         Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
116     }
117
118     @Test
119     public void instantiateServiceInstanceFail() throws Exception {
120         Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
121         contextChain.instantiateServiceInstance();
122         Mockito.verify(contextChainMastershipWatcher)
123                 .onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());
124     }
125 }