Skip to content

Commit 1a18033

Browse files
committed
Merge pull request #571 from mziccard/acl-of
Adds of factory method to bigquery and storage Acl
2 parents 0c3e935 + c411b23 commit 1a18033

File tree

11 files changed

+58
-48
lines changed

11 files changed

+58
-48
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Acl.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,11 @@ Access toPb() {
370370
}
371371
}
372372

373-
/**
374-
* Build an ACL for an {@code entity} and a {@code role}.
375-
*/
376-
public Acl(Entity entity, Role role) {
373+
private Acl(Entity entity, Role role) {
377374
this.entity = checkNotNull(entity);
378375
this.role = role;
379376
}
380377

381-
/**
382-
* Build an ACL for a view entity.
383-
*/
384-
public Acl(View view) {
385-
this.entity = checkNotNull(view);
386-
this.role = null;
387-
}
388-
389378
/**
390379
* Returns the entity for this ACL.
391380
*/
@@ -400,6 +389,23 @@ public Role role() {
400389
return role;
401390
}
402391

392+
/**
393+
* Returns an Acl object.
394+
*
395+
* @param entity the entity for this ACL object
396+
* @param role the role to associate to the {@code entity} object
397+
*/
398+
public static Acl of(Entity entity, Role role) {
399+
return new Acl(entity, role);
400+
}
401+
402+
/**
403+
* Returns an Acl object for a view entity.
404+
*/
405+
public static Acl of(View view) {
406+
return new Acl(view, null);
407+
}
408+
403409
@Override
404410
public int hashCode() {
405411
return Objects.hash(entity, role);
@@ -432,7 +438,7 @@ Access toPb() {
432438
}
433439

434440
static Acl fromPb(Access access) {
435-
return new Acl(Entity.fromPb(access),
441+
return Acl.of(Entity.fromPb(access),
436442
access.getRole() != null ? Role.valueOf(access.getRole()) : null);
437443
}
438444
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ private DatasetInfo setProjectId(DatasetInfo dataset) {
621621
if (viewReferencePb.getProjectId() == null) {
622622
viewReferencePb.setProjectId(options().projectId());
623623
}
624-
acls.add(new Acl(new Acl.View(TableId.fromPb(viewReferencePb))));
624+
acls.add(Acl.of(new Acl.View(TableId.fromPb(viewReferencePb))));
625625
} else {
626626
acls.add(acl);
627627
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/AclTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public void testViewEntity() {
8181
}
8282

8383
@Test
84-
public void testAcl() {
85-
Acl acl = new Acl(Group.ofAllAuthenticatedUsers(), Role.READER);
84+
public void testOf() {
85+
Acl acl = Acl.of(Group.ofAllAuthenticatedUsers(), Role.READER);
8686
assertEquals(Group.ofAllAuthenticatedUsers(), acl.entity());
8787
assertEquals(Role.READER, acl.role());
8888
Dataset.Access pb = acl.toPb();
8989
assertEquals(acl, Acl.fromPb(pb));
9090
View view = new View(TableId.of("project", "dataset", "view"));
91-
acl = new Acl(view);
91+
acl = Acl.of(view);
9292
assertEquals(view, acl.entity());
9393
assertEquals(null, acl.role());
9494
}

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public class BigQueryImplTest {
6868
private static final String OTHER_TABLE = "otherTable";
6969
private static final String OTHER_DATASET = "otherDataset";
7070
private static final List<Acl> ACCESS_RULES = ImmutableList.of(
71-
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
72-
new Acl(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER));
71+
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
72+
Acl.of(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER));
7373
private static final List<Acl> ACCESS_RULES_WITH_PROJECT = ImmutableList.of(
74-
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
75-
new Acl(new Acl.View(TableId.of(PROJECT, "dataset", "table"))));
74+
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
75+
Acl.of(new Acl.View(TableId.of(PROJECT, "dataset", "table"))));
7676
private static final DatasetInfo DATASET_INFO = DatasetInfo.builder(DATASET)
7777
.acl(ACCESS_RULES)
7878
.description("description")

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetInfoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
public class DatasetInfoTest {
2929

3030
private static final List<Acl> ACCESS_RULES = ImmutableList.of(
31-
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
32-
new Acl(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER));
31+
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER),
32+
Acl.of(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER));
3333
private static final Long CREATION_TIME = System.currentTimeMillis();
3434
private static final Long DEFAULT_TABLE_EXPIRATION = CREATION_TIME + 100;
3535
private static final String DESCRIPTION = "description";

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
public class SerializationTest {
4343

4444
private static final Acl DOMAIN_ACCESS =
45-
new Acl(new Acl.Domain("domain"), Acl.Role.WRITER);
45+
Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER);
4646
private static final Acl GROUP_ACCESS =
47-
new Acl(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER);
48-
private static final Acl USER_ACCESS = new Acl(new Acl.User("user"), Acl.Role.OWNER);
47+
Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER);
48+
private static final Acl USER_ACCESS = Acl.of(new Acl.User("user"), Acl.Role.OWNER);
4949
private static final Acl VIEW_ACCESS =
50-
new Acl(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER);
50+
Acl.of(new Acl.View(TableId.of("project", "dataset", "table")), Acl.Role.WRITER);
5151
private static final List<Acl> ACCESS_RULES = ImmutableList.of(DOMAIN_ACCESS, GROUP_ACCESS,
5252
VIEW_ACCESS, USER_ACCESS);
5353
private static final Long CREATION_TIME = System.currentTimeMillis() - 10;

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Acl.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,7 @@ String toPb() {
274274
}
275275
}
276276

277-
/**
278-
* Creates an ACL object.
279-
*
280-
* @param entity the entity for this ACL object
281-
* @param role the role to associate to the {@code entity} object
282-
*/
283-
public Acl(Entity entity, Role role) {
277+
private Acl(Entity entity, Role role) {
284278
this.entity = entity;
285279
this.role = role;
286280
}
@@ -299,6 +293,16 @@ public Role role() {
299293
return role;
300294
}
301295

296+
/**
297+
* Returns an Acl object.
298+
*
299+
* @param entity the entity for this ACL object
300+
* @param role the role to associate to the {@code entity} object
301+
*/
302+
public static Acl of(Entity entity, Role role) {
303+
return new Acl(entity, role);
304+
}
305+
302306
@Override
303307
public int hashCode() {
304308
return Objects.hash(entity, role);
@@ -333,11 +337,11 @@ ObjectAccessControl toObjectPb() {
333337

334338
static Acl fromPb(ObjectAccessControl objectAccessControl) {
335339
Role role = Role.valueOf(objectAccessControl.getRole());
336-
return new Acl(Entity.fromPb(objectAccessControl.getEntity()), role);
340+
return Acl.of(Entity.fromPb(objectAccessControl.getEntity()), role);
337341
}
338342

339343
static Acl fromPb(BucketAccessControl bucketAccessControl) {
340344
Role role = Role.valueOf(bucketAccessControl.getRole());
341-
return new Acl(Entity.fromPb(bucketAccessControl.getEntity()), role);
345+
return Acl.of(Entity.fromPb(bucketAccessControl.getEntity()), role);
342346
}
343347
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/AclTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public void testRawEntity() {
8282

8383

8484
@Test
85-
public void testAcl() {
86-
Acl acl = new Acl(User.ofAllUsers(), Role.READER);
85+
public void testOf() {
86+
Acl acl = Acl.of(User.ofAllUsers(), Role.READER);
8787
assertEquals(User.ofAllUsers(), acl.entity());
8888
assertEquals(Role.READER, acl.role());
8989
ObjectAccessControl objectPb = acl.toObjectPb();

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobInfoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
public class BlobInfoTest {
3535

3636
private static final List<Acl> ACL = ImmutableList.of(
37-
new Acl(User.ofAllAuthenticatedUsers(), READER),
38-
new Acl(new Project(VIEWERS, "p1"), WRITER));
37+
Acl.of(User.ofAllAuthenticatedUsers(), READER),
38+
Acl.of(new Project(VIEWERS, "p1"), WRITER));
3939
private static final Integer COMPONENT_COUNT = 2;
4040
private static final String CONTENT_TYPE = "text/html";
4141
private static final String CACHE_CONTROL = "cache";

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketInfoTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
public class BucketInfoTest {
4242

4343
private static final List<Acl> ACL = ImmutableList.of(
44-
new Acl(User.ofAllAuthenticatedUsers(), Role.READER),
45-
new Acl(new Project(VIEWERS, "p1"), Role.WRITER));
44+
Acl.of(User.ofAllAuthenticatedUsers(), Role.READER),
45+
Acl.of(new Project(VIEWERS, "p1"), Role.WRITER));
4646
private static final String ETAG = "0xFF00";
4747
private static final String ID = "B/N:1";
4848
private static final Long META_GENERATION = 10L;
@@ -51,7 +51,7 @@ public class BucketInfoTest {
5151
private static final Long CREATE_TIME = System.currentTimeMillis();
5252
private static final List<Cors> CORS = Collections.singletonList(Cors.builder().build());
5353
private static final List<Acl> DEFAULT_ACL =
54-
Collections.singletonList(new Acl(User.ofAllAuthenticatedUsers(), Role.WRITER));
54+
Collections.singletonList(Acl.of(User.ofAllAuthenticatedUsers(), Role.WRITER));
5555
private static final List<? extends DeleteRule> DELETE_RULES =
5656
Collections.singletonList(new AgeDeleteRule(5));
5757
private static final String INDEX_PAGE = "index.html";

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class SerializationTest {
4747
private static final Acl.Project ACL_PROJECT_ = new Acl.Project(ProjectRole.VIEWERS, "pid");
4848
private static final Acl.User ACL_USER = new Acl.User("user");
4949
private static final Acl.RawEntity ACL_RAW = new Acl.RawEntity("raw");
50+
private static final Acl ACL = Acl.of(ACL_DOMAIN, Acl.Role.OWNER);
5051
private static final BlobInfo BLOB_INFO = BlobInfo.builder("b", "n").build();
5152
private static final BucketInfo BUCKET_INFO = BucketInfo.of("b");
5253
private static final Cors.Origin ORIGIN = Cors.Origin.any();
@@ -94,11 +95,10 @@ public void testServiceOptions() throws Exception {
9495

9596
@Test
9697
public void testModelAndRequests() throws Exception {
97-
Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, BLOB_INFO,
98-
BUCKET_INFO,
99-
ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS,
100-
BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS,
101-
BUCKET_TARGET_OPTIONS};
98+
Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL,
99+
BLOB_INFO, BUCKET_INFO, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT,
100+
BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, BUCKET_LIST_OPTIONS,
101+
BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS};
102102
for (Serializable obj : objects) {
103103
Object copy = serializeAndDeserialize(obj);
104104
assertEquals(obj, obj);

0 commit comments

Comments
 (0)