2 * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.lang.reflect.Field;
20 import java.net.HttpURLConnection;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.opendaylight.neutron.spi.NeutronPort;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
33 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
34 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
35 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.osgi.framework.ServiceReference;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
45 * Unit test fort {@link PortHandler}
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest(ServiceHelper.class)
49 public class PortHandlerTest {
51 @InjectMocks private PortHandler portHandler;
53 @Mock private NeutronL3Adapter neutronL3Adapter;
54 @Mock private NodeCacheManager nodeCacheManager;
55 @Mock private Southbound southbound;
58 public void testCanCreatePort() {
59 assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canCreatePort(mock(NeutronPort.class)));
63 public void testCanUpdatePort() {
64 assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canUpdatePort(mock(NeutronPort.class), mock(NeutronPort.class)));
68 public void testCanDeletePort() {
69 assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canDeletePort(mock(NeutronPort.class)));
73 public void testProcessEvent() {
74 PortHandler portHandlerSpy = Mockito.spy(portHandler);
76 NeutronPort neutronPort = mock(NeutronPort.class);
77 when(neutronPort.getTenantID()).thenReturn("tenantID");
78 when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
79 when(neutronPort.getID()).thenReturn("ID");
80 when(neutronPort.getPortUUID()).thenReturn("portUUID");
82 NorthboundEvent ev = mock(NorthboundEvent.class);
83 when(ev.getPort()).thenReturn(neutronPort);
85 when(ev.getAction()).thenReturn(Action.ADD);
86 portHandlerSpy.processEvent(ev);
87 verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.ADD);
89 when(ev.getAction()).thenReturn(Action.UPDATE);
90 portHandlerSpy.processEvent(ev);
91 verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.UPDATE);
93 List<Node> nodes = new ArrayList<Node>();
94 nodes.add(mock(Node.class));
95 when(nodeCacheManager.getNodes()).thenReturn(nodes);
97 List<OvsdbTerminationPointAugmentation> ports = new ArrayList<OvsdbTerminationPointAugmentation>();
98 OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
100 when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports);
102 when(southbound.getInterfaceExternalIdsValue(any(OvsdbTerminationPointAugmentation.class), anyString())).thenReturn("portUUID");
104 when(ev.getAction()).thenReturn(Action.DELETE);
105 portHandlerSpy.processEvent(ev);
106 verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.DELETE);
107 verify(southbound, times(1)).deleteTerminationPoint(any(Node.class), anyString());
111 public void testSetDependencies() throws Exception {
112 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
113 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
114 Southbound southbound = mock(Southbound.class);
115 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
117 PowerMockito.mockStatic(ServiceHelper.class);
118 PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, portHandler)).thenReturn(nodeCacheManager);
119 PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, portHandler)).thenReturn(neutronL3Adapter);
120 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, portHandler)).thenReturn(southbound);
121 PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, portHandler)).thenReturn(eventDispatcher);
123 portHandler.setDependencies(mock(ServiceReference.class));
125 assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
126 assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
127 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
128 assertEquals("Error, did not return the correct object", portHandler.eventDispatcher, eventDispatcher);
131 private Object getField(String fieldName) throws Exception {
132 Field field = PortHandler.class.getDeclaredField(fieldName);
133 field.setAccessible(true);
134 return field.get(portHandler);