Package org.jclouds.azureblob.domain

Examples of org.jclouds.azureblob.domain.AzureBlob


   @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testCreateContainer", "testCreatePublicContainer" })
   public void testObjectOperations() throws Exception {
      String data = "Here is my data";

      // Test PUT with string data, ETag hash, and a piece of metadata
      AzureBlob object = getApi().newBlob();
      object.getProperties().setName("object");
      object.setPayload(data);
      Payloads.calculateMD5(object);
      object.getProperties().getContentMetadata().setContentType("text/plain");
      object.getProperties().getMetadata().put("mykey", "metadata-value");
      byte[] md5 = object.getProperties().getContentMetadata().getContentMD5();
      String newEtag = getApi().putBlob(privateContainer, object);
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(object.getProperties().getContentMetadata().getContentMD5()));
      // Test HEAD of missing object
      assert getApi().getBlobProperties(privateContainer, "non-existent-object") == null;

      // Test HEAD of object
      BlobProperties metadata = getApi().getBlobProperties(privateContainer, object.getProperties().getName());
      // TODO assertEquals(metadata.getName(),
      // object.getProperties().getName());
      // we can't check this while hacking around lack of content-md5, as GET of
      // the first byte will
      // show incorrect length 1, the returned size, as opposed to the real
      // length. This is an ok
      // tradeoff, as a container list will contain the correct size of the
      // objects in an
      // inexpensive fashion
      // http://code.google.com/p/jclouds/issues/detail?id=92
      // assertEquals(metadata.getSize(), data.length());
      assertEquals(metadata.getContentMetadata().getContentType(), "text/plain");
      // Azure doesn't return the Content-MD5 on head request..
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(object.getProperties().getContentMetadata().getContentMD5()));
      assertEquals(metadata.getETag(), newEtag);
      assertEquals(metadata.getMetadata().entrySet().size(), 1);
      assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

      // // Test POST to update object's metadata
      // Multimap<String, String> userMetadata = LinkedHashMultimap.create();
      // userMetadata.put("New-Metadata-1", "value-1");
      // userMetadata.put("New-Metadata-2", "value-2");
      // assertTrue(getApi().setBlobProperties(privateContainer,
      // object.getProperties().getName(),
      // userMetadata));

      // Test GET of missing object
      assert getApi().getBlob(privateContainer, "non-existent-object") == null;

      // Test GET of object (including updated metadata)
      AzureBlob getBlob = getApi().getBlob(privateContainer, object.getProperties().getName());
      assertEquals(Strings2.toString(getBlob.getPayload()), data);
      // TODO assertEquals(getBlob.getName(), object.getProperties().getName());
      assertEquals(getBlob.getPayload().getContentMetadata().getContentLength(), Long.valueOf(data.length()));
      assertEquals(getBlob.getProperties().getContentMetadata().getContentType(), "text/plain");
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(getBlob.getProperties().getContentMetadata().getContentMD5()));
      assertEquals(newEtag, getBlob.getProperties().getETag());
      // wait until we can update metadata
      // assertEquals(getBlob.getProperties().getMetadata().entries().size(),
      // 2);
      // assertEquals(
      // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-1")),
      // "value-1");
      // assertEquals(
      // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-2")),
      // "value-2");
      assertEquals(metadata.getMetadata().entrySet().size(), 1);
      assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

      // test listing
      ListBlobsResponse response = getApi().listBlobs(
            privateContainer,
            ListBlobsOptions.Builder
                  .prefix(object.getProperties().getName().substring(0, object.getProperties().getName().length() - 1))
                  .maxResults(1).includeMetadata());
      assertEquals(response.size(), 1);
      assertEquals(Iterables.getOnlyElement(response).getName(), object.getProperties().getName());
      assertEquals(Iterables.getOnlyElement(response).getMetadata(), ImmutableMap.of("mykey", "metadata-value"));

      // Test PUT with invalid ETag (as if object's data was corrupted in
      // transit)
      String correctEtag = newEtag;
      String incorrectEtag = "0" + correctEtag.substring(1);
      object.getProperties().setETag(incorrectEtag);
      try {
         getApi().putBlob(privateContainer, object);
      } catch (Throwable e) {
         assertEquals(e.getCause().getClass(), HttpResponseException.class);
         assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
      }

      ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
      object = getApi().newBlob();
      object.getProperties().setName("chunked-object");
      object.setPayload(bais);
      object.getPayload().getContentMetadata().setContentLength(Long.valueOf(data.getBytes().length));
      newEtag = getApi().putBlob(privateContainer, object);
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(getBlob.getProperties().getContentMetadata().getContentMD5()));

      // Test GET with options
      // Non-matching ETag
      try {
         getApi().getBlob(privateContainer, object.getProperties().getName(),
               GetOptions.Builder.ifETagDoesntMatch(newEtag));
      } catch (Exception e) {
         HttpResponseException httpEx = Throwables2.getFirstThrowableOfType(e, HttpResponseException.class);
         assert httpEx != null : "expected http exception, not " + e;
         assertEquals(httpEx.getResponse().getStatusCode(), 304);
      }

      // Matching ETag TODO this shouldn't fail!!!
      try {
         getBlob = getApi().getBlob(privateContainer, object.getProperties().getName(),
               GetOptions.Builder.ifETagMatches(newEtag));
         assertEquals(getBlob.getProperties().getETag(), newEtag);
      } catch (HttpResponseException e) {
         assertEquals(e.getResponse().getStatusCode(), 412);
      }

      // Range
