Ejemplo n.º 1
0
  @Test
  public void testBatchRequest() {
    String sourceBlobName1 = "test-batch-request-blob-1";
    String sourceBlobName2 = "test-batch-request-blob-2";
    BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
    assertNotNull(storage.create(sourceBlob1));
    assertNotNull(storage.create(sourceBlob2));

    // Batch update request
    BlobInfo updatedBlob1 = sourceBlob1.toBuilder().contentType(CONTENT_TYPE).build();
    BlobInfo updatedBlob2 = sourceBlob2.toBuilder().contentType(CONTENT_TYPE).build();
    BatchRequest updateRequest =
        BatchRequest.builder().update(updatedBlob1).update(updatedBlob2).build();
    BatchResponse updateResponse = storage.apply(updateRequest);
    assertEquals(2, updateResponse.updates().size());
    assertEquals(0, updateResponse.deletes().size());
    assertEquals(0, updateResponse.gets().size());
    BlobInfo remoteUpdatedBlob1 = updateResponse.updates().get(0).get();
    BlobInfo remoteUpdatedBlob2 = updateResponse.updates().get(1).get();
    assertEquals(sourceBlob1.blobId(), remoteUpdatedBlob1.blobId());
    assertEquals(sourceBlob2.blobId(), remoteUpdatedBlob2.blobId());
    assertEquals(updatedBlob1.contentType(), remoteUpdatedBlob1.contentType());
    assertEquals(updatedBlob2.contentType(), remoteUpdatedBlob2.contentType());

    // Batch get request
    BatchRequest getRequest =
        BatchRequest.builder().get(BUCKET, sourceBlobName1).get(BUCKET, sourceBlobName2).build();
    BatchResponse getResponse = storage.apply(getRequest);
    assertEquals(2, getResponse.gets().size());
    assertEquals(0, getResponse.deletes().size());
    assertEquals(0, getResponse.updates().size());
    BlobInfo remoteBlob1 = getResponse.gets().get(0).get();
    BlobInfo remoteBlob2 = getResponse.gets().get(1).get();
    assertEquals(remoteUpdatedBlob1, remoteBlob1);
    assertEquals(remoteUpdatedBlob2, remoteBlob2);

    // Batch delete request
    BatchRequest deleteRequest =
        BatchRequest.builder()
            .delete(BUCKET, sourceBlobName1)
            .delete(BUCKET, sourceBlobName2)
            .build();
    BatchResponse deleteResponse = storage.apply(deleteRequest);
    assertEquals(2, deleteResponse.deletes().size());
    assertEquals(0, deleteResponse.gets().size());
    assertEquals(0, deleteResponse.updates().size());
    assertTrue(deleteResponse.deletes().get(0).get());
    assertTrue(deleteResponse.deletes().get(1).get());
  }
Ejemplo n.º 2
0
 @Test
 public void testBatchRequestFail() {
   String blobName = "test-batch-request-blob-fail";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   BlobInfo updatedBlob = blob.toBuilder().generation(-1L).build();
   BatchRequest batchRequest =
       BatchRequest.builder()
           .update(updatedBlob, Storage.BlobTargetOption.generationMatch())
           .delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L))
           .get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L))
           .build();
   BatchResponse updateResponse = storage.apply(batchRequest);
   assertEquals(1, updateResponse.updates().size());
   assertEquals(1, updateResponse.deletes().size());
   assertEquals(1, updateResponse.gets().size());
   assertTrue(updateResponse.updates().get(0).failed());
   assertTrue(updateResponse.gets().get(0).failed());
   assertTrue(updateResponse.deletes().get(0).failed());
   assertTrue(storage.delete(BUCKET, blobName));
 }