Add SouthboundHandler#processBridgeUpdate
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / FlowCapableNodeDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies.  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.ovsdb.openstack.netvirt.providers.openflow13;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.same;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import java.lang.reflect.Field;
24 import java.util.List;
25
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
39 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
40 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
41 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.neutron.spi.INeutronPortCRUD;
51 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
52 import org.opendaylight.ovsdb.openstack.netvirt.NeutronCacheUtils;
53 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
55 import org.opendaylight.ovsdb.openstack.netvirt.impl.EventDispatcherImpl;
56 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
57
58 /**
59  * Unit test for {@link FlowCapableNodeDataChangeListener}
60  */
61 /* TODO SB_MIGRATION */
62 @Ignore
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest(ServiceHelper.class)
65 public class FlowCapableNodeDataChangeListenerTest {
66
67     @InjectMocks
68     private FlowCapableNodeDataChangeListener nodeListener;
69
70     @Mock
71     private DataBroker dataBroker;
72
73     @Mock
74     private ListenerRegistration<DataChangeListener> registration;
75
76     @Mock
77     private NodeCacheManager nodeCacheManager;
78
79     @Mock
80     private PipelineOrchestrator orchestrator;
81
82     @Before
83     public void setUp() {
84
85         DataBroker dataBroker = mock(DataBroker.class);
86         registration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
87                 FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
88                 , nodeListener
89                 , AsyncDataBroker.DataChangeScope.BASE);
90
91         when(dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
92                 FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
93                 , nodeListener
94                 , AsyncDataBroker.DataChangeScope.BASE)).thenReturn(registration);
95
96         nodeListener = new FlowCapableNodeDataChangeListener(dataBroker);
97         orchestrator = Mockito.mock(PipelineOrchestrator.class);
98         nodeCacheManager = Mockito.mock(NodeCacheManager.class);
99
100
101         PowerMockito.mockStatic(ServiceHelper.class);
102
103         Mockito.when(ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, nodeListener)).thenReturn(orchestrator);
104         Mockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, nodeListener)).thenReturn(nodeCacheManager);
105
106     }
107
108
109     /**
110      * Test method {@link FlowCapableNodeDataChangeListener#notifyFlowCapableNodeEventTest(String,Action)}
111      */
112     /* TODO SB_MIGRATION *//*
113     @Test
114     public void notifyFlowCapableNodeEventTest() throws Exception{
115
116         List<Node> nodeCache = (List<Node>) getClassField(nodeListener, "nodeCache");
117
118         nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.ADD);
119         nodeListener.notifyFlowCapableNodeEvent("flowid2", Action.ADD);
120         assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after an ADD operation is incorrect", 2, nodeCache.size());
121         verify(nodeCacheManager,times(1)).nodeAdded("flowid1");
122         verify(nodeCacheManager,times(1)).nodeAdded("flowid2");
123         verify(orchestrator, times(1)).enqueue("flowid1");
124
125         nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.UPDATE);
126         assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after an UPDATE operation is incorrect", 2, nodeCache.size());
127         verify(nodeCacheManager, times(1)).nodeAdded("flowid1");
128         verify(orchestrator, times(1)).enqueue("flowid1");
129
130         nodeListener.notifyFlowCapableNodeEvent("flowid1", Action.DELETE);
131         assertEquals("Error, notifyFlowCapableNodeEvent() - Controller's node inventory size after a DELETE operation is incorrect", 2, nodeCache.size());
132         verify(nodeCacheManager, times(1)).nodeAdded("flowid1");
133         verify(nodeCacheManager, times(1)).nodeRemoved("flowid1");
134         verify(orchestrator, times(1)).enqueue("flowid1");
135
136     }*/
137
138     /**
139      * Get the specified field from FlowCapableNodeDataChangeListener using reflection
140      * @param instance - the class instance
141      * @param fieldName - the field to retrieve
142      *
143      * @return the desired field
144      */
145     private Object getClassField(FlowCapableNodeDataChangeListener instance, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
146         Field field = FlowCapableNodeDataChangeListener.class.getDeclaredField(fieldName);
147         field.setAccessible(true);
148         return field.get(instance);
149     }
150
151 }