888f31776109fed27b87a1b78235cf6107b371f9
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedConfigListenerTest.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.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.openflowplugin.applications.frsync.util.SyncupEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 /**
42  * Test for {@link SimplifiedConfigListener}.
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class SimplifiedConfigListenerTest {
46
47     private static final NodeId NODE_ID = new NodeId("testNode");
48     private InstanceIdentifier<FlowCapableNode> fcNodePath;
49     private SimplifiedConfigListener nodeListenerConfig;
50     private final LogicalDatastoreType confgDS = LogicalDatastoreType.CONFIGURATION;
51     private final LogicalDatastoreType operationalDS = LogicalDatastoreType.OPERATIONAL;
52
53     @Mock
54     private SyncReactor reactor;
55     @Mock
56     private ReadOnlyTransaction roTx;
57     @Mock
58     private DataTreeModification<FlowCapableNode> dataTreeModification;
59     @Mock
60     private DataObjectModification<FlowCapableNode> configModification;
61     @Mock
62     private FlowCapableNode dataBefore;
63     @Mock
64     private FlowCapableNode dataAfter;
65
66     @Before
67     public void setUp() throws Exception {
68         final DataBroker db = Mockito.mock(DataBroker.class);
69         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
70         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
71         final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
72                 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.OPERATIONAL));
73
74         nodeListenerConfig = new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
75         fcNodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID))
76                 .augmentation(FlowCapableNode.class);
77
78         final DataTreeIdentifier<FlowCapableNode> dataTreeIdentifier =
79                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fcNodePath);
80
81         Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
82         Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
83         Mockito.when(dataTreeModification.getRootNode()).thenReturn(configModification);
84     }
85
86     @Test
87     public void testDSLogicalType() throws Exception {
88         Assert.assertEquals(LogicalDatastoreType.CONFIGURATION, nodeListenerConfig.dsType());
89     }
90
91     @Test
92     public void testOnDataTreeChangedAdd() throws InterruptedException {
93         Mockito.when(configModification.getDataBefore()).thenReturn(null);
94         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
95         final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, operationalDS);
96
97         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
98
99         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
100         Mockito.verifyNoMoreInteractions(reactor);
101         Mockito.verify(roTx).close();
102     }
103
104     @Test
105     public void testOnDataTreeChangedUpdate() throws InterruptedException {
106         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
107         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
108         final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, confgDS);
109
110         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
111
112         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
113         Mockito.verifyNoMoreInteractions(reactor);
114         Mockito.verify(roTx).close();
115     }
116
117     @Test
118     public void testOnDataTreeChangedDelete() throws InterruptedException {
119         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
120         Mockito.when(configModification.getDataAfter()).thenReturn(null);
121         final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(null, confgDS, dataBefore, confgDS);
122
123         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
124
125         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
126         Mockito.verifyNoMoreInteractions(reactor);
127         Mockito.verify(roTx).close();
128     }
129
130     @Test
131     public void testOnDataTreeChangedSkip() {
132         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath)).
133                 thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
134
135         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
136
137         Mockito.verifyZeroInteractions(reactor);
138         Mockito.verify(roTx).close();
139     }
140
141     private SyncupEntry loadOperationalDSAndPrepareSyncupEntry(final FlowCapableNode after, final LogicalDatastoreType dsTypeAfter,
142                                                                final FlowCapableNode before, final LogicalDatastoreType dsTypeBefore) throws InterruptedException {
143         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
144                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
145         final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
146         Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Mockito.eq(syncupEntry)))
147                 .thenReturn(Futures.immediateFuture(Boolean.TRUE));
148         return syncupEntry;
149     }
150
151 }