Bug 6745 Improve compression queue locking and handle InterruptedException
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorGuardDecoratorTest.java
1 /**
2  * Copyright (c) 2016 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.applications.frsync.impl;
10
11 import com.google.common.util.concurrent.Futures;
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.openflowplugin.applications.frsync.SemaphoreKeeper;
20 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
21 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
22 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 /**
31  * Test for {@link SyncReactorGuardDecorator}.
32  */
33 @RunWith(MockitoJUnitRunner.class)
34 public class SyncReactorGuardDecoratorTest {
35
36     private static final NodeId NODE_ID = new NodeId("test-node");
37     private SyncReactorGuardDecorator reactor;
38     private InstanceIdentifier<FlowCapableNode> fcNodePath;
39
40     @Mock
41     private SyncReactor delegate;
42     @Mock
43     private FlowCapableNode fcConfigNode;
44     @Mock
45     private FlowCapableNode fcOperationalNode;
46     @Mock
47     private SyncupEntry syncupEntry;
48
49     @Before
50     public void setUp() throws Exception {
51         final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper = new SemaphoreKeeperGuavaImpl<>(1, true);
52         reactor = new SyncReactorGuardDecorator(delegate, semaphoreKeeper);
53         InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID));
54         fcNodePath = nodePath.augmentation(FlowCapableNode.class);
55
56         final Node operationalNode = Mockito.mock(Node.class);
57         Mockito.when(operationalNode.getId()).thenReturn(NODE_ID);
58         Mockito.when(operationalNode.getAugmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
59     }
60
61     @Test
62     public void testSyncupSuccess() {
63         Mockito.when(delegate.syncup(Matchers.any(), Matchers.any()))
64                 .thenReturn(Futures.immediateFuture(Boolean.TRUE));
65
66         reactor.syncup(fcNodePath, syncupEntry);
67
68         Mockito.verify(delegate).syncup(fcNodePath, syncupEntry);
69         Mockito.verifyNoMoreInteractions(delegate);
70     }
71
72     @Test
73     public void testSyncupFail() {
74         Mockito.when(delegate.syncup(Matchers.any(), Matchers.any()))
75                 .thenReturn(Futures.immediateFailedFuture(new Exception()));
76
77         reactor.syncup(fcNodePath, syncupEntry);
78
79         Mockito.verify(delegate).syncup(fcNodePath, syncupEntry);
80         Mockito.verifyNoMoreInteractions(delegate);
81     }
82 }