Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ITBucketSnippets and improve Storage snippets tests #1203

Merged
merged 3 commits into from
Aug 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

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

import com.google.cloud.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Bucket.BucketSourceOption;
Expand Down Expand Up @@ -110,15 +111,16 @@ public boolean delete() {
* Example of listing the blobs in the bucket.
*/
// [TARGET list(BlobListOption...)]
public Iterator<Blob> listBlobs() {
public Page<Blob> listBlobs() {

This comment was marked as spam.

// [START listBlobs]
Iterator<Blob> blobIterator = bucket.list().iterateAll();
Page<Blob> blobs = bucket.list();
Iterator<Blob> blobIterator = blobs.iterateAll();
while (blobIterator.hasNext()) {
Blob blob = blobIterator.next();
// do something with the blob
}
// [END listBlobs]
return blobIterator;
return blobs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName,
*/
// [TARGET list(BucketListOption...)]
// [VARIABLE "bucket_"]
public Iterator<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
public Page<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
// [START listBucketsWithSizeAndPrefix]
Page<Bucket> buckets = storage.list(BucketListOption.pageSize(100),
BucketListOption.prefix(prefix));
Expand All @@ -211,16 +211,16 @@ public Iterator<Bucket> listBucketsWithSizeAndPrefix(String prefix) {
// do something with the bucket
}
// [END listBucketsWithSizeAndPrefix]
return bucketIterator;
return buckets;
}

/**
* Example of listing buckets, specifying the page size and a name prefix.
* Example of listing blobs in a provided directory.
*/
// [TARGET list(String, BlobListOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_directory"]
public Iterator<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String directory) {
// [VARIABLE "my_directory/"]
public Page<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String directory) {
// [START listBlobsWithDirectoryAndPrefix]
Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(),
BlobListOption.prefix(directory));
Expand All @@ -230,7 +230,7 @@ public Iterator<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String
// do something with the blob
}
// [END listBlobsWithDirectoryAndPrefix]
return blobIterator;
return blobs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.cloud.example.storage.snippets;
package com.google.cloud.examples.storage.snippets;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertArrayEquals;
Expand All @@ -24,7 +24,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.cloud.examples.storage.snippets.BlobSnippets;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
Expand All @@ -50,14 +50,16 @@ public class ITBucketSnippets {
private static final String BLOB2 = "blob2";
private static final String BLOB3 = "blob3";
private static final String BLOB4 = "blob4";
private static final Set<String> BLOBS = ImmutableSet.of(BLOB1, BLOB2, BLOB3, BLOB4);

private static Storage storage;
private static BucketSnippets bucketSnippets;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Rule
public Timeout globalTimeout = Timeout.seconds(300);

@BeforeClass
public static void beforeClass() {
RemoteStorageHelper helper = RemoteStorageHelper.create();
Expand All @@ -76,7 +78,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
}

@Test
public void testBucket() {
public void testBucket() throws InterruptedException {
assertTrue(bucketSnippets.exists());
Bucket bucket = bucketSnippets.reload();
assertNotNull(bucket);
Expand All @@ -90,10 +92,15 @@ public void testBucket() {
assertNotNull(blob3);
Blob blob4 = bucketSnippets.createBlobFromInputStreamWithContentType(BLOB4);
assertNotNull(blob4);
Iterator<Blob> blobIterator = bucketSnippets.listBlobs();
while (blobIterator.hasNext()) {
assertTrue(BLOBS.contains(blobIterator.next().name()));
Set<Blob> blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
while (blobSet.size() < 4) {
Thread.sleep(500);
blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll());
}
assertTrue(blobSet.contains(blob1));
assertTrue(blobSet.contains(blob2));
assertTrue(blobSet.contains(blob3));
assertTrue(blobSet.contains(blob4));
blob1 = bucketSnippets.getBlob(BLOB1, blob1.generation());
assertEquals(BLOB1, blob1.name());
List<Blob> blobs = bucketSnippets.getBlobFromStrings(BLOB2, BLOB3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,30 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.cloud.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.Iterators;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand All @@ -59,6 +64,9 @@ public class ITStorageSnippets {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Rule
public Timeout globalTimeout = Timeout.seconds(300);

@BeforeClass
public static void beforeClass() {
RemoteStorageHelper helper = RemoteStorageHelper.create();
Expand All @@ -78,7 +86,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
}

@Test
public void testBlob() {
public void testBlob() throws InterruptedException {
String blobName = "directory/test-blob";
Blob blob = storageSnippets.createBlob(BUCKET, blobName);
assertNotNull(blob);
Expand All @@ -88,19 +96,28 @@ public void testBlob() {
assertNotNull(blob);
blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName);
assertNotNull(blob);
blob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
assertNotNull(blob);
Iterator<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory");
while (blobs.hasNext()) {
assertTrue(blobs.next().name().startsWith("directory"));
} blob.delete();
Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob");
assertNotNull(copiedBlob);
Page<Blob> blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
while (Iterators.size(blobs.iterateAll()) < 2) {
Thread.sleep(500);
blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/");
}
Set<String> blobNames = new HashSet<>();
Iterator<Blob> blobIterator = blobs.iterateAll();
while (blobIterator.hasNext()) {
blobNames.add(blobIterator.next().name());
}
assertTrue(blobNames.contains(blobName));
assertTrue(blobNames.contains("directory/copy-blob"));
try {
storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1);
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
assertTrue(storageSnippets.deleteBlob(BUCKET, blobName));
copiedBlob.delete();
}

@Test
Expand Down Expand Up @@ -136,10 +153,15 @@ public void testGetBucketWithMetageneration() {
}

@Test
public void testListBucketsWithSizeAndPrefix() {
Iterator<Bucket> buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
while (buckets.hasNext()) {
assertTrue(buckets.next().name().startsWith(BUCKET));
public void testListBucketsWithSizeAndPrefix() throws InterruptedException {
Page<Bucket> buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
while (Iterators.size(buckets.iterateAll()) < 1) {
Thread.sleep(500);
buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
}
Iterator<Bucket> bucketIterator = buckets.iterateAll();
while (bucketIterator.hasNext()) {
assertTrue(bucketIterator.next().name().startsWith(BUCKET));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ public boolean delete(BucketSourceOption... options) {
*
* <p>Example of listing the blobs in the bucket.
* <pre> {@code
* Iterator<Blob> blobIterator = bucket.list().iterateAll();
* Page<Blob> blobs = bucket.list();
* Iterator<Blob> blobIterator = blobs.iterateAll();
* while (blobIterator.hasNext()) {
* Blob blob = blobIterator.next();
* // do something with the blob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,10 @@ public static Builder builder() {
* Lists the bucket's blobs. If the {@link BlobListOption#currentDirectory()} option is provided,
* results are returned in a directory-like mode.
*
* <p>Example of listing buckets, specifying the page size and a name prefix.
* <p>Example of listing blobs in a provided directory.
* <pre> {@code
* String bucketName = "my_unique_bucket";
* String directory = "my_directory";
* String directory = "my_directory/";
* Page<Blob> blobs = storage.list(bucketName, BlobListOption.currentDirectory(),
* BlobListOption.prefix(directory));
* Iterator<Blob> blobIterator = blobs.iterateAll();
Expand Down