View Full Code Here


                       .addHeader("x-ms-meta-foo", "bar").build());
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testNoContentLengthIsBad() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(null);
      blob.setPayload(payload);
      blob.getProperties().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);
      binder.bindToRequest(request, blob);
   }
View Full Code Here

      binder.bindToRequest(request, blob);
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testNoNameIsBad() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(5368709120000l);
      blob.setPayload(payload);

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);
      binder.bindToRequest(request, blob);
   }
View Full Code Here

      binder.bindToRequest(request, blob);
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testOver64MBIsBad() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(64 * 1024 * 1024l + 1);
      blob.setPayload(payload);
      blob.getProperties().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);
      binder.bindToRequest(request, blob);
   }
View Full Code Here

   }

   public AzureBlob apply(Blob from) {
      if (from == null)
         return null;
      AzureBlob object = objectProvider.create(blob2ObjectMd.apply(from.getMetadata()));
      object.setPayload(checkNotNull(from.getPayload(), "payload: " + from));
      object.setAllHeaders(from.getAllHeaders());
      return object;
   }
View Full Code Here

   @SuppressWarnings("unchecked")
   @Override
   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
      checkArgument(checkNotNull(input, "input") instanceof AzureBlob, "this binder is only valid for AzureBlobs!");
      checkNotNull(request, "request");
      AzureBlob blob = AzureBlob.class.cast(input);

      checkArgument(blob.getPayload().getContentMetadata().getContentLength() != null
            && blob.getPayload().getContentMetadata().getContentLength() >= 0, "size must be set");

      Builder<String, String> headers = ImmutableMap.builder();

      headers.put("x-ms-blob-type", blob.getProperties().getType().toString());

      switch (blob.getProperties().getType()) {
      case PAGE_BLOB:
         headers.put(HttpHeaders.CONTENT_LENGTH, "0");
         headers.put("x-ms-blob-content-length", blob.getPayload().getContentMetadata().getContentLength().toString());
         break;
      case BLOCK_BLOB:
         checkArgument(
               checkNotNull(blob.getPayload().getContentMetadata().getContentLength(), "blob.getContentLength()") <= 64l * 1024 * 1024,
               "maximum size for put Blob is 64MB");
         break;
      }
      request = (R) request.toBuilder().replaceHeaders(Multimaps.forMap(headers.build())).build();

