Skip to content

Commit f3b4e67

Browse files
authored
Move ITBucketSnippets and improve Storage snippets tests (#1203)
1 parent 972d578 commit f3b4e67

File tree

7 files changed

+63
-32
lines changed

7 files changed

+63
-32
lines changed

gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static java.nio.charset.StandardCharsets.UTF_8;
2626

27+
import com.google.cloud.Page;
2728
import com.google.cloud.storage.Blob;
2829
import com.google.cloud.storage.Bucket;
2930
import com.google.cloud.storage.Bucket.BucketSourceOption;
@@ -110,15 +111,16 @@ public boolean delete() {
110111
* Example of listing the blobs in the bucket.
111112
*/
112113
// [TARGET list(BlobListOption...)]
113-
public Iterator<Blob> listBlobs() {
114+
public Page<Blob> listBlobs() {
114115
// [START listBlobs]
115-
Iterator<Blob> blobIterator = bucket.list().iterateAll();
116+
Page<Blob> blobs = bucket.list();
117+
Iterator<Blob> blobIterator = blobs.iterateAll();
116118
while (blobIterator.hasNext()) {
117119
Blob blob = blobIterator.next();
118120
// do something with the blob
119121
}
120122
// [END listBlobs]
121-
return blobIterator;
123+
return blobs;
122124
}
123125

124126
/**

gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName,
201201
*/
202202
// [TARGET list(BucketListOption...)]
203203
// [VARIABLE "bucket_"]
204-
public Iterator<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
204+
public Page<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
205205
// [START listBucketsWithSizeAndPrefix]
206206
Page<Bucket> buckets = storage.list(BucketListOption.pageSize(100),
207207
BucketListOption.prefix(prefix));
@@ -211,16 +211,16 @@ public Iterator<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
211211
// do something with the bucket
212212
}
213213
// [END listBucketsWithSizeAndPrefix]
214-
return bucketIterator;
214+
return buckets;
215215
}
216216

217217
/**
218-
* Example of listing buckets, specifying the page size and a name prefix.
218+
* Example of listing blobs in a provided directory.
219219
*/
220220
// [TARGET list(String, BlobListOption...)]
221221
// [VARIABLE "my_unique_bucket"]
222-
// [VARIABLE "my_directory"]
223-
public Iterator<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String directory) {
222+
// [VARIABLE "my_directory/"]
223+
public Page<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String directory) {
224224
// [START listBlobsWithDirectoryAndPrefix]
225225
Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(),
226226
BlobListOption.prefix(directory));
@@ -230,7 +230,7 @@ public Iterator<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String
230230
// do something with the blob
231231
}
232232
// [END listBlobsWithDirectoryAndPrefix]
233-
return blobIterator;
233+
return blobs;
234234
}
235235

