2 * Copyright (c) 2015 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.groupbasedpolicy.renderer.ofoverlay.node;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.util.Collection;
18 import java.util.Collections;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
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.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 public class FlowCapableNodeListenerTest {
37 private FlowCapableNodeListener listener;
39 private DataBroker dataProvider;
40 private SwitchManager switchManager;
41 private ListenerRegistration<?> listenerRegistration;
42 private DataTreeModification<FlowCapableNode> mockDataTreeModification;
43 private DataObjectModification<FlowCapableNode> mockModification;
44 private Collection<DataTreeModification<FlowCapableNode>> changeEvent;
46 @SuppressWarnings("unchecked")
48 public void initialisation() {
49 dataProvider = mock(DataBroker.class);
50 switchManager = mock(SwitchManager.class);
51 listenerRegistration = mock(ListenerRegistration.class);
52 when(dataProvider.registerDataTreeChangeListener(any(DataTreeIdentifier.class),
53 any(DataTreeChangeListener.class))).thenReturn(listenerRegistration);
55 listener = new FlowCapableNodeListener(dataProvider, switchManager);
57 mockDataTreeModification = mock(DataTreeModification.class);
58 mockModification = mock(DataObjectModification.class);
59 doReturn(mockModification).when(mockDataTreeModification).getRootNode();
60 changeEvent = Collections.singletonList(mockDataTreeModification);
64 public void constructorTest() throws Exception {
66 verify(listenerRegistration).close();
69 @SuppressWarnings("unchecked")
71 public void onDataChangeTestCreatedData() {
72 FlowCapableNode entryValue = mock(FlowCapableNode.class);
73 NodeId childNodeId = mock(NodeId.class);
75 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
76 .child(Node.class, new NodeKey(childNodeId))
77 .augmentation(FlowCapableNode.class)
80 doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
82 doReturn(DataObjectModification.ModificationType.WRITE).when(mockModification).getModificationType();
83 doReturn(entryValue).when(mockModification).getDataAfter();
85 listener.onDataTreeChanged(changeEvent);
86 verify(switchManager).updateSwitch(childNodeId, entryValue);
89 @SuppressWarnings("unchecked")
91 public void onDataChangeTestUpdatedData() {
92 FlowCapableNode entryValue = mock(FlowCapableNode.class);
93 NodeId childNodeId = mock(NodeId.class);
95 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
96 .child(Node.class, new NodeKey(childNodeId))
97 .augmentation(FlowCapableNode.class)
100 doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
102 doReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED).when(mockModification).getModificationType();
103 doReturn(entryValue).when(mockModification).getDataAfter();
105 listener.onDataTreeChanged(changeEvent);
106 verify(switchManager).updateSwitch(childNodeId, entryValue);
109 @SuppressWarnings("unchecked")
111 public void onDataChangeTestRemovedPaths() {
112 NodeId childNodeId = mock(NodeId.class);
114 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
115 .child(Node.class, new NodeKey(childNodeId))
116 .augmentation(FlowCapableNode.class)
119 doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
121 doReturn(DataObjectModification.ModificationType.DELETE).when(mockModification).getModificationType();
123 listener.onDataTreeChanged(changeEvent);
124 verify(switchManager).updateSwitch(childNodeId, null);