View Full Code Here

   @Test(timeOut = 5 * 60 * 1000, dependsOnMethods = { "testCreateContainer", "testCreatePublicContainer" })
   public void testObjectOperations() throws Exception {
      String data = "Here is my data";

      // Test PUT with string data, ETag hash, and a piece of metadata
      AzureBlob object = getApi().newBlob();
      object.getProperties().setName("object");
      object.setPayload(data);
      object.getProperties().getContentMetadata().setContentMD5(Hashing.md5().hashString(data, Charsets.UTF_8).asBytes());
      object.getProperties().getContentMetadata().setContentType("text/plain");
      object.getProperties().getMetadata().put("mykey", "metadata-value");
      byte[] md5 = object.getProperties().getContentMetadata().getContentMD5();
      String newEtag = getApi().putBlob(privateContainer, object);
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(object.getProperties().getContentMetadata().getContentMD5()));
      // Test HEAD of missing object
      assert getApi().getBlobProperties(privateContainer, "non-existent-object") == null;

      // Test HEAD of object
      BlobProperties metadata = getApi().getBlobProperties(privateContainer, object.getProperties().getName());
      // TODO assertEquals(metadata.getName(),
      // object.getProperties().getName());
      // we can't check this while hacking around lack of content-md5, as GET of
      // the first byte will
      // show incorrect length 1, the returned size, as opposed to the real
      // length. This is an ok
      // tradeoff, as a container list will contain the correct size of the
      // objects in an
      // inexpensive fashion
      // http://code.google.com/p/jclouds/issues/detail?id=92
      // assertEquals(metadata.getSize(), data.length());
      assertEquals(metadata.getContentMetadata().getContentType(), "text/plain");
      // Azure doesn't return the Content-MD5 on head request..
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(object.getProperties().getContentMetadata().getContentMD5()));
      assertEquals(metadata.getETag(), newEtag);
      assertEquals(metadata.getMetadata().entrySet().size(), 1);
      assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

      // // Test POST to update object's metadata
      // Multimap<String, String> userMetadata = LinkedHashMultimap.create();
      // userMetadata.put("New-Metadata-1", "value-1");
      // userMetadata.put("New-Metadata-2", "value-2");
      // assertTrue(getApi().setBlobProperties(privateContainer,
      // object.getProperties().getName(),
      // userMetadata));

      // Test GET of missing object
      assert getApi().getBlob(privateContainer, "non-existent-object") == null;

      // Test GET of object (including updated metadata)
      AzureBlob getBlob = getApi().getBlob(privateContainer, object.getProperties().getName());
      assertEquals(Strings2.toStringAndClose(getBlob.getPayload().openStream()), data);
      // TODO assertEquals(getBlob.getName(), object.getProperties().getName());
      assertEquals(getBlob.getPayload().getContentMetadata().getContentLength(), Long.valueOf(data.length()));
      assertEquals(getBlob.getProperties().getContentMetadata().getContentType(), "text/plain");
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(getBlob.getProperties().getContentMetadata().getContentMD5()));
      assertEquals(newEtag, getBlob.getProperties().getETag());
      // wait until we can update metadata
      // assertEquals(getBlob.getProperties().getMetadata().entries().size(),
      // 2);
      // assertEquals(
      // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-1")),
      // "value-1");
      // assertEquals(
      // Iterables.getLast(getBlob.getProperties().getMetadata().get("New-Metadata-2")),
      // "value-2");
      assertEquals(metadata.getMetadata().entrySet().size(), 1);
      assertEquals(metadata.getMetadata().get("mykey"), "metadata-value");

      // test listing
      ListBlobsResponse response = getApi().listBlobs(
            privateContainer,
            ListBlobsOptions.Builder
                  .prefix(object.getProperties().getName().substring(0, object.getProperties().getName().length() - 1))
                  .maxResults(1).includeMetadata());
      assertEquals(response.size(), 1);
      assertEquals(Iterables.getOnlyElement(response).getName(), object.getProperties().getName());
      assertEquals(Iterables.getOnlyElement(response).getMetadata(), ImmutableMap.of("mykey", "metadata-value"));

      // Test PUT with invalid ETag (as if object's data was corrupted in
      // transit)
      String correctEtag = newEtag;
      String incorrectEtag = "0" + correctEtag.substring(1);
      object.getProperties().setETag(incorrectEtag);
      try {
         getApi().putBlob(privateContainer, object);
      } catch (Throwable e) {
         assertEquals(e.getCause().getClass(), HttpResponseException.class);
         assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
      }

      ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
      object = getApi().newBlob();
      object.getProperties().setName("chunked-object");
      object.setPayload(bais);
      object.getPayload().getContentMetadata().setContentLength(Long.valueOf(data.getBytes().length));
      newEtag = getApi().putBlob(privateContainer, object);
      assertEquals(base16().lowerCase().encode(md5),
            base16().lowerCase().encode(getBlob.getProperties().getContentMetadata().getContentMD5()));

      // Test GET with options
      // Non-matching ETag
      try {
         getApi().getBlob(privateContainer, object.getProperties().getName(),
               GetOptions.Builder.ifETagDoesntMatch(newEtag));
      } catch (Exception e) {
         HttpResponseException httpEx = Throwables2.getFirstThrowableOfType(e, HttpResponseException.class);
         assert httpEx != null : "expected http exception, not " + e;
         assertEquals(httpEx.getResponse().getStatusCode(), 304);
      }

      // Matching ETag TODO this shouldn't fail!!!
      try {
         getBlob = getApi().getBlob(privateContainer, object.getProperties().getName(),
               GetOptions.Builder.ifETagMatches(newEtag));
         assertEquals(getBlob.getProperties().getETag(), newEtag);
      } catch (HttpResponseException e) {
         assertEquals(e.getResponse().getStatusCode(), 412);
      }

      // Range
View Full Code Here

@Test(groups = "unit", testName = "BindAzureBlobMetadataToRequestTest")
public class BindAzureBlobMetadataToRequestTest extends BaseAsyncClientTest<AzureBlobAsyncClient> {

   @Test
   public void testPassWithMinimumDetailsAndPayload64MB() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(64 * 1024 * 1024l);
      blob.setPayload(payload);
      blob.getProperties().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);

      assertEquals(
View Full Code Here

                  .addHeader("x-ms-blob-type", "BlockBlob").build());
   }

   @Test
   public void testExtendedPropertiesBind() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(64 * 1024 * 1024l);
      blob.setPayload(payload);
      blob.getProperties().setName("foo");
      blob.getProperties().setMetadata(ImmutableMap.of("foo", "bar"));

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);

      assertEquals(
View Full Code Here

                       .addHeader("x-ms-meta-foo", "bar").build());
   }

   @Test(expectedExceptions = IllegalArgumentException.class)
   public void testNoContentLengthIsBad() {
      AzureBlob blob = injector.getInstance(AzureBlob.Factory.class).create(null);
      Payload payload = Payloads.newStringPayload("");
      payload.getContentMetadata().setContentLength(null);
      blob.setPayload(payload);
      blob.getProperties().setName("foo");

      HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http://localhost").build();
      BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class);
      binder.bindToRequest(request, blob);
   }
View Full Code Here

TOP

Related Classes of org.jclouds.azureblob.domain.AzureBlob

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.