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;
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());
}
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;
@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
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));