/**
   * 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 testPageBlobCopyTest()
      throws URISyntaxException, StorageException, InterruptedException, IOException {
    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");
    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);

    try {
      copy.abortCopy(copy.getCopyState().getCopyId());
    } catch (StorageException ex) {
      assertEquals(HttpURLConnection.HTTP_CONFLICT, ex.getHttpStatusCode());
    }

    source.downloadAttributes();
    assertNotNull(copy.getProperties().getEtag());
    assertFalse(source.getProperties().getEtag().equals(copy.getProperties().getEtag()));
    assertTrue(
        copy.getProperties()
                .getLastModified()
                .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();
    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("value", copy.getMetadata().get("Test"));

    copy.delete();
  }