Merge "BUG-5006: Reduce usage of PowerMock"
[ovsdb.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandlerTest.java
1 /*
2  * Copyright (c) 2015, 2016 Inocybe 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.ovsdb.openstack.netvirt;
10
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;
18
19 import java.lang.reflect.Field;
20 import java.net.HttpURLConnection;
21 import java.util.ArrayList;
22 import java.util.List;
23
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.mockito.runners.MockitoJUnitRunner;
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.openstack.netvirt.translator.NeutronPort;
36 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105
38         .OvsdbTerminationPointAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology
40         .Node;
41 import org.osgi.framework.ServiceReference;
42
43 /**
44  * Unit test fort {@link PortHandler}
45  */
46 @RunWith(MockitoJUnitRunner.class)
47 public class PortHandlerTest {
48
49     @InjectMocks private PortHandler portHandler;
50
51     @Mock private NeutronL3Adapter neutronL3Adapter;
52     @Mock private NodeCacheManager nodeCacheManager;
53     @Mock private Southbound southbound;
54
55     @Test
56     public void testCanCreatePort() {
57         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canCreatePort(mock(NeutronPort.class)));
58     }
59
60     @Test
61     public void testCanUpdatePort() {
62         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canUpdatePort(mock(NeutronPort.class), mock(NeutronPort.class)));
63     }
64
65     @Test
66     public void testCanDeletePort() {
67         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canDeletePort(mock(NeutronPort.class)));
68     }
69
70     @Test
71     public void testProcessEvent() {
72         PortHandler portHandlerSpy = Mockito.spy(portHandler);
73
74         NeutronPort neutronPort = mock(NeutronPort.class);
75         when(neutronPort.getTenantID()).thenReturn("tenantID");
76         when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
77         when(neutronPort.getID()).thenReturn("ID");
78         when(neutronPort.getPortUUID()).thenReturn("portUUID");
79
80         NorthboundEvent ev = mock(NorthboundEvent.class);
81         when(ev.getPort()).thenReturn(neutronPort);
82
83         when(ev.getAction()).thenReturn(Action.ADD);
84         portHandlerSpy.processEvent(ev);
85         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.ADD);
86
87         when(ev.getAction()).thenReturn(Action.UPDATE);
88         portHandlerSpy.processEvent(ev);
89         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.UPDATE);
90
91         List<Node> nodes = new ArrayList<>();
92         nodes.add(mock(Node.class));
93         when(nodeCacheManager.getNodes()).thenReturn(nodes);
94
95         List<OvsdbTerminationPointAugmentation> ports = new ArrayList<>();
96         OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
97         ports.add(port);
98         when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports);
99
100         when(southbound.getInterfaceExternalIdsValue(any(OvsdbTerminationPointAugmentation.class), anyString())).thenReturn("portUUID");
101
102         when(ev.getAction()).thenReturn(Action.DELETE);
103         portHandlerSpy.processEvent(ev);
104         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.DELETE);
105         verify(southbound, times(1)).deleteTerminationPoint(any(Node.class), anyString());
106     }
107
108     @Test
109     public void testSetDependencies() throws Exception {
110         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
111         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
112         Southbound southbound = mock(Southbound.class);
113         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
114
115         ServiceHelper.overrideGlobalInstance(NodeCacheManager.class, nodeCacheManager);
116         ServiceHelper.overrideGlobalInstance(NeutronL3Adapter.class, neutronL3Adapter);
117         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
118         ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
119
120         portHandler.setDependencies(mock(ServiceReference.class));
121
122         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
123         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
124         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
125         assertEquals("Error, did not return the correct object", portHandler.eventDispatcher, eventDispatcher);
126     }
127
128     private Object getField(String fieldName) throws Exception {
129         Field field = PortHandler.class.getDeclaredField(fieldName);
130         field.setAccessible(true);
131         return field.get(portHandler);
132     }
133 }