236236
/**

gcloud-java-examples/src/test/java/com/google/cloud/example/storage/snippets/ITBlobSnippets.java renamed to gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.example.storage.snippets;
17+
package com.google.cloud.examples.storage.snippets;
1818

1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020
import static org.junit.Assert.assertArrayEquals;
@@ -24,7 +24,6 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

27-
import com.google.cloud.examples.storage.snippets.BlobSnippets;
2827
import com.google.cloud.storage.Blob;
2928
import com.google.cloud.storage.BlobId;
3029
import com.google.cloud.storage.BlobInfo;

gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import com.google.cloud.storage.Storage;
2727
import com.google.cloud.storage.StorageException;
2828
import com.google.cloud.storage.testing.RemoteStorageHelper;
29-
import com.google.common.collect.ImmutableSet;
29+
import com.google.common.collect.Sets;
3030

3131
import org.junit.AfterClass;
3232
import org.junit.BeforeClass;
3333
import org.junit.Rule;
3434
import org.junit.Test;
3535
import org.junit.rules.ExpectedException;
36+
import org.junit.rules.Timeout;
3637

37-
import java.util.Iterator;
3838
import java.util.List;
3939
import java.util.Set;
4040
import java.util.concurrent.ExecutionException;
@@ -50,14 +50,16 @@ public class ITBucketSnippets {
5050
private static final String BLOB2 = "blob2";
5151
private static final String BLOB3 = "blob3";
5252
private static final String BLOB4 = "blob4";
53-
private static final Set<String> BLOBS = ImmutableSet.of(BLOB1, BLOB2, BLOB3, BLOB4);
5453

5554
private static Storage storage;
5655
private static BucketSnippets bucketSnippets;
5756

5857
@Rule
5958
public ExpectedException thrown = ExpectedException.none();
6059

60+
@Rule
61+
public Timeout globalTimeout = Timeout.seconds(300);
62+
6163
@BeforeClass
6264
public static void beforeClass() {
6365
RemoteStorageHelper helper = RemoteStorageHelper.create();
@@ -76,7 +78,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
7678
}
7779

7880
@Test
79-
public void testBucket() {
81+
public void testBucket() throws InterruptedException {
8082
assertTrue(bucketSnippets.exists());
8183
Bucket bucket = bucketSnippets.reload();
8284
assertNotNull(bucket);
@@ -90,10 +92,15 @@ public void testBucket() {
9092
assertNotNull(blob3);
9193
Blob blob4 = bucketSnippets.createBlobFromInputStreamWithContentType(BLOB4);
9294
assertNotNull(blob4);
93-
Iterator<Blob> blobIterator = bucketSnippets.listBlobs();
94-
while (blobIterator.hasNext()) {
95-
assertTrue(BLOBS.contains(blobIterator.next().name()));
95+
Set<Blob> blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
96+
while (blobSet.size() < 4) {
97+
Thread.sleep(500);
98+
blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
9699
}
100+
assertTrue(blobSet.contains(blob1));
101+
assertTrue(blobSet.contains(blob2));
102+
assertTrue(blobSet.contains(blob3));
103+
assertTrue(blobSet.contains(blob4));
97104
blob1 = bucketSnippets.getBlob(BLOB1, blob1.generation());
98105
assertEquals(BLOB1, blob1.name());
99106
List<Blob> blobs = bucketSnippets.getBlobFromStrings(BLOB2, BLOB3);

gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,30 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

27+
import com.google.cloud.Page;
2728
import com.google.cloud.storage.Blob;
2829
import com.google.cloud.storage.BlobInfo;
2930
import com.google.cloud.storage.Bucket;
3031
import com.google.cloud.storage.Storage;
3132
import com.google.cloud.storage.StorageException;
3233
import com.google.cloud.storage.testing.RemoteStorageHelper;
34+
import com.google.common.collect.Iterators;
3335

3436
import org.junit.AfterClass;
3537
import org.junit.BeforeClass;
3638
import org.junit.Rule;
3739
import org.junit.Test;
3840
import org.junit.rules.ExpectedException;
41+
import org.junit.rules.Timeout;
3942

4043
import java.io.IOException;
4144
import java.io.InputStream;
4245
import java.net.URL;
4346
import java.net.URLConnection;
47+
import java.util.HashSet;
4448
import java.util.Iterator;
4549
import java.util.List;
50+
import java.util.Set;
4651
import java.util.concurrent.ExecutionException;
4752
import java.util.concurrent.TimeUnit;
4853
import java.util.logging.Level;
@@ -59,6 +64,9 @@ public class ITStorageSnippets {
5964
@Rule
6065
public ExpectedException thrown = ExpectedException.none();
6166

67+
@Rule
68+
public Timeout globalTimeout = Timeout.seconds(300);
69+
6270
@BeforeClass
6371
public static void beforeClass() {
6472
RemoteStorageHelper helper = RemoteStorageHelper.create();
@@ -78,7 +86,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
7886
}
7987

8088
@Test
81-
public void testBlob() {
89+
public void testBlob() throws InterruptedException {
8290
String blobName = "directory/test-blob";
8391
Blob blob = storageSnippets.createBlob(BUCKET, blobName);
8492
assertNotNull(blob);
@@ -88,19 +96,28 @@ public void testBlob() {
8896
assertNotNull(blob);
8997
blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName);
9098
assertNotNull(blob);
91-
blob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
92-
assertNotNull(blob);
93-
Iterator<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory");
94-
while (blobs.hasNext()) {
95-
assertTrue(blobs.next().name().startsWith("directory"));
96-
} blob.delete();
99+
Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
100+
assertNotNull(copiedBlob);
101+
Page<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
102+
while (Iterators.size(blobs.iterateAll()) < 2) {
103+
Thread.sleep(500);
104+
blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
105+
}
106+
Set<String> blobNames = new HashSet<>();
107+
Iterator<Blob> blobIterator = blobs.iterateAll();
108+
while (blobIterator.hasNext()) {
109+
blobNames.add(blobIterator.next().name());
110+
}
111+
assertTrue(blobNames.contains(blobName));
112+
assertTrue(blobNames.contains("directory/copy-blob"));
97113
try {
98114
storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1);
99115
fail("Expected StorageException to be thrown");
100116
} catch (StorageException ex) {
101117
// expected
102118
}
103119
assertTrue(storageSnippets.deleteBlob(BUCKET, blobName));
120+
copiedBlob.delete();
104121
}
105122

106123
@Test
@@ -136,10 +153,15 @@ public void testGetBucketWithMetageneration() {
136153
}
137154

138155
@Test
139-
public void testListBucketsWithSizeAndPrefix() {
140-
Iterator<Bucket> buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
141-
while (buckets.hasNext()) {
142-
assertTrue(buckets.next().name().startsWith(BUCKET));
156+
public void testListBucketsWithSizeAndPrefix() throws InterruptedException {
157+
Page<Bucket> buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
158+
while (Iterators.size(buckets.iterateAll()) < 1) {
159+
Thread.sleep(500);
160+
buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
161+
}
162+
Iterator<Bucket> bucketIterator = buckets.iterateAll();
163+
while (bucketIterator.hasNext()) {
164+
assertTrue(bucketIterator.next().name().startsWith(BUCKET));
143165
}
144166
}
145167

gcloud-java-storage/src/main/java/com/google/cloud/storage/Bucket.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ public boolean delete(BucketSourceOption... options) {
618618
*
619619
* <p>Example of listing the blobs in the bucket.
620620
* <pre> {@code
621-
* Iterator<Blob> blobIterator = bucket.list().iterateAll();
621+
* Page<Blob> blobs = bucket.list();
622+
* Iterator<Blob> blobIterator = blobs.iterateAll();
622623
* while (blobIterator.hasNext()) {
623624
* Blob blob = blobIterator.next();
624625
* // do something with the blob

gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,10 +1371,10 @@ public static Builder builder() {
13711371
* Lists the bucket's blobs. If the {@link BlobListOption#currentDirectory()} option is provided,
13721372
* results are returned in a directory-like mode.
13731373
*
1374-
* <p>Example of listing buckets, specifying the page size and a name prefix.
1374+
* <p>Example of listing blobs in a provided directory.
13751375
* <pre> {@code
13761376
* String bucketName = "my_unique_bucket";
1377-
* String directory = "my_directory";
1377+
* String directory = "my_directory/";
13781378
* Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(),
13791379
* BlobListOption.prefix(directory));
13801380
* Iterator<Blob> blobIterator = blobs.iterateAll();

0 commit comments

Comments
 (0)