Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / VlanConfigurationCacheImplTest.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.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.lang.reflect.Field;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.junit.Before;
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.NeutronNetwork;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
27 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31
32 import org.osgi.framework.ServiceReference;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37 /**
38  * Unit test for {@link VlanConfigurationCacheImpl}
39  */
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(ServiceHelper.class)
42 public class VlanConfigurationCacheImplTest {
43
44     @InjectMocks public VlanConfigurationCacheImpl vlanConfigurationCacheImpl;
45
46     @Mock private TenantNetworkManager tenantNetworkManager;
47     @Mock private Southbound southbound;
48
49     private static final String NODE_UUID = "nodeUUID";
50     private static final String NETWORK_ID= "networkId";
51     private static final int VLAN_ID = 4;
52     /**
53      * Function configuring the node
54      */
55     @Before
56     public void setUp(){
57         when(southbound.getOvsdbNodeUUID(any(Node.class))).thenReturn(NODE_UUID);
58         List<OvsdbTerminationPointAugmentation> ports = new ArrayList<OvsdbTerminationPointAugmentation>();
59         OvsdbTerminationPointAugmentation port = mock(OvsdbTerminationPointAugmentation.class);
60         VlanId vlanId = mock(VlanId.class);
61         when(vlanId.getValue()).thenReturn(VLAN_ID);
62         when(port.getVlanTag()).thenReturn(vlanId);
63         when(southbound.getTerminationPointsOfBridge(any(Node.class))).thenReturn(ports );
64         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
65         when(neutronNetwork.getNetworkUUID()).thenReturn(NETWORK_ID);
66         when(tenantNetworkManager.getTenantNetwork(any(OvsdbTerminationPointAugmentation.class))).thenReturn(neutronNetwork );
67     }
68
69     /**
70      * Test method {@link VlanConfigurationCacheImpl#assignInternalVlan(Node, String)}
71      */
72     @Test
73     public void testAssignReclaimAndGetInternalVlan() {
74         assertEquals("Error, assignInternalVlan() did not return the correct internalVlanId (first added)", 1, (int) vlanConfigurationCacheImpl.assignInternalVlan(any(Node.class), NETWORK_ID));
75         assertEquals("Error, assignInternalVlan () did not return the correct internalVlanId (second added)", 2, (int) vlanConfigurationCacheImpl.assignInternalVlan(any(Node.class), NETWORK_ID + "1"));
76
77         assertEquals("Error, getInternalVlan() did not return the correct internalVlan", 1, (int) vlanConfigurationCacheImpl.getInternalVlan(any(Node.class), NETWORK_ID));
78
79         assertEquals("Error, reclaimInternalVlan() did not return the correct internalVlanId", 1, (int) vlanConfigurationCacheImpl.reclaimInternalVlan(any(Node.class), NETWORK_ID));
80         assertEquals("Error, reclaimInternalVlan() did not return the correct internalVlanId", 2, (int) vlanConfigurationCacheImpl.reclaimInternalVlan(any(Node.class), NETWORK_ID + "1"));
81     }
82
83     @Test
84     public void testSetDependencies() throws Exception {
85         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
86         Southbound southbound = mock(Southbound.class);
87
88         PowerMockito.mockStatic(ServiceHelper.class);
89         PowerMockito.when(ServiceHelper.getGlobalInstance(TenantNetworkManager.class, vlanConfigurationCacheImpl)).thenReturn(tenantNetworkManager);
90         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, vlanConfigurationCacheImpl)).thenReturn(southbound);
91
92         vlanConfigurationCacheImpl.setDependencies(mock(ServiceReference.class));
93
94         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
95         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
96     }
97
98     private Object getField(String fieldName) throws Exception {
99         Field field = VlanConfigurationCacheImpl.class.getDeclaredField(fieldName);
100         field.setAccessible(true);
101         return field.get(vlanConfigurationCacheImpl);
102     }
103 }