Add UT for SouthboundMapper and SouthboundProvider
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / RouterHandlerTest.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.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.lang.reflect.Field;
18 import java.net.HttpURLConnection;
19
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.InjectMocks;
23 import org.mockito.Mock;
24 import org.opendaylight.neutron.spi.NeutronRouter;
25 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
27 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
28 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
29 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceReference;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 /**
37  * Unit test fort {@link RouterHandler}
38  */
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(ServiceHelper.class)
41 public class RouterHandlerTest {
42
43     @InjectMocks RouterHandler routerHandler;
44
45     @Mock NeutronL3Adapter neutronL3Adapter;
46
47     @Test
48     public void testCanCreateRouter() {
49         assertEquals("Error, canCreateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canCreateRouter(mock(NeutronRouter.class)));
50     }
51
52     @Test
53     public void testCanUpdateRouter() {
54         assertEquals("Error, canUpdateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canUpdateRouter(mock(NeutronRouter.class), mock(NeutronRouter.class)));
55     }
56
57     @Test
58     public void testCanDeleteRouter() {
59         assertEquals("Error, canDeleteRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDeleteRouter(mock(NeutronRouter.class)));
60     }
61
62     @Test
63     public void testCanAttachAndDettachInterface() {
64         NeutronRouter router = mock(NeutronRouter.class);
65         when(router.getName()).thenReturn("router_name");
66
67         NeutronRouter_Interface routerInterface = mock(NeutronRouter_Interface.class);
68         when(routerInterface.getPortUUID()).thenReturn("portUUID");
69         when(routerInterface.getSubnetUUID()).thenReturn("subnetUUID");
70
71         assertEquals("Error, canAttachInterface() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canAttachInterface(router, routerInterface));
72         assertEquals("Error, canDetachInterface() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDetachInterface(router, routerInterface));
73     }
74
75     @Test
76     public void testProcessEvent() {
77         NorthboundEvent ev = mock(NorthboundEvent.class);
78
79         when(ev.getAction()).thenReturn(Action.UPDATE);
80         when(ev.getRouter()).thenReturn(mock(NeutronRouter.class));
81         when(ev.getRouterInterface())
82                                 .thenReturn(null)
83                                 .thenReturn(mock(NeutronRouter_Interface.class));
84
85         routerHandler.processEvent(ev);
86         verify(neutronL3Adapter, times(1)).handleNeutronRouterEvent(ev.getRouter(), Action.UPDATE);
87         routerHandler.processEvent(ev);
88         verify(neutronL3Adapter, times(1)).handleNeutronRouterInterfaceEvent(ev.getRouter(), ev.getRouterInterface(), Action.UPDATE);
89     }
90
91     @Test
92     public void testSetDependencies() throws Exception {
93         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
94         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
95
96         PowerMockito.mockStatic(ServiceHelper.class);
97         PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, routerHandler)).thenReturn(neutronL3Adapter);
98         PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, routerHandler)).thenReturn(eventDispatcher);
99
100         routerHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
101
102         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
103         assertEquals("Error, did not return the correct object", routerHandler.eventDispatcher, eventDispatcher);
104     }
105
106     private Object getField(String fieldName) throws Exception {
107         Field field = RouterHandler.class.getDeclaredField(fieldName);
108         field.setAccessible(true);
109         return field.get(routerHandler);
110     }
111 }