04cc1f6dd6af7503f3ac18f3b83eda5a9042ec22
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceTransactionChainManagerProviderTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.device;
10
11 import junit.framework.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22
23 @RunWith(MockitoJUnitRunner.class)
24 public class DeviceTransactionChainManagerProviderTest {
25
26
27     @Mock
28     DataBroker dataBroker;
29     @Mock
30     ConnectionContext connectionContext;
31     @Mock
32     ConnectionContext concurrentConnectionContex;
33
34     @Mock
35     DeviceManager deviceManager;
36
37     private static final NodeId nodeId = new NodeId("OPF:TEST");
38     private DeviceTransactionChainManagerProvider deviceTransactionChainManagerProvider;
39
40     @Before
41     public void setup() {
42         deviceTransactionChainManagerProvider = new DeviceTransactionChainManagerProvider(dataBroker);
43         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
44         Mockito.when(concurrentConnectionContex.getNodeId()).thenReturn(nodeId);
45     }
46
47     /**
48      * This test verifies code path for registering new connection when no {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
49      * is present in registry.
50      *
51      * @throws Exception
52      */
53     @Test
54     public void testProvideTransactionChainManagerOrWaitForNotification1() throws Exception {
55         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
56         Assert.assertTrue(transactionChainManagerRegistration.ownedByInvokingConnectionContext());
57     }
58
59     /**
60      * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
61      * is present in registry.
62      *
63      * @throws Exception
64      */
65     @Test
66     public void testProvideTransactionChainManagerOrWaitForNotification2() throws Exception {
67         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
68         Assert.assertTrue(TransactionChainManager.TransactionChainManagerStatus.WORKING.equals(transactionChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus()));
69         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_2 = deviceTransactionChainManagerProvider.provideTransactionChainManager(concurrentConnectionContex);
70         Assert.assertFalse(transactionChainManagerRegistration_2.ownedByInvokingConnectionContext());
71     }
72
73 }