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.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.lang.reflect.Field;
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;
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;
38 * Unit test for {@link ConfigurationServiceImpl}
40 @PrepareForTest({ConfigProperties.class, ServiceHelper.class})
41 @RunWith(PowerMockRunner.class)
42 public class ConfigurationServiceImplTest {
44 @InjectMocks private ConfigurationServiceImpl configurationServiceImpl;
46 @Mock private Southbound southbound;
48 private static final String ADDRESS = "127.0.0.1";
49 private static final String IS_FOWARDING_ENABLE = "yes";
52 * Test method {@link ConfigurationServiceImpl#getTunnelEndPoint(Node)}
55 public void testGetTunnelEndPoint() throws Exception {
56 when(southbound.getOtherConfig(any(Node.class), any(OvsdbTables.class), anyString())).thenReturn(ADDRESS);
58 assertEquals("Error, did not return the expected address", ADDRESS, configurationServiceImpl.getTunnelEndPoint(mock(Node.class)).getHostAddress());
62 public void testGetOpenFlowVersion() {
63 assertEquals("Error, did not return the correct OF version", Constants.OPENFLOW13, configurationServiceImpl.getOpenflowVersion(mock(Node.class)));
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);
71 assertTrue("Error, l3 fowarding should be activated", configurationServiceImpl.isL3ForwardingEnabled());
75 * Test method {@link ConfigurationServiceImpl#getDefaultGatewayMacAddress(Node)}
78 public void testGetDefaultGatewayMacAddress(){
79 Node node = mock(Node.class);
80 NodeId nodeId = mock(NodeId.class);
81 PowerMockito.mockStatic(ConfigProperties.class);
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");
88 assertEquals("Error, did not return the defaultGatewayMacAddress of the node", "gateway",
89 configurationServiceImpl.getDefaultGatewayMacAddress(node));
93 public void testSetDependencies() throws Exception {
94 Southbound southbound = mock(Southbound.class);
96 PowerMockito.mockStatic(ServiceHelper.class);
97 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, configurationServiceImpl)).thenReturn(southbound);
99 configurationServiceImpl.setDependencies(mock(ServiceReference.class));
101 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
104 private Object getField(String fieldName) throws Exception {
105 Field field = ConfigurationServiceImpl.class.getDeclaredField(fieldName);
106 field.setAccessible(true);
107 return field.get(configurationServiceImpl);