Skip to content

Commit

Permalink
fix: FakeStorageRpc#list to filtering the files by bucket and prefix (#…
Browse files Browse the repository at this point in the history
…208)

* fix: list method of FakeStorageRpc

* fix: list method of FakeStorageRpc
  • Loading branch information
athakor authored Sep 9, 2020
1 parent 33fdc31 commit 21f606e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
continue;
}
so.setSize(size(so));
values.add(so);
if (so.getBucket().equals(bucket)) {
values.add(so);
}
}
values.addAll(folders.values());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertSame;

import com.google.api.client.http.HttpResponseException;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
Expand All @@ -37,8 +38,10 @@
import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem;
import com.google.cloud.storage.contrib.nio.CloudStorageFileSystemProvider;
import com.google.cloud.storage.contrib.nio.CloudStoragePath;
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
Expand All @@ -62,6 +65,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand Down Expand Up @@ -1074,6 +1078,32 @@ public void testCopyWithDifferentProvider() throws IOException {
assertNotEquals(sourceFileSystem.config(), targetFileSystem.config());
}

@Test
public void testListObject() throws IOException {
String firstBucket = "first-bucket-" + UUID.randomUUID().toString();
String secondBucket = "second-bucket" + UUID.randomUUID().toString();
Storage localStorageService = LocalStorageHelper.customOptions(true).getService();
fillFile(localStorageService, firstBucket, "object", SML_SIZE);
fillFile(localStorageService, firstBucket, "test-object", SML_SIZE);
fillFile(localStorageService, secondBucket, "test-object", SML_SIZE);

// Listing objects from first bucket without prefix.
List<Blob> objects = Lists.newArrayList(localStorageService.list(firstBucket).getValues());
assertThat(objects.size()).isEqualTo(2);

// Listing objects from first bucket with prefix.
objects =
Lists.newArrayList(
localStorageService
.list(firstBucket, Storage.BlobListOption.prefix("test-"))
.getValues());
assertThat(objects.size()).isEqualTo(1);

// Listing objects from second bucket.
objects = Lists.newArrayList(localStorageService.list(secondBucket).getValues());
assertThat(objects.size()).isEqualTo(1);
}

private CloudStorageFileSystem getTestBucket() throws IOException {
// in typical usage we use the single-argument version of forBucket
// and rely on the user being logged into their project with the
Expand Down

0 comments on commit 21f606e

Please sign in to comment.