Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
10 import com.google.common.util.concurrent.Futures;
11 import java.util.Collections;
12 import java.util.Optional;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentMatchers;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification;
23 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.binding.api.ReadTransaction;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
28 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
30 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
31 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
32 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
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.util.concurrent.FluentFutures;
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 ReadTransaction 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                 DataTreeIdentifier.create(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.doReturn(FluentFutures.immediateFluentFuture(Optional.empty())).when(roTx)
134             .read(LogicalDatastoreType.OPERATIONAL, fcNodePath);
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.doReturn(FluentFutures.immediateFluentFuture(Optional.of(dataBefore))).when(roTx)
146             .read(LogicalDatastoreType.OPERATIONAL, fcNodePath);
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 }