Merge "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.ServiceReference;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34
35 /**
36  * Unit test fort {@link RouterHandler}
37  */
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class RouterHandlerTest {
41
42     @InjectMocks RouterHandler routerHandler;
43
44     @Mock NeutronL3Adapter neutronL3Adapter;
45
46     @Test
47     public void testCanCreateRouter() {
48         assertEquals("Error, canCreateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canCreateRouter(mock(NeutronRouter.class)));
49     }
50
51     @Test
52     public void testCanUpdateRouter() {
53         assertEquals("Error, canUpdateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canUpdateRouter(mock(NeutronRouter.class), mock(NeutronRouter.class)));
54     }
55
56     @Test
57     public void testCanDeleteRouter() {
58         assertEquals("Error, canDeleteRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDeleteRouter(mock(NeutronRouter.class)));
59     }
60
61     @Test
62     public void testCanAttachAndDettachInterface() {
63         NeutronRouter router = mock(NeutronRouter.class);
64         when(router.getName()).thenReturn("router_name");
65
66         NeutronRouter_Interface routerInterface = mock(NeutronRouter_Interface.class);
67         when(routerInterface.getPortUUID()).thenReturn("portUUID");
68         when(routerInterface.getSubnetUUID()).thenReturn("subnetUUID");
69
70         assertEquals("Error, canAttachInterface() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canAttachInterface(router, routerInterface));
71         assertEquals("Error, canDetachInterface() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDetachInterface(router, routerInterface));
72     }
73
74     @Test
75     public void testProcessEvent() {
76         NorthboundEvent ev = mock(NorthboundEvent.class);
77
78         when(ev.getAction()).thenReturn(Action.UPDATE);
79         when(ev.getRouter()).thenReturn(mock(NeutronRouter.class));
80         when(ev.getRouterInterface())
81                                 .thenReturn(null)
82                                 .thenReturn(mock(NeutronRouter_Interface.class));
83
84         routerHandler.processEvent(ev);
85         verify(neutronL3Adapter, times(1)).handleNeutronRouterEvent(ev.getRouter(), Action.UPDATE);
86         routerHandler.processEvent(ev);
87         verify(neutronL3Adapter, times(1)).handleNeutronRouterInterfaceEvent(ev.getRouter(), ev.getRouterInterface(), Action.UPDATE);
88     }
89
90     @Test
91     public void testSetDependencies() throws Exception {
92         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
93         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
94
95         PowerMockito.mockStatic(ServiceHelper.class);
96         PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, routerHandler)).thenReturn(neutronL3Adapter);
97         PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, routerHandler)).thenReturn(eventDispatcher);
98
99         routerHandler.setDependencies(mock(ServiceReference.class));
100
101         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
102         assertEquals("Error, did not return the correct object", routerHandler.eventDispatcher, eventDispatcher);
103     }
104
105     private Object getField(String fieldName) throws Exception {
106         Field field = RouterHandler.class.getDeclaredField(fieldName);
107         field.setAccessible(true);
108         return field.get(routerHandler);
109     }
110 }