Do not mock enumeration 72/74172/8
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Jul 2018 02:49:34 +0000 (04:49 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Jul 2018 17:44:49 +0000 (19:44 +0200)
This reworks mocking to eliminate open-mocked implementation, but
rather use a Guava implementation.

Change-Id: Ie4d557ca09a06d206c11160219687c006b8d2d07
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/SouthboundUtilTest.java
southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommandTest.java

index 85751cdacf26cc0111ee87715c8ce7473f8f9b30..f1c2b6cd5764be1c92b3541b236501d3ab95a4f2 100644 (file)
@@ -17,10 +17,10 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.Iterators;
 import com.google.common.util.concurrent.CheckedFuture;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
-import java.util.Enumeration;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -115,22 +115,18 @@ public class SouthboundUtilTest {
         when(NetworkInterface.getNetworkInterfaces()).thenReturn(null);
         assertNull(SouthboundUtil.getLocalControllerHostIpAddress());
 
-        @SuppressWarnings("unchecked")
-        Enumeration<NetworkInterface> ifaces = mock(Enumeration.class);
-        when(NetworkInterface.getNetworkInterfaces()).thenReturn(ifaces);
-        when(ifaces.hasMoreElements()).thenReturn(true).thenReturn(false);
-        NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
-        when(ifaces.nextElement()).thenReturn(iface);
-
-        @SuppressWarnings("unchecked")
-        Enumeration<InetAddress> inetAddrs = mock(Enumeration.class);
-        when(iface.getInetAddresses()).thenReturn(inetAddrs);
-        when(inetAddrs.hasMoreElements()).thenReturn(true).thenReturn(false);
         InetAddress inetAddr = mock(InetAddress.class);
-        when(inetAddrs.nextElement()).thenReturn(inetAddr);
         when(inetAddr.isLoopbackAddress()).thenReturn(false);
         when(inetAddr.isSiteLocalAddress()).thenReturn(true);
         when(inetAddr.getHostAddress()).thenReturn("HostAddress");
+
+        NetworkInterface iface = PowerMockito.mock(NetworkInterface.class);
+        when(iface.getInetAddresses()).thenReturn(Iterators.asEnumeration(
+            Iterators.singletonIterator(inetAddr)));
+
+        when(NetworkInterface.getNetworkInterfaces()).thenReturn(Iterators.asEnumeration(
+            Iterators.singletonIterator(iface)));
+
         assertEquals("HostAddress", SouthboundUtil.getLocalControllerHostIpAddress());
     }
 
index 0da83ac03577d39c937aba2164b4a900b075c27a..c8b13dc32ff39399e4c99940f5c6d90894ce67e9 100644 (file)
@@ -19,11 +19,10 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.Iterators;
 import com.google.common.net.InetAddresses;
-import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -87,8 +86,8 @@ import org.powermock.reflect.Whitebox;
 @PrepareForTest({ TyperUtils.class, OvsdbBridgeUpdateCommand.class, SouthboundUtil.class, InstanceIdentifier.class,
         SouthboundMapper.class, NetworkInterface.class })
 public class OvsdbBridgeUpdateCommandTest {
-    private Map<UUID,Bridge> updatedBridgeRows = new HashMap<>();
-    private Map<UUID, Bridge> oldBridgeRows = new HashMap<>();
+    private final Map<UUID,Bridge> updatedBridgeRows = new HashMap<>();
+    private final Map<UUID, Bridge> oldBridgeRows = new HashMap<>();
     private OvsdbBridgeUpdateCommand ovsdbBridgeUpdateCommand;
 
     @Before
@@ -457,27 +456,19 @@ public class OvsdbBridgeUpdateCommandTest {
         when(controllerEntry.getTarget()).thenReturn(uri);
         when(uri.getValue()).thenReturn("tcp:192.168.12.56:6633");
 
+        Ipv4Address ipv4Address = mock(Ipv4Address.class);
+        when(ipv4Address.getValue()).thenReturn("127.0.0.1");
         IpAddress bridgeControllerIpAddress = mock(IpAddress.class);
+        when(bridgeControllerIpAddress.getIpv4Address()).thenReturn(ipv4Address);
         PowerMockito.whenNew(IpAddress.class).withAnyArguments().thenReturn(bridgeControllerIpAddress);
 
         PowerMockito.mockStatic(NetworkInterface.class);
-        Enumeration<NetworkInterface> networkInterfaces = mock(Enumeration.class);
-        when(NetworkInterface.getNetworkInterfaces()).thenReturn(networkInterfaces);
-
-        when(networkInterfaces.hasMoreElements()).thenReturn(true, false);
         NetworkInterface networkInterface = PowerMockito.mock(NetworkInterface.class);
-        when(networkInterfaces.nextElement()).thenReturn(networkInterface);
-
-        Enumeration<InetAddress> networkInterfaceAddresses = mock(Enumeration.class);
-        when(networkInterface.getInetAddresses()).thenReturn(networkInterfaceAddresses);
-        when(networkInterfaceAddresses.hasMoreElements()).thenReturn(true, false);
+        when(networkInterface.getInetAddresses()).thenReturn(Iterators.asEnumeration(
+            Iterators.singletonIterator(InetAddresses.forString("127.0.0.1"))));
+        when(NetworkInterface.getNetworkInterfaces()).thenReturn(Iterators.asEnumeration(
+            Iterators.singletonIterator(networkInterface)));
 
-        InetAddress networkInterfaceAddress = InetAddresses.forString("127.0.0.1");
-        when(networkInterfaceAddresses.nextElement()).thenReturn(networkInterfaceAddress);
-
-        Ipv4Address ipv4Address = mock(Ipv4Address.class);
-        when(bridgeControllerIpAddress.getIpv4Address()).thenReturn(ipv4Address);
-        when(ipv4Address.getValue()).thenReturn("127.0.0.1");
         OvsdbConnectionInstance ovsdbConnectionInstance = mock(OvsdbConnectionInstance.class);
         when(ovsdbBridgeUpdateCommand.getOvsdbConnectionInstance()).thenReturn(ovsdbConnectionInstance);
         when(ovsdbConnectionInstance.getInstanceIdentifier()).thenReturn(mock(InstanceIdentifier.class));