Add working integration-test
[ovsdb.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandlerTest.java
1 /*
2  * Copyright (c) 2015 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.Matchers.same;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.net.HttpURLConnection;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.opendaylight.neutron.spi.NeutronPort;
38 import org.opendaylight.ovsdb.lib.notation.Column;
39 import org.opendaylight.ovsdb.lib.notation.Row;
40 import org.opendaylight.ovsdb.lib.notation.UUID;
41 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
42 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
44 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
45 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
46 import org.opendaylight.ovsdb.schema.openvswitch.Port;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
48
49 /**
50  * Unit test fort {@link PortHandler}
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class PortHandlerTest {
54
55     @InjectMocks PortHandler portHandler;
56
57     @Mock private NeutronL3Adapter neutronL3Adapter;
58
59     @Test
60     public void testCanCreatePort() {
61         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canCreatePort(mock(NeutronPort.class)));
62     }
63
64     @Test
65     public void testCanUpdatePort() {
66         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canUpdatePort(mock(NeutronPort.class), mock(NeutronPort.class)));
67     }
68
69     @Test
70     public void testCanDeletePort() {
71         assertEquals("Error, did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, portHandler.canDeletePort(mock(NeutronPort.class)));
72     }
73
74     @Ignore
75     @Test
76     public void testProcessEvent() {
77         PortHandler portHandlerSpy = Mockito.spy(portHandler);
78
79         NeutronPort neutronPort = mock(NeutronPort.class);
80         when(neutronPort.getTenantID()).thenReturn("tenantID");
81         when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
82         when(neutronPort.getID()).thenReturn("ID");
83         when(neutronPort.getPortUUID()).thenReturn("portUUID");
84
85         NorthboundEvent ev = mock(NorthboundEvent.class);
86         when(ev.getPort()).thenReturn(neutronPort);
87
88         when(ev.getAction()).thenReturn(Action.ADD);
89         portHandlerSpy.processEvent(ev);
90         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.ADD);
91
92         when(ev.getAction()).thenReturn(Action.UPDATE);
93         portHandlerSpy.processEvent(ev);
94         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.UPDATE);
95
96
97         Node node = mock(Node.class);
98         List<Node> nodes = new ArrayList();
99         nodes.add(node);
100         /* TODO SB_MIGRATION */
101         //when(connectionService.getNodes()).thenReturn(nodes);
102
103         Row row = mock(Row.class);
104         ConcurrentMap<String, Row> portRows = new ConcurrentHashMap();
105         portRows.put("key", row);
106         //when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(portRows );
107
108         Port port = mock(Port.class);
109         Column<GenericTableSchema, Set<UUID>> itfaceColumns = mock(Column.class);
110         when(port.getInterfacesColumn()).thenReturn(itfaceColumns);
111         Set<UUID> ifaceUUIDs = new HashSet();
112         ifaceUUIDs.add(mock(UUID.class));
113         when(itfaceColumns.getData()).thenReturn(ifaceUUIDs );
114         //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Port.class), any(Row.class))).thenReturn(port);
115
116         Interface itface = mock(Interface.class);
117         Column<GenericTableSchema, Map<String, String>> externalIdColumns = mock(Column.class);
118         Map<String, String> externalIds = new HashMap();
119         externalIds.put(Constants.EXTERNAL_ID_INTERFACE_ID, "portUUID");
120         when(externalIdColumns.getData()).thenReturn(externalIds);
121         when(itface.getExternalIdsColumn()).thenReturn(externalIdColumns);
122         //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Interface.class), any(Row.class))).thenReturn(itface);
123
124
125         when(ev.getAction()).thenReturn(Action.DELETE);
126         //portHandlerSpy.processEvent(ev);
127         verify(neutronL3Adapter, times(1)).handleNeutronPortEvent(neutronPort, Action.DELETE);
128     }
129 }