Convert DataChangeListeners to DataTreeChangeListeners
[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.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
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;
34
35 public class FlowCapableNodeListenerTest {
36
37     private FlowCapableNodeListener listener;
38
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;
45
46     @SuppressWarnings("unchecked")
47     @Before
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);
54
55         listener = new FlowCapableNodeListener(dataProvider, switchManager);
56
57         mockDataTreeModification = mock(DataTreeModification.class);
58         mockModification = mock(DataObjectModification.class);
59         doReturn(mockModification).when(mockDataTreeModification).getRootNode();
60         changeEvent = Collections.singletonList(mockDataTreeModification);
61     }
62
63     @Test
64     public void constructorTest() throws Exception {
65         listener.close();
66         verify(listenerRegistration).close();
67     }
68
69     @SuppressWarnings("unchecked")
70     @Test
71     public void onDataChangeTestCreatedData() {
72         FlowCapableNode entryValue = mock(FlowCapableNode.class);
73         NodeId childNodeId = mock(NodeId.class);
74
75         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
76             .child(Node.class, new NodeKey(childNodeId))
77             .augmentation(FlowCapableNode.class)
78             .build();
79
80         doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
81                 .getRootPath();
82         doReturn(DataObjectModification.ModificationType.WRITE).when(mockModification).getModificationType();
83         doReturn(entryValue).when(mockModification).getDataAfter();
84
85         listener.onDataTreeChanged(changeEvent);
86         verify(switchManager).updateSwitch(childNodeId, entryValue);
87     }
88
89     @SuppressWarnings("unchecked")
90     @Test
91     public void onDataChangeTestUpdatedData() {
92         FlowCapableNode entryValue = mock(FlowCapableNode.class);
93         NodeId childNodeId = mock(NodeId.class);
94
95         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
96             .child(Node.class, new NodeKey(childNodeId))
97             .augmentation(FlowCapableNode.class)
98             .build();
99
100         doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
101                 .getRootPath();
102         doReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED).when(mockModification).getModificationType();
103         doReturn(entryValue).when(mockModification).getDataAfter();
104
105         listener.onDataTreeChanged(changeEvent);
106         verify(switchManager).updateSwitch(childNodeId, entryValue);
107     }
108
109     @SuppressWarnings("unchecked")
110     @Test
111     public void onDataChangeTestRemovedPaths() {
112         NodeId childNodeId = mock(NodeId.class);
113
114         InstanceIdentifier<FlowCapableNode> entryKey = InstanceIdentifier.builder(Nodes.class)
115             .child(Node.class, new NodeKey(childNodeId))
116             .augmentation(FlowCapableNode.class)
117             .build();
118
119         doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
120                 .getRootPath();
121         doReturn(DataObjectModification.ModificationType.DELETE).when(mockModification).getModificationType();
122
123         listener.onDataTreeChanged(changeEvent);
124         verify(switchManager).updateSwitch(childNodeId, null);
125     }
126
127 }