Add UT for SouthboundMapper and SouthboundProvider
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / SubnetHandlerTest.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.NeutronSubnet;
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 fort {@link SubnetHandler}
37  */
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class SubnetHandlerTest {
41
42     @InjectMocks private SubnetHandler subnetHandler;
43
44     @Mock NeutronL3Adapter neutronl3Adapter;
45
46     @Test
47     public void testCanCreateSubnet() {
48         assertEquals("Error, canCreateSubnet() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, subnetHandler.canCreateSubnet(mock(NeutronSubnet.class)));
49     }
50
51     @Test
52     public void testCanUpdateSubnet() {
53         assertEquals("Error, canUpdateSubnet() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, subnetHandler.canUpdateSubnet(mock(NeutronSubnet.class), mock(NeutronSubnet.class)));
54     }
55
56     @Test
57     public void testCanDeleteSubnet() {
58         assertEquals("Error, canDeleteSubnet() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, subnetHandler.canDeleteSubnet(mock(NeutronSubnet.class)));
59     }
60
61     @Test
62     public void testProcessEvent() {
63         NorthboundEvent ev = mock(NorthboundEvent.class);
64         when(ev.getSubnet()).thenReturn(mock(NeutronSubnet.class));
65
66         when(ev.getAction()).thenReturn(Action.ADD);
67         subnetHandler.processEvent(ev);
68         verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
69
70         when(ev.getAction()).thenReturn(Action.DELETE);
71         subnetHandler.processEvent(ev);
72         verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
73
74         when(ev.getAction()).thenReturn(Action.UPDATE);
75         subnetHandler.processEvent(ev);
76         verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
77     }
78
79     @Test
80     public void testSetDependencies() throws Exception {
81         NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
82         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
83
84         PowerMockito.mockStatic(ServiceHelper.class);
85         PowerMockito.when(ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, subnetHandler)).thenReturn(neutronL3Adapter);
86         PowerMockito.when(ServiceHelper.getGlobalInstance(EventDispatcher.class, subnetHandler)).thenReturn(eventDispatcher);
87
88         subnetHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
89
90         assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
91         assertEquals("Error, did not return the correct object", subnetHandler.eventDispatcher, eventDispatcher);
92     }
93
94     private Object getField(String fieldName) throws Exception {
95         Field field = SubnetHandler.class.getDeclaredField(fieldName);
96         field.setAccessible(true);
97         return field.get(subnetHandler);
98     }
99 }