@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 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 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);
  }
  /**
   * @throws StorageException
   * @throws URISyntaxException
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testPageBlobNamePlusEncodingTest()
      throws StorageException, URISyntaxException, IOException, InterruptedException {
    final int length = 1 * 1024;

    final CloudPageBlob originalBlob =
        (CloudPageBlob)
            BlobTestHelper.uploadNewBlob(
                this.container, BlobType.PAGE_BLOB, "a+b.txt", length, null);
    final CloudPageBlob copyBlob =
        this.container.getPageBlobReference(originalBlob.getName() + "copyed");

    copyBlob.startCopy(originalBlob);
    BlobTestHelper.waitForCopy(copyBlob);
    copyBlob.downloadAttributes();
  }
  @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 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();
  }