@Test(dependsOnMethods = "testDetachNics")
  public void testSetDefaultGateway() {
    PublicNetwork network = publicIpCloud.getNetwork();
    env.virtualMachine.setGatewayNetwork(network);

    Integer configId = env.virtualMachine.unwrap().getIdFromLink("network_configuration");
    assertEquals(configId, network.getId());
  }
  public void testAttachPublicIp() {
    AsyncTask task = env.virtualMachine.setNics(Lists.<Ip<?, ?>>newArrayList(publicIpCloud));
    assertNull(task);

    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 1);
    assertEquals(nics.get(0).getId(), publicIpCloud.getId());
  }
  // TODO: Infrastructure edit link for public ips can not be used to attach
  @Test(enabled = false)
  public void testAttachInfrastructurePublicIp() {
    AsyncTask task =
        env.virtualMachine.setNics(Lists.<Ip<?, ?>>newArrayList(publicIpInfrastructure));
    assertNull(task);

    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 1);
    assertEquals(nics.get(0).getId(), publicIpInfrastructure.getId());
  }
  @Test(dependsOnMethods = "testReorderNics")
  public void testDetachNics() {
    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();

    AsyncTask task =
        env.virtualMachine.setNics(Lists.<Ip<?, ?>>newArrayList(nics.get(1), nics.get(2)));
    assertNull(task);

    nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 2);
    assertEquals(nics.get(0).getId(), privateIp.getId());
    assertEquals(nics.get(1).getId(), publicIpCloud.getId());
  }
  @Test(dependsOnMethods = "testAttachPublicIp")
  public void testAttachPrivateIp() {
    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();
    nics.add(privateIp);

    AsyncTask task = env.virtualMachine.setNics(nics);
    assertNull(task);

    nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 2);
    assertEquals(nics.get(0).getId(), publicIpCloud.getId());
    assertEquals(nics.get(1).getId(), privateIp.getId());
  }
  @AfterClass
  public void restorePrivateIp() {
    AsyncTask task = env.virtualMachine.setNics(Lists.<Ip<?, ?>>newArrayList(privateIp));
    assertNull(task);

    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 1);
    assertEquals(nics.get(0).getId(), privateIp.getId());

    String address = publicIpCloud.getIp();
    env.virtualDatacenter.releaseePublicIp(publicIpCloud);
    assertNull(
        env.virtualDatacenter.findPurchasedPublicIp(IpPredicates.<PublicIp>address(address)));
  }
  @BeforeClass
  public void setupIps() {
    privateIp = env.privateNetwork.listUnusedIps().get(0);
    assertNotNull(privateIp);

    externalIp = env.externalNetwork.listUnusedIps().get(0);
    assertNotNull(externalIp);

    publicIpInfrastructure = env.virtualDatacenter.listAvailablePublicIps().get(0);
    env.virtualDatacenter.purchasePublicIp(publicIpInfrastructure);

    publicIpCloud =
        env.virtualDatacenter.findPurchasedPublicIp(
            IpPredicates.<PublicIp>address(publicIpInfrastructure.getIp()));
    assertNotNull(publicIpCloud);
  }
  @Test(dependsOnMethods = "testAddUnmanagedNics")
  public void testReorderNics() {
    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();

    AsyncTask task =
        env.virtualMachine.setNics(
            Lists.<Ip<?, ?>>newArrayList(
                nics.get(2), nics.get(1), nics.get(0), nics.get(4), nics.get(3)));
    assertNull(task);

    nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 5);
    assertEquals(nics.get(0).getId(), externalIp.getId());
    assertEquals(nics.get(1).getId(), privateIp.getId());
    assertEquals(nics.get(2).getId(), publicIpCloud.getId());
    assertEquals(nics.get(3).getId(), unmanagedIp2.getId());
    assertEquals(nics.get(4).getId(), unmanagedIp1.getId());
  }
  @Test(dependsOnMethods = "testAttachExternalIp")
  public void testAddUnmanagedNics() {
    List<Ip<?, ?>> nics = env.virtualMachine.listAttachedNics();

    AsyncTask task =
        env.virtualMachine.setNics(
            nics, Lists.<UnmanagedNetwork>newArrayList(env.unmanagedNetwork, env.unmanagedNetwork));
    assertNull(task);

    nics = env.virtualMachine.listAttachedNics();
    assertEquals(nics.size(), 5);
    assertEquals(nics.get(0).getId(), publicIpCloud.getId());
    assertEquals(nics.get(1).getId(), privateIp.getId());
    assertEquals(nics.get(2).getId(), externalIp.getId());
    // Unmanaged ips are created during the attach.
    assertEquals(nics.get(3).getNetworkName(), env.unmanagedNetwork.getName());
    assertEquals(nics.get(4).getNetworkName(), env.unmanagedNetwork.getName());

    unmanagedIp1 = (UnmanagedIp) nics.get(3);
    unmanagedIp2 = (UnmanagedIp) nics.get(4);
  }