@Test
    public void testPrepareVmDiskOperation() throws Throwable {
      com.vmware.photon.controller.api.Operation operation =
          com.vmware.photon.controller.api.Operation.DETACH_DISK;

      DiskService.State diskState = new DiskService.State();
      diskState.name = "test-vm-disk-1";
      diskState.projectId = projectId;
      diskState.flavorId =
          flavorDcpBackend.getEntityByNameAndKind("core-100", PersistentDisk.KIND).getId();
      diskState.capacityGb = 64;
      diskState.diskType = DiskType.PERSISTENT;
      diskState.state = DiskState.ATTACHED;

      Operation result = dcpClient.post(DiskServiceFactory.SELF_LINK, diskState);
      DiskService.State createdDiskState = result.getBody(DiskService.State.class);
      String diskId = ServiceUtils.getIDFromDocumentSelfLink(createdDiskState.documentSelfLink);

      List<String> disks = new ArrayList<>();
      disks.add(diskId);
      TaskEntity task = vmDcpBackend.prepareVmDiskOperation(vmId, disks, operation);

      assertThat(task, is(notNullValue()));
      assertThat(task.getState(), is(TaskEntity.State.QUEUED));
      assertThat(task.getSteps().size(), is(1));
      assertThat(task.getSteps().get(0).getOperation(), is(operation));

      try {
        vmDcpBackend.prepareVmDiskOperation(
            vmId, disks, com.vmware.photon.controller.api.Operation.MOCK_OP);
        fail("vmDcpBackend.prepareVmOperation with invalid operation should have failed");
      } catch (NotImplementedException e) {
        // do nothing
      }
    }
    @BeforeMethod
    public void setUp() throws Throwable {

      commonHostAndClientSetup(basicServiceHost, apiFeDcpRestClient);
      commonDataSetup(
          tenantDcpBackend,
          resourceTicketDcpBackend,
          projectDcpBackend,
          flavorDcpBackend,
          flavorLoader);

      vm = new VmService.State();
      vm.name = UUID.randomUUID().toString();
      flavorEntity = flavorDcpBackend.getEntityByNameAndKind("core-100", Vm.KIND);
      vm.flavorId = flavorEntity.getId();
      vm.imageId = UUID.randomUUID().toString();
      vm.projectId = projectId;
      vm.vmState = VmState.CREATING;

      vm.affinities = new ArrayList<>();
      vm.affinities.add(new LocalitySpec("id1", "kind1"));

      Iso iso = new Iso();
      iso.setName(UUID.randomUUID().toString());
      iso.setSize(-1L);
      vm.isos = new ArrayList<>();
      vm.isos.add(iso);

      vm.metadata = new HashMap<>();
      vm.metadata.put("key1", UUID.randomUUID().toString());

      vm.networks = new ArrayList<>();
      vm.networks.add(UUID.randomUUID().toString());
      vm.agent = UUID.randomUUID().toString();
      vm.host = UUID.randomUUID().toString();
      vm.datastore = UUID.randomUUID().toString();
      vm.datastoreName = UUID.randomUUID().toString();

      vm.tags = new HashSet<>();
      vm.tags.add("namespace1:predicate1=value1");
      vm.tags.add("namespace2:predicate2=value2");

      Operation result = dcpClient.post(VmServiceFactory.SELF_LINK, vm);
      createdVm = result.getBody(VmService.State.class);
      vmId = ServiceUtils.getIDFromDocumentSelfLink(createdVm.documentSelfLink);

      DeploymentCreateSpec deploymentCreateSpec = new DeploymentCreateSpec();
      deploymentCreateSpec.setImageDatastores(Collections.singleton(UUID.randomUUID().toString()));
      TaskEntity deploymentTask = deploymentBackend.prepareCreateDeployment(deploymentCreateSpec);

      HostCreateSpec hostCreateSpec = new HostCreateSpec();
      hostCreateSpec.setAddress(vm.host);
      hostCreateSpec.setUsageTags(ImmutableList.of(UsageTag.CLOUD));
      hostCreateSpec.setUsername(UUID.randomUUID().toString());
      hostCreateSpec.setPassword(UUID.randomUUID().toString());
      TaskEntity hostTask =
          hostBackend.prepareHostCreate(hostCreateSpec, deploymentTask.getEntityId());
      hostId = hostTask.getEntityId();
    }
  /**
   * This method performs document state updates in response to an operation which sets the state to
   * STARTED.
   *
   * @param currentState Supplies the current state object.
   * @param vmState Supplies the state object of the VmService entity.
   * @param hostState Supplies the state object of the HostService entity.
   * @param projectState Supplies the state object of the ProjectService entity.
   * @param imageState Supplies the state object of the Image Service entity.
   * @param projectState Supplies the state object of the Project Service entity.
   * @param vmFlavorState Supplies the state object of the FlavorService entity.
   * @param diskFlavorState Supplies the state object of the FlavorService entity.
   */
  private void processStartedStage(
      final State currentState,
      final VmService.State vmState,
      final HostService.State hostState,
      final ImageService.State imageState,
      final ProjectService.State projectState,
      final FlavorService.State vmFlavorState,
      final FlavorService.State diskFlavorState)
      throws IOException {

    final Service service = this;

    FutureCallback<CreateVmTaskService.State> callback =
        new FutureCallback<CreateVmTaskService.State>() {
          @Override
          public void onSuccess(@Nullable CreateVmTaskService.State result) {
            switch (result.taskState.stage) {
              case FINISHED:
                updateVmId(currentState, result.vmId);
                break;
              case CANCELLED:
                TaskUtils.sendSelfPatch(
                    service,
                    buildPatch(TaskState.TaskStage.CANCELLED, (ServiceErrorResponse) null));
                break;
              case FAILED:
                ServiceErrorResponse failed = new ServiceErrorResponse();
                failed.message =
                    String.format(
                        "CreateVmTaskService failed to create the vm. documentSelfLink: %s. failureReason: %s",
                        result.documentSelfLink, result.taskState.failure.message);
                TaskUtils.sendSelfPatch(service, buildPatch(TaskState.TaskStage.FAILED, failed));
                break;
            }
          }

          @Override
          public void onFailure(Throwable t) {
            failTask(t);
          }
        };

    CreateVmTaskService.State startState = new CreateVmTaskService.State();
    startState.projectId = ServiceUtils.getIDFromDocumentSelfLink(projectState.documentSelfLink);
    startState.vmCreateSpec =
        composeVmCreateSpec(vmState, hostState, imageState, vmFlavorState, diskFlavorState);
    startState.queryCreateVmTaskInterval = currentState.queryCreateVmTaskInterval;

    TaskUtils.startTaskAsync(
        this,
        CreateVmTaskFactoryService.SELF_LINK,
        startState,
        (state) -> TaskUtils.finalTaskStages.contains(state.taskState.stage),
        CreateVmTaskService.State.class,
        HostUtils.getDeployerContext(this).getTaskPollDelay(),
        callback);
  }
  /**
   * This method creates a VmCreateSpec object for creating an VM.
   *
   * @param vmState Supplies the state object of the VmService entity.
   * @param hostState Supplies the state object of the HostService entity.
   * @param imageState Supplies the state object of the ImageService entity.
   * @param vmFlavorState Supplies the state object of the FlavorService entity.
   * @param diskFlavorState Supplies the state object of the FlavorService entity.
   * @return Returns the VmCreateSpec object.
   */
  private VmCreateSpec composeVmCreateSpec(
      final VmService.State vmState,
      final HostService.State hostState,
      final ImageService.State imageState,
      final FlavorService.State vmFlavorState,
      final FlavorService.State diskFlavorState) {

    // Craft the VM creation spec.
    VmCreateSpec spec = new VmCreateSpec();
    spec.setName(vmState.name);
    spec.setFlavor(vmFlavorState.name);

    spec.setSourceImageId(ServiceUtils.getIDFromDocumentSelfLink(imageState.documentSelfLink));

    List<AttachedDiskCreateSpec> attachedDisks = new ArrayList<>();

    AttachedDiskCreateSpec bootDisk = new AttachedDiskCreateSpec();
    bootDisk.setName(vmState.name + "-bootdisk");
    bootDisk.setBootDisk(true);
    bootDisk.setFlavor(diskFlavorState.name);
    bootDisk.setKind(EphemeralDisk.KIND);
    attachedDisks.add(bootDisk);

    spec.setAttachedDisks(attachedDisks);

    Map<String, String> environment = new HashMap<>();
    spec.setEnvironment(environment);

    List<LocalitySpec> affinities = new ArrayList<>();

    LocalitySpec hostSpec = new LocalitySpec();
    hostSpec.setId(hostState.hostAddress);
    hostSpec.setKind("host");
    affinities.add(hostSpec);

    LocalitySpec datastoreSpec = new LocalitySpec();
    datastoreSpec.setId(
        hostState.metadata.get(HostService.State.METADATA_KEY_NAME_MANAGEMENT_DATASTORE));
    datastoreSpec.setKind("datastore");
    affinities.add(datastoreSpec);

    LocalitySpec portGroupSpec = new LocalitySpec();
    portGroupSpec.setId(
        hostState.metadata.get(HostService.State.METADATA_KEY_NAME_MANAGEMENT_PORTGROUP));
    portGroupSpec.setKind("portGroup");
    affinities.add(portGroupSpec);

    spec.setAffinities(affinities);

    return spec;
  }
    private void createTestEnvironment() throws Throwable {
      ZookeeperClientFactory zkFactory = mock(ZookeeperClientFactory.class);
      sourceEnvironment =
          new TestEnvironment.Builder()
              .listeningExecutorService(listeningExecutorService)
              .apiClientFactory(apiClientFactory)
              .cloudServerSet(sourceCloudStore.getServerSet())
              .hostCount(1)
              .build();

      destinationEnvironment =
          new TestEnvironment.Builder()
              .hostCount(1)
              .apiClientFactory(apiClientFactory)
              .cloudServerSet(destinationCloudStore.getServerSet())
              .httpFileServiceClientFactory(httpFileServiceClientFactory)
              .zookeeperServersetBuilderFactory(zkFactory)
              .build();

      ZookeeperClient zkBuilder = mock(ZookeeperClient.class);
      doReturn(zkBuilder).when(zkFactory).create();
      doReturn(
              Collections.singleton(
                  new InetSocketAddress(
                      "127.0.0.1", sourceEnvironment.getHosts()[0].getState().httpPort)))
          .when(zkBuilder)
          .getServers(Matchers.startsWith("127.0.0.1:2181"), eq("cloudstore"));

      ServiceHost sourceHost = sourceEnvironment.getHosts()[0];
      startState.sourceLoadBalancerAddress = sourceHost.getPublicUri().toString();

      TestHelper.createHostService(sourceCloudStore, Collections.singleton(UsageTag.MGMT.name()));
      TestHelper.createHostService(sourceCloudStore, Collections.singleton(UsageTag.CLOUD.name()));
      DeploymentService.State deploymentService =
          TestHelper.createDeploymentService(destinationCloudStore);
      startState.destinationDeploymentId =
          ServiceUtils.getIDFromDocumentSelfLink(deploymentService.documentSelfLink);
    }
  private static void commonVmAndImageSetup(
      VmDcpBackend vmDcpBackend, NetworkDcpBackend networkDcpBackend) throws Throwable {
    AttachedDiskCreateSpec disk1 =
        new AttachedDiskCreateSpecBuilder().name("disk1").flavor("core-100").bootDisk(true).build();
    AttachedDiskCreateSpec disk2 =
        new AttachedDiskCreateSpecBuilder().name("disk2").flavor("core-200").capacityGb(10).build();

    List<LocalitySpec> affinities = new ArrayList<>();
    affinities.add(new LocalitySpec("disk-id1", "disk"));
    affinities.add(new LocalitySpec("disk-id2", "disk"));

    ImageService.State imageServiceState = new ImageService.State();
    imageServiceState.name = "image-1";
    imageServiceState.state = ImageState.READY;
    imageServiceState.size = 1024L * 1024L;
    imageServiceState.replicationType = ImageReplicationType.EAGER;
    imageServiceState.imageSettings = new ArrayList<>();
    ImageService.State.ImageSetting imageSetting = new ImageService.State.ImageSetting();
    imageSetting.name = "n1";
    imageSetting.defaultValue = "v1";
    imageServiceState.imageSettings.add(imageSetting);
    imageSetting = new ImageService.State.ImageSetting();
    imageSetting.name = "n2";
    imageSetting.defaultValue = "v2";
    imageServiceState.imageSettings.add(imageSetting);

    Operation result = dcpClient.post(ImageServiceFactory.SELF_LINK, imageServiceState);

    createdImageState = result.getBody(ImageService.State.class);

    imageId = ServiceUtils.getIDFromDocumentSelfLink(createdImageState.documentSelfLink);

    vmCreateSpec = new VmCreateSpec();
    vmCreateSpec.setName("test-vm");
    vmCreateSpec.setFlavor("core-100");
    vmCreateSpec.setSourceImageId(imageId);
    vmCreateSpec.setAttachedDisks(ImmutableList.of(disk1, disk2));
    vmCreateSpec.setAffinities(affinities);
    vmCreateSpec.setTags(ImmutableSet.of("value1", "value2"));

    List<String> networks = new ArrayList<>();

    List<String> portGroups = new ArrayList<>();
    portGroups.add("p1");
    NetworkCreateSpec networkCreateSpec = new NetworkCreateSpec();
    networkCreateSpec.setName("n1");
    networkCreateSpec.setPortGroups(portGroups);
    TaskEntity networkTask = networkDcpBackend.createNetwork(networkCreateSpec);
    networks.add(networkTask.getEntityId());

    portGroups = new ArrayList<>();
    portGroups.add("p2");
    networkCreateSpec.setName("n2");
    networkCreateSpec.setPortGroups(portGroups);
    networkTask = networkDcpBackend.createNetwork(networkCreateSpec);
    networks.add(networkTask.getEntityId());

    vmCreateSpec.setNetworks(networks);

    createdVmTaskEntity = vmDcpBackend.prepareVmCreate(projectId, vmCreateSpec);
  }