Search in sources :

Example 1 with User

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

the class AclTest method testUserEntity.

@Test
public void testUserEntity() {
    User acl = new User("u1");
    assertEquals("u1", acl.getEmail());
    assertEquals(Type.USER, acl.getType());
    String pb = acl.toPb();
    assertEquals(acl, Entity.fromPb(pb));
}
Also used : User(com.google.cloud.storage.Acl.User) Test(org.junit.Test)

Example 2 with User

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

the class ITStorageSnippets method testBlobAcl.

@Test
public void testBlobAcl() {
    String blobName = "test-blob-acl";
    BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
    BlobInfo blob = BlobInfo.newBuilder(blobId).build();
    Blob createdBlob = storage.create(blob);
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(acls.contains(updatedAcl));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
    storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
    Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
    assertNotNull(userAcl);
    assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
    assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
    // test non-existing blob
    String nonExistingBlob = "test-blob-acl";
    assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, 1L));
    assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, 1L));
    try {
        storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
    try {
        storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, 1L);
        fail("Expected StorageException");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : Blob(com.google.cloud.storage.Blob) User(com.google.cloud.storage.Acl.User) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Example 3 with User

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

the class StorageSnippets method getBlobAcl.

/**
 * Example of getting the ACL entry for a specific user on a blob.
 */
// [TARGET getAcl(BlobId, Entity)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"]
public Acl getBlobAcl(String bucketName, String blobName, String userEmail) {
    // [START storagePrintFileAclForUser]
    BlobId blobId = BlobId.of(bucketName, blobName);
    Acl acl = storage.getAcl(blobId, new User(userEmail));
    // [END storagePrintFileAclForUser]
    return acl;
}
Also used : User(com.google.cloud.storage.Acl.User) Acl(com.google.cloud.storage.Acl) BlobId(com.google.cloud.storage.BlobId)

Example 4 with User

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

the class ITStorageSnippets method testBucketAcl.

@Test
public void testBucketAcl() {
    assertNull(storageSnippets.getBucketAcl(BUCKET));
    assertFalse(storageSnippets.deleteBucketAcl(BUCKET));
    assertNotNull(storageSnippets.createBucketAcl(BUCKET));
    Acl updatedAcl = storageSnippets.updateBucketAcl(BUCKET);
    assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
    Set<Acl> acls = Sets.newHashSet(storageSnippets.listBucketAcls(BUCKET));
    assertTrue(acls.contains(updatedAcl));
    assertNotNull(storageSnippets.getBucketAcl(BUCKET));
    assertNull(storageSnippets.getBucketAcl(BUCKET, USER_EMAIL));
    storage.createAcl(BUCKET, Acl.of(new User(USER_EMAIL), Role.READER));
    Acl userAcl = storageSnippets.getBucketAcl(BUCKET, USER_EMAIL);
    assertNotNull(userAcl);
    assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
    assertTrue(storageSnippets.deleteBucketAcl(BUCKET));
    assertNull(storageSnippets.getBucketAcl(BUCKET));
}
Also used : User(com.google.cloud.storage.Acl.User) Acl(com.google.cloud.storage.Acl) Test(org.junit.Test)

Aggregations

User (com.google.cloud.storage.Acl.User)4 Acl (com.google.cloud.storage.Acl)3 Test (org.junit.Test)3 BlobId (com.google.cloud.storage.BlobId)2 Blob (com.google.cloud.storage.Blob)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 StorageException (com.google.cloud.storage.StorageException)1