BindAzureBlobMetadataToRequest implements Binder { @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(); return blobBinder.bindToRequest(request, azureBlob2Blob.apply(blob)); } @Inject  BindAzureBlobMetadataToRequest(AzureBlobToBlob azureBlob2Blob, BindUserMetadataToHeadersWithPrefix blobBinder); @SuppressWarnings("unchecked") @Override R bindToRequest(R request, Object input);  }
@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: BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); assertEquals( binder.bindToRequest(request, blob), HttpRequest.builder().method("PUT").endpoint("http: .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: BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); assertEquals( binder.bindToRequest(request, blob), HttpRequest.builder().method("PUT").endpoint("http: .addHeader("x-ms-blob-type", "BlockBlob") .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: BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); 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: BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); 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: BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); binder.bindToRequest(request, blob); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeAzureBlob() { HttpRequest request = HttpRequest.builder().method("POST").endpoint("http: injector.getInstance(BindAzureBlobMetadataToRequest.class).bindToRequest(request, new File("foo")); }
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class }) public void testNullIsBad() { BindAzureBlobMetadataToRequest binder = injector.getInstance(BindAzureBlobMetadataToRequest.class); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: binder.bindToRequest(request, null); }