/**
   * Create a snapshot
   *
   * @throws StorageException
   * @throws URISyntaxException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testPageBlobSnapshotValidationTest()
      throws StorageException, URISyntaxException, IOException {
    final int length = 1024;
    CloudPageBlob blockBlobRef =
        (CloudPageBlob)
            BlobTestHelper.uploadNewBlob(
                this.container, BlobType.PAGE_BLOB, "originalBlob", length, null);
    final CloudBlob blobSnapshot = blockBlobRef.createSnapshot();

    for (ListBlobItem blob :
        this.container.listBlobs(null, true, EnumSet.allOf(BlobListingDetails.class), null, null)) {
      final ByteArrayOutputStream outStream = new ByteArrayOutputStream(length);
      ((CloudBlob) blob).download(outStream);
    }

    ByteArrayOutputStream outStream = new ByteArrayOutputStream(length);

    blobSnapshot.download(outStream);
    byte[] retrievedBuff = outStream.toByteArray();
    assertEquals(length, retrievedBuff.length);

    // Read operation should work fine.
    blobSnapshot.downloadAttributes();

    final CloudPageBlob blobSnapshotUsingRootUri =
        this.container.getPageBlobReference(blockBlobRef.getName(), blobSnapshot.getSnapshotID());
    outStream = new ByteArrayOutputStream(length);

    blobSnapshotUsingRootUri.download(outStream);
    retrievedBuff = outStream.toByteArray();
    assertEquals(length, retrievedBuff.length);
    assertEquals(blobSnapshot.getSnapshotID(), blobSnapshotUsingRootUri.getSnapshotID());

    // Expect an IllegalArgumentException from upload.
    try {
      final Random randGenerator = new Random();
      final byte[] buff = new byte[length];
      randGenerator.nextBytes(buff);
      blobSnapshot.upload(new ByteArrayInputStream(buff), -1);
      fail("Expect an IllegalArgumentException from upload");
    } catch (IllegalArgumentException e) {
      assertEquals(
          "Cannot perform this operation on a blob representing a snapshot.", e.getMessage());
    }

    // Expect an IllegalArgumentException from uploadMetadata.
    try {
      blobSnapshot.uploadMetadata();
      fail("Expect an IllegalArgumentException from uploadMetadata");
    } catch (IllegalArgumentException e) {
      assertEquals(
          "Cannot perform this operation on a blob representing a snapshot.", e.getMessage());
    }

    // Expect an IllegalArgumentException from uploadProperties.
    try {
      blobSnapshot.uploadProperties();
      fail("Expect an IllegalArgumentException from uploadProperties");
    } catch (IllegalArgumentException e) {
      assertEquals(
          "Cannot perform this operation on a blob representing a snapshot.", e.getMessage());
    }

    // Expect an IllegalArgumentException from createSnapshot.
    try {
      blobSnapshot.createSnapshot();
      fail("Expect an IllegalArgumentException from createSnapshot");
    } catch (IllegalArgumentException e) {
      assertEquals(
          "Cannot perform this operation on a blob representing a snapshot.", e.getMessage());
    }
  }
  @Test
  public void testPageBlobCopyFromSnapshot()
      throws StorageException, IOException, URISyntaxException, InterruptedException {
    CloudPageBlob source = this.container.getPageBlobReference("source");

    byte[] buffer = BlobTestHelper.getRandomBuffer(512);
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);

    source.upload(stream, buffer.length);

    source.getMetadata().put("Test", "value");
    source.uploadMetadata();

    CloudPageBlob snapshot = (CloudPageBlob) source.createSnapshot();

    // Modify source
    byte[] buffer2 = BlobTestHelper.getRandomBuffer(512);
    ByteArrayInputStream stream2 = new ByteArrayInputStream(buffer2);
    source.getMetadata().put("Test", "newvalue");
    source.uploadMetadata();
    source.getProperties().setContentMD5(null);
    source.upload(stream2, buffer.length);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    source.download(outputStream);

    ByteArrayOutputStream snapshotStream = new ByteArrayOutputStream();
    snapshot.download(snapshotStream);
    BlobTestHelper.assertStreamsAreEqual(
        stream2, new ByteArrayInputStream(outputStream.toByteArray()));
    BlobTestHelper.assertStreamsAreEqual(
        stream, new ByteArrayInputStream(snapshotStream.toByteArray()));

    source.downloadAttributes();
    snapshot.downloadAttributes();
    assertFalse(source.getMetadata().get("Test").equals(snapshot.getMetadata().get("Test")));

    CloudPageBlob copy = this.container.getPageBlobReference("copy");
    String copyId = copy.startCopy(BlobTestHelper.defiddler(snapshot));
    BlobTestHelper.waitForCopy(copy);

    ByteArrayOutputStream copyStream = new ByteArrayOutputStream();
    copy.download(copyStream);

    assertEquals(CopyStatus.SUCCESS, copy.getCopyState().getStatus());
    BlobTestHelper.assertStreamsAreEqual(
        stream, new ByteArrayInputStream(copyStream.toByteArray()));
    assertEquals(copyId, copy.getProperties().getCopyState().getCopyId());

    copy.downloadAttributes();
    BlobProperties prop1 = copy.getProperties();
    BlobProperties prop2 = snapshot.getProperties();

    assertEquals(prop1.getCacheControl(), prop2.getCacheControl());
    assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding());
    assertEquals(prop1.getContentDisposition(), prop2.getContentDisposition());
    assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage());
    assertEquals(prop1.getContentMD5(), prop2.getContentMD5());
    assertEquals(prop1.getContentType(), prop2.getContentType());

    assertEquals("value", copy.getMetadata().get("Test"));

    copy.delete();
  }