3333import com .google .cloud .RetryHelper .RetryHelperException ;
3434import com .google .cloud .http .BaseHttpServiceException ;
3535import com .google .cloud .storage .Acl ;
36+ import com .google .cloud .storage .Acl .Entity ;
37+ import com .google .cloud .storage .Acl .Project .ProjectRole ;
3638import com .google .cloud .storage .Acl .Role ;
3739import com .google .cloud .storage .Acl .User ;
3840import com .google .cloud .storage .Blob ;
7072import java .util .Map ;
7173import java .util .Set ;
7274import java .util .concurrent .Callable ;
75+ import java .util .function .Predicate ;
7376import java .util .stream .Collector ;
7477import java .util .stream .Collectors ;
7578import org .junit .Ignore ;
@@ -207,6 +210,7 @@ public void bucket_defaultAcl_create() throws Exception {
207210 List <Acl > expectedAcls = dropEtags (bucket .getDefaultAcl ());
208211 List <Acl > actualAcls = dropEtags (bucketUpdated .getDefaultAcl ());
209212 assertThat (actualAcls ).containsAtLeastElementsIn (expectedAcls );
213+ assertThat (actualAcls ).contains (readAll );
210214 }
211215 }
212216
@@ -230,12 +234,47 @@ public void bucket_defaultAcl_update() throws Exception {
230234 TemporaryBucket .newBuilder ().setBucketInfo (bucketInfo ).setStorage (storage ).build ()) {
231235 BucketInfo bucket = tempB .getBucket ();
232236
233- Acl readAll = Acl .of (User .ofAllAuthenticatedUsers (), Role .READER );
234- Acl actual = retry429s (() -> storage .updateDefaultAcl (bucket .getName (), readAll ), storage );
237+ List <Acl > defaultAcls = bucket .getDefaultAcl ();
238+ System .out .println ("defaultAcls = " + defaultAcls );
239+ assertThat (defaultAcls ).isNotEmpty ();
235240
236- assertThat (actual .getEntity ()).isEqualTo (readAll .getEntity ());
237- assertThat (actual .getRole ()).isEqualTo (readAll .getRole ());
241+ Predicate <Acl > isProjectEditor = hasProjectRole (ProjectRole .EDITORS );
242+
243+ //noinspection OptionalGetWithoutIsPresent
244+ Acl projectEditorAsOwner =
245+ defaultAcls .stream ().filter (hasRole (Role .OWNER ).and (isProjectEditor )).findFirst ().get ();
246+ System .out .println ("projectEditorAsOwner = " + projectEditorAsOwner );
247+
248+ // lower the privileges of project editors to writer from owner
249+ Entity entity = projectEditorAsOwner .getEntity ();
250+ System .out .println ("entity = " + entity );
251+ Acl projectEditorAsReader = Acl .of (entity , Role .READER );
252+ System .out .println ("projectEditorAsReader = " + projectEditorAsReader );
253+
254+ Acl actual =
255+ retry429s (
256+ () -> storage .updateDefaultAcl (bucket .getName (), projectEditorAsReader ), storage );
257+
258+ assertThat (actual .getEntity ()).isEqualTo (projectEditorAsReader .getEntity ());
259+ assertThat (actual .getRole ()).isEqualTo (projectEditorAsReader .getRole ());
238260 assertThat (actual .getEtag ()).isNotEmpty ();
261+
262+ Bucket bucketUpdated =
263+ storage .get (bucket .getName (), BucketGetOption .fields (BucketField .values ()));
264+ assertThat (bucketUpdated .getMetageneration ()).isNotEqualTo (bucket .getMetageneration ());
265+
266+ // etags change when updates happen, drop before our comparison
267+ List <Acl > expectedAcls =
268+ dropEtags (
269+ bucket .getDefaultAcl ().stream ()
270+ .filter (isProjectEditor .negate ())
271+ .collect (Collectors .toList ()));
272+ System .out .println ("expectedAcls = " + expectedAcls );
273+ List <Acl > actualAcls = dropEtags (bucketUpdated .getDefaultAcl ());
274+ System .out .println ("actualAcls = " + actualAcls );
275+ assertThat (actualAcls ).containsAtLeastElementsIn (expectedAcls );
276+ assertThat (actualAcls ).doesNotContain (projectEditorAsOwner );
277+ assertThat (actualAcls ).contains (projectEditorAsReader );
239278 }
240279 }
241280
@@ -1098,4 +1137,18 @@ private static ImmutableList<Acl> dropEtags(List<Acl> defaultAcls) {
10981137 .map (acl -> Acl .of (acl .getEntity (), acl .getRole ()))
10991138 .collect (ImmutableList .toImmutableList ());
11001139 }
1140+
1141+ private static Predicate <Acl > hasRole (Acl .Role expected ) {
1142+ return acl -> acl .getRole ().equals (expected );
1143+ }
1144+
1145+ private static Predicate <Acl > hasProjectRole (Acl .Project .ProjectRole expected ) {
1146+ return acl -> {
1147+ Entity entity = acl .getEntity ();
1148+ if (entity .getType ().equals (Entity .Type .PROJECT )) {
1149+ return ((Acl .Project ) entity ).getProjectRole ().equals (expected );
1150+ }
1151+ return false ;
1152+ };
1153+ }
11011154}
0 commit comments