@Test
  public void testPageBlobCopyWithMetadataOverride()
      throws URISyntaxException, StorageException, IOException, InterruptedException {
    Calendar calendar = Calendar.getInstance(Utility.UTC_ZONE);
    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 copy = this.container.getPageBlobReference("copy");
    copy.getMetadata().put("Test2", "value2");
    String copyId = copy.startCopy(BlobTestHelper.defiddler(source));
    BlobTestHelper.waitForCopy(copy);

    assertEquals(CopyStatus.SUCCESS, copy.getCopyState().getStatus());
    assertEquals(source.getQualifiedUri().getPath(), copy.getCopyState().getSource().getPath());
    assertEquals(buffer.length, copy.getCopyState().getTotalBytes().intValue());
    assertEquals(buffer.length, copy.getCopyState().getBytesCopied().intValue());
    assertEquals(copyId, copy.getCopyState().getCopyId());
    assertTrue(
        copy.getCopyState()
                .getCompletionTime()
                .compareTo(new Date(calendar.get(Calendar.MINUTE) - 1))
            > 0);

    ByteArrayOutputStream copyStream = new ByteArrayOutputStream();
    copy.download(copyStream);
    BlobTestHelper.assertStreamsAreEqual(
        stream, new ByteArrayInputStream(copyStream.toByteArray()));

    copy.downloadAttributes();
    source.downloadAttributes();
    BlobProperties prop1 = copy.getProperties();
    BlobProperties prop2 = source.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("value2", copy.getMetadata().get("Test2"));
    assertFalse(copy.getMetadata().containsKey("Test"));

    copy.delete();
  }
  @Test
  public void testClearPages() throws URISyntaxException, StorageException, IOException {
    int blobLengthToUse = 8 * 512;
    byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512);

    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);
    blobRef.create(blobLengthToUse);
    assertNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload one page (page 0)
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    blobRef.uploadPages(inputStream, 0, blobLengthToUse);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    try {
      blobRef.clearPages(0, 256);
      fail("Did not throw expected exception on non-512-byte-aligned length");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_BLOB_LENGTH, ex.getMessage());
    }

    try {
      blobRef.clearPages(3 * 256, 3 * 512);
      fail("Did not throw expected exception on non-512-byte-aligned offset");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_START_OFFSET, ex.getMessage());
    }

    blobRef.clearPages(3 * 512, 2 * 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    byte[] result = new byte[blobLengthToUse];
    blobRef.downloadToByteArray(result, 0);

    int i = 0;

    for (; i < 3 * 512; i++) {
      assertEquals(buffer[i], result[i]);
    }

    for (; i < 5 * 512; i++) {
      assertEquals(0, result[i]);
    }

    for (; i < 8 * 512; i++) {
      assertEquals(buffer[i], result[i]);
    }
  }
  @Test
  public void testBlobUploadWithoutMD5Validation()
      throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    BlobRequestOptions options = new BlobRequestOptions();
    options.setDisableContentMD5Validation(false);
    options.setStoreBlobContentMD5(false);

    pageBlobRef.upload(srcStream, length, null, options, null);
    pageBlobRef.downloadAttributes();
    pageBlobRef.getProperties().setContentMD5("MDAwMDAwMDA=");
    pageBlobRef.uploadProperties(null, options, null);

    try {
      pageBlobRef.download(new ByteArrayOutputStream(), null, options, null);
      fail();
    } catch (StorageException ex) {
      assertEquals(306, ex.getHttpStatusCode());
      assertEquals("InvalidMd5", ex.getErrorCode());
    }

    options.setDisableContentMD5Validation(true);
    pageBlobRef.download(new ByteArrayOutputStream(), null, options, null);
  }
  /**
   * Create a blob and try to download a range of its contents
   *
   * @throws StorageException
   * @throws URISyntaxException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testPageBlobDownloadRangeValidationTest()
      throws StorageException, URISyntaxException, IOException {
    final int length = 5 * 1024 * 1024;

    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(blockBlobName);

    pageBlobRef.upload(BlobTestHelper.getRandomDataStream(length), length);

    // Download full blob
    pageBlobRef.download(new ByteArrayOutputStream());
    assertEquals(length, pageBlobRef.getProperties().getLength());

    // Download blob range.
    byte[] downloadBuffer = new byte[100];
    int downloadLength = pageBlobRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0);
    assertEquals(length, pageBlobRef.getProperties().getLength());
    assertEquals(100, downloadLength);
  }
  @Test
  public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException {
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    CloudPageBlob pageBlob = this.container.getPageBlobReference(blobName);

    try {
      pageBlob.openWriteExisting();
    } catch (StorageException ex) {
      assertEquals("The specified blob does not exist.", ex.getMessage());
      assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode());
    }

    pageBlob.openWriteNew(1024);
    pageBlob.openWriteExisting();

    CloudPageBlob pageBlob2 = this.container.getPageBlobReference(blobName);
    pageBlob2.downloadAttributes();
    assertEquals(1024, pageBlob2.getProperties().getLength());
    assertEquals(BlobType.PAGE_BLOB, pageBlob2.getProperties().getBlobType());
  }
  @Test
  public void testUploadDownloadBlobProperties()
      throws URISyntaxException, StorageException, IOException {
    final int length = 512;

    // do this to make sure the set MD5 can be compared without an exception being thrown
    BlobRequestOptions options = new BlobRequestOptions();
    options.setDisableContentMD5Validation(true);

    // with explicit upload/download of properties
    String pageBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    CloudPageBlob pageBlobRef1 = this.container.getPageBlobReference(pageBlobName1);

    pageBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length);

    BlobTestHelper.setBlobProperties(pageBlobRef1);
    BlobProperties props1 = pageBlobRef1.getProperties();
    pageBlobRef1.uploadProperties();

    pageBlobRef1.downloadAttributes(null, options, null);
    BlobProperties props2 = pageBlobRef1.getProperties();

    BlobTestHelper.assertAreEqual(props1, props2);

    // by uploading/downloading the blob
    pageBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    pageBlobRef1 = this.container.getPageBlobReference(pageBlobName1);

    BlobTestHelper.setBlobProperties(pageBlobRef1);
    props1 = pageBlobRef1.getProperties();

    pageBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length);

    pageBlobRef1.download(new ByteArrayOutputStream(), null, options, null);
    props2 = pageBlobRef1.getProperties();

    BlobTestHelper.assertAreEqual(props1, props2);
  }
  @Test
  public void testCloudPageBlobUploadFromStreamWithAccessCondition()
      throws URISyntaxException, StorageException, IOException {
    CloudPageBlob blob1 = this.container.getPageBlobReference("blob1");
    AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition("\"*\"");
    final int length = 6 * 512;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    blob1.upload(srcStream, length, accessCondition, null, null);

    srcStream.reset();
    blob1.create(1024);
    accessCondition = AccessCondition.generateIfNoneMatchCondition(blob1.getProperties().getEtag());
    try {
      blob1.upload(srcStream, length, accessCondition, null, null);
    } catch (StorageException ex) {
      assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, ex.getHttpStatusCode());
    }

    srcStream.reset();
    accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag());
    blob1.upload(srcStream, length, accessCondition, null, null);

    srcStream.reset();
    CloudPageBlob blob2 = this.container.getPageBlobReference("blob2");
    blob2.create(1024);
    accessCondition = AccessCondition.generateIfMatchCondition(blob1.getProperties().getEtag());
    try {
      blob1.upload(srcStream, length, accessCondition, null, null);
    } catch (StorageException ex) {
      assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, ex.getHttpStatusCode());
    }

    srcStream.reset();
    accessCondition = AccessCondition.generateIfNoneMatchCondition(blob2.getProperties().getEtag());
    blob1.upload(srcStream, length, accessCondition, null, null);
  }
  /**
   * Start copying a blob and then abort
   *
   * @throws StorageException
   * @throws URISyntaxException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testCopyFromPageBlobAbortTest()
      throws StorageException, URISyntaxException, IOException {
    final int length = 512;
    CloudPageBlob originalBlob =
        (CloudPageBlob)
            BlobTestHelper.uploadNewBlob(
                this.container, BlobType.PAGE_BLOB, "originalBlob", length, null);
    CloudPageBlob copyBlob = this.container.getPageBlobReference(originalBlob.getName() + "copyed");
    copyBlob.startCopy(originalBlob);

    try {
      copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId());
    } catch (StorageException e) {
      if (!e.getErrorCode().contains("NoPendingCopyOperation")) {
        throw e;
      }
    }
  }
  @Test
  public void testResize() throws StorageException, URISyntaxException {
    CloudPageBlob blob = this.container.getPageBlobReference("blob1");
    CloudPageBlob blob2 = this.container.getPageBlobReference("blob1");

    blob.create(1024);
    assertEquals(1024, blob.getProperties().getLength());
    assertNull(blob.getProperties().getPageBlobSequenceNumber());

    blob2.downloadAttributes();
    assertEquals(1024, blob2.getProperties().getLength());
    assertNull(blob.getProperties().getPageBlobSequenceNumber());

    blob2.getProperties().setContentType("text/plain");
    blob2.uploadProperties();

    blob.resize(2048);
    assertEquals(2048, blob.getProperties().getLength());
    assertNotNull(blob.getProperties().getPageBlobSequenceNumber());

    blob.downloadAttributes();
    assertEquals("text/plain", blob.getProperties().getContentType());
    assertNotNull(blob.getProperties().getPageBlobSequenceNumber());

    blob2.downloadAttributes();
    assertEquals(2048, blob2.getProperties().getLength());
    assertNotNull(blob.getProperties().getPageBlobSequenceNumber());
  }
  @Test
  public void testUploadPages() throws URISyntaxException, StorageException, IOException {
    int blobLengthToUse = 8 * 512;
    byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512);

    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);
    blobRef.create(blobLengthToUse);
    assertNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload one page (page 0)
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    blobRef.uploadPages(inputStream, 0, 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload pages 2-4
    inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512);
    blobRef.uploadPages(inputStream, 2 * 512, 3 * 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Now, we expect the first 512 bytes of the blob to be the first 512 bytes of the random buffer
    // (page 0)
    // the next 512 bytes should be 0 (page 1)
    // The next 3 * 512 bytes should be equal to bytes (512 -> 4 * 512) of the random buffer (pages
    // 2-4)
    // The next 3 * 512 bytes should be 0 (pages 5-7)

    byte[] result = new byte[blobLengthToUse];
    blobRef.downloadToByteArray(result, 0);

    for (int i = 0; i < 512; i++) {
      assertEquals(buffer[i], result[i]);
    }

    for (int i = 0; i < 512; i++) {
      assertEquals(0, result[i + 512]);
    }

    for (int i = 0; i < 3 * 512; i++) {
      assertEquals(buffer[i + 512], result[i + 2 * 512]);
    }

    for (int i = 0; i < 3 * 512; i++) {
      assertEquals(0, result[i + 5 * 512]);
    }

    inputStream = new ByteArrayInputStream(buffer);

    try {
      blobRef.uploadPages(inputStream, 0, 256);
      fail("Did not throw expected exception on non-512-byte-aligned length");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_BLOB_LENGTH, ex.getMessage());
    }

    try {
      blobRef.uploadPages(inputStream, 3 * 256, 3 * 512);
      fail("Did not throw expected exception on non-512-byte-aligned offset");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_START_OFFSET, ex.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();
  }