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