@Test
  public void testUpdateSettings() throws Throwable {
    ImageEntity imageEntity = new ImageEntity();
    imageEntity.setName("image-name");
    imageEntity.setSize(1000L);
    imageEntity.setState(ImageState.READY);

    imageDao.create(imageEntity);
    String imageId = imageEntity.getId();

    Map<String, String> imageSettings = new HashMap<>();
    imageSettings.put("property-1", "value-1");
    imageSettings.put("property-2", null);

    imageBackend.updateSettings(imageEntity, imageSettings);

    List<ImageSettingsEntity> imageSettingsEntityList = imageSettingsDao.listAll();
    assertThat(imageSettingsEntityList.size(), is(2));

    ImageSettingsEntity settings1 = imageSettingsEntityList.get(0);
    assertThat(settings1.getImage().getId(), is(imageId));
    assertThat(settings1.getName(), is("property-1"));
    assertThat(settings1.getDefaultValue(), is("value-1"));

    ImageSettingsEntity settings2 = imageSettingsEntityList.get(1);
    assertThat(settings2.getImage().getId(), is(imageId));
    assertThat(settings2.getName(), is("property-2"));
    assertThat(settings2.getDefaultValue(), is(""));

    assertThat(imageDao.listAll().size(), is(1));
    assertThat(imageDao.listAll().get(0).getImageSettings().size(), is(2));
  }
  @Test(expectedExceptions = InvalidImageStateException.class)
  public void testPrepareImageDeleteInPendingDelete() throws Throwable {
    ImageEntity imageInDb = new ImageEntity();
    imageInDb.setName("image-0");
    imageInDb.setState(ImageState.PENDING_DELETE);
    imageInDb.setSize(1000L);

    imageDao.create(imageInDb);
    imageBackend.prepareImageDelete(imageInDb.getId());
  }
  @Test
  public void testPrepareImageDelete() throws Throwable {
    ImageEntity imageInDb = new ImageEntity();
    imageInDb.setName("image-0");
    imageInDb.setState(ImageState.READY);
    imageInDb.setSize(1000L);

    imageDao.create(imageInDb);
    TaskEntity taskDelete = imageBackend.prepareImageDelete(imageInDb.getId());
    assertThat(taskDelete.getSteps().size(), is(2));
    assertThat(taskDelete.getSteps().get(0).getOperation(), is(Operation.DELETE_IMAGE));
    assertThat(taskDelete.getSteps().get(1).getOperation(), is(Operation.DELETE_IMAGE_REPLICAS));
  }
  @Test
  public void testGetTasksWithGivenState() throws Exception {
    ImageEntity imageInDb = new ImageEntity();
    imageInDb.setName("image-0");
    imageInDb.setState(ImageState.READY);
    imageInDb.setSize(1000L);

    imageDao.create(imageInDb);
    TaskEntity taskDelete = imageBackend.prepareImageDelete(imageInDb.getId());

    List<Task> tasks = imageBackend.getTasks(imageInDb.getId(), Optional.of("FINISHED"));

    assertThat(tasks.size(), is(0));
  }
  @Test
  public void testGetTasks() throws Exception {
    ImageEntity imageInDb = new ImageEntity();
    imageInDb.setName("image-0");
    imageInDb.setState(ImageState.READY);
    imageInDb.setSize(1000L);

    imageDao.create(imageInDb);
    TaskEntity taskDelete = imageBackend.prepareImageDelete(imageInDb.getId());

    List<Task> tasks = imageBackend.getTasks(imageInDb.getId(), Optional.<String>absent());

    assertThat(tasks.size(), is(1));
    assertThat(tasks.get(0).getState(), is("QUEUED"));
  }