Skip to content

Commit 4eaa511

Browse files
committed
chore: review fixes
1 parent 5180f5e commit 4eaa511

2 files changed

Lines changed: 51 additions & 84 deletions

File tree

google-cloud-storage/src/main/java/com/google/cloud/storage/JsonConversions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.storage.Storage.BucketField.IP_FILTER;
2020
import static com.google.cloud.storage.Storage.BucketField.SOFT_DELETE_POLICY;
21+
import static com.google.cloud.storage.Utils.bucketNameCodec;
2122
import static com.google.cloud.storage.Utils.dateTimeCodec;
2223
import static com.google.cloud.storage.Utils.durationSecondsCodec;
2324
import static com.google.cloud.storage.Utils.ifNonNull;
@@ -609,7 +610,7 @@ private Bucket bucketInfoEncode(BucketInfo from) {
609610

610611
@SuppressWarnings("deprecation")
611612
private BucketInfo bucketInfoDecode(com.google.api.services.storage.model.Bucket from) {
612-
BucketInfo.Builder to = new BucketInfo.BuilderImpl(from.getName());
613+
BucketInfo.Builder to = new BucketInfo.BuilderImpl(bucketNameCodec.decode(from.getName()));
613614
ifNonNull(from.getProjectNumber(), to::setProject);
614615
ifNonNull(from.getAcl(), toListOf(bucketAcl()::decode), to::setAcl);
615616
ifNonNull(from.getCors(), toListOf(cors()::decode), to::setCors);

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITListBucketTest.java

Lines changed: 49 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717
package com.google.cloud.storage.it;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.junit.Assert.assertTrue;
2120

2221
import com.google.api.gax.paging.Page;
2322
import com.google.cloud.storage.Bucket;
2423
import com.google.cloud.storage.BucketInfo;
2524
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.Storage.BucketListOption;
2626
import com.google.cloud.storage.TransportCompatibility.Transport;
2727
import com.google.cloud.storage.it.runner.StorageITRunner;
2828
import com.google.cloud.storage.it.runner.annotations.Backend;
29+
import com.google.cloud.storage.it.runner.annotations.BucketFixture;
30+
import com.google.cloud.storage.it.runner.annotations.BucketType;
2931
import com.google.cloud.storage.it.runner.annotations.CrossRun;
3032
import com.google.cloud.storage.it.runner.annotations.Inject;
31-
import com.google.common.collect.ImmutableList;
32-
import com.google.common.collect.Iterables;
33-
import java.util.stream.StreamSupport;
34-
import org.junit.After;
35-
import org.junit.Before;
33+
import com.google.cloud.storage.it.runner.registry.Generator;
34+
import com.google.common.collect.ImmutableMap;
35+
import java.util.Map;
36+
import java.util.stream.Collectors;
3637
import org.junit.Test;
3738
import org.junit.runner.RunWith;
3839

@@ -43,93 +44,58 @@
4344
public class ITListBucketTest {
4445
@Inject public Storage storage;
4546

46-
private static final String NORMAL_BUCKET_NAME = "normal_bucket";
47-
// For testing purposes, the TESTBENCH considers a bucket to be unreachable if the bucket name
48-
// contains "unreachable"
49-
private static final String UNREACHABLE_BUCKET_NAME_1 = "unreachable_bucket_1";
50-
private static final String UNREACHABLE_BUCKET_NAME_2 = "unreachable_bucket_2";
47+
@Inject public BucketInfo defaultBucket;
5148

52-
// The unreachable buckets are returned as a list of bucket resource names in string form. (e.g.
53-
// "projects/_/buckets/bucket1")
54-
private static final String EXPECTED_UNREACHABLE_BUCKET_NAME_1 =
55-
"projects/_/buckets/" + UNREACHABLE_BUCKET_NAME_1;
56-
private static final String EXPECTED_UNREACHABLE_BUCKET_NAME_2 =
57-
"projects/_/buckets/" + UNREACHABLE_BUCKET_NAME_2;
49+
@Inject
50+
@BucketFixture(BucketType.HNS)
51+
public BucketInfo hnsBucket;
5852

59-
@Before
60-
public void setup() {
61-
Bucket normalBucket = storage.create(BucketInfo.of(NORMAL_BUCKET_NAME));
62-
Bucket unreachableBucket = storage.create(BucketInfo.of(UNREACHABLE_BUCKET_NAME_1));
63-
}
64-
65-
@After
66-
public void tearDown() {
67-
BucketCleaner.doCleanup(NORMAL_BUCKET_NAME, storage);
68-
BucketCleaner.doCleanup(UNREACHABLE_BUCKET_NAME_1, storage);
69-
}
53+
@Inject public Generator generator;
7054

7155
@Test
72-
public void testListBucketWithPartialSuccess() {
73-
Page<Bucket> page = storage.list(Storage.BucketListOption.returnPartialSuccess(true));
74-
Iterable<Bucket> allBuckets = page.getValues();
75-
76-
Bucket actualNormalBucket =
77-
Iterables.getOnlyElement(
78-
Iterables.filter(allBuckets, b -> b.getName().equals(NORMAL_BUCKET_NAME)));
79-
80-
Bucket actualUnreachableBucket =
81-
Iterables.getOnlyElement(
82-
Iterables.filter(allBuckets, b -> b.getName().contains(UNREACHABLE_BUCKET_NAME_1)));
83-
84-
assertThat(actualNormalBucket.getName()).isEqualTo(NORMAL_BUCKET_NAME);
85-
assertThat(actualUnreachableBucket.getName()).isEqualTo(EXPECTED_UNREACHABLE_BUCKET_NAME_1);
86-
assertTrue(
87-
"The unreachable bucket must have the isUnreachable flag set to true",
88-
actualUnreachableBucket.isUnreachable());
56+
public void testListBucketWithPartialSuccess() throws Exception {
57+
doTest(Reachability.Unreachable, BucketListOption.returnPartialSuccess(true));
8958
}
9059

9160
@Test
92-
public void testMultipleUnreachableBuckets() {
93-
Bucket unreachableBucket2 = storage.create(BucketInfo.of(UNREACHABLE_BUCKET_NAME_2));
94-
95-
try {
96-
Page<Bucket> page = storage.list(Storage.BucketListOption.returnPartialSuccess(true));
97-
Iterable<Bucket> allBuckets = page.getValues();
98-
99-
Bucket actualNormalBucket =
100-
Iterables.getOnlyElement(
101-
Iterables.filter(allBuckets, b -> b.getName().equals(NORMAL_BUCKET_NAME)));
102-
103-
Bucket actualUnreachableBucket1 =
104-
Iterables.getOnlyElement(
105-
Iterables.filter(allBuckets, b -> b.getName().contains(UNREACHABLE_BUCKET_NAME_1)));
106-
107-
Bucket actualUnreachableBucket2 =
108-
Iterables.getOnlyElement(
109-
Iterables.filter(allBuckets, b -> b.getName().contains(UNREACHABLE_BUCKET_NAME_2)));
61+
public void testListBucketWithoutPartialSuccess() throws Exception {
62+
doTest(Reachability.Reachable);
63+
}
11064

111-
assertThat(actualNormalBucket.getName()).isEqualTo(NORMAL_BUCKET_NAME);
112-
assertThat(actualUnreachableBucket1.getName()).isEqualTo(EXPECTED_UNREACHABLE_BUCKET_NAME_1);
113-
assertTrue(
114-
"The unreachable bucket 1 must have the isUnreachable flag set to true",
115-
actualUnreachableBucket1.isUnreachable());
116-
assertThat(actualUnreachableBucket2.getName()).isEqualTo(EXPECTED_UNREACHABLE_BUCKET_NAME_2);
117-
assertTrue(
118-
"The unreachable bucket 2 must have the isUnreachable flag set to true",
119-
actualUnreachableBucket2.isUnreachable());
120-
} finally {
121-
BucketCleaner.doCleanup(UNREACHABLE_BUCKET_NAME_2, storage);
65+
private void doTest(
66+
Reachability expectedReachabilityOfUnreachableBucket, BucketListOption... bucketListOption)
67+
throws Exception {
68+
// TESTBENCH considers a bucket to be unreachable if the bucket name contains "unreachable"
69+
String name = generator.randomBucketName() + ".unreachable";
70+
BucketInfo info = BucketInfo.of(name);
71+
try (TemporaryBucket tmpBucket =
72+
TemporaryBucket.newBuilder().setBucketInfo(info).setStorage(storage).build()) {
73+
// bucket name to unreachable status
74+
Map<String, Reachability> expected =
75+
ImmutableMap.of(
76+
defaultBucket.getName(), Reachability.Reachable,
77+
hnsBucket.getName(), Reachability.Reachable,
78+
tmpBucket.getBucket().getName(), expectedReachabilityOfUnreachableBucket);
79+
80+
Page<Bucket> page = storage.list(bucketListOption);
81+
82+
Map<String, Reachability> actual =
83+
page.streamAll().collect(Collectors.toMap(BucketInfo::getName, Reachability::forBucket));
84+
85+
assertThat(actual).containsAtLeastEntriesIn(expected);
12286
}
12387
}
12488

125-
@Test
126-
public void testListBucketWithoutPartialSuccess() {
127-
Page<Bucket> page = storage.list();
128-
ImmutableList<String> bucketNames =
129-
StreamSupport.stream(page.iterateAll().spliterator(), false)
130-
.map(Bucket::getName)
131-
.collect(ImmutableList.toImmutableList());
132-
assertThat(bucketNames).contains(NORMAL_BUCKET_NAME);
133-
assertThat(bucketNames).doesNotContain(EXPECTED_UNREACHABLE_BUCKET_NAME_1);
89+
private enum Reachability {
90+
Reachable,
91+
Unreachable;
92+
93+
static Reachability forBucket(BucketInfo b) {
94+
if (b.isUnreachable() != null && b.isUnreachable()) {
95+
return Unreachable;
96+
} else {
97+
return Reachable;
98+
}
99+
}
134100
}
135101
}

0 commit comments

Comments
 (0)