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
8 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import java.lang.reflect.Field;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.opendaylight.neutron.spi.INeutronPortCRUD;
27 import org.opendaylight.neutron.spi.NeutronPort;
28 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
30 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.ServiceReference;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.core.classloader.annotations.PrepareForTest;
36 import org.powermock.modules.junit4.PowerMockRunner;
39 * Unit test for {@link SecurityServicesImpl}
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(ServiceHelper.class)
43 public class SecurityServicesImplTest {
45 @InjectMocks private SecurityServicesImpl securityServicesImpl;
47 @Mock private INeutronPortCRUD neutronPortCache;
48 @Mock private Southbound southbound;
50 @Mock private NeutronSecurityGroup neutronSecurityGroup;
52 private static final String NEUTRON_PORT_ID = "neutronID";
53 private static final String DEVICE_OWNER = "compute";
57 NeutronPort neutronPort = mock(NeutronPort.class);
59 List<NeutronSecurityGroup> securityGroups = new ArrayList<NeutronSecurityGroup>();
60 securityGroups.add(neutronSecurityGroup);
62 when(neutronPort.getSecurityGroups()).thenReturn(securityGroups);
63 when(neutronPort.getDeviceOwner()).thenReturn(DEVICE_OWNER);
65 when(southbound.getInterfaceExternalIdsValue(any(OvsdbTerminationPointAugmentation.class), anyString())).thenReturn(NEUTRON_PORT_ID);
66 when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
70 * Test method {@link SecurityServicesImpl#isPortSecurityReady(Interface)}
73 public void testIsPortSecurityReady(){
74 assertTrue("Error, did not return expected boolean for isPortSecurityReady", securityServicesImpl.isPortSecurityReady(mock(OvsdbTerminationPointAugmentation.class)));
78 * Test method {@link SecurityServicesImpl#getSecurityGroupInPortList(Interface)}
81 public void testSecurityGroupInPort(){
82 assertEquals("Error, did not return the good neutronSecurityGroup of securityGroups",
83 neutronSecurityGroup, securityServicesImpl.getSecurityGroupInPortList(mock(OvsdbTerminationPointAugmentation.class)).get(0));
87 public void testSetDependencies() throws Exception {
88 Southbound southbound = mock(Southbound.class);
90 PowerMockito.mockStatic(ServiceHelper.class);
91 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, securityServicesImpl)).thenReturn(southbound);
93 securityServicesImpl.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
95 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
99 public void testSetDependenciesObject() throws Exception{
100 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
101 securityServicesImpl.setDependencies(neutronPortCache);
102 assertEquals("Error, did not return the correct object", getField("neutronPortCache"), neutronPortCache);
105 private Object getField(String fieldName) throws Exception {
106 Field field = SecurityServicesImpl.class.getDeclaredField(fieldName);
107 field.setAccessible(true);
108 return field.get(securityServicesImpl);