Search in sources :

Example 11 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testCopyBlobFail.

@Test
public void testCopyBlobFail() {
    String sourceBlobName = "test-copy-blob-source-fail";
    BlobId source = BlobId.of(BUCKET, sourceBlobName, -1L);
    Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT);
    assertNotNull(remoteSourceBlob);
    String targetBlobName = "test-copy-blob-target-fail";
    BlobInfo target = BlobInfo.newBuilder(BUCKET, targetBlobName).setContentType(CONTENT_TYPE).build();
    Storage.CopyRequest req = Storage.CopyRequest.newBuilder().setSource(BUCKET, sourceBlobName).setSourceOptions(Storage.BlobSourceOption.generationMatch(-1L)).setTarget(target).build();
    try {
        storage.copy(req);
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    Storage.CopyRequest req2 = Storage.CopyRequest.newBuilder().setSource(source).setSourceOptions(Storage.BlobSourceOption.generationMatch()).setTarget(target).build();
    try {
        storage.copy(req2);
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteSourceBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 12 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testCreateBlobMd5Fail.

@Test
public void testCreateBlobMd5Fail() {
    String blobName = "test-create-blob-md5-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMd5("O1R4G1HJSDUISJjoIYmVhQ==").build();
    ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8));
    try {
        storage.create(blob, stream, Storage.BlobWriteOption.md5Match());
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 13 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testComposeBlobFail.

@Test
public void testComposeBlobFail() {
    String sourceBlobName1 = "test-compose-blob-fail-source-1";
    String sourceBlobName2 = "test-compose-blob-fail-source-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    Blob remoteSourceBlob1 = storage.create(sourceBlob1);
    Blob remoteSourceBlob2 = storage.create(sourceBlob2);
    assertNotNull(remoteSourceBlob1);
    assertNotNull(remoteSourceBlob2);
    String targetBlobName = "test-compose-blob-fail-target";
    BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).build();
    Storage.ComposeRequest req = Storage.ComposeRequest.newBuilder().addSource(sourceBlobName1, -1L).addSource(sourceBlobName2, -1L).setTarget(targetBlob).build();
    try {
        storage.compose(req);
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteSourceBlob1.delete());
    assertTrue(remoteSourceBlob2.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 14 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testWriteChannelFail.

@Test
public void testWriteChannelFail() throws IOException {
    String blobName = "test-write-channel-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
    try {
        try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.generationMatch())) {
            writer.write(ByteBuffer.allocate(42));
        }
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 15 with StorageException

use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testDeleteBlobFail.

@Test
public void testDeleteBlobFail() {
    String blobName = "test-delete-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    try {
        storage.delete(BUCKET, blob.getName(), Storage.BlobSourceOption.generationMatch(-1L));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Aggregations

StorageException (com.google.cloud.storage.StorageException)29 Test (org.junit.Test)20 BlobInfo (com.google.cloud.storage.BlobInfo)16 Blob (com.google.cloud.storage.Blob)15 BlobId (com.google.cloud.storage.BlobId)8 IOException (java.io.IOException)5 Acl (com.google.cloud.storage.Acl)4 ByteBuffer (java.nio.ByteBuffer)4 Storage (com.google.cloud.storage.Storage)3 StorageBatch (com.google.cloud.storage.StorageBatch)3 ReadChannel (com.google.cloud.ReadChannel)2 WriteChannel (com.google.cloud.WriteChannel)2 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpResponse (com.google.api.client.http.HttpResponse)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 Get (com.google.api.services.storage.Storage.Objects.Get)1 BatchResult (com.google.cloud.BatchResult)1 User (com.google.cloud.storage.Acl.User)1