use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testBatchRequestFail.
@Test
public void testBatchRequestFail() {
String blobName = "test-batch-request-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobInfo updatedBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
StorageBatch batch = storage.batch();
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob, Storage.BlobTargetOption.generationMatch());
StorageBatchResult<Boolean> deleteResult1 = batch.delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L));
StorageBatchResult<Boolean> deleteResult2 = batch.delete(BlobId.of(BUCKET, blobName, -1L));
StorageBatchResult<Blob> getResult1 = batch.get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L));
StorageBatchResult<Blob> getResult2 = batch.get(BlobId.of(BUCKET, blobName, -1L));
batch.submit();
try {
updateResult.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
deleteResult1.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
assertFalse(deleteResult2.get());
try {
getResult1.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
assertNull(getResult2.get());
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadChannelFailUpdatedGeneration.
@Test
public void testReadChannelFailUpdatedGeneration() throws IOException {
String blobName = "test-read-blob-fail-updated-generation";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Random random = new Random();
int chunkSize = 1024;
int blobSize = 2 * chunkSize;
byte[] content = new byte[blobSize];
random.nextBytes(content);
Blob remoteBlob = storage.create(blob, content);
assertNotNull(remoteBlob);
assertEquals(blobSize, (long) remoteBlob.getSize());
try (ReadChannel reader = storage.reader(blob.getBlobId())) {
reader.setChunkSize(chunkSize);
ByteBuffer readBytes = ByteBuffer.allocate(chunkSize);
int numReadBytes = reader.read(readBytes);
assertEquals(chunkSize, numReadBytes);
assertArrayEquals(Arrays.copyOf(content, chunkSize), readBytes.array());
try (WriteChannel writer = storage.writer(blob)) {
byte[] newContent = new byte[blobSize];
random.nextBytes(newContent);
int numWrittenBytes = writer.write(ByteBuffer.wrap(newContent));
assertEquals(blobSize, numWrittenBytes);
}
readBytes = ByteBuffer.allocate(chunkSize);
reader.read(readBytes);
fail("StorageException was expected");
} catch (StorageException ex) {
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.append("Blob ").append(blob.getBlobId()).append(" was updated while reading");
assertEquals(messageBuilder.toString(), ex.getMessage());
}
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadChannelFail.
@Test
public void testReadChannelFail() throws IOException {
String blobName = "test-read-channel-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) {
reader.read(ByteBuffer.allocate(42));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.generationMatch(-1L))) {
reader.read(ByteBuffer.allocate(42));
fail("StorageException was expected");
} catch (StorageException ex) {
// expected
}
BlobId blobIdWrongGeneration = BlobId.of(BUCKET, blobName, -1L);
try (ReadChannel reader = storage.reader(blobIdWrongGeneration, Storage.BlobSourceOption.generationMatch())) {
reader.read(ByteBuffer.allocate(42));
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 testGetBlobFail.
@Test
public void testGetBlobFail() {
String blobName = "test-get-blob-fail";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob);
assertNotNull(remoteBlob);
BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName);
try {
storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1));
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 testBlobAcl.
@Test
public void testBlobAcl() {
BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
BlobInfo blob = BlobInfo.newBuilder(blobId).build();
storage.create(blob);
assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER);
assertNotNull(storage.createAcl(blobId, acl));
Acl updatedAcl = storage.updateAcl(blobId, acl.toBuilder().setRole(Role.OWNER).build());
assertEquals(Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(storage.listAcls(blobId));
assertTrue(acls.contains(updatedAcl));
assertTrue(storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers()));
assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers()));
// test non-existing blob
BlobId otherBlobId = BlobId.of(BUCKET, "test-blob-acl", -1L);
assertNull(storage.getAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
assertFalse(storage.deleteAcl(otherBlobId, User.ofAllAuthenticatedUsers()));
try {
storage.createAcl(otherBlobId, acl);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storage.updateAcl(otherBlobId, acl);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storage.listAcls(otherBlobId);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
Aggregations