Skip to content

Commit

Permalink
Merge pull request #571 from mziccard/acl-of
Browse files Browse the repository at this point in the history
Adds of factory method to bigquery and storage Acl
  • Loading branch information
aozarov committed Jan 22, 2016
2 parents 0c3e935 + c411b23 commit 1a18033
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,11 @@ Access toPb() {
}
}

/**
* Build an ACL for an {@code entity} and a {@code role}.
*/
public Acl(Entity entity, Role role) {
private Acl(Entity entity, Role role) {
this.entity = checkNotNull(entity);
this.role = role;
}

/**
* Build an ACL for a view entity.
*/
public Acl(View view) {
this.entity = checkNotNull(view);
this.role = null;
}

/**
* Returns the entity for this ACL.
*/
Expand All @@ -400,6 +389,23 @@ public Role role() {
return role;
}

/**
* Returns an Acl object.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public static Acl of(Entity entity, Role role) {
return new Acl(entity, role);
}

/**
* Returns an Acl object for a view entity.
*/
public static Acl of(View view) {
return new Acl(view, null);
}

@Override
public int hashCode() {
return Objects.hash(entity, role);
Expand Down Expand Up @@ -432,7 +438,7 @@ Access toPb() {
}

static Acl fromPb(Access access) {
return new Acl(Entity.fromPb(access),
return Acl.of(Entity.fromPb(access),
access.getRole() != null ? Role.valueOf(access.getRole()) : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ private DatasetInfo setProjectId(DatasetInfo dataset) {
if (viewReferencePb.getProjectId() == null) {
viewReferencePb.setProjectId(options().projectId());
}
acls.add(new Acl(new Acl.View(TableId.fromPb(viewReferencePb))));
acls.add(Acl.of(new Acl.View(TableId.fromPb(viewReferencePb))));
} else {
acls.add(acl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ public void testViewEntity() {
}

@Test
public void testAcl() {
Acl acl = new Acl(Group.ofAllAuthenticatedUsers(), Role.READER);
public void testOf() {
Acl acl = Acl.of(Group.ofAllAuthenticatedUsers(), Role.READER);
assertEquals(Group.ofAllAuthenticatedUsers(), acl.entity());
assertEquals(Role.READER, acl.role());
Dataset.Access pb = acl.toPb();
assertEquals(acl, Acl.fromPb(pb));
View view = new View(TableId.of("project", "dataset", "view"));
acl = new Acl(view);
acl = Acl.of(view);
assertEquals(view, acl.entity());
assertEquals(null, acl.role());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public class BigQueryImplTest {
private static final String OTHER_TABLE = "otherTable";
private static final String OTHER_DATASET = "otherDataset";
private static final List<Acl> ACCESS_RULES = ImmutableList.of(
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
new Acl(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER));
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
Acl.of(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER));
private static final List<Acl> ACCESS_RULES_WITH_PROJECT = ImmutableList.of(
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
new Acl(new Acl.View(TableId.of(PROJECT, "dataset", "table"))));
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
Acl.of(new Acl.View(TableId.of(PROJECT, "dataset", "table"))));
private static final DatasetInfo DATASET_INFO = DatasetInfo.builder(DATASET)
.acl(ACCESS_RULES)
.description("description")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public class DatasetInfoTest {

private static final List<Acl> ACCESS_RULES = ImmutableList.of(
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
new Acl(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER));
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
Acl.of(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER));
private static final Long CREATION_TIME = System.currentTimeMillis();
private static final Long DEFAULT_TABLE_EXPIRATION = CREATION_TIME + 100;
private static final String DESCRIPTION = "description";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
public class SerializationTest {

private static final Acl DOMAIN_ACCESS =
new Acl(new Acl.Domain("domain"), Acl.Role.WRITER);
Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER);
private static final Acl GROUP_ACCESS =
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER);
private static final Acl USER_ACCESS = new Acl(new Acl.User("user"), Acl.Role.OWNER);
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER);
private static final Acl USER_ACCESS = Acl.of(new Acl.User("user"), Acl.Role.OWNER);
private static final Acl VIEW_ACCESS =
new Acl(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER);
Acl.of(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER);
private static final List<Acl> ACCESS_RULES = ImmutableList.of(DOMAIN_ACCESS, GROUP_ACCESS,
VIEW_ACCESS, USER_ACCESS);
private static final Long CREATION_TIME = System.currentTimeMillis() - 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,7 @@ String toPb() {
}
}

/**
* Creates an ACL object.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public Acl(Entity entity, Role role) {
private Acl(Entity entity, Role role) {
this.entity = entity;
this.role = role;
}
Expand All @@ -299,6 +293,16 @@ public Role role() {
return role;
}

/**
* Returns an Acl object.
*
* @param entity the entity for this ACL object
* @param role the role to associate to the {@code entity} object
*/
public static Acl of(Entity entity, Role role) {
return new Acl(entity, role);
}

@Override
public int hashCode() {
return Objects.hash(entity, role);
Expand Down Expand Up @@ -333,11 +337,11 @@ ObjectAccessControl toObjectPb() {

static Acl fromPb(ObjectAccessControl objectAccessControl) {
Role role = Role.valueOf(objectAccessControl.getRole());
return new Acl(Entity.fromPb(objectAccessControl.getEntity()), role);
return Acl.of(Entity.fromPb(objectAccessControl.getEntity()), role);
}

static Acl fromPb(BucketAccessControl bucketAccessControl) {
Role role = Role.valueOf(bucketAccessControl.getRole());
return new Acl(Entity.fromPb(bucketAccessControl.getEntity()), role);
return Acl.of(Entity.fromPb(bucketAccessControl.getEntity()), role);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void testRawEntity() {


@Test
public void testAcl() {
Acl acl = new Acl(User.ofAllUsers(), Role.READER);
public void testOf() {
Acl acl = Acl.of(User.ofAllUsers(), Role.READER);
assertEquals(User.ofAllUsers(), acl.entity());
assertEquals(Role.READER, acl.role());
ObjectAccessControl objectPb = acl.toObjectPb();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
public class BlobInfoTest {

private static final List<Acl> ACL = ImmutableList.of(
new Acl(User.ofAllAuthenticatedUsers(), READER),
new Acl(new Project(VIEWERS, "p1"), WRITER));
Acl.of(User.ofAllAuthenticatedUsers(), READER),
Acl.of(new Project(VIEWERS, "p1"), WRITER));
private static final Integer COMPONENT_COUNT = 2;
private static final String CONTENT_TYPE = "text/html";
private static final String CACHE_CONTROL = "cache";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
public class BucketInfoTest {

private static final List<Acl> ACL = ImmutableList.of(
new Acl(User.ofAllAuthenticatedUsers(), Role.READER),
new Acl(new Project(VIEWERS, "p1"), Role.WRITER));
Acl.of(User.ofAllAuthenticatedUsers(), Role.READER),
Acl.of(new Project(VIEWERS, "p1"), Role.WRITER));
private static final String ETAG = "0xFF00";
private static final String ID = "B/N:1";
private static final Long META_GENERATION = 10L;
Expand All @@ -51,7 +51,7 @@ public class BucketInfoTest {
private static final Long CREATE_TIME = System.currentTimeMillis();
private static final List<Cors> CORS = Collections.singletonList(Cors.builder().build());
private static final List<Acl> DEFAULT_ACL =
Collections.singletonList(new Acl(User.ofAllAuthenticatedUsers(), Role.WRITER));
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), Role.WRITER));
private static final List<? extends DeleteRule> DELETE_RULES =
Collections.singletonList(new AgeDeleteRule(5));
private static final String INDEX_PAGE = "index.html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SerializationTest {
private static final Acl.Project ACL_PROJECT_ = new Acl.Project(ProjectRole.VIEWERS, "pid");
private static final Acl.User ACL_USER = new Acl.User("user");
private static final Acl.RawEntity ACL_RAW = new Acl.RawEntity("raw");
private static final Acl ACL = Acl.of(ACL_DOMAIN, Acl.Role.OWNER);
private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").build();
private static final BucketInfo BUCKET_INFO = BucketInfo.of("b");
private static final Cors.Origin ORIGIN = Cors.Origin.any();
Expand Down Expand Up @@ -94,11 +95,10 @@ public void testServiceOptions() throws Exception {

@Test
public void testModelAndRequests() throws Exception {
Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, BLOB_INFO,
BUCKET_INFO,
ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS,
BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS,
BUCKET_TARGET_OPTIONS};
Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL,
BLOB_INFO, BUCKET_INFO, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT,
BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS,
BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS};
for (Serializable obj : objects) {
Object copy = serializeAndDeserialize(obj);
assertEquals(obj, obj);
Expand Down

0 comments on commit 1a18033

Please sign in to comment.