Skip to content

Commit 438896b

Browse files
committed
add list versioned blobs to integration tests
1 parent 21e68cc commit 438896b

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.gcloud.RetryParams;
2121
import com.google.gcloud.storage.BlobInfo;
2222
import com.google.gcloud.storage.Storage;
23+
import com.google.gcloud.storage.Storage.BlobListOption;
2324
import com.google.gcloud.storage.StorageException;
2425
import com.google.gcloud.storage.StorageOptions;
2526

@@ -173,8 +174,8 @@ public DeleteBucketTask(Storage storage, String bucket) {
173174
@Override
174175
public Boolean call() {
175176
while (true) {
176-
for (BlobInfo info : storage.list(bucket).values()) {
177-
storage.delete(bucket, info.name());
177+
for (BlobInfo info : storage.list(bucket, BlobListOption.versions(true)).values()) {
178+
storage.delete(info.blobId());
178179
}
179180
try {
180181
storage.delete(bucket);

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.ImmutableList;
2323
import com.google.gcloud.Page;
24+
import com.google.gcloud.storage.Storage.BlobListOption;
2425
import com.google.gcloud.storage.testing.RemoteGcsHelper;
2526

2627
import org.easymock.EasyMock;
@@ -117,9 +118,10 @@ public Iterator<Blob> iterateAll() {
117118
@Test
118119
public void testForceDelete() throws InterruptedException, ExecutionException {
119120
Storage storageMock = EasyMock.createMock(Storage.class);
120-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
121+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
122+
.andReturn(blobPage);
121123
for (BlobInfo info : blobList) {
122-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
124+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
123125
}
124126
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
125127
EasyMock.replay(storageMock);
@@ -132,7 +134,7 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
132134
Storage storageMock = EasyMock.createMock(Storage.class);
133135
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage).anyTimes();
134136
for (BlobInfo info : blobList) {
135-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true).anyTimes();
137+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true).anyTimes();
136138
}
137139
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(RETRYABLE_EXCEPTION).anyTimes();
138140
EasyMock.replay(storageMock);
@@ -143,9 +145,10 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
143145
@Test
144146
public void testForceDeleteFail() throws InterruptedException, ExecutionException {
145147
Storage storageMock = EasyMock.createMock(Storage.class);
146-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
148+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
149+
.andReturn(blobPage);
147150
for (BlobInfo info : blobList) {
148-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
151+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
149152
}
150153
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
151154
EasyMock.replay(storageMock);
@@ -160,9 +163,10 @@ public void testForceDeleteFail() throws InterruptedException, ExecutionExceptio
160163
@Test
161164
public void testForceDeleteNoTimeout() {
162165
Storage storageMock = EasyMock.createMock(Storage.class);
163-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
166+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
167+
.andReturn(blobPage);
164168
for (BlobInfo info : blobList) {
165-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
169+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
166170
}
167171
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
168172
EasyMock.replay(storageMock);
@@ -173,9 +177,10 @@ public void testForceDeleteNoTimeout() {
173177
@Test
174178
public void testForceDeleteNoTimeoutFail() {
175179
Storage storageMock = EasyMock.createMock(Storage.class);
176-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
180+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
181+
.andReturn(blobPage);
177182
for (BlobInfo info : blobList) {
178-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
183+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
179184
}
180185
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
181186
EasyMock.replay(storageMock);

gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,14 @@
2828
import com.google.api.client.util.Lists;
2929
import com.google.common.collect.ImmutableList;
3030
import com.google.common.collect.ImmutableMap;
31+
import com.google.common.collect.ImmutableSet;
3132
import com.google.gcloud.Page;
3233
import com.google.gcloud.ReadChannel;
3334
import com.google.gcloud.RestorableState;
3435
import com.google.gcloud.WriteChannel;
35-
import com.google.gcloud.storage.BatchRequest;
36-
import com.google.gcloud.storage.BatchResponse;
37-
import com.google.gcloud.storage.Blob;
38-
import com.google.gcloud.storage.BlobId;
39-
import com.google.gcloud.storage.BlobInfo;
40-
import com.google.gcloud.storage.Bucket;
41-
import com.google.gcloud.storage.BucketInfo;
42-
import com.google.gcloud.storage.CopyWriter;
43-
import com.google.gcloud.storage.HttpMethod;
44-
import com.google.gcloud.storage.Storage;
36+
import com.google.gcloud.storage.*;
4537
import com.google.gcloud.storage.Storage.BlobField;
4638
import com.google.gcloud.storage.Storage.BucketField;
47-
import com.google.gcloud.storage.StorageException;
4839
import com.google.gcloud.storage.testing.RemoteGcsHelper;
4940

5041
import org.junit.AfterClass;
@@ -63,6 +54,7 @@
6354
import java.util.List;
6455
import java.util.Map;
6556
import java.util.Random;
57+
import java.util.Set;
6658
import java.util.concurrent.ExecutionException;
6759
import java.util.concurrent.TimeUnit;
6860
import java.util.logging.Level;
@@ -302,8 +294,7 @@ public void testListBlobsSelectedFields() {
302294
Blob remoteBlob2 = storage.create(blob2);
303295
assertNotNull(remoteBlob1);
304296
assertNotNull(remoteBlob2);
305-
Page<Blob> page =
306-
storage.list(BUCKET,
297+
Page<Blob> page = storage.list(BUCKET,
307298
Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"),
308299
Storage.BlobListOption.fields(BlobField.METADATA));
309300
int index = 0;
@@ -331,8 +322,7 @@ public void testListBlobsEmptySelectedFields() {
331322
Blob remoteBlob2 = storage.create(blob2);
332323
assertNotNull(remoteBlob1);
333324
assertNotNull(remoteBlob2);
334-
Page<Blob> page = storage.list(
335-
BUCKET,
325+
Page<Blob> page = storage.list(BUCKET,
336326
Storage.BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"),
337327
Storage.BlobListOption.fields());
338328
int index = 0;
@@ -345,6 +335,41 @@ public void testListBlobsEmptySelectedFields() {
345335
assertTrue(remoteBlob2.delete());
346336
}
347337

338+
@Test
339+
public void testListBlobsVersioned() throws ExecutionException, InterruptedException {
340+
String bucketName = RemoteGcsHelper.generateBucketName();
341+
Bucket bucket = storage.create(BucketInfo.builder(bucketName).versioningEnabled(true).build());
342+
try {
343+
String[] blobNames = {"test-list-blobs-versioned-blob1", "test-list-blobs-versioned-blob2"};
344+
BlobInfo blob1 = BlobInfo.builder(bucket, blobNames[0])
345+
.contentType(CONTENT_TYPE)
346+
.build();
347+
BlobInfo blob2 = BlobInfo.builder(bucket, blobNames[1])
348+
.contentType(CONTENT_TYPE)
349+
.build();
350+
Blob remoteBlob1 = storage.create(blob1);
351+
Blob remoteBlob2 = storage.create(blob2);
352+
Blob remoteBlob3 = storage.create(blob2);
353+
assertNotNull(remoteBlob1);
354+
assertNotNull(remoteBlob2);
355+
assertNotNull(remoteBlob3);
356+
Page<Blob> page = storage.list(bucketName,
357+
Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"),
358+
Storage.BlobListOption.versions(true));
359+
Set<String> blobSet = ImmutableSet.of(blobNames[0], blobNames[1]);
360+
for (Blob remoteBlob : page.values()) {
361+
assertEquals(bucketName, remoteBlob.bucket());
362+
assertTrue(blobSet.contains(remoteBlob.name()));
363+
assertNotNull(remoteBlob.generation());
364+
}
365+
assertTrue(remoteBlob1.delete());
366+
assertTrue(remoteBlob2.delete());
367+
assertTrue(remoteBlob3.delete());
368+
} finally {
369+
RemoteGcsHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
370+
}
371+
}
372+
348373
@Test
349374
public void testUpdateBlob() {
350375
String blobName = "test-update-blob";

0 commit comments

Comments
 (0)