2 * Copyright (c) 2015 Inocybe and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
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;
17 import java.lang.reflect.Field;
18 import java.net.HttpURLConnection;
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;
36 * Unit test fort {@link SubnetHandler}
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest(ServiceHelper.class)
40 public class SubnetHandlerTest {
42 @InjectMocks private SubnetHandler subnetHandler;
44 @Mock NeutronL3Adapter neutronl3Adapter;
47 public void testCanCreateSubnet() {
48 assertEquals("Error, canCreateSubnet() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, subnetHandler.canCreateSubnet(mock(NeutronSubnet.class)));
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)));
57 public void testCanDeleteSubnet() {
58 assertEquals("Error, canDeleteSubnet() did not return the correct HTTP flag", HttpURLConnection.HTTP_OK, subnetHandler.canDeleteSubnet(mock(NeutronSubnet.class)));
62 public void testProcessEvent() {
63 NorthboundEvent ev = mock(NorthboundEvent.class);
64 when(ev.getSubnet()).thenReturn(mock(NeutronSubnet.class));
66 when(ev.getAction()).thenReturn(Action.ADD);
67 subnetHandler.processEvent(ev);
68 verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
70 when(ev.getAction()).thenReturn(Action.DELETE);
71 subnetHandler.processEvent(ev);
72 verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
74 when(ev.getAction()).thenReturn(Action.UPDATE);
75 subnetHandler.processEvent(ev);
76 verify(neutronl3Adapter, times(1)).handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());;
80 public void testSetDependencies() throws Exception {
81 NeutronL3Adapter neutronL3Adapter = mock(NeutronL3Adapter.class);
82 EventDispatcher eventDispatcher = mock(EventDispatcher.class);
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);
88 subnetHandler.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
90 assertEquals("Error, did not return the correct object", getField("neutronL3Adapter"), neutronL3Adapter);
91 assertEquals("Error, did not return the correct object", subnetHandler.eventDispatcher, eventDispatcher);
94 private Object getField(String fieldName) throws Exception {
95 Field field = SubnetHandler.class.getDeclaredField(fieldName);
96 field.setAccessible(true);
97 return field.get(subnetHandler);