Update .gitreview for new repo
[netvirt.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.DistributedArpService;
35 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
36 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
37 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105
39         .OvsdbTerminationPointAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology
41         .Node;
42 import org.osgi.framework.ServiceReference;
43
44 /**
45  * Unit test fort {@link PortHandler}
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class PortHandlerTest {
49
50     @InjectMocks private PortHandler portHandler;
51
52     @Mock private NeutronL3Adapter neutronL3Adapter;
53     @Mock private DistributedArpService distributedArpService;
54     @Mock private NodeCacheManager nodeCacheManager;
55     @Mock private Southbound southbound;
56
57     @Test
58     public void testCanCreatePort() {
59         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canCreatePort(mock(NeutronPort.class)));
60     }
61
62     @Test
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)));
65     }
66
67     @Test
68     public void testCanDeletePort() {
69         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canDeletePort(mock(NeutronPort.class)));
70     }
71
72     @Test
73     public void testProcessEvent() {
74         PortHandler portHandlerSpy = Mockito.spy(portHandler);
75
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");
81
82         NorthboundEvent ev = mock(NorthboundEvent.class);
83         when(ev.getPort()).thenReturn(neutronPort);
84
85         when(ev.getAction()).thenReturn(Action.ADD);
86         portHandlerSpy.processEvent(ev);
87         verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.ADD);
88         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.ADD);
89
90         when(ev.getAction()).thenReturn(Action.UPDATE);
91         portHandlerSpy.processEvent(ev);
92         verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.UPDATE);
93         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.UPDATE);
94
95         List<Node> nodes = new ArrayList<>();
96         nodes.add(mock(Node.class));
97         when(nodeCacheManager.getNodes()).thenReturn(nodes);
98
99         List<OvsdbTerminationPointAugmentation> ports = new ArrayList<>();
100         OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
101         ports.add(port);
102         when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports);
103
104         when(southbound.getInterfaceExternalIdsValue(any(OvsdbTerminationPointAugmentation.class), anyString())).thenReturn("portUUID");
105
106         when(ev.getAction()).thenReturn(Action.DELETE);
107         portHandlerSpy.processEvent(ev);
108         verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.DELETE);
109         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.DELETE);
110         verify(southbound, times(1)).deleteTerminationPoint(any(Node.class), anyString());
111     }
112
113     @Test
114     public void testSetDependencies() throws Exception {
115         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
116         DistributedArpService distributedArpService = mock(DistributedArpService.class);
117         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
118         Southbound southbound = mock(Southbound.class);
119         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
120
121         ServiceHelper.overrideGlobalInstance(NodeCacheManager.class, nodeCacheManager);
122         ServiceHelper.overrideGlobalInstance(DistributedArpService.class, distributedArpService);
123         ServiceHelper.overrideGlobalInstance(NeutronL3Adapter.class, neutronL3Adapter);
124         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
125         ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
126
127         portHandler.setDependencies(mock(ServiceReference.class));
128
129         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
130         assertEquals("Error, did not return the correct object", getField("distributedArpService"), distributedArpService);
131         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
132         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
133         assertEquals("Error, did not return the correct object", portHandler.eventDispatcher, eventDispatcher);
134     }
135
136     private Object getField(String fieldName) throws Exception {
137         Field field = PortHandler.class.getDeclaredField(fieldName);
138         field.setAccessible(true);
139         return field.get(portHandler);
140     }
141 }