Cleaning and preparation before bug 6170
[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.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 SimplifiedConfigListener}.
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class SimplifiedConfigListenerTest {
45
46     private static final NodeId NODE_ID = new NodeId("testNode");
47     private InstanceIdentifier<FlowCapableNode> fcNodePath;
48     private SimplifiedConfigListener nodeListenerConfig;
49     private final LogicalDatastoreType dsType = LogicalDatastoreType.CONFIGURATION;
50
51     @Mock
52     private SyncReactor reactor;
53     @Mock
54     private ReadOnlyTransaction roTx;
55     @Mock
56     private DataTreeModification<FlowCapableNode> dataTreeModification;
57     @Mock
58     private DataObjectModification<FlowCapableNode> configModification;
59     @Mock
60     private FlowCapableNode dataBefore;
61     @Mock
62     private FlowCapableNode dataAfter;
63
64     @Before
65     public void setUp() throws Exception {
66         final DataBroker db = Mockito.mock(DataBroker.class);
67         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
68         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
69         final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
70                 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.OPERATIONAL));
71
72         nodeListenerConfig = new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
73         fcNodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID))
74                 .augmentation(FlowCapableNode.class);
75
76         final DataTreeIdentifier<FlowCapableNode> dataTreeIdentifier =
77                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fcNodePath);
78
79         Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
80         Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
81         Mockito.when(dataTreeModification.getRootNode()).thenReturn(configModification);
82         Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Matchers.<FlowCapableNode>any(),
83                 Matchers.<FlowCapableNode>any(), Matchers.<LogicalDatastoreType>any())).thenReturn(Futures.immediateFuture(Boolean.TRUE));
84     }
85
86     @Test
87     public void testDSLogicalType() throws Exception {
88         Assert.assertEquals(LogicalDatastoreType.CONFIGURATION, nodeListenerConfig.dsType());
89     }
90
91     @Test
92     public void testOnDataTreeChangedSyncupAdd() throws InterruptedException {
93         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
94                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
95         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
96
97         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
98
99         Mockito.verify(reactor).syncup(fcNodePath, dataAfter, dataBefore, dsType);
100         Mockito.verifyNoMoreInteractions(reactor);
101         Mockito.verify(roTx).close();
102     }
103
104     @Test
105     public void testOnDataTreeChangedSyncupUpdate() throws InterruptedException {
106         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
107                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
108         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
109         Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
110
111         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
112
113         Mockito.verify(reactor).syncup(fcNodePath, dataAfter, dataBefore, dsType);
114         Mockito.verifyNoMoreInteractions(reactor);
115         Mockito.verify(roTx).close();
116     }
117
118     @Test
119     public void testOnDataTreeChangedSyncupDelete() throws InterruptedException {
120         Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
121                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
122         Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
123
124         nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
125
126         Mockito.verify(reactor).syncup(fcNodePath, null, dataBefore, dsType);
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 }