2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
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;
41 * Test for {@link SimplifiedConfigListener}.
43 @RunWith(MockitoJUnitRunner.class)
44 public class SimplifiedConfigListenerTest {
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;
52 private SyncReactor reactor;
54 private ReadOnlyTransaction roTx;
56 private DataTreeModification<FlowCapableNode> dataTreeModification;
58 private DataObjectModification<FlowCapableNode> configModification;
60 private FlowCapableNode dataBefore;
62 private FlowCapableNode dataAfter;
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));
72 nodeListenerConfig = new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
73 fcNodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID))
74 .augmentation(FlowCapableNode.class);
76 final DataTreeIdentifier<FlowCapableNode> dataTreeIdentifier =
77 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fcNodePath);
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));
87 public void testDSLogicalType() throws Exception {
88 Assert.assertEquals(LogicalDatastoreType.CONFIGURATION, nodeListenerConfig.dsType());
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);
97 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
99 Mockito.verify(reactor).syncup(fcNodePath, dataAfter, dataBefore, dsType);
100 Mockito.verifyNoMoreInteractions(reactor);
101 Mockito.verify(roTx).close();
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);
111 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
113 Mockito.verify(reactor).syncup(fcNodePath, dataAfter, dataBefore, dsType);
114 Mockito.verifyNoMoreInteractions(reactor);
115 Mockito.verify(roTx).close();
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);
124 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
126 Mockito.verify(reactor).syncup(fcNodePath, null, dataBefore, dsType);
127 Mockito.verifyNoMoreInteractions(reactor);
128 Mockito.verify(roTx).close();
132 public void testOnDataTreeChangedSkip() {
133 Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath)).
134 thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
136 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
138 Mockito.verifyZeroInteractions(reactor);
139 Mockito.verify(roTx).close();