Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / FloatingIPHandlerTest.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.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.ServiceReference;
30 import org.powermock.api.mockito.PowerMockito;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 /**
35  * Unit test for {@link FloatingIPHandler}
36  */
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest(ServiceHelper.class)
39 public class FloatingIPHandlerTest {
40
41     @InjectMocks FloatingIPHandler floatingHandler;
42     @Mock NeutronL3Adapter neutronL3Adapter;
43
44     @Test
45     public void testCanCreateFloatingIP(){
46         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canCreateFloatingIP(mock(NeutronFloatingIP.class)));
47     }
48
49     @Test
50     public void testCanUpdateFloatingIP(){
51         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canUpdateFloatingIP(mock(NeutronFloatingIP.class), mock(NeutronFloatingIP.class)));
52     }
53
54     @Test
55     public void testCanDeleteFloatingIP(){
56         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canDeleteFloatingIP(mock(NeutronFloatingIP.class)));
57     }
58
59     @Test
60     public void testProcessEvent(){
61         NorthboundEvent ev = mock(NorthboundEvent.class);
62
63         when(ev.getNeutronFloatingIP()).thenReturn(mock(NeutronFloatingIP.class));
64
65         when(ev.getAction()).thenReturn(Action.UPDATE);
66         floatingHandler.processEvent((AbstractEvent) ev);
67         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
68
69         when(ev.getAction()).thenReturn(Action.ADD);
70         floatingHandler.processEvent((AbstractEvent) ev);
71         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
72
73         when(ev.getAction()).thenReturn(Action.DELETE);
74         floatingHandler.processEvent((AbstractEvent) ev);
75         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
76     }
77
78     @Test
79     public void testSetDependencies() throws Exception {
80         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
81         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
82
83         PowerMockito.mockStatic(ServiceHelper.class);
84         PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, floatingHandler)).thenReturn(eventDispatcher);
85         PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, floatingHandler)).thenReturn(neutronL3Adapter);
86
87         floatingHandler.setDependencies(mock(ServiceReference.class));
88
89         assertEquals("Error, did not return the correct object", floatingHandler.eventDispatcher, eventDispatcher);
90         assertEquals("Error, did not return the correct object", getNeutronL3Adapter(), neutronL3Adapter);
91     }
92
93     private NeutronL3Adapter getNeutronL3Adapter() throws Exception {
94         Field field = FloatingIPHandler.class.getDeclaredField("neutronL3Adapter");
95         field.setAccessible(true);
96         return (NeutronL3Adapter) field.get(floatingHandler);
97     }
98 }