2 * Copyright (c) 2015, 2016 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.netvirt.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;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbTables;
25 import org.opendaylight.netvirt.openstack.netvirt.api.Constants;
26 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
27 import org.opendaylight.netvirt.utils.config.ConfigProperties;
28 import org.opendaylight.netvirt.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
32 import org.osgi.framework.ServiceReference;
35 * Unit test for {@link ConfigurationServiceImpl}
37 @RunWith(MockitoJUnitRunner.class)
38 public class ConfigurationServiceImplTest {
40 @InjectMocks private ConfigurationServiceImpl configurationServiceImpl;
42 @Mock private Southbound southbound;
44 private static final String ADDRESS = "127.0.0.1";
45 private static final String IS_FOWARDING_ENABLE = "yes";
46 private static final String IS_LEARN_ENABLE = "yes";
47 private static final String IS_MULTIPLE_EXTERNAL_NETWORK_ENABLE = "yes";
50 * Test method {@link ConfigurationServiceImpl#getTunnelEndPoint(Node)}
53 public void testGetTunnelEndPoint() throws Exception {
54 when(southbound.getOtherConfig(any(Node.class), any(OvsdbTables.class), anyString())).thenReturn(ADDRESS);
56 assertEquals("Error, did not return the expected address", ADDRESS, configurationServiceImpl.getTunnelEndPoint(mock(Node.class)).getHostAddress());
60 public void testGetOpenFlowVersion() {
61 assertEquals("Error, did not return the correct OF version", Constants.OPENFLOW13, configurationServiceImpl.getOpenflowVersion(mock(Node.class)));
65 public void testIsL3FowardingEnable() {
66 ConfigProperties.overrideProperty("ovsdb.l3.fwd.enabled", IS_FOWARDING_ENABLE);
68 assertTrue("Error, l3 fowarding should be activated", configurationServiceImpl.isL3ForwardingEnabled());
72 public void testIsRemoteMacLearnEnabled() {
73 ConfigProperties.overrideProperty("ovsdb.remotemac.learn.enabled", IS_LEARN_ENABLE);
75 assertTrue("Error, remote mac learn should be activated", configurationServiceImpl.isRemoteMacLearnEnabled());
79 public void testIsL3MultipleExternalNetworkEnabled() {
80 ConfigProperties.overrideProperty("ovsdb.l3.multiple.network.enabled", IS_MULTIPLE_EXTERNAL_NETWORK_ENABLE);
82 assertTrue("Error, l3 multiple external network should be activated", configurationServiceImpl.isL3MultipleExternalNetworkEnabled());
86 * Test method {@link ConfigurationServiceImpl#getDefaultGatewayMacAddress(Node)}
89 public void testGetDefaultGatewayMacAddress(){
90 Node node = mock(Node.class);
91 NodeId nodeId = mock(NodeId.class);
93 when(node.getNodeId()).thenReturn(nodeId);
94 when(nodeId.getValue()).thenReturn("nodeIdValue");
95 ConfigProperties.overrideProperty("ovsdb.l3gateway.mac." + node.getNodeId().getValue(), "gateway");
97 assertEquals("Error, did not return the defaultGatewayMacAddress of the node", "gateway",
98 configurationServiceImpl.getDefaultGatewayMacAddress(node));
102 public void testSetDependencies() throws Exception {
103 Southbound southbound = mock(Southbound.class);
105 ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
107 configurationServiceImpl.setDependencies(mock(ServiceReference.class));
109 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
112 private Object getField(String fieldName) throws Exception {
113 Field field = ConfigurationServiceImpl.class.getDeclaredField(fieldName);
114 field.setAccessible(true);
115 return field.get(configurationServiceImpl);