c5f70217784b701171f853f0919c4c968f84dbaf
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / node / FlowCapableNodeListenerTest.java
1 /*
2  * Copyright (c) 2015 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.groupbasedpolicy.renderer.ofoverlay.node;
10
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;
15
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
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;
36
37 public class FlowCapableNodeListenerTest {
38
39     private FlowCapableNodeListener listener;
40
41     private DataBroker dataProvider;
42     private SwitchManager switchManager;
43     private ListenerRegistration<DataChangeListener> listenerRegistration;
44
45     @SuppressWarnings("unchecked")
46     @Before
47     public void initialisation() {
48         dataProvider = mock(DataBroker.class);
49         switchManager = mock(SwitchManager.class);
50         listenerRegistration = mock(ListenerRegistration.class);
51         when(
52                 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
53                         any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(listenerRegistration);
54
55         listener = new FlowCapableNodeListener(dataProvider, switchManager);
56     }
57
58     @Test
59     public void constructorTest() throws Exception {
60         listener.close();
61         verify(listenerRegistration).close();
62     }
63
64     @SuppressWarnings("unchecked")
65     @Test
66     public void onDataChangeTestCreatedData() {
67         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
68         FlowCapableNode entryValue = mock(FlowCapableNode.class);
69         NodeId childNodeId = mock(NodeId.class);
70
71         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
72             .child(Node.class, new NodeKey(childNodeId))
73             .augmentation(FlowCapableNode.class)
74             .build();
75
76         Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
77         entrySet.put(entryKey, entryValue);
78         when(change.getCreatedData()).thenReturn(entrySet);
79
80         listener.onDataChanged(change);
81         verify(switchManager).updateSwitch(childNodeId, entryValue);
82     }
83
84     @SuppressWarnings("unchecked")
85     @Test
86     public void onDataChangeTestUpdatedData() {
87         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
88         FlowCapableNode entryValue = mock(FlowCapableNode.class);
89         NodeId childNodeId = mock(NodeId.class);
90
91         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
92             .child(Node.class, new NodeKey(childNodeId))
93             .augmentation(FlowCapableNode.class)
94             .build();
95
96         Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
97         entrySet.put(entryKey, entryValue);
98         when(change.getUpdatedData()).thenReturn(entrySet);
99
100         listener.onDataChanged(change);
101         verify(switchManager).updateSwitch(childNodeId, entryValue);
102     }
103
104     @SuppressWarnings("unchecked")
105     @Test
106     public void onDataChangeTestRemovedPaths() {
107         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
108         NodeId childNodeId = mock(NodeId.class);
109
110         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
111             .child(Node.class, new NodeKey(childNodeId))
112             .augmentation(FlowCapableNode.class)
113             .build();
114
115         Set<InstanceIdentifier<?>> removedPaths = new HashSet<InstanceIdentifier<?>>();
116         removedPaths.add(entryKey);
117         when(change.getRemovedPaths()).thenReturn(removedPaths);
118
119         listener.onDataChanged(change);
120         verify(switchManager).updateSwitch(childNodeId, null);
121     }
122
123 }