Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / ConfigurationServiceImplTest.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.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.lang.reflect.Field;
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.ovsdb.openstack.netvirt.api.Constants;
25 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
26 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
27 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
28 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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 ConfigurationServiceImpl}
39  */
40 @PrepareForTest({ConfigProperties.class, ServiceHelper.class})
41 @RunWith(PowerMockRunner.class)
42 public class ConfigurationServiceImplTest {
43
44     @InjectMocks private ConfigurationServiceImpl configurationServiceImpl;
45
46     @Mock private Southbound southbound;
47
48     private static final String ADDRESS = "127.0.0.1";
49     private static final String IS_FOWARDING_ENABLE = "yes";
50
51     /**
52      * Test method {@link ConfigurationServiceImpl#getTunnelEndPoint(Node)}
53      */
54     @Test
55     public void testGetTunnelEndPoint() throws Exception {
56         when(southbound.getOtherConfig(any(Node.class), any(OvsdbTables.class), anyString())).thenReturn(ADDRESS);
57
58         assertEquals("Error, did not return the expected address",  ADDRESS, configurationServiceImpl.getTunnelEndPoint(mock(Node.class)).getHostAddress());
59     }
60
61     @Test
62     public void testGetOpenFlowVersion() {
63         assertEquals("Error, did not return the correct OF version", Constants.OPENFLOW13, configurationServiceImpl.getOpenflowVersion(mock(Node.class)));
64     }
65
66     @Test
67     public void testIsL3FowardingEnable() {
68         PowerMockito.mockStatic(ConfigProperties.class);
69         when(ConfigProperties.getProperty(any(Class.class), eq("ovsdb.l3.fwd.enabled"))).thenReturn(IS_FOWARDING_ENABLE);
70
71         assertTrue("Error, l3 fowarding should be activated", configurationServiceImpl.isL3ForwardingEnabled());
72     }
73
74     /**
75      * Test method {@link ConfigurationServiceImpl#getDefaultGatewayMacAddress(Node)}
76      */
77     @Test
78     public void testGetDefaultGatewayMacAddress(){
79         Node node = mock(Node.class);
80         NodeId nodeId = mock(NodeId.class);
81         PowerMockito.mockStatic(ConfigProperties.class);
82
83         when(node.getNodeId()).thenReturn(nodeId);
84         when(nodeId.getValue()).thenReturn("nodeIdValue");
85         PowerMockito.when(ConfigProperties.getProperty(configurationServiceImpl.getClass(),
86                 "ovsdb.l3gateway.mac." + node.getNodeId().getValue())).thenReturn("gateway");
87
88         assertEquals("Error, did not return the defaultGatewayMacAddress of the node", "gateway",
89                 configurationServiceImpl.getDefaultGatewayMacAddress(node));
90     }
91
92     @Test
93     public void testSetDependencies() throws Exception {
94         Southbound southbound = mock(Southbound.class);
95
96         PowerMockito.mockStatic(ServiceHelper.class);
97         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, configurationServiceImpl)).thenReturn(southbound);
98
99         configurationServiceImpl.setDependencies(mock(ServiceReference.class));
100
101         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
102     }
103
104     private Object getField(String fieldName) throws Exception {
105         Field field = ConfigurationServiceImpl.class.getDeclaredField(fieldName);
106         field.setAccessible(true);
107         return field.get(configurationServiceImpl);
108     }
109 }