Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SecurityServicesImplTest.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 package org.opendaylight.ovsdb.openstack.netvirt.impl;
9
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;
16
17 import java.lang.reflect.Field;
18 import java.util.ArrayList;
19 import java.util.List;
20
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
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;
37
38 /**
39  * Unit test for {@link SecurityServicesImpl}
40  */
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(ServiceHelper.class)
43 public class SecurityServicesImplTest {
44
45     @InjectMocks private SecurityServicesImpl securityServicesImpl;
46
47     @Mock private INeutronPortCRUD neutronPortCache;
48     @Mock private Southbound southbound;
49
50     @Mock private NeutronSecurityGroup neutronSecurityGroup;
51
52     private static final String NEUTRON_PORT_ID = "neutronID";
53     private static final String DEVICE_OWNER = "compute";
54
55     @Before
56     public void setUp(){
57         NeutronPort neutronPort = mock(NeutronPort.class);
58
59         List<NeutronSecurityGroup> securityGroups = new ArrayList<NeutronSecurityGroup>();
60         securityGroups.add(neutronSecurityGroup);
61
62         when(neutronPort.getSecurityGroups()).thenReturn(securityGroups);
63         when(neutronPort.getDeviceOwner()).thenReturn(DEVICE_OWNER);
64
65         when(southbound.getInterfaceExternalIdsValue(any(OvsdbTerminationPointAugmentation.class), anyString())).thenReturn(NEUTRON_PORT_ID);
66         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
67     }
68
69     /**
70      * Test method {@link SecurityServicesImpl#isPortSecurityReady(Interface)}
71      */
72     @Test
73     public void testIsPortSecurityReady(){
74         assertTrue("Error, did not return expected boolean for isPortSecurityReady", securityServicesImpl.isPortSecurityReady(mock(OvsdbTerminationPointAugmentation.class)));
75     }
76
77     /**
78      * Test method {@link SecurityServicesImpl#getSecurityGroupInPortList(Interface)}
79      */
80     @Test
81     public void testSecurityGroupInPort(){
82         assertEquals("Error, did not return the good neutronSecurityGroup of securityGroups",
83                      neutronSecurityGroup, securityServicesImpl.getSecurityGroupInPortList(mock(OvsdbTerminationPointAugmentation.class)).get(0));
84     }
85
86     @Test
87     public void testSetDependencies() throws Exception {
88         Southbound southbound = mock(Southbound.class);
89
90         PowerMockito.mockStatic(ServiceHelper.class);
91         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, securityServicesImpl)).thenReturn(southbound);
92
93         securityServicesImpl.setDependencies(mock(ServiceReference.class));
94
95         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
96     }
97
98     @Test
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);
103     }
104
105     private Object getField(String fieldName) throws Exception {
106         Field field = SecurityServicesImpl.class.getDeclaredField(fieldName);
107         field.setAccessible(true);
108         return field.get(securityServicesImpl);
109     }
110 }