Bug 7922 - Use counter to keep track of duplicate flow entries
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / FloatingIPHandlerTest.java
1 /*
2  * Copyright (c) 2015, 2016 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.netvirt.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.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;
31
32 /**
33  * Unit test for {@link FloatingIPHandler}
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class FloatingIPHandlerTest {
37
38     @InjectMocks FloatingIPHandler floatingHandler;
39     @Mock NeutronL3Adapter neutronL3Adapter;
40
41     @Test
42     public void testCanCreateFloatingIP(){
43         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canCreateFloatingIP(mock(NeutronFloatingIP.class)));
44     }
45
46     @Test
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)));
49     }
50
51     @Test
52     public void testCanDeleteFloatingIP(){
53         assertEquals("Error, did not return the correct HTTP status code", HttpURLConnection.HTTP_OK, floatingHandler.canDeleteFloatingIP(mock(NeutronFloatingIP.class)));
54     }
55
56     @Test
57     public void testProcessEvent(){
58         NorthboundEvent ev = mock(NorthboundEvent.class);
59
60         when(ev.getNeutronFloatingIP()).thenReturn(mock(NeutronFloatingIP.class));
61
62         when(ev.getAction()).thenReturn(Action.UPDATE);
63         floatingHandler.processEvent(ev);
64         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
65
66         when(ev.getAction()).thenReturn(Action.ADD);
67         floatingHandler.processEvent(ev);
68         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
69
70         when(ev.getAction()).thenReturn(Action.DELETE);
71         floatingHandler.processEvent(ev);
72         verify(neutronL3Adapter, times(1)).handleNeutronFloatingIPEvent(ev.getNeutronFloatingIP(), ev.getAction());
73     }
74
75     @Test
76     public void testSetDependencies() throws Exception {
77         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
78         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
79
80         ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
81         ServiceHelper.overrideGlobalInstance(NeutronL3Adapter.class, neutronL3Adapter);
82
83         floatingHandler.setDependencies(mock(ServiceReference.class));
84
85         assertEquals("Error, did not return the correct object", eventDispatcher, floatingHandler.eventDispatcher);
86         assertEquals("Error, did not return the correct object", neutronL3Adapter, getNeutronL3Adapter());
87     }
88
89     private NeutronL3Adapter getNeutronL3Adapter() throws Exception {
90         Field field = FloatingIPHandler.class.getDeclaredField("neutronL3Adapter");
91         field.setAccessible(true);
92         return (NeutronL3Adapter) field.get(floatingHandler);
93     }
94 }