Revert: Revert "Add JUnit testing for ConfigurationServiceImpl class."
[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.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Matchers.same;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.opendaylight.ovsdb.lib.notation.Column;
27 import org.opendaylight.ovsdb.lib.notation.Row;
28 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
30 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
31 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
32 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 /**
40  * Unit test for {@link ConfigurationServiceImpl}
41  */
42 @PrepareForTest(ConfigProperties.class)
43 @RunWith(PowerMockRunner.class)
44 public class ConfigurationServiceImplTest {
45
46     @Mock
47     private OvsdbConfigurationService ovsdbConfigurationService;
48
49     @InjectMocks
50     private ConfigurationServiceImpl configurationServiceImpl;
51
52     private static final String HOST_ADDRESS = "127.0.0.1";
53
54     /**
55      * Test method {@link ConfigurationServiceImpl#getTunnelEndPoint(Node)}
56      */
57     @Test
58     public void testGetTunnelEndPoint() throws Exception {
59         Row row = mock(Row.class);
60         ConcurrentMap<String, Row> ovsTable = new ConcurrentHashMap();
61         ovsTable.put("key", row);
62
63         OpenVSwitch ovsRow = mock(OpenVSwitch.class);
64         Map<String, String> configs = new HashMap();
65         configs.put(Constants.TUNNEL_ENDPOINT_KEY, HOST_ADDRESS);
66         Column<GenericTableSchema, Map<String, String>> otherConfigColumn = mock(Column.class);
67
68         when(ovsRow.getOtherConfigColumn()).thenReturn(otherConfigColumn);
69         when(otherConfigColumn.getData()).thenReturn(configs);
70
71         when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(ovsTable);
72         when(ovsdbConfigurationService.getTypedRow(any(Node.class),same(OpenVSwitch.class), any(Row.class))).thenReturn(ovsRow);
73
74         assertEquals("Error, did not return address of tunnelEndPoint", HOST_ADDRESS, configurationServiceImpl.getTunnelEndPoint(mock(Node.class)).getHostAddress());
75     }
76
77     /**
78      * Test method {@link ConfigurationServiceImpl#getDefaultGatewayMacAddress(Node)}
79      */
80     @Test
81     public void testGetDefaultGatewayMacAddress(){
82         Node node = mock(Node.class);
83         NodeId nodeId = mock(NodeId.class);
84         PowerMockito.mockStatic(ConfigProperties.class);
85
86         when(node.getId()).thenReturn(nodeId);
87         when(nodeId.getValue()).thenReturn("nodeIdValue");
88         PowerMockito.when(ConfigProperties.getProperty(configurationServiceImpl.getClass(), "ovsdb.l3gateway.mac." + node.getId().getValue())).thenReturn("gateway");
89
90         assertEquals("Error, did not return the defaultGatewayMacAddress of the node", "gateway", configurationServiceImpl.getDefaultGatewayMacAddress(node));
91     }
92 }