2 * Copyright (c) 2015 Inocybe Technologies. 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.ovsdb.openstack.netvirt.providers.openflow13;
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;
23 import java.lang.reflect.Field;
24 import java.util.List;
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;
59 * Unit test for {@link FlowCapableNodeDataChangeListener}
61 /* TODO SB_MIGRATION */
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest(ServiceHelper.class)
65 public class FlowCapableNodeDataChangeListenerTest {
68 private FlowCapableNodeDataChangeListener nodeListener;
71 private DataBroker dataBroker;
74 private ListenerRegistration<DataChangeListener> registration;
77 private NodeCacheManager nodeCacheManager;
80 private PipelineOrchestrator orchestrator;
85 DataBroker dataBroker = mock(DataBroker.class);
86 registration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
87 FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
89 , AsyncDataBroker.DataChangeScope.BASE);
91 when(dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
92 FlowCapableNodeDataChangeListener.createFlowCapableNodePath()
94 , AsyncDataBroker.DataChangeScope.BASE)).thenReturn(registration);
96 nodeListener = new FlowCapableNodeDataChangeListener(dataBroker);
97 orchestrator = Mockito.mock(PipelineOrchestrator.class);
98 nodeCacheManager = Mockito.mock(NodeCacheManager.class);
101 PowerMockito.mockStatic(ServiceHelper.class);
103 Mockito.when(ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, nodeListener)).thenReturn(orchestrator);
104 Mockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, nodeListener)).thenReturn(nodeCacheManager);
110 * Test method {@link FlowCapableNodeDataChangeListener#notifyFlowCapableNodeEventTest(String,Action)}
112 /* TODO SB_MIGRATION *//*
114 public void notifyFlowCapableNodeEventTest() throws Exception{
116 List<Node> nodeCache = (List<Node>) getClassField(nodeListener, "nodeCache");
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");
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");
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");
139 * Get the specified field from FlowCapableNodeDataChangeListener using reflection
140 * @param instance - the class instance
141 * @param fieldName - the field to retrieve
143 * @return the desired field
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);