2 * Copyright (c) 2015 Inocybe and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
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;
17 import java.lang.reflect.Field;
18 import java.net.HttpURLConnection;
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;
36 * Unit test fort {@link RouterHandler}
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class RouterHandlerTest {
42 @InjectMocks RouterHandler routerHandler;
44 @Mock NeutronL3Adapter neutronL3Adapter;
47 public void testCanCreateRouter() {
48 assertEquals("Error, canCreateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canCreateRouter(mock(NeutronRouter.class)));
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)));
57 public void testCanDeleteRouter() {
58 assertEquals("Error, canDeleteRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDeleteRouter(mock(NeutronRouter.class)));
62 public void testCanAttachAndDettachInterface() {
63 NeutronRouter router = mock(NeutronRouter.class);
64 when(router.getName()).thenReturn("router_name");
66 NeutronRouter_Interface routerInterface = mock(NeutronRouter_Interface.class);
67 when(routerInterface.getPortUUID()).thenReturn("portUUID");
68 when(routerInterface.getSubnetUUID()).thenReturn("subnetUUID");
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));
75 public void testProcessEvent() {
76 NorthboundEvent ev = mock(NorthboundEvent.class);
78 when(ev.getAction()).thenReturn(Action.UPDATE);
79 when(ev.getRouter()).thenReturn(mock(NeutronRouter.class));
80 when(ev.getRouterInterface())
82 .thenReturn(mock(NeutronRouter_Interface.class));
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);
91 public void testSetDependencies() throws Exception {
92 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
93 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
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);
99 routerHandler.setDependencies(mock(ServiceReference.class));
101 assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
102 assertEquals("Error, did not return the correct object", routerHandler.eventDispatcher, eventDispatcher);
105 private Object getField(String fieldName) throws Exception {
106 Field field = RouterHandler.class.getDeclaredField(fieldName);
107 field.setAccessible(true);
108 return field.get(routerHandler);