Do not mock enumeration
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / SouthboundUtilTest.java
1 /*
2  * Copyright © 2015, 2017 Inocybe Technologies 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
9 package org.opendaylight.ovsdb.southbound;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Iterators;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import java.net.InetAddress;
23 import java.net.NetworkInterface;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.mockito.stubbing.Answer;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.api.support.membermodification.MemberMatcher;
45 import org.powermock.api.support.membermodification.MemberModifier;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({SouthboundUtil.class, NetworkInterface.class})
51 public class SouthboundUtilTest {
52
53     @Before
54     public void setUp() {
55         PowerMockito.mockStatic(SouthboundUtil.class, Mockito.CALLS_REAL_METHODS);
56     }
57
58     @SuppressWarnings("unchecked")
59     @Test
60     public void testGetManagingNode() throws Exception {
61         OvsdbBridgeAttributes mn = mock(OvsdbBridgeAttributes.class);
62         DataBroker db = mock(DataBroker.class);
63         OvsdbNodeRef ref = mock(OvsdbNodeRef.class);
64         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
65         when(db.newReadOnlyTransaction()).thenReturn(transaction);
66         when(mn.getManagedBy()).thenReturn(ref);
67         when(ref.getValue()).thenAnswer(
68                 (Answer<InstanceIdentifier<Node>>) invocation -> (InstanceIdentifier<Node>) mock(
69                         InstanceIdentifier.class));
70         CheckedFuture<Optional<Node>, ReadFailedException> nf = mock(CheckedFuture.class);
71         when(transaction.read(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class))).thenReturn(nf);
72         doNothing().when(transaction).close();
73
74         //node, ovsdbNode not null
75         Node node = mock(Node.class);
76         OvsdbNodeAugmentation ovsdbNode = mock(OvsdbNodeAugmentation.class);
77         when(nf.get()).thenReturn(Optional.of(node));
78         when(node.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNode);
79         assertEquals("Failed to return correct Optional object", Optional.of(ovsdbNode),
80                 SouthboundUtil.getManagingNode(db, mn));
81
82         //node not null, ovsdbNode null
83         when(nf.get()).thenReturn(Optional.absent());
84         assertEquals("Failed to return correct Optional object", Optional.absent(),
85                 SouthboundUtil.getManagingNode(db, mn));
86
87         //optional null
88         when(nf.get()).thenReturn(null);
89         assertEquals("Failed to return correct Optional object", Optional.absent(),
90                 SouthboundUtil.getManagingNode(db, mn));
91
92         //ref null
93         when(mn.getManagedBy()).thenReturn(null);
94         assertEquals("Failed to return correct Optional object", Optional.absent(),
95                 SouthboundUtil.getManagingNode(db, mn));
96     }
97
98     @SuppressWarnings("unchecked")
99     @Test
100     public void testReadNode() throws Exception {
101         Optional<DataObject> node = Optional.of(mock(DataObject.class));
102         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
103         InstanceIdentifier<DataObject> connectionIid = mock(InstanceIdentifier.class);
104         CheckedFuture<Optional<DataObject>, ReadFailedException> value = mock(CheckedFuture.class);
105         when(transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid)).thenReturn(value);
106         when(value.checkedGet()).thenReturn(node);
107         assertEquals("Incorrect Optional object received", node, SouthboundUtil.readNode(transaction, connectionIid));
108     }
109
110     @Test
111     public void testGetLocalControllerHostIpAddress() throws Exception {
112
113         //NetworkInterface.getNetworkInterfaces() returns null case
114         PowerMockito.mockStatic(NetworkInterface.class);
115         when(NetworkInterface.getNetworkInterfaces()).thenReturn(null);
116         assertNull(SouthboundUtil.getLocalControllerHostIpAddress());
117
118         InetAddress inetAddr = mock(InetAddress.class);
119         when(inetAddr.isLoopbackAddress()).thenReturn(false);
120         when(inetAddr.isSiteLocalAddress()).thenReturn(true);
121         when(inetAddr.getHostAddress()).thenReturn("HostAddress");
122
123         NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
124         when(iface.getInetAddresses()).thenReturn(Iterators.asEnumeration(
125             Iterators.singletonIterator(inetAddr)));
126
127         when(NetworkInterface.getNetworkInterfaces()).thenReturn(Iterators.asEnumeration(
128             Iterators.singletonIterator(iface)));
129
130         assertEquals("HostAddress", SouthboundUtil.getLocalControllerHostIpAddress());
131     }
132
133     @Test
134     public void testGetControllerTarget() throws Exception {
135         Node ovsdbNode = mock(Node.class);
136         OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
137         when(ovsdbNode.augmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
138         ConnectionInfo connectionInfo = mock(ConnectionInfo.class, Mockito.RETURNS_DEEP_STUBS);
139         when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo);
140
141         //ipAddr not null case
142         IpAddress ipAddr = new IpAddress(new Ipv4Address("0.0.0.0"));
143         when(connectionInfo.getLocalIp()).thenReturn(ipAddr);
144         String testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
145                 + "0.0.0.0" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
146         assertEquals("Incorrect controller IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
147         verify(ovsdbNode).augmentation(OvsdbNodeAugmentation.class);
148         verify(ovsdbNodeAugmentation).getConnectionInfo();
149
150         //ipAddr null case
151         when(connectionInfo.getLocalIp()).thenReturn(null);
152
153         //suppress call to getLocalControllerHostIpAddress()
154         MemberModifier.suppress(MemberMatcher.method(SouthboundUtil.class, "getLocalControllerHostIpAddress"));
155         PowerMockito.when(SouthboundUtil.class, "getLocalControllerHostIpAddress").thenReturn("127.0.0.1");
156         testTarget = SouthboundConstants.OPENFLOW_CONNECTION_PROTOCOL + ":"
157                 + "127.0.0.1" + ":" + SouthboundConstants.DEFAULT_OPENFLOW_PORT;
158         assertEquals("Incorrect Local controller host IP", testTarget, SouthboundUtil.getControllerTarget(ovsdbNode));
159     }
160 }