@Test(expectedExceptions = ConcurrentTaskException.class)
  public void testDeletingAnUploadingImage() throws Throwable {
    TaskEntity task =
        imageBackend.prepareImageUpload(inputStream, "image-name", ImageReplicationType.ON_DEMAND);
    StepEntity step = task.getSteps().get(0);
    ImageEntity image = (ImageEntity) step.getTransientResourceEntities().get(0);

    imageBackend.prepareImageDelete(image.getId());
  }
  @Test(dataProvider = "ImageFileNames")
  public void testPrepareImageUploadImageFileNames(String imageFileName) throws ExternalException {
    TaskEntity task =
        imageBackend.prepareImageUpload(inputStream, imageFileName, ImageReplicationType.ON_DEMAND);
    assertThat(task.getSteps().size(), is(1));

    StepEntity step = task.getSteps().get(0);
    assertThat(step.getTransientResourceEntities().size(), is(1));
    assertThat(step.getOperation(), is(Operation.UPLOAD_IMAGE));
    ImageEntity image = (ImageEntity) step.getTransientResourceEntities().get(0);
    assertThat(image.getState(), is(ImageState.CREATING));
    assertThat(image.getName(), is(imageName));
  }
  @Test
  public void testPrepareImageUploadOnDemand() throws ExternalException {
    TaskEntity task =
        imageBackend.prepareImageUpload(inputStream, "image-name", ImageReplicationType.ON_DEMAND);
    assertThat(task.getSteps().size(), is(1));

    StepEntity step = task.getSteps().get(0);
    assertThat(step.getTransientResourceEntities().size(), is(1));
    assertThat(step.getOperation(), is(Operation.UPLOAD_IMAGE));
    ImageEntity image = (ImageEntity) step.getTransientResourceEntities().get(0);
    assertThat(image.getName(), is("image-name"));
    assertThat(image.getState(), is(ImageState.CREATING));
    assertThat(image.getReplicationType(), is(ImageReplicationType.ON_DEMAND));
    assertThat(
        (InputStream) step.getTransientResource(ImageUploadStepCmd.INPUT_STREAM), is(inputStream));
  }
  @Test
  public void testTombstone() throws Throwable {
    TaskEntity task =
        imageBackend.prepareImageUpload(inputStream, "image-name", ImageReplicationType.ON_DEMAND);
    StepEntity step = task.getSteps().get(0);
    ImageEntity image = (ImageEntity) step.getTransientResourceEntities().get(0);
    assertThat(image.getState(), is(ImageState.CREATING));

    image.setState(ImageState.READY);
    flushSession();

    TenantEntity tenant = new TenantEntity();
    tenant.setName("t1");
    tenantDao.create(tenant);

    ProjectEntity project = new ProjectEntity();
    project.setTenantId(tenant.getId());
    project.setName("staging");
    projectDao.create(project);

    VmEntity vm = new VmEntity();
    vm.setName("vm-1");
    vm.setProjectId(project.getId());
    vm.setImageId(image.getId());
    vmDao.create(vm);

    flushSession();

    imageBackend.tombstone(image);
    assertThat(image.getState(), is(ImageState.PENDING_DELETE));

    vm = vmDao.findById(vm.getId()).get();
    vmDao.delete(vm);
    flushSession();

    imageBackend.tombstone(image);
    try {
      imageBackend.findById(image.getId());
      fail("Image should be deleted in database");
    } catch (ImageNotFoundException e) {
    }
  }
  @BeforeMethod
  public void setUp() {
    taskCommand = mock(TaskCommand.class);
    stepBackend = mock(StepBackend.class);
    hostBackend = mock(HostBackend.class);
    deployerClient = mock(DeployerClient.class);

    hostEntity = new HostEntity();
    hostEntity.setId("host-1");
    hostEntity.setUsername("username");
    hostEntity.setPassword("password");
    hostEntity.setAddress("192.168.0.1");
    hostEntity.setUsageTags(UsageTag.MGMT.toString());

    step = new StepEntity();
    step.setId("step-1");
    step.addResource(hostEntity);

    command = new HostEnterSuspendedModeStepCmd(taskCommand, stepBackend, step, hostBackend);
    when(taskCommand.getDeployerClient()).thenReturn(deployerClient);
  }
    @Test(dataProvider = "vmCreateImageReplicationType")
    public void testPrepareVmCreateImage(ImageReplicationType replicationType) throws Throwable {
      ImageCreateSpec imageCreateSpec = new ImageCreateSpec();
      imageCreateSpec.setName("i1");
      imageCreateSpec.setReplicationType(replicationType);

      TaskEntity task = vmDcpBackend.prepareVmCreateImage(vmId, imageCreateSpec);

      assertThat(task, is(notNullValue()));
      assertThat(task.getState(), is(TaskEntity.State.QUEUED));
      assertThat(task.getSteps().size(), is(2));
      StepEntity step = task.getSteps().get(0);
      assertThat(
          step.getOperation(), is(com.vmware.photon.controller.api.Operation.CREATE_VM_IMAGE));
      assertThat(step.getTransientResourceEntities().size(), is(3));
      assertThat(step.getTransientResourceEntities(Vm.KIND).size(), is(1));
      assertThat(step.getTransientResourceEntities(Image.KIND).size(), is(2));

      assertThat(step.getTransientResourceEntities(Vm.KIND).get(0).getId(), is(vm.getId()));

      ImageEntity image = (ImageEntity) step.getTransientResourceEntities(ImageEntity.KIND).get(0);
      assertThat(image.getName(), is(imageCreateSpec.getName()));
      assertThat(image.getReplicationType(), is(imageCreateSpec.getReplicationType()));
      assertThat(image.getState(), is(ImageState.CREATING));
      assertThat(image.getSize(), is(createdImageState.size));
      assertThat(
          image.getImageSettingsMap(),
          is((Map<String, String>) ImmutableMap.of("n1", "v1", "n2", "v2")));

      ImageEntity vmImage =
          (ImageEntity) step.getTransientResourceEntities(ImageEntity.KIND).get(1);
      assertThat(vmImage.getId(), is(vm.getImageId()));

      step = task.getSteps().get(1);
      assertThat(
          step.getOperation(), is(com.vmware.photon.controller.api.Operation.REPLICATE_IMAGE));
    }