Merge "Added Security Rule for Custom ICMP"
[ovsdb.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.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;
34
35 /**
36  * Unit test for {@link FloatingIPHandler}
37  */
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class FloatingIPHandlerTest {
41
42     @InjectMocks FloatingIPHandler floatingHandler;
43     @Mock NeutronL3Adapter neutronL3Adapter;
44
45     @Test
46     public void testCanCreateFloatingIP(){
47         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canCreateFloatingIP(mock(NeutronFloatingIP.class)));
48     }
49
50     @Test
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)));
53     }
54
55     @Test
56     public void testCanDeleteFloatingIP(){
57         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canDeleteFloatingIP(mock(NeutronFloatingIP.class)));
58     }
59
60     @Test
61     public void testProcessEvent(){
62         NorthboundEvent ev = mock(NorthboundEvent.class);
63
64         when(ev.getNeutronFloatingIP()).thenReturn(mock(NeutronFloatingIP.class));
65
66         when(ev.getAction()).thenReturn(Action.UPDATE);
67         floatingHandler.processEvent((AbstractEvent) ev);
68         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
69
70         when(ev.getAction()).thenReturn(Action.ADD);
71         floatingHandler.processEvent((AbstractEvent) ev);
72         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
73
74         when(ev.getAction()).thenReturn(Action.DELETE);
75         floatingHandler.processEvent((AbstractEvent) ev);
76         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());;
77     }
78
79     @Test
80     public void testSetDependencies() throws Exception {
81         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
82         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
83
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);
87
88         floatingHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
89
90         assertEquals("Error, did not return the correct object", floatingHandler.eventDispatcher, eventDispatcher);
91         assertEquals("Error, did not return the correct object", getNeutronL3Adapter(), neutronL3Adapter);
92     }
93
94     private NeutronL3Adapter getNeutronL3Adapter() throws Exception {
95         Field field = FloatingIPHandler.class.getDeclaredField("neutronL3Adapter");
96         field.setAccessible(true);
97         return (NeutronL3Adapter) field.get(floatingHandler);
98     }
99 }