7ef27dec559a6e2055ff5ee11b60d4dd3934faf6
[netvirt.git] / neutron / src / test / java / org / opendaylight / ovsdb / neutron / AdminConfigManagerTest.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Dave Tucker (HP) - Added unit tests for the AdminConfigManager class.
10  *******************************************************************************/
11
12 package org.opendaylight.ovsdb.neutron;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.anyObject;
17 import static org.mockito.Matchers.anyString;
18 import static org.mockito.Matchers.eq;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21
22 import java.net.InetAddress;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.utils.ServiceHelper;
30 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
31 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
32 import org.opendaylight.ovsdb.lib.table.Table;
33 import org.opendaylight.ovsdb.plugin.ConfigurationService;
34 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest(ServiceHelper.class)
41 public class AdminConfigManagerTest {
42
43     @Test
44     public void testGetTunnelEndpoint() throws Exception {
45         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
46
47         Node mockNode = mock(Node.class);
48
49         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
50
51         Open_vSwitch ovsTable = new Open_vSwitch();
52         OvsDBMap localIp = new OvsDBMap();
53         localIp.put("local_ip", "10.10.10.10");
54         ovsTable.setOther_config(localIp);
55         ovsMap.put("Open_vSwitch", ovsTable);
56
57         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
58         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
59                                                                .thenReturn(ovsMap);
60
61         PowerMockito.mockStatic(ServiceHelper.class);
62         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
63
64         // OVSDBConfigService is null
65         assertEquals(null, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
66
67         // Success...
68         assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
69     }
70
71     @Test
72     public void testGetTunnelEndpointWithNullRows() throws Exception {
73         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
74
75         Node mockNode = mock(Node.class);
76
77         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
78
79         Open_vSwitch nullRow = new Open_vSwitch();
80         Open_vSwitch ovsRow1 = new Open_vSwitch();
81         Open_vSwitch ovsRow2 = new Open_vSwitch();
82         OvsDBMap invalidLocalIp = new OvsDBMap();
83         OvsDBMap localIp = new OvsDBMap();
84
85         ovsRow1.setOther_config(invalidLocalIp);
86
87         localIp.put("local_ip","10.10.10.10");
88         ovsRow2.setOther_config(localIp);
89
90         ovsMap.put("0", nullRow);
91         ovsMap.put("1", ovsRow1);
92         ovsMap.put("2", ovsRow2);
93
94         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
95         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
96
97         PowerMockito.mockStatic(ServiceHelper.class);
98         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
99
100         // Success...
101         assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
102     }
103 }