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.mock;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import java.util.HashMap;
17 import java.util.HashSet;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 public class FlowCapableNodeListenerTest {
39 private FlowCapableNodeListener listener;
41 private DataBroker dataProvider;
42 private SwitchManager switchManager;
43 private ListenerRegistration<DataChangeListener> listenerRegistration;
45 @SuppressWarnings("unchecked")
47 public void initialisation() {
48 dataProvider = mock(DataBroker.class);
49 switchManager = mock(SwitchManager.class);
50 listenerRegistration = mock(ListenerRegistration.class);
52 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
53 any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(listenerRegistration);
55 listener = new FlowCapableNodeListener(dataProvider, switchManager);
59 public void constructorTest() throws Exception {
61 verify(listenerRegistration).close();
64 @SuppressWarnings("unchecked")
66 public void onDataChangeTestCreatedData() {
67 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
68 FlowCapableNode entryValue = mock(FlowCapableNode.class);
69 NodeId childNodeId = mock(NodeId.class);
71 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
72 .child(Node.class, new NodeKey(childNodeId))
73 .augmentation(FlowCapableNode.class)
76 Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
77 entrySet.put(entryKey, entryValue);
78 when(change.getCreatedData()).thenReturn(entrySet);
80 listener.onDataChanged(change);
81 verify(switchManager).updateSwitch(childNodeId, entryValue);
84 @SuppressWarnings("unchecked")
86 public void onDataChangeTestUpdatedData() {
87 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
88 FlowCapableNode entryValue = mock(FlowCapableNode.class);
89 NodeId childNodeId = mock(NodeId.class);
91 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
92 .child(Node.class, new NodeKey(childNodeId))
93 .augmentation(FlowCapableNode.class)
96 Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
97 entrySet.put(entryKey, entryValue);
98 when(change.getUpdatedData()).thenReturn(entrySet);
100 listener.onDataChanged(change);
101 verify(switchManager).updateSwitch(childNodeId, entryValue);
104 @SuppressWarnings("unchecked")
106 public void onDataChangeTestRemovedPaths() {
107 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
108 NodeId childNodeId = mock(NodeId.class);
110 InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
111 .child(Node.class, new NodeKey(childNodeId))
112 .augmentation(FlowCapableNode.class)
115 Set<InstanceIdentifier<?>> removedPaths = new HashSet<InstanceIdentifier<?>>();
116 removedPaths.add(entryKey);
117 when(change.getRemovedPaths()).thenReturn(removedPaths);
119 listener.onDataChanged(change);
120 verify(switchManager).updateSwitch(childNodeId, null);