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

test: migrate tests which previously had hard coded object names to use new Generator#randomObjectName() #1827

Merged
merged 9 commits into from
Jan 4, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ public void testRetentionPolicyNoLock() throws Exception {
assertNotNull(remoteBucket2.getRetentionEffectiveTime());
assertThat(remoteBucket2.retentionPolicyIsLocked()).isAnyOf(null, false);

String blobName = "test-create-with-retention-policy-hold";
String blobName = generator.randomObjectName();
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
Blob remoteBlob = storage.create(blobInfo);
assertNotNull(remoteBlob.getRetentionExpirationTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.google.cloud.storage.it;

import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -29,6 +28,7 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.StorageBatch;
import com.google.cloud.storage.StorageBatchResult;
import com.google.cloud.storage.StorageException;
Expand All @@ -38,23 +38,23 @@
import com.google.cloud.storage.it.runner.annotations.Inject;
import com.google.cloud.storage.it.runner.annotations.SingleBackend;
import com.google.cloud.storage.it.runner.annotations.StorageFixture;
import com.google.common.collect.Lists;
import java.util.List;
import com.google.cloud.storage.it.runner.registry.Generator;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(StorageITRunner.class)
@SingleBackend(Backend.PROD)
public class ITBatchTest {
private static final int MAX_BATCH_SIZE = 100;
private static final String CONTENT_TYPE = "text/plain";

@Inject
@StorageFixture(Transport.HTTP)
public Storage storage;

@Inject public BucketInfo bucket;
@Inject public Generator generator;

private String bucketName;

Expand Down Expand Up @@ -108,67 +108,56 @@ public void testBatchRequest() {
}

@Test
public void testBatchRequestManyOperations() {
List<StorageBatchResult<Boolean>> deleteResults =
Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
List<StorageBatchResult<Blob>> updateResults =
Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
public void testBatchRequestManyOperations() throws Exception {
// define some object ids for use in the batch operations
BlobId id1 = BlobId.of(bucketName, generator.randomObjectName());
BlobId id2 = BlobId.of(bucketName, generator.randomObjectName());
BlobId id3 = BlobId.of(bucketName, generator.randomObjectName());
BlobId id4 = BlobId.of(bucketName, generator.randomObjectName());
BlobId id5 = BlobId.of(bucketName, generator.randomObjectName());

ImmutableMap<String, String> ka = ImmutableMap.of("k", "a");
ImmutableMap<String, String> kB = ImmutableMap.of("k", "B");

// Create objects which exist before the batch operations
BlobInfo info1 = BlobInfo.newBuilder(id1).setMetadata(ka).build();
BlobInfo info2 = BlobInfo.newBuilder(id2).setMetadata(ka).build();
BlobInfo info3 = BlobInfo.newBuilder(id3).setMetadata(ka).build();
Blob obj1 = storage.create(info1, BlobTargetOption.doesNotExist());
Blob obj2 = storage.create(info2, BlobTargetOption.doesNotExist());
Blob obj3 = storage.create(info3, BlobTargetOption.doesNotExist());

// Define our batch operations
StorageBatch batch = storage.batch();
for (int i = 0; i < MAX_BATCH_SIZE; i++) {
BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i);
deleteResults.add(batch.delete(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i);
getResults.add(batch.get(blobId));
}
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
BlobInfo blob =
BlobInfo.newBuilder(BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i))
.build();
updateResults.add(batch.update(blob));
}

String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucketName, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucketName, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();

StorageBatchResult<Blob> getResult = batch.get(bucketName, sourceBlobName1);
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
StorageBatchResult<Blob> get1Success = batch.get(id1);
StorageBatchResult<Blob> update2Success =
batch.update(
obj2.toBuilder().setMetadata(kB).build(), BlobTargetOption.metagenerationMatch());
StorageBatchResult<Boolean> delete3Success = batch.delete(id3);
StorageBatchResult<Blob> get4Error = batch.get(id4);
StorageBatchResult<Boolean> delete5Error = batch.delete(id5);

// submit the batch
batch.submit();

// Check deletes
for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
assertFalse(failedDeleteResult.get());
}

// Check gets
for (StorageBatchResult<Blob> failedGetResult : getResults) {
assertNull(failedGetResult.get());
}
Blob remoteBlob1 = getResult.get();
assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
assertEquals(sourceBlob1.getName(), remoteBlob1.getName());

// Check updates
for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
try {
failedUpdateResult.get();
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
Blob remoteUpdatedBlob2 = updateResult.get();
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
// verify our expected results
assertAll(
() -> {
Blob blob = get1Success.get();
assertThat(blob.getBucket()).isEqualTo(bucketName);
assertThat(blob.getName()).isEqualTo(id1.getName());
assertThat(blob.getMetadata()).isEqualTo(ka);
},
() -> {
Blob blob = update2Success.get();
assertThat(blob.getBucket()).isEqualTo(bucketName);
assertThat(blob.getName()).isEqualTo(id2.getName());
assertThat(blob.getMetadata()).isEqualTo(kB);
},
() -> assertThat(delete3Success.get()).isTrue(),
() -> assertThat(get4Error.get()).isNull(),
() -> assertThat(delete5Error.get()).isFalse());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public void testEnableDisableBucketDefaultEventBasedHold() {
storage.get(
bucketName, Storage.BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));
assertTrue(remoteBucket.getDefaultEventBasedHold());
String blobName = "test-create-with-event-based-hold";
String blobName = generator.randomObjectName();
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
Blob remoteBlob = storage.create(blobInfo);
assertTrue(remoteBlob.getEventBasedHold());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.cloud.storage.it.runner.annotations.Backend;
import com.google.cloud.storage.it.runner.annotations.CrossRun;
import com.google.cloud.storage.it.runner.annotations.Inject;
import com.google.cloud.storage.it.runner.registry.Generator;
import java.util.Iterator;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -45,6 +46,7 @@ public class ITDownloadBlobWithoutAuth {
@Inject public Storage storage;

@Inject public BucketInfo bucket;
@Inject public Generator generator;

@Test
public void testDownloadPublicBlobWithoutAuthentication() {
Expand Down Expand Up @@ -75,7 +77,7 @@ public void testDownloadPublicBlobWithoutAuthentication() {
// try to download blobs from a bucket that requires authentication
// authenticated client will succeed
// unauthenticated client will receive an exception
String sourceBlobName = "source-blob-name";
String sourceBlobName = generator.randomObjectName();
BlobInfo sourceBlob = BlobInfo.newBuilder(bucketName, sourceBlobName).build();
assertThat(storage.create(sourceBlob)).isNotNull();
assertThat(storage.readAllBytes(bucketName, sourceBlobName)).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.cloud.storage.it.runner.annotations.Backend;
import com.google.cloud.storage.it.runner.annotations.CrossRun;
import com.google.cloud.storage.it.runner.annotations.Inject;
import com.google.cloud.storage.it.runner.registry.Generator;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -49,19 +50,22 @@ public final class ITDownloadToTest {

@Inject public Storage storage;
@Inject public BucketInfo bucket;
@Inject public Generator generator;

private BlobId blobId;

@Before
public void before() {
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
String objectString = generator.randomObjectName();
blobId = BlobId.of(bucket.getName(), objectString);
BlobInfo blobInfo =
BlobInfo.newBuilder(blobId).setContentEncoding("gzip").setContentType("text/plain").build();
storage.create(blobInfo, helloWorldGzipBytes);
}

@Test
public void downloadTo_returnRawInputStream_yes() throws IOException {
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
Path helloWorldTxtGz = File.createTempFile("helloWorld", ".txt.gz").toPath();
Path helloWorldTxtGz = File.createTempFile(blobId.getName(), ".txt.gz").toPath();
storage.downloadTo(
blobId, helloWorldTxtGz, Storage.BlobSourceOption.shouldReturnRawInputStream(true));

Expand All @@ -74,8 +78,7 @@ public void downloadTo_returnRawInputStream_yes() throws IOException {

@Test
public void downloadTo_returnRawInputStream_no() throws IOException {
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
Path helloWorldTxt = File.createTempFile("helloWorld", ".txt").toPath();
Path helloWorldTxt = File.createTempFile(blobId.getName(), ".txt").toPath();
storage.downloadTo(
blobId, helloWorldTxt, Storage.BlobSourceOption.shouldReturnRawInputStream(false));
byte[] actualTxtBytes = Files.readAllBytes(helloWorldTxt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testUpdateBucketDefaultKmsKeyName() {

@Test
public void testCreateBlobWithKmsKeyName() {
String blobName = "test-create-with-kms-key-name-blob";
String blobName = generator.randomObjectName();
String bucketName = bucket.getName();
BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build();
Blob remoteBlob =
Expand All @@ -130,7 +130,7 @@ public void testCreateBlobWithKmsKeyName() {

@Test(expected = StorageException.class)
public void testCreateBlobWithKmsKeyNameAndCustomerSuppliedKeyFails() {
String blobName = "test-create-with-kms-key-name-blob";
String blobName = generator.randomObjectName();
BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build();
storage.create(
blob,
Expand Down Expand Up @@ -168,7 +168,7 @@ public void testCreateBlobWithDefaultKmsKeyName() {

@Test
public void testGetBlobKmsKeyNameField() {
String blobName = "test-get-selected-kms-key-name-field-blob";
String blobName = generator.randomObjectName();
BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setContentType(CONTENT_TYPE).build();
assertNotNull(
storage.create(blob, Storage.BlobTargetOption.kmsKeyName(kms.getKey1().getName())));
Expand All @@ -181,7 +181,7 @@ public void testGetBlobKmsKeyNameField() {

@Test
public void testRotateFromCustomerEncryptionToKmsKey() {
String sourceBlobName = "test-copy-blob-encryption-key-source";
String sourceBlobName = generator.randomObjectName();
BlobId source = BlobId.of(bucket.getName(), sourceBlobName);
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
Blob remoteBlob =
Expand All @@ -190,7 +190,7 @@ public void testRotateFromCustomerEncryptionToKmsKey() {
BLOB_BYTE_CONTENT,
Storage.BlobTargetOption.encryptionKey(KEY));
assertNotNull(remoteBlob);
String targetBlobName = "test-copy-blob-kms-key-target";
String targetBlobName = generator.randomObjectName();
BlobInfo target =
BlobInfo.newBuilder(bucket, targetBlobName)
.setContentType(CONTENT_TYPE)
Expand All @@ -216,7 +216,7 @@ public void testRotateFromCustomerEncryptionToKmsKey() {

@Test(expected = StorageException.class)
public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
String sourceBlobName = "test-copy-blob-encryption-key-source";
String sourceBlobName = generator.randomObjectName();
BlobId source = BlobId.of(bucket.getName(), sourceBlobName);
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
Blob remoteBlob =
Expand All @@ -225,7 +225,7 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
BLOB_BYTE_CONTENT,
Storage.BlobTargetOption.encryptionKey(KEY));
assertNotNull(remoteBlob);
String targetBlobName = "test-copy-blob-kms-key-target";
String targetBlobName = generator.randomObjectName();
BlobInfo target =
BlobInfo.newBuilder(bucket, targetBlobName)
.setContentType(CONTENT_TYPE)
Expand All @@ -246,7 +246,7 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
@Test
public void testWriterWithKmsKeyName() throws IOException {
// Write an empty object with a kmsKeyName.
String blobName = "test-empty-blob";
String blobName = generator.randomObjectName();
BlobInfo blobInfo = BlobInfo.newBuilder(bucket, blobName).build();
Blob blob =
storage.create(blobInfo, Storage.BlobTargetOption.kmsKeyName(kms.getKey1().getName()));
Expand Down
Loading