From 8707590ff004efc472aada0ae84f8b2840a80f29 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 18 Jul 2018 04:49:34 +0200 Subject: [PATCH] Do not mock enumeration This reworks mocking to eliminate open-mocked implementation, but rather use a Guava implementation. Change-Id: Ie4d557ca09a06d206c11160219687c006b8d2d07 Signed-off-by: Robert Varga --- .../ovsdb/southbound/SouthboundUtilTest.java | 22 ++++++-------- .../md/OvsdbBridgeUpdateCommandTest.java | 29 +++++++------------ 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/SouthboundUtilTest.java b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/SouthboundUtilTest.java index 85751cdac..f1c2b6cd5 100644 --- a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/SouthboundUtilTest.java +++ b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/SouthboundUtilTest.java @@ -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 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 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()); } diff --git a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommandTest.java b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommandTest.java index 0da83ac03..c8b13dc32 100644 --- a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommandTest.java +++ b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OvsdbBridgeUpdateCommandTest.java @@ -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 updatedBridgeRows = new HashMap<>(); - private Map oldBridgeRows = new HashMap<>(); + private final Map updatedBridgeRows = new HashMap<>(); + private final Map 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 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 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)); -- 2.36.6