Merge "Fix bug 1028: Don't ignore exception in prepareInternalNetwork"
[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.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.opendaylight.controller.sal.core.Node;
30 import org.opendaylight.controller.sal.utils.ServiceHelper;
31 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
32 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
33 import org.opendaylight.ovsdb.lib.table.internal.Table;
34 import org.opendaylight.ovsdb.plugin.ConfigurationService;
35 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(ServiceHelper.class)
42 public class AdminConfigManagerTest {
43
44     AdminConfigManager adminConfigManager;
45
46     @Before
47     public void setUp(){
48         adminConfigManager = new AdminConfigManager();
49     }
50
51     @Test
52     public void testGetTunnelEndpoint() throws Exception {
53         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
54
55         Node mockNode = mock(Node.class);
56
57         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
58
59         Open_vSwitch ovsTable = new Open_vSwitch();
60         OvsDBMap localIp = new OvsDBMap();
61         localIp.put("local_ip", "10.10.10.10");
62         ovsTable.setOther_config(localIp);
63         ovsMap.put("Open_vSwitch", ovsTable);
64
65         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
66         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
67                                                                .thenReturn(ovsMap);
68
69         PowerMockito.mockStatic(ServiceHelper.class);
70         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
71
72         // OVSDBConfigService is null
73         assertEquals(null, adminConfigManager.getTunnelEndPoint(mockNode));
74
75         // Success...
76         assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
77     }
78
79     @Test
80     public void testGetTunnelEndpointWithNullRows() throws Exception {
81         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
82
83         Node mockNode = mock(Node.class);
84
85         ConcurrentMap<String, Table<?>> ovsMap = new ConcurrentHashMap<>();
86
87         Open_vSwitch nullRow = new Open_vSwitch();
88         Open_vSwitch ovsRow1 = new Open_vSwitch();
89         Open_vSwitch ovsRow2 = new Open_vSwitch();
90         OvsDBMap invalidLocalIp = new OvsDBMap();
91         OvsDBMap localIp = new OvsDBMap();
92
93         ovsRow1.setOther_config(invalidLocalIp);
94
95         localIp.put("local_ip","10.10.10.10");
96         ovsRow2.setOther_config(localIp);
97
98         ovsMap.put("0", nullRow);
99         ovsMap.put("1", ovsRow1);
100         ovsMap.put("2", ovsRow2);
101
102         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
103         when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
104
105         PowerMockito.mockStatic(ServiceHelper.class);
106         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
107
108         // Success...
109         assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
110     }
111 }