OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[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.ArgumentMatchers;
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() {
93         Mockito.when(configModification.getDataBefore()).thenReturn(null);
94         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
95         final SyncupEntry syncupEntry =
96                 loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, operationalDS);
97
98         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
99
100         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
101         Mockito.verifyNoMoreInteractions(reactor);
102         Mockito.verify(roTx).close();
103     }
104
105     @Test
106     public void testOnDataTreeChangedUpdate() {
107         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
108         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
109         final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, confgDS);
110
111         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
112
113         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
114         Mockito.verifyNoMoreInteractions(reactor);
115         Mockito.verify(roTx).close();
116     }
117
118     @Test
119     public void testOnDataTreeChangedDelete() {
120         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
121         Mockito.when(configModification.getDataAfter()).thenReturn(null);
122         final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(null, confgDS, dataBefore, confgDS);
123
124         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
125
126         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
127         Mockito.verifyNoMoreInteractions(reactor);
128         Mockito.verify(roTx).close();
129     }
130
131     @Test
132     public void testOnDataTreeChangedSkip() {
133         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
134                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
135
136         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
137
138         Mockito.verifyZeroInteractions(reactor);
139         Mockito.verify(roTx).close();
140     }
141
142     private SyncupEntry loadOperationalDSAndPrepareSyncupEntry(final FlowCapableNode after,
143             final LogicalDatastoreType dsTypeAfter, final FlowCapableNode before,
144             final LogicalDatastoreType dsTypeBefore) {
145         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
146                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
147         final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
148         Mockito.when(reactor.syncup(ArgumentMatchers.<InstanceIdentifier<FlowCapableNode>>any(),
149                 Mockito.eq(syncupEntry))).thenReturn(Futures.immediateFuture(Boolean.TRUE));
150         return syncupEntry;
151     }
152
153 }