2 * Copyright (c) 2015, 2016 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.netvirt.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.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
26 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronFloatingIP;
27 import org.opendaylight.netvirt.openstack.netvirt.api.EventDispatcher;
28 import org.opendaylight.netvirt.openstack.netvirt.impl.NeutronL3Adapter;
29 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
30 import org.osgi.framework.ServiceReference;
33 * Unit test for {@link FloatingIPHandler}
35 @RunWith(MockitoJUnitRunner.class)
36 public class FloatingIPHandlerTest {
38 @InjectMocks FloatingIPHandler floatingHandler;
39 @Mock NeutronL3Adapter neutronL3Adapter;
42 public void testCanCreateFloatingIP(){
43 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canCreateFloatingIP(mock(NeutronFloatingIP.class)));
47 public void testCanUpdateFloatingIP(){
48 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canUpdateFloatingIP(mock(NeutronFloatingIP.class), mock(NeutronFloatingIP.class)));
52 public void testCanDeleteFloatingIP(){
53 assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canDeleteFloatingIP(mock(NeutronFloatingIP.class)));
57 public void testProcessEvent(){
58 NorthboundEvent ev = mock(NorthboundEvent.class);
60 when(ev.getNeutronFloatingIP()).thenReturn(mock(NeutronFloatingIP.class));
62 when(ev.getAction()).thenReturn(Action.UPDATE);
63 floatingHandler.processEvent(ev);
64 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
66 when(ev.getAction()).thenReturn(Action.ADD);
67 floatingHandler.processEvent(ev);
68 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
70 when(ev.getAction()).thenReturn(Action.DELETE);
71 floatingHandler.processEvent(ev);
72 verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
76 public void testSetDependencies() throws Exception {
77 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
78 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
80 ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
81 ServiceHelper.overrideGlobalInstance(NeutronL3Adapter.class, neutronL3Adapter);
83 floatingHandler.setDependencies(mock(ServiceReference.class));
85 assertEquals("Error, did not return the correct object", eventDispatcher, floatingHandler.eventDispatcher);
86 assertEquals("Error, did not return the correct object", neutronL3Adapter, getNeutronL3Adapter());
89 private NeutronL3Adapter getNeutronL3Adapter() throws Exception {
90 Field field = FloatingIPHandler.class.getDeclaredField("neutronL3Adapter");
91 field.setAccessible(true);
92 return (NeutronL3Adapter) field.get(floatingHandler);