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