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.NeutronFloatingIP;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
27 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
28 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
29 import org.osgi.framework.BundleContext;
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 for {@link FloatingIPHandler}
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class FloatingIPHandlerTest {
42 @InjectMocks FloatingIPHandler floatingHandler;
43 @Mock NeutronL3Adapter neutronL3Adapter;
46 public void testCanCreateFloatingIP(){
47 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canCreateFloatingIP(mock(NeutronFloatingIP.class)));
51 public void testCanUpdateFloatingIP(){
52 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canUpdateFloatingIP(mock(NeutronFloatingIP.class), mock(NeutronFloatingIP.class)));
56 public void testCanDeleteFloatingIP(){
57 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canDeleteFloatingIP(mock(NeutronFloatingIP.class)));
61 public void testProcessEvent(){
62 NorthboundEvent ev = mock(NorthboundEvent.class);
64 when(ev.getNeutronFloatingIP()).thenReturn(mock(NeutronFloatingIP.class));
66 when(ev.getAction()).thenReturn(Action.UPDATE);
67 floatingHandler.processEvent((AbstractEvent) ev);
68 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
70 when(ev.getAction()).thenReturn(Action.ADD);
71 floatingHandler.processEvent((AbstractEvent) ev);
72 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
74 when(ev.getAction()).thenReturn(Action.DELETE);
75 floatingHandler.processEvent((AbstractEvent) ev);
76 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
80 public void testSetDependencies() throws Exception {
81 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
82 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
84 PowerMockito.mockStatic(ServiceHelper.class);
85 PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, floatingHandler)).thenReturn(eventDispatcher);
86 PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, floatingHandler)).thenReturn(neutronL3Adapter);
88 floatingHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
90 assertEquals("Error, did not return the correct object", floatingHandler.eventDispatcher, eventDispatcher);
91 assertEquals("Error, did not return the correct object", getNeutronL3Adapter(), neutronL3Adapter);
94 private NeutronL3Adapter getNeutronL3Adapter() throws Exception {
95 Field field = FloatingIPHandler.class.getDeclaredField("neutronL3Adapter");
96 field.setAccessible(true);
97 return (NeutronL3Adapter) field.get(floatingHandler);