/** Test of blobExists method, of class FilesystemAsyncBlobStore. */
  public void testBlobExists() throws IOException {
    boolean result;
    String blobKey;

    // when location doesn't exists
    blobKey = TestUtils.createRandomBlobKey();
    try {
      blobStore.blobExists(CONTAINER_NAME, blobKey);
      fail();
    } catch (ContainerNotFoundException cnfe) {
      // expected
    }

    // when location exists
    blobStore.createContainerInLocation(null, CONTAINER_NAME);
    result = blobStore.blobExists(CONTAINER_NAME, blobKey);
    assertFalse(result, "Blob exists");

    // create blob
    TestUtils.createBlobAsFile(CONTAINER_NAME, blobKey, TestUtils.getImageForBlobPayload());
    result = blobStore.blobExists(CONTAINER_NAME, blobKey);
    assertTrue(result, "Blob doesn't exist");

    // complex path test
    blobKey = TestUtils.createRandomBlobKey("ss/asdas/", "");
    result = blobStore.blobExists(CONTAINER_NAME, blobKey);
    assertFalse(result, "Blob exists");
    TestUtils.createBlobAsFile(CONTAINER_NAME, blobKey, TestUtils.getImageForBlobPayload());
    result = blobStore.blobExists(CONTAINER_NAME, blobKey);
    assertTrue(result, "Blob doesn't exist");
  }
  /** Test of removeBlob method, with only one blob with a complex path as key */
  @Test
  public void testRemoveBlob_ComplexBlobKey() throws IOException {
    final String BLOB_KEY = TestUtils.createRandomBlobKey("aa/bb/cc/dd/", null);
    boolean result;

    // checks that blob doesn't exists
    blobStore.createContainerInLocation(null, CONTAINER_NAME);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertFalse(result, "Blob exists");
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY, false);

    // create the blob
    TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertTrue(result, "Blob doesn't exist");

    // remove it
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertFalse(result, "Blob still exists");
    // file removed
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY, false);
    // also the entire directory structure was removed
    TestUtils.directoryExists(TARGET_CONTAINER_NAME + "/aa", false);
  }
  public void testRemoveBlob_TwoSimpleBlobKeys() throws IOException {
    final String BLOB_KEY1 = TestUtils.createRandomBlobKey(null, null);
    final String BLOB_KEY2 = TestUtils.createRandomBlobKey(null, null);
    boolean result;

    // create the container and checks that blob doesn't exists
    blobStore.createContainerInLocation(null, CONTAINER_NAME);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertFalse(result, "Blob1 exists");
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertFalse(result, "Blob2 exists");

    // create the blob
    TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY1, BLOB_KEY2);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertTrue(result, "Blob " + BLOB_KEY1 + " doesn't exist");
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertTrue(result, "Blob " + BLOB_KEY2 + " doesn't exist");

    // remove first blob
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY1);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertFalse(result, "Blob1 still exists");
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertTrue(result, "Blob2 doesn't exist");
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY1, false);
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY2, true);
    // remove second blob
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY2);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertFalse(result, "Blob2 still exists");
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY2, false);
  }
  public void testRemoveBlob_SimpleBlobKey() throws IOException {
    final String BLOB_KEY = TestUtils.createRandomBlobKey(null, ".txt");
    boolean result;

    blobStore.createContainerInLocation(null, CONTAINER_NAME);

    // checks that blob doesn't exists
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertFalse(result, "Blob exists");

    // create the blob
    TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertTrue(result, "Blob exists");

    // remove it
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY);
    assertFalse(result, "Blob still exists");
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY, false);
  }
  /**
   * Test of removeBlob method, with two blobs with a complex path as key and when first blob is
   * removed, not all of its key's path is removed, because it is shared with the second blob's key
   */
  @Test
  public void testRemoveBlob_TwoComplexBlobKeys() throws IOException {
    final String BLOB_KEY1 = TestUtils.createRandomBlobKey("aa/bb/cc/dd/", null);
    final String BLOB_KEY2 = TestUtils.createRandomBlobKey("aa/bb/ee/ff/", null);
    boolean result;

    blobStore.createContainerInLocation(null, CONTAINER_NAME);

    // checks that blob doesn't exist
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertFalse(result, "Blob1 exists");
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertFalse(result, "Blob2 exists");

    // create the blobs
    TestUtils.createBlobsInContainer(CONTAINER_NAME, BLOB_KEY1, BLOB_KEY2);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertTrue(result, "Blob " + BLOB_KEY1 + " doesn't exist");
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertTrue(result, "Blob " + BLOB_KEY2 + " doesn't exist");

    // remove first blob
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY1);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY1);
    assertFalse(result, "Blob still exists");
    // first file deleted, not the second
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY1, false);
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY2, true);
    // only partial directory structure was removed, because it shares a path
    // with the second blob created
    TestUtils.directoryExists(TARGET_CONTAINER_NAME + "/aa/bb/cc/dd", false);
    TestUtils.directoryExists(TARGET_CONTAINER_NAME + "/aa/bb", true);
    // remove second blob
    blobStore.removeBlob(CONTAINER_NAME, BLOB_KEY2);
    result = blobStore.blobExists(CONTAINER_NAME, BLOB_KEY2);
    assertFalse(result, "Blob still exists");
    TestUtils.fileExists(TARGET_CONTAINER_NAME + File.separator + BLOB_KEY2, false);
    // now all the directory structure is empty
    TestUtils.directoryExists(TARGET_CONTAINER_NAME + "/aa", false);
  }