Merge "BUG-2771: Converting String.split() to Guava Splitter for Inventory Utils"
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedOperationalListenerTest.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 terms of the Eclipse
5  * Public License v1.0 which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
30 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
31 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
32 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 /**
41  * Test for {@link SimplifiedOperationalListener}.
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class SimplifiedOperationalListenerTest {
45
46     public static final NodeId NODE_ID = new NodeId("testNode");
47     @Mock
48     private SyncReactor reactor;
49     @Mock
50     private ReadOnlyTransaction roTx;
51     @Mock
52     private DataTreeModification<Node> dataTreeModification;
53     @Mock
54     private FlowCapableNode configNode;
55     @Mock
56     private FlowCapableNode fcOperationalNode;
57
58     private InstanceIdentifier<Node> nodePath;
59     private InstanceIdentifier<FlowCapableNode> fcNodePath;
60     private SimplifiedOperationalListener nodeListenerOperational;
61
62     @Before
63     public void setUp() throws Exception {
64         final DataBroker db = Mockito.mock(DataBroker.class);
65         final DataObjectModification<Node> operationalModification = Mockito.mock(DataObjectModification.class);
66         final Node operationalNode = Mockito.mock(Node.class);
67
68         final FlowCapableNodeSnapshotDao configSnaphot = new FlowCapableNodeSnapshotDao();
69         final FlowCapableNodeSnapshotDao operationalSnaphot = new FlowCapableNodeSnapshotDao();
70         final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnaphot,
71                 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.CONFIGURATION));
72         nodeListenerOperational = new SimplifiedOperationalListener(reactor, operationalSnaphot, configDao);
73         nodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID));
74         fcNodePath = nodePath.augmentation(FlowCapableNode.class);
75
76         final DataTreeIdentifier<Node> dataTreeIdentifier =
77                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
78
79         Mockito.when(operationalNode.getId()).thenReturn(NODE_ID);
80         Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
81         Mockito.when(operationalNode.getAugmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
82         Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
83         Mockito.when(dataTreeModification.getRootNode()).thenReturn(operationalModification);
84         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
85     }
86
87     @Test
88     public void testDSLogicalType() throws Exception {
89         Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
90     }
91
92     @Test
93     public void testOnDataTreeChangedSyncup() throws Exception {
94         Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(),Matchers.<FlowCapableNode>any(),
95                 Matchers.<FlowCapableNode>any())).thenReturn(Futures.immediateFuture(Boolean.TRUE));
96         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
97                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(configNode)));
98
99         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
100
101         Mockito.verify(reactor, Mockito.times(1)).syncup(fcNodePath, configNode, fcOperationalNode);
102         Mockito.verify(roTx).close();
103     }
104
105     @Test
106     public void testOnDataTreeChangedSkip() throws Exception {
107         // Related to bug 5920 -> https://bugs.opendaylight.org/show_bug.cgi?id=5920
108         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
109                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
110
111         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
112
113         Mockito.verifyZeroInteractions(reactor);
114         Mockito.verify(roTx).close();
115     }
116 }