Fix NPE triggered after disabling SG on a port
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / impl / ConfigurationServiceImplTest.java
1 /*
2  * Copyright (c) 2015, 2016 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.netvirt.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
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
31         .Node;
32 import org.osgi.framework.ServiceReference;
33
34 /**
35  * Unit test for {@link ConfigurationServiceImpl}
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 public class ConfigurationServiceImplTest {
39
40     @InjectMocks private ConfigurationServiceImpl configurationServiceImpl;
41
42     @Mock private Southbound southbound;
43
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";
48
49     /**
50      * Test method {@link ConfigurationServiceImpl#getTunnelEndPoint(Node)}
51      */
52     @Test
53     public void testGetTunnelEndPoint() throws Exception {
54         when(southbound.getOtherConfig(any(Node.class), any(OvsdbTables.class), anyString())).thenReturn(ADDRESS);
55
56         assertEquals("Error, did not return the expected address",  ADDRESS, configurationServiceImpl.getTunnelEndPoint(mock(Node.class)).getHostAddress());
57     }
58
59     @Test
60     public void testGetOpenFlowVersion() {
61         assertEquals("Error, did not return the correct OF version", Constants.OPENFLOW13, configurationServiceImpl.getOpenflowVersion(mock(Node.class)));
62     }
63
64     @Test
65     public void testIsL3FowardingEnable() {
66         ConfigProperties.overrideProperty("ovsdb.l3.fwd.enabled", IS_FOWARDING_ENABLE);
67
68         assertTrue("Error, l3 fowarding should be activated", configurationServiceImpl.isL3ForwardingEnabled());
69     }
70
71     @Test
72     public void testIsRemoteMacLearnEnabled() {
73         ConfigProperties.overrideProperty("ovsdb.remotemac.learn.enabled", IS_LEARN_ENABLE);
74
75         assertTrue("Error, remote mac learn should be activated", configurationServiceImpl.isRemoteMacLearnEnabled());
76     }
77
78     @Test
79     public void testIsL3MultipleExternalNetworkEnabled() {
80         ConfigProperties.overrideProperty("ovsdb.l3.multiple.network.enabled", IS_MULTIPLE_EXTERNAL_NETWORK_ENABLE);
81
82         assertTrue("Error, l3 multiple external network should be activated", configurationServiceImpl.isL3MultipleExternalNetworkEnabled());
83     }
84
85     /**
86      * Test method {@link ConfigurationServiceImpl#getDefaultGatewayMacAddress(Node)}
87      */
88     @Test
89     public void testGetDefaultGatewayMacAddress(){
90         Node node = mock(Node.class);
91         NodeId nodeId = mock(NodeId.class);
92
93         when(node.getNodeId()).thenReturn(nodeId);
94         when(nodeId.getValue()).thenReturn("nodeIdValue");
95         ConfigProperties.overrideProperty("ovsdb.l3gateway.mac." + node.getNodeId().getValue(), "gateway");
96
97         assertEquals("Error, did not return the defaultGatewayMacAddress of the node", "gateway",
98                 configurationServiceImpl.getDefaultGatewayMacAddress(node));
99     }
100
101     @Test
102     public void testSetDependencies() throws Exception {
103         Southbound southbound = mock(Southbound.class);
104
105         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
106
107         configurationServiceImpl.setDependencies(mock(ServiceReference.class));
108
109         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
110     }
111
112     private Object getField(String fieldName) throws Exception {
113         Field field = ConfigurationServiceImpl.class.getDeclaredField(fieldName);
114         field.setAccessible(true);
115         return field.get(configurationServiceImpl);
116     }
117 }