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.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;
37 * Unit test fort {@link RouterHandler}
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(ServiceHelper.class)
41 public class RouterHandlerTest {
43 @InjectMocks RouterHandler routerHandler;
45 @Mock NeutronL3Adapter neutronL3Adapter;
48 public void testCanCreateRouter() {
49 assertEquals("Error, canCreateRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canCreateRouter(mock(NeutronRouter.class)));
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)));
58 public void testCanDeleteRouter() {
59 assertEquals("Error, canDeleteRouter() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, routerHandler.canDeleteRouter(mock(NeutronRouter.class)));
63 public void testCanAttachAndDettachInterface() {
64 NeutronRouter router = mock(NeutronRouter.class);
65 when(router.getName()).thenReturn("router_name");
67 NeutronRouter_Interface routerInterface = mock(NeutronRouter_Interface.class);
68 when(routerInterface.getPortUUID()).thenReturn("portUUID");
69 when(routerInterface.getSubnetUUID()).thenReturn("subnetUUID");
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));
76 public void testProcessEvent() {
77 NorthboundEvent ev = mock(NorthboundEvent.class);
79 when(ev.getAction()).thenReturn(Action.UPDATE);
80 when(ev.getRouter()).thenReturn(mock(NeutronRouter.class));
81 when(ev.getRouterInterface())
83 .thenReturn(mock(NeutronRouter_Interface.class));
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);
92 public void testSetDependencies() throws Exception {
93 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
94 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
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);
100 routerHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
102 assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
103 assertEquals("Error, did not return the correct object", routerHandler.eventDispatcher, eventDispatcher);
106 private Object getField(String fieldName) throws Exception {
107 Field field = RouterHandler.class.getDeclaredField(fieldName);
108 field.setAccessible(true);
109 return field.get(routerHandler);