@Test(
      expectedExceptions =
          com.vmware.photon.controller.api.frontend.exceptions.external.VmNotFoundException.class)
  public void testFailedAttachDiskVmNotFound() throws Exception {
    when(hostClient.attachDisks(vmId, attachedDiskIds)).thenThrow(VmNotFoundException.class);

    VmDiskOpStepCmd command = getVmDiskOpStepCmd(Operation.ATTACH_DISK);
    vm.setAgent("some-agent");
    vm.setHost("0.0.0.0");
    command.execute();
  }
  @Test
  public void testSuccessfulDetachOperation() throws Exception {
    when(hostClient.detachDisks(vmId, attachedDiskIds)).thenReturn(SUCCESSFUL_VM_DISKOP_RESPONSE);

    VmDiskOpStepCmd command = getVmDiskOpStepCmd(Operation.DETACH_DISK);
    vm.setAgent("some-agent");
    command.execute();
  }
  @Test(expectedExceptions = RpcException.class)
  public void testFailedDetachOperationDiskDetached() throws Exception {
    when(hostClient.detachDisks(vmId, attachedDiskIds))
        .thenReturn(DISK_DETACHED_VM_DISKOP_RESPONSE);

    VmDiskOpStepCmd command = getVmDiskOpStepCmd(Operation.DETACH_DISK);
    vm.setAgent("some-agent");
    command.execute();
  }
  @Test
  public void testSuccessfulAttachOperation() throws Exception {
    when(hostClient.attachDisks(vmId, attachedDiskIds)).thenReturn(SUCCESSFUL_VM_DISKOP_RESPONSE);

    VmDiskOpStepCmd command = getVmDiskOpStepCmd(Operation.ATTACH_DISK);
    vm.setAgent("some-agent");
    command.execute();

    verify(hostClient).attachDisks(vmId, attachedDiskIds);
    verify(diskBackend).updateState(disk1, DiskState.ATTACHED);
    verify(diskBackend).updateState(disk2, DiskState.ATTACHED);
    verify(attachedDiskBackend).attachDisks(vm, persistentDiskEntities);
  }
  @Test
  public void testSuccessfulDetachDiskNotFoundOperation() throws Exception {
    when(hostClient.detachDisks(vmId, attachedDiskIds))
        .thenReturn(DISK_NOT_FOUND_VM_DISKOP_RESPONSE);

    VmDiskOpStepCmd command = getVmDiskOpStepCmd(Operation.DETACH_DISK);
    vm.setAgent("some-agent");
    command.execute();

    verify(hostClient).detachDisks(vmId, attachedDiskIds);
    verify(diskBackend).updateState(disk1, DiskState.ERROR);
    verify(diskBackend).updateState(disk2, DiskState.DETACHED);
    verify(attachedDiskBackend).deleteAttachedDisks(vm, persistentDiskEntities);
  }
  @BeforeMethod
  public void setUp() throws Exception, DocumentNotFoundException {
    attachedDiskIds = new ArrayList<>();
    attachedDiskEntities = new ArrayList<>();
    persistentDiskEntities = new ArrayList<>();
    baseDiskEntities = new ArrayList<>();

    FlavorEntity vmFlavorEntity = new FlavorEntity();
    vmFlavorEntity.setName("vm-100");
    vmFlavorEntity.setKind(Vm.KIND);
    FlavorEntity diskFlavorEntity = new FlavorEntity();
    diskFlavorEntity.setName("core-100");
    diskFlavorEntity.setKind(PersistentDisk.KIND);

    task = new TaskEntity();
    task.setId("task-1");

    vm = new VmEntity();
    vm.setName("vm-name-1");
    vm.setId(vmId);
    vm.setFlavorId(vmFlavorEntity.getId());
    vm.setState(VmState.STOPPED);

    disk1 = new PersistentDiskEntity();
    disk1.setName(diskName1);
    disk1.setId(diskId1);
    disk1.setFlavorId(diskFlavorEntity.getId());
    disk1.setCapacityGb(1);
    persistentDiskEntities.add(disk1);
    baseDiskEntities.add(disk1);

    disk2 = new PersistentDiskEntity();
    disk2.setName(diskName2);
    disk2.setId(diskId2);
    disk2.setFlavorId(diskFlavorEntity.getId());
    disk2.setCapacityGb(2);
    persistentDiskEntities.add(disk2);
    baseDiskEntities.add(disk2);

    // String id, String name, String kind, String flavor, String state
    attachedDisk1 =
        AttachedDisk.create(disk1.getId(), disk1.getName(), disk1.getKind(), "core-100", 1, false);
    attachedDisk2 =
        AttachedDisk.create(disk2.getId(), disk2.getName(), disk2.getKind(), "core-100", 2, false);
    attachedDiskIds.add(attachedDisk1.getId());
    attachedDiskIds.add(attachedDisk2.getId());

    attachedDiskEntity1 = new AttachedDiskEntity();
    attachedDiskEntity1.setId(attachedDiskId1);
    attachedDiskEntities.add(attachedDiskEntity1);

    attachedDiskEntity2 = new AttachedDiskEntity();
    attachedDiskEntity2.setId(attachedDiskId2);
    attachedDiskEntities.add(attachedDiskEntity2);

    Datastore datastore = new Datastore();
    datastore.setId("datastore-id");

    when(diskBackend.find(PersistentDisk.KIND, diskId1)).thenReturn(disk1);
    when(diskBackend.find(PersistentDisk.KIND, diskId2)).thenReturn(disk2);

    taskCommand =
        spy(
            new TaskCommand(
                xenonClient,
                photonControllerXenonRestClient,
                hostClient,
                housekeeperClient,
                deployerClient,
                deployerXenonClient,
                housekeeperXenonClient,
                entityLockBackend,
                task));
    when(taskCommand.getHostClient()).thenReturn(hostClient);
    when(taskCommand.getPhotonControllerXenonRestClient())
        .thenReturn(photonControllerXenonRestClient);
    when(vmBackend.findById(vmId)).thenReturn(vm);
    when(diskBackend.find(PersistentDisk.KIND, diskId1)).thenReturn(disk1);
    when(diskBackend.find(PersistentDisk.KIND, diskId2)).thenReturn(disk2);

    when(attachedDiskBackend.findAttachedDisk(disk1)).thenReturn(attachedDiskEntity1);
    when(attachedDiskBackend.findAttachedDisk(disk2)).thenReturn(attachedDiskEntity2);
    when(taskCommand.getHostClient()).thenReturn(hostClient);
    HostService.State hostServiceState = new HostService.State();
    hostServiceState.hostAddress = "host-ip";
    when(hostServiceOp.getBody(Matchers.any())).thenReturn(hostServiceState);
    when(xenonClient.get(Matchers.startsWith(HostServiceFactory.SELF_LINK)))
        .thenReturn(hostServiceOp);
  }