@Test
  public void isNonNetworkDeviceNetworkFree() {
    HostDevice device = new HostDevice();
    device.setHostId(HOST_ID);
    device.setDeviceName(PCI_DEVICE_NAME_2);

    assertTrue(networkDeviceHelper.isDeviceNetworkFree(device));
  }
  @Test
  public void getNicByNetDeviceNoNic() {
    VdsNetworkInterface newNic = new VdsNetworkInterface();
    newNic.setName(netDevice.getNetworkInterfaceName() + "not");
    mockNics(Collections.singletonList(newNic), false);

    assertNull(networkDeviceHelper.getNicByPciDevice(pciDevice));
  }
  @Test
  public void getNicByNetDeviceWithNonDbDevicesNoNetDevice() {
    mockNics(Collections.<VdsNetworkInterface>emptyList(), true);
    Collection<HostDevice> devices = new ArrayList<>();
    devices.add(pciDevice);

    assertNull(networkDeviceHelper.getNicByPciDevice(pciDevice, devices));
  }
  @Test
  public void testGetVfMapHostDoesNotSupportSriov() {
    mockHostSupportsSriov(false);

    final Map<Guid, Guid> actual = networkDeviceHelper.getVfMap(HOST_ID);

    assertTrue(actual.isEmpty());
  }
  private void commonUpdateHostNicVfsConfigWithNumVfsData(int numOfVfs) {
    when(pciDevice.getTotalVirtualFunctions()).thenReturn(TOTAL_NUM_OF_VFS);
    List<HostDevice> vfs = mockVfsOnNetDevice(numOfVfs);
    mockHostDevices(vfs);

    networkDeviceHelper.updateHostNicVfsConfigWithNumVfsData(hostNicVfsConfig);

    verify(hostNicVfsConfig).setMaxNumOfVfs(TOTAL_NUM_OF_VFS);
    verify(hostNicVfsConfig).setNumOfVfs(numOfVfs);
  }
  private void removeVmIdFromVfsCommonTest(int numOfVfWithVmId, int numOfOtherDeviceWithVmId) {
    List<HostDevice> allDevices = new ArrayList<>();
    List<HostDevice> otherDeviceWithVmId = new ArrayList<>();

    Guid vmId = Guid.newGuid();
    List<HostDevice> vfs = mockVfsOnNetDevice(numOfVfWithVmId, vmId);
    allDevices.addAll(vfs);

    for (int i = 0; i <= numOfOtherDeviceWithVmId; ++i) {
      HostDevice hostDevice = createHostDevice(vmId);
      otherDeviceWithVmId.add(hostDevice);
    }

    allDevices.addAll(otherDeviceWithVmId);
    mockHostDevices(allDevices);

    for (HostDevice vf : vfs) {
      assertEquals(vmId, vf.getVmId());
    }

    networkDeviceHelper.removeVmIdFromVfs(vmId);

    for (HostDevice vf : vfs) {
      vf.setVmId(null);
    }

    if (numOfVfWithVmId == 0) {
      verify(hostDeviceDao, never()).setVmIdOnHostDevice(any(HostDeviceId.class), any(Guid.class));
    } else {
      verify(hostDeviceDao, times(numOfVfWithVmId))
          .setVmIdOnHostDevice(hostDeviceIdCaptor.capture(), vmIdCaptor.capture());

      List<HostDeviceId> capturedDeviceIds = hostDeviceIdCaptor.getAllValues();
      List<Guid> capturedVmIds = vmIdCaptor.getAllValues();

      for (HostDevice vf : vfs) {
        assertTrue(capturedDeviceIds.contains(vf.getId()));
      }

      for (HostDevice hostDevice : otherDeviceWithVmId) {
        assertFalse(capturedDeviceIds.contains(hostDevice.getId()));
      }

      for (Guid capturedVmId : capturedVmIds) {
        assertEquals(null, capturedVmId);
      }
    }
  }
  @Test
  public void getHostNicVfsConfigsWithNumVfsDataByHostId() {
    when(hostNicVfsConfigDao.getAllVfsConfigByHostId(HOST_ID))
        .thenReturn(Collections.singletonList(hostNicVfsConfig));

    when(pciDevice.getTotalVirtualFunctions()).thenReturn(TOTAL_NUM_OF_VFS);
    List<HostDevice> vfs = mockVfsOnNetDevice(2);
    mockHostDevices(vfs);

    List<HostNicVfsConfig> vfsConfigList =
        networkDeviceHelper.getHostNicVfsConfigsWithNumVfsDataByHostId(HOST_ID);

    assertEquals(1, vfsConfigList.size());
    assertEquals(hostNicVfsConfig, vfsConfigList.get(0));

    verify(hostNicVfsConfig).setMaxNumOfVfs(TOTAL_NUM_OF_VFS);
    verify(hostNicVfsConfig).setNumOfVfs(2);
  }
  @Test
  public void setVmIdOnVfs() {
    List<HostDevice> vfs = mockVfsOnNetDevice(1);
    mockHostDevices(vfs);

    HostDevice vf = vfs.get(0);
    Guid vmId = Guid.newGuid();
    vf.setVmId(vmId);
    networkDeviceHelper.setVmIdOnVfs(HOST_ID, vmId, Collections.singleton(vf.getDeviceName()));

    verify(hostDeviceDao).setVmIdOnHostDevice(hostDeviceIdCaptor.capture(), vmIdCaptor.capture());

    HostDeviceId capturedDeviceId = hostDeviceIdCaptor.getValue();
    Guid capturedVmId = vmIdCaptor.getValue();

    assertEquals(vf.getId(), capturedDeviceId);
    assertEquals(vmId, capturedVmId);
  }
  @Test
  public void getFreeVfWithExcludedVfs() {
    List<HostDevice> freeVfs = freeVfCommon(5, 2, 2, 2, 2, 2);
    assertEquals(5, freeVfs.size());
    List<HostDevice> excludedVfs = new ArrayList<>();
    excludedVfs.add(freeVfs.get(0));
    excludedVfs.add(freeVfs.get(1));
    freeVfs.removeAll(excludedVfs);

    List<String> excludedVfsNames =
        LinqUtils.transformToList(
            excludedVfs,
            new Function<HostDevice, String>() {

              @Override
              public String eval(HostDevice excludedVf) {
                return excludedVf.getDeviceName();
              }
            });

    assertTrue(freeVfs.contains(networkDeviceHelper.getFreeVf(nic, excludedVfsNames)));
  }
  @Test
  public void testGetVfMap() {
    final HostDevice pfNetDevice = new HostDevice();
    final HostDevice pfPciDevice = new HostDevice();

    final Guid pfNicId = Guid.newGuid();
    final String pfNicName = "pf" + NIC_NAME;
    final String pfPciDeviceName = "pf" + PCI_DEVICE_NAME;

    pfNetDevice.setHostId(HOST_ID);
    pfNetDevice.setDeviceName("pf" + NET_DEVICE_NAME);
    pfNetDevice.setNetworkInterfaceName(pfNicName);
    pfNetDevice.setParentDeviceName(pfPciDeviceName);

    pfPciDevice.setHostId(HOST_ID);
    pfPciDevice.setDeviceName(pfPciDeviceName);
    pfPciDevice.setDeviceName(pfPciDeviceName);

    when(pciDevice.getParentPhysicalFunction()).thenReturn(pfPciDeviceName);
    mockHostDevices(Arrays.asList(pfNetDevice, pfPciDevice, new HostDevice()));

    when(nic.getVlanId()).thenReturn(null);
    final VdsNetworkInterface pfNic = new VdsNetworkInterface();
    pfNic.setId(pfNicId);
    pfNic.setName(pfNetDevice.getNetworkInterfaceName());
    final VdsNetworkInterface bondNic = new VdsNetworkInterface();
    bondNic.setBonded(true);
    final VdsNetworkInterface vlanNic = new VdsNetworkInterface();
    vlanNic.setVlanId(666);
    mockNics(Arrays.asList(pfNic, bondNic, vlanNic), true);

    mockHostSupportsSriov(true);

    final Map<Guid, Guid> actual = networkDeviceHelper.getVfMap(HOST_ID);

    assertEquals(1, actual.size());
    assertThat(actual, hasEntry(NIC_ID, pfNicId));
  }
 @Test
 public void slaveDeviceNonNetworkFree() {
   freeVfCommon(0, 0, 0, 0, 0, 1);
   HostDevice hostDevice = getSingleMockedNonFreeDevice();
   assertFalse(networkDeviceHelper.isDeviceNetworkFree(hostDevice));
 }
 @Test
 public void getPciDeviceNameByNic() {
   assertEquals(PCI_DEVICE_NAME, networkDeviceHelper.getPciDeviceNameByNic(nic));
 }
 @Test
 public void isNetworkDevicePossitive() {
   assertFalse(networkDeviceHelper.isNetworkDevice(pciDevice));
 }
  private void commonIsSriovDevice(boolean isSriov) {
    when(pciDevice.getTotalVirtualFunctions()).thenReturn(isSriov ? TOTAL_NUM_OF_VFS : null);

    assertEquals(isSriov, networkDeviceHelper.isSriovDevice(pciDevice));
  }
 @Test
 public void getFreeVfOneFreeVf() {
   List<HostDevice> freeVfs = freeVfCommon(1, 4, 3, 2, 1, 1);
   assertEquals(1, freeVfs.size());
   assertTrue(freeVfs.contains(networkDeviceHelper.getFreeVf(nic, null)));
 }
 @Test
 public void getFreeVfMoreThanOneFreeVf() {
   List<HostDevice> freeVfs = freeVfCommon(5, 2, 2, 2, 2, 2);
   assertEquals(5, freeVfs.size());
   assertTrue(freeVfs.contains(networkDeviceHelper.getFreeVf(nic, null)));
 }
 @Test
 public void getFreeVfNoVfs() {
   freeVfCommon(0, 0, 0, 0, 0, 0);
   assertNull(networkDeviceHelper.getFreeVf(nic, null));
 }
 @Test
 public void areAllVfsFreeFalseNoNic() {
   freeVfCommon(6, 0, 1, 0, 0, 0);
   assertFalse(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test
 public void areAllVfsFreeFalseHasVfPartOfBond() {
   freeVfCommon(4, 0, 0, 0, 0, 1);
   assertFalse(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test(expected = UnsupportedOperationException.class)
 public void getFreeVfNotSriovNic() {
   commonIsSriovDevice(false);
   networkDeviceHelper.getFreeVf(nic, null);
 }
 @Test
 public void isNetworkDeviceNegtive() {
   assertTrue(networkDeviceHelper.isNetworkDevice(netDevice));
 }
 @Test(expected = UnsupportedOperationException.class)
 public void areAllVfsFreeNotSriovNic() {
   commonIsSriovDevice(false);
   networkDeviceHelper.areAllVfsFree(nic);
 }
 @Test
 public void areAllVfsFreeFalseHasVlanDevice() {
   freeVfCommon(4, 0, 0, 0, 3, 0);
   assertFalse(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test
 public void getNicByNetDeviceValid() {
   mockNics(Collections.<VdsNetworkInterface>emptyList(), true);
   assertEquals(nic, networkDeviceHelper.getNicByPciDevice(pciDevice));
 }
 @Test
 public void getFreeVfNoFreeVf() {
   freeVfCommon(0, 1, 2, 3, 4, 5);
   assertNull(networkDeviceHelper.getFreeVf(nic, null));
 }
 @Test
 public void areAllVfsFreeTrue() {
   freeVfCommon(5, 0, 0, 0, 0, 0);
   assertTrue(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test
 public void areAllVfsFreeFalseAttachedToVm() {
   freeVfCommon(7, 3, 0, 0, 0, 0);
   assertFalse(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test
 public void areAllVfsFreeFalseMix() {
   freeVfCommon(1, 2, 3, 4, 5, 6);
   assertFalse(networkDeviceHelper.areAllVfsFree(nic));
 }
 @Test
 public void getNicByPciDeviceNotParentOfNetDevice() {
   assertNull(networkDeviceHelper.getNicByPciDevice(netDevice));
 }