use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageReadChannelTest method testReadRetry.
@Test
public void testReadRetry() throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1);
when(gcsChannel.read(eq(buffer))).thenThrow(new StorageException(new IOException("outer", new IOException("Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040")))).thenReturn(1);
assertThat(chan.position()).isEqualTo(0L);
assertThat(chan.read(buffer)).isEqualTo(1);
assertThat(chan.position()).isEqualTo(1L);
verify(gcsChannel, times(2)).read(any(ByteBuffer.class));
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCreateBlobFail.
@Test
public void testCreateBlobFail() {
String blobName = "test-create-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
try {
storage.create(wrongGenerationBlob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.generationMatch());
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteBlob.delete());
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobFail.
@Test
public void testUpdateBlobFail() {
String blobName = "test-update-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobInfo wrongGenerationBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).setContentType(CONTENT_TYPE).build();
try {
storage.update(wrongGenerationBlob, Storage.BlobTargetOption.generationMatch());
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
assertTrue(remoteBlob.delete());
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBatchRequestManyOperations.
@Test
public void testBatchRequestManyOperations() {
List<StorageBatchResult<Boolean>> deleteResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
List<StorageBatchResult<Blob>> updateResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
StorageBatch batch = storage.batch();
for (int i = 0; i < MAX_BATCH_SIZE; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
deleteResults.add(batch.delete(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobId blobId = BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i);
getResults.add(batch.get(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobInfo blob = BlobInfo.newBuilder(BlobId.of(BUCKET, "test-batch-request-many-operations-blob-" + i)).build();
updateResults.add(batch.update(blob));
}
String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
StorageBatchResult<Blob> getResult = batch.get(BUCKET, sourceBlobName1);
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
batch.submit();
// Check deletes
for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
assertFalse(failedDeleteResult.get());
}
// Check gets
for (StorageBatchResult<Blob> failedGetResult : getResults) {
assertNull(failedGetResult.get());
}
Blob remoteBlob1 = getResult.get();
assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
assertEquals(sourceBlob1.getName(), remoteBlob1.getName());
// Check updates
for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
try {
failedUpdateResult.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
Blob remoteUpdatedBlob2 = updateResult.get();
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
assertTrue(remoteBlob1.delete());
assertTrue(remoteUpdatedBlob2.delete());
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testBlob.
@Test
public void testBlob() throws InterruptedException {
String blobName = "directory/test-blob";
Blob blob = storageSnippets.createBlob(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.updateBlob(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName);
assertNotNull(blob);
Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
assertNotNull(copiedBlob);
Page<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
while (Iterators.size(blobs.iterateAll().iterator()) < 2) {
Thread.sleep(500);
blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
}
Set<String> blobNames = new HashSet<>();
Iterator<Blob> blobIterator = blobs.iterateAll().iterator();
while (blobIterator.hasNext()) {
blobNames.add(blobIterator.next().getName());
}
assertTrue(blobNames.contains(blobName));
assertTrue(blobNames.contains("directory/copy-blob"));
try {
storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1);
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
assertTrue(storageSnippets.deleteBlob(BUCKET, blobName));
copiedBlob.delete();
}
Aggregations