From a3bc72145c61b7ad87ccc6710fc68fa6b76e23ed Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 12:58:02 -0500 Subject: [PATCH 1/9] chore: use Generator#randomObjectName() for all object names in ITSignedUrlTest --- .../cloud/storage/it/ITSignedUrlTest.java | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITSignedUrlTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITSignedUrlTest.java index 57839550e3..89dcbb1bf2 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITSignedUrlTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITSignedUrlTest.java @@ -40,23 +40,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.cloud.storage.it.runner.registry.Generator; import com.google.common.collect.ImmutableMap; -import java.io.File; -import java.io.FileInputStream; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; -import java.nio.file.Files; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.junit.Before; import org.junit.Test; @@ -74,6 +74,7 @@ public class ITSignedUrlTest { public Storage storage; @Inject public BucketInfo bucket; + @Inject public Generator generator; private String bucketName; @@ -87,7 +88,7 @@ public void testGetSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } - String blobName = "test-get-signed-url-blob/with/slashes/and?special=!#$&'()*+,:;=?@[]"; + String blobName = generator.randomObjectName() + "/with/slashes/and?special=!#$&'()*+,:;=?@[]"; BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); @@ -110,7 +111,7 @@ public void testGetV2SignedUrlWithAddlQueryParam() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } - String blobName = "test-get-v2-with-generation-param"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); @@ -142,7 +143,7 @@ public void testPostSignedUrl() throws IOException { if (storage.getOptions().getCredentials() != null) { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } - String blobName = "test-post-signed-url-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build(); assertNotNull(storage.create(blob)); for (Storage.SignUrlOption urlStyle : @@ -169,7 +170,7 @@ public void testV4SignedUrl() throws IOException { assumeTrue(storage.getOptions().getCredentials() instanceof ServiceAccountSigner); } - String blobName = "test-get-signed-url-blob/with/slashes/and?special=!#$&'()*+,:;=?@[]"; + String blobName = generator.randomObjectName() + "/with/slashes/and?special=!#$&'()*+,:;=?@[]"; BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); @@ -194,30 +195,38 @@ public void testV4SignedUrl() throws IOException { public void testSignedPostPolicyV4() throws Exception { PostFieldsV4 fields = PostFieldsV4.newBuilder().setAcl("public-read").build(); + BlobId id = BlobId.of(bucketName, generator.randomObjectName()); + PostPolicyV4 policy = storage.generateSignedPostPolicyV4( - BlobInfo.newBuilder(bucketName, "my-object").build(), 7, TimeUnit.DAYS, fields); + BlobInfo.newBuilder(id).build(), 7, TimeUnit.DAYS, fields); - HttpClient client = HttpClientBuilder.create().build(); - HttpPost request = new HttpPost(policy.getUrl()); - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + String content = "Hello, World!"; + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + HttpPost request = new HttpPost(policy.getUrl()); + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - for (Map.Entry entry : policy.getFields().entrySet()) { - builder.addTextBody(entry.getKey(), entry.getValue()); + for (Map.Entry entry : policy.getFields().entrySet()) { + builder.addTextBody(entry.getKey(), entry.getValue()); + } + builder.addBinaryBody( + "file", + new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)), + ContentType.APPLICATION_OCTET_STREAM, + id.getName()); + request.setEntity(builder.build()); + client.execute(request); } - File file = File.createTempFile("temp", "file"); - Files.write(file.toPath(), "hello world".getBytes()); - builder.addBinaryBody( - "file", new FileInputStream(file), ContentType.APPLICATION_OCTET_STREAM, file.getName()); - request.setEntity(builder.build()); - client.execute(request); - - assertEquals("hello world", new String(storage.get(bucketName, "my-object").getContent())); + + Blob blob = storage.get(id); + byte[] actualContent = blob.getContent(); + String actual = new String(actualContent, StandardCharsets.UTF_8); + assertEquals(content, actual); } @Test public void testUploadUsingSignedURL() throws Exception { - String blobName = "test-signed-url-upload"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build(); assertNotNull(storage.create(blob)); Map extensionHeaders = new HashMap<>(); From fb9dee69e4d33b82f4879d337b1da491ed117078 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:07:56 -0500 Subject: [PATCH 2/9] chore: use Generator#randomObjectName() for all object names in ITObjectTest --- .../google/cloud/storage/it/ITObjectTest.java | 111 +++++++++--------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java index 56f671f986..5a0fa76c83 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java @@ -129,7 +129,7 @@ public class ITObjectTest { @Test public void testCreateBlob() { - String blobName = "test-create-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setCustomTime(System.currentTimeMillis()).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); @@ -144,7 +144,7 @@ public void testCreateBlob() { @Test public void testCreateBlobMd5Crc32cFromHexString() { - String blobName = "test-create-blob-md5-crc32c-from-hex-string"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName) .setContentType(CONTENT_TYPE) @@ -164,7 +164,7 @@ public void testCreateBlobMd5Crc32cFromHexString() { @Test public void testCreateGetBlobWithEncryptionKey() { - String blobName = "test-create-with-customer-key-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT, BlobTargetOption.encryptionKey(KEY)); assertNotNull(remoteBlob); @@ -185,7 +185,7 @@ public void testCreateGetBlobWithEncryptionKey() { @Test public void testCreateEmptyBlob() { - String blobName = "test-create-empty-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); @@ -198,7 +198,7 @@ public void testCreateEmptyBlob() { @Test @SuppressWarnings({"unchecked", "deprecation"}) public void testCreateBlobStream() { - String blobName = "test-create-blob-stream"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setContentType(CONTENT_TYPE).build(); ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); Blob remoteBlob = storage.create(blob, stream); @@ -213,7 +213,7 @@ public void testCreateBlobStream() { @Test @SuppressWarnings({"unchecked", "deprecation"}) public void testCreateBlobStreamDisableGzipContent() { - String blobName = "test-create-blob-stream-disable-gzip-compression"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setContentType(CONTENT_TYPE).build(); ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); Blob remoteBlob = storage.create(blob, stream, BlobWriteOption.disableGzipContent()); @@ -227,7 +227,7 @@ public void testCreateBlobStreamDisableGzipContent() { @Test public void testCreateBlobFail() { - String blobName = "test-create-blob-fail"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); @@ -243,7 +243,7 @@ public void testCreateBlobFail() { @Test public void testGetBlobEmptySelectedFields() { - String blobName = "test-get-empty-selected-fields-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), BlobGetOption.fields()); @@ -254,7 +254,7 @@ public void testGetBlobEmptySelectedFields() { @Test public void testGetBlobSelectedFields() { - String blobName = "test-get-selected-fields-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName) .setContentType(CONTENT_TYPE) @@ -270,7 +270,7 @@ public void testGetBlobSelectedFields() { @Test public void testGetBlobAllSelectedFields() { - String blobName = "test-get-all-selected-fields-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName) .setContentType(CONTENT_TYPE) @@ -285,7 +285,7 @@ public void testGetBlobAllSelectedFields() { @Test public void testGetBlobFail() { - String blobName = "test-get-blob-fail"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); @@ -348,18 +348,19 @@ public void testListBlobsSelectedFields() { @Test public void getBlobReturnNullOn404() { String bucketName = bucket.getName(); - BlobId id = BlobId.of(bucketName, "__d_o_e_s__n_o_t__e_x_i_s_t__"); + String objectName = generator.randomObjectName() + "__d_o_e_s__n_o_t__e_x_i_s_t__"; + BlobId id = BlobId.of(bucketName, objectName); Blob blob = storage.get(id); assertThat(blob).isNull(); } - @Test(timeout = 7500) + @Test public void testListBlobRequesterPays() throws InterruptedException { String projectId = storage.getOptions().getProjectId(); + String prefix = generator.randomObjectName(); BlobInfo blobInfo1 = - BlobInfo.newBuilder( - requesterPaysBucket.getName(), "test-list-blobs-empty-selected-fields-blob1") + BlobInfo.newBuilder(requesterPaysBucket.getName(), prefix + "1") .setContentType(CONTENT_TYPE) .build(); Blob blob1 = storage.create(blobInfo1, BlobTargetOption.userProject(projectId)); @@ -379,7 +380,7 @@ public void testListBlobRequesterPays() throws InterruptedException { try { storage.list( requesterPaysBucket.getName(), - BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"), + BlobListOption.prefix(prefix), BlobListOption.fields(), BlobListOption.userProject("fakeBillingProjectId")); fail("Expected bad user project error."); @@ -390,7 +391,7 @@ public void testListBlobRequesterPays() throws InterruptedException { Page page = storage.list( requesterPaysBucket.getName(), - BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"), + BlobListOption.prefix(prefix), BlobListOption.userProject(projectId)); List blobs = StreamSupport.stream(page.iterateAll().spliterator(), false) @@ -400,7 +401,7 @@ public void testListBlobRequesterPays() throws InterruptedException { assertThat(blobs).contains(PackagePrivateMethodWorkarounds.noAcl(blob1)); } - @Test(timeout = 15000) + @Test public void testListBlobsVersioned() throws ExecutionException, InterruptedException { String bucketName = generator.randomBucketName(); Bucket bucket = @@ -781,14 +782,14 @@ public void testComposeBlobFail() { @Exclude(transports = Transport.GRPC) public void testCopyBlob() { - String sourceBlobName = "test-copy-blob-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); ImmutableMap metadata = ImmutableMap.of("k", "v"); BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); - String targetBlobName = "test-copy-blob-target"; + String targetBlobName = generator.randomObjectName() + "-target"; CopyRequest req = CopyRequest.of(source, BlobId.of(bucket.getName(), targetBlobName)); CopyWriter copyWriter = storage.copy(req); assertEquals(bucket.getName(), copyWriter.getResult().getBucket()); @@ -805,14 +806,14 @@ public void testCopyBlob() { @Exclude(transports = Transport.GRPC) public void testCopyBlobWithPredefinedAcl() { - String sourceBlobName = "test-copy-blob-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); ImmutableMap metadata = ImmutableMap.of("k", "v"); BlobInfo blob = BlobInfo.newBuilder(source).setContentType(CONTENT_TYPE).setMetadata(metadata).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); - String targetBlobName = "test-copy-blob-target"; + String targetBlobName = generator.randomObjectName() + "-target"; CopyRequest req = CopyRequest.newBuilder() .setSource(source) @@ -836,7 +837,7 @@ public void testCopyBlobWithPredefinedAcl() { @Exclude(transports = Transport.GRPC) public void testCopyBlobWithEncryptionKeys() { - String sourceBlobName = "test-copy-blob-encryption-key-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); ImmutableMap metadata = ImmutableMap.of("k", "v"); Blob remoteBlob = @@ -845,7 +846,7 @@ public void testCopyBlobWithEncryptionKeys() { BLOB_BYTE_CONTENT, BlobTargetOption.encryptionKey(KEY)); assertNotNull(remoteBlob); - String targetBlobName = "test-copy-blob-encryption-key-target"; + String targetBlobName = generator.randomObjectName() + "-target"; BlobInfo target = BlobInfo.newBuilder(bucket, targetBlobName) .setContentType(CONTENT_TYPE) @@ -888,11 +889,11 @@ public void testCopyBlobWithEncryptionKeys() { @Exclude(transports = Transport.GRPC) public void testCopyBlobUpdateMetadata() { - String sourceBlobName = "test-copy-blob-update-metadata-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob); - String targetBlobName = "test-copy-blob-update-metadata-target"; + String targetBlobName = generator.randomObjectName() + "-target"; ImmutableMap metadata = ImmutableMap.of("k", "v"); BlobInfo target = BlobInfo.newBuilder(bucket, targetBlobName) @@ -914,7 +915,7 @@ public void testCopyBlobUpdateMetadata() { // @Test @Exclude(transports = Transport.GRPC) public void testCopyBlobUpdateStorageClass() { - String sourceBlobName = "test-copy-blob-update-storage-class-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); BlobInfo sourceInfo = BlobInfo.newBuilder(source).setStorageClass(StorageClass.STANDARD).build(); @@ -922,7 +923,7 @@ public void testCopyBlobUpdateStorageClass() { assertNotNull(remoteSourceBlob); assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass()); - String targetBlobName = "test-copy-blob-update-storage-class-target"; + String targetBlobName = generator.randomObjectName() + "-target"; BlobInfo targetInfo = BlobInfo.newBuilder(bucket, targetBlobName).setStorageClass(StorageClass.COLDLINE).build(); CopyRequest req = CopyRequest.of(source, targetInfo); @@ -940,11 +941,11 @@ public void testCopyBlobUpdateStorageClass() { @Exclude(transports = Transport.GRPC) public void testCopyBlobNoContentType() { - String sourceBlobName = "test-copy-blob-no-content-type-source"; + String sourceBlobName = generator.randomObjectName() + "-source"; BlobId source = BlobId.of(bucket.getName(), sourceBlobName); Blob remoteSourceBlob = storage.create(BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob); - String targetBlobName = "test-copy-blob-no-content-type-target"; + String targetBlobName = generator.randomObjectName() + "-target"; ImmutableMap metadata = ImmutableMap.of("k", "v"); BlobInfo target = BlobInfo.newBuilder(bucket, targetBlobName).setMetadata(metadata).build(); CopyRequest req = CopyRequest.of(source, target); @@ -1057,7 +1058,7 @@ public void testReadAndWriteChannelsWithDifferentFileSize_4MiB_plus1() throws IO } private void doTestReadAndWriteChannelsWithSize(int blobSize) throws IOException { - String blobName = String.format("test-read-and-write-channels-blob-%d", blobSize); + String blobName = String.format("%s-%d", generator.randomObjectName(), blobSize); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); Random rnd = new Random(); byte[] bytes = new byte[blobSize]; @@ -1079,7 +1080,7 @@ private void doTestReadAndWriteChannelsWithSize(int blobSize) throws IOException @Exclude(transports = Transport.GRPC) public void testReadAndWriteCaptureChannels() throws IOException { - String blobName = "test-read-and-write-capture-channels-blob"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build(); byte[] stringBytes; WriteChannel writer = storage.writer(blob); @@ -1110,8 +1111,8 @@ public void testReadAndWriteCaptureChannels() throws IOException { // Only supported in JSON right now @Exclude(transports = Transport.GRPC) public void testGetBlobs() { - String sourceBlobName1 = "test-get-blobs-1"; - String sourceBlobName2 = "test-get-blobs-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); @@ -1128,8 +1129,8 @@ public void testGetBlobs() { @Exclude(transports = Transport.GRPC) public void testGetBlobsFail() { - String sourceBlobName1 = "test-get-blobs-fail-1"; - String sourceBlobName2 = "test-get-blobs-fail-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); @@ -1144,8 +1145,8 @@ public void testGetBlobsFail() { @Exclude(transports = Transport.GRPC) public void testDeleteBlobs() { - String sourceBlobName1 = "test-delete-blobs-1"; - String sourceBlobName2 = "test-delete-blobs-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); @@ -1159,8 +1160,8 @@ public void testDeleteBlobs() { // Only supported in JSON right now @Exclude(transports = Transport.GRPC) public void testDeleteBlobsFail() { - String sourceBlobName1 = "test-delete-blobs-fail-1"; - String sourceBlobName2 = "test-delete-blobs-fail-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); @@ -1171,7 +1172,7 @@ public void testDeleteBlobsFail() { @Test public void testDeleteBlob() { - String sourceBlobName = "test-delete-one-success"; + String sourceBlobName = generator.randomObjectName(); BlobInfo sourceBlob = BlobInfo.newBuilder(bucket, sourceBlobName).build(); assertNotNull(storage.create(sourceBlob)); boolean result = storage.delete(sourceBlob.getBlobId()); @@ -1183,8 +1184,8 @@ public void testDeleteBlob() { @Exclude(transports = Transport.GRPC) public void testUpdateBlobs() { - String sourceBlobName1 = "test-update-blobs-1"; - String sourceBlobName2 = "test-update-blobs-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); Blob remoteBlob1 = storage.create(sourceBlob1); @@ -1208,8 +1209,8 @@ public void testUpdateBlobs() { @Exclude(transports = Transport.GRPC) public void testUpdateBlobsFail() { - String sourceBlobName1 = "test-update-blobs-fail-1"; - String sourceBlobName2 = "test-update-blobs-fail-2"; + String sourceBlobName1 = generator.randomObjectName(); + String sourceBlobName2 = generator.randomObjectName(); BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucket, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucket, sourceBlobName2).build(); BlobInfo remoteBlob1 = storage.create(sourceBlob1); @@ -1232,7 +1233,7 @@ public void testAttemptObjectDeleteWithRetentionPolicy() storage.create( BucketInfo.newBuilder(bucketName).setRetentionPeriod(RETENTION_PERIOD).build()); assertEquals(RETENTION_PERIOD, remoteBucket.getRetentionPeriod()); - String blobName = "test-create-with-retention-policy"; + String blobName = generator.randomObjectName(); BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build(); Blob remoteBlob = storage.create(blobInfo); assertNotNull(remoteBlob.getRetentionExpirationTime()); @@ -1249,7 +1250,7 @@ public void testAttemptObjectDeleteWithRetentionPolicy() @Test public void testEnableDisableTemporaryHold() { - String blobName = "test-create-with-temporary-hold"; + String blobName = generator.randomObjectName(); BlobInfo blobInfo = BlobInfo.newBuilder(bucket, blobName).setTemporaryHold(true).build(); Blob remoteBlob = storage.create(blobInfo); assertTrue(remoteBlob.getTemporaryHold()); @@ -1262,7 +1263,7 @@ public void testEnableDisableTemporaryHold() { @Test public void testAttemptObjectDeleteWithEventBasedHold() { - String blobName = "test-create-with-event-based-hold"; + String blobName = generator.randomObjectName(); BlobInfo blobInfo = BlobInfo.newBuilder(bucket, blobName).setEventBasedHold(true).build(); Blob remoteBlob = storage.create(blobInfo); assertTrue(remoteBlob.getEventBasedHold()); @@ -1278,7 +1279,7 @@ public void testAttemptObjectDeleteWithEventBasedHold() { @Test public void testAttemptDeletionObjectTemporaryHold() { - String blobName = "test-create-with-temporary-hold"; + String blobName = generator.randomObjectName(); BlobInfo blobInfo = BlobInfo.newBuilder(bucket, blobName).setTemporaryHold(true).build(); Blob remoteBlob = storage.create(blobInfo); assertTrue(remoteBlob.getTemporaryHold()); @@ -1294,7 +1295,7 @@ public void testAttemptDeletionObjectTemporaryHold() { @Test public void testBlobReload() throws Exception { - String blobName = "test-blob-reload"; + String blobName = generator.randomObjectName(); BlobId blobId = BlobId.of(bucket.getName(), blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); Blob blob = storage.create(blobInfo, new byte[] {0, 1, 2}); @@ -1323,7 +1324,7 @@ public void testBlobReload() throws Exception { @Test public void testUploadWithEncryption() throws Exception { - String blobName = "test-upload-withEncryption"; + String blobName = generator.randomObjectName(); BlobId blobId = BlobId.of(bucket.getName(), blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); @@ -1367,7 +1368,11 @@ private Blob createBlob(String method, BlobInfo blobInfo, boolean detectType) th } private void testAutoContentType(String method) throws IOException { - String[] names = {"file1.txt", "dir with spaces/Pic.Jpg", "no_extension"}; + String[] names = { + generator.randomObjectName() + ".txt", + generator.randomObjectName() + "with space/Pic.Jpg", + generator.randomObjectName() + "no_extension" + }; String[] types = {"text/plain", "image/jpeg", "application/octet-stream"}; for (int i = 0; i < names.length; i++) { BlobId blobId = BlobId.of(bucket.getName(), names[i]); @@ -1403,7 +1408,7 @@ public void testAutoContentTypeWriter() throws IOException { @Test public void testBlobTimeStorageClassUpdated() { - String blobName = "test-blob-with-storage-class"; + String blobName = generator.randomObjectName(); StorageClass storageClass = StorageClass.COLDLINE; BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setStorageClass(storageClass).build(); Blob remoteBlob = storage.create(blob); From 1080d622b3c618f73b295864deaf97ca0f67117b Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:08:52 -0500 Subject: [PATCH 3/9] chore: use Generator#randomObjectName() for all object names in ITBucketTest --- .../src/test/java/com/google/cloud/storage/it/ITBucketTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java index 7f451d9c06..b16e0c3b30 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java @@ -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()); From 94dfb6c96c4374f3914bec2c17c188f2136e4388 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:10:01 -0500 Subject: [PATCH 4/9] chore: use Generator#randomObjectName() for all object names in ITDownloadBlobWithoutAuth --- .../google/cloud/storage/it/ITDownloadBlobWithoutAuth.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadBlobWithoutAuth.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadBlobWithoutAuth.java index 8a17bfa3ba..3febdab092 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadBlobWithoutAuth.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadBlobWithoutAuth.java @@ -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; @@ -45,6 +46,7 @@ public class ITDownloadBlobWithoutAuth { @Inject public Storage storage; @Inject public BucketInfo bucket; + @Inject public Generator generator; @Test public void testDownloadPublicBlobWithoutAuthentication() { @@ -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(); From 4ecf06a780d51db426755968cf4d0b49b4fdc066 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:11:06 -0500 Subject: [PATCH 5/9] chore: use Generator#randomObjectName() for all object names in ITDownloadToTest --- .../google/cloud/storage/it/ITDownloadToTest.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadToTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadToTest.java index ac65e5cb29..097609a57b 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadToTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadToTest.java @@ -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; @@ -49,10 +50,14 @@ 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); @@ -60,8 +65,7 @@ public void before() { @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)); @@ -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); From 471665f3d7543afa2a7b06ec218e72b4c9e71bc1 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:11:32 -0500 Subject: [PATCH 6/9] chore: use Generator#randomObjectName() for all object names in ITAccessTest --- .../src/test/java/com/google/cloud/storage/it/ITAccessTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java index 99b29818d3..0392866877 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java @@ -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()); From 21801a5cdda364163db8f4501a0aa03b00b91839 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 14:11:58 -0500 Subject: [PATCH 7/9] chore: use Generator#randomObjectName() for all object names in ITKmsTest --- .../com/google/cloud/storage/it/ITKmsTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java index e49acf8800..5e325a1ae9 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java @@ -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 = @@ -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, @@ -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()))); @@ -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 metadata = ImmutableMap.of("k", "v"); Blob remoteBlob = @@ -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) @@ -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 metadata = ImmutableMap.of("k", "v"); Blob remoteBlob = @@ -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) @@ -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())); From adf0afd3b60fcf3c5e38eeacd57ab23346747c2a Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 15:01:40 -0500 Subject: [PATCH 8/9] chore: use Generator#randomObjectName() for all object names in ITWriteChannelConnectionPoolTest Log a warning message if a test has defined a junit timeout. Defining a junit timeout runs the test on another thread which we don't control, and don't control the migration of. --- .../it/ITWriteChannelConnectionPoolTest.java | 6 ++-- .../it/runner/StorageITLeafRunner.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITWriteChannelConnectionPoolTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITWriteChannelConnectionPoolTest.java index 5396e58cb3..1b2e3b97bc 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITWriteChannelConnectionPoolTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITWriteChannelConnectionPoolTest.java @@ -32,6 +32,7 @@ import com.google.cloud.storage.it.runner.annotations.Backend; 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.registry.Generator; import java.io.IOException; import java.nio.ByteBuffer; import org.apache.http.impl.client.HttpClients; @@ -46,6 +47,7 @@ public class ITWriteChannelConnectionPoolTest { private static final String BLOB_STRING_CONTENT = "Hello Google Cloud Storage!"; @Inject public BucketInfo bucket; + @Inject public Generator generator; private static class CustomHttpTransportFactory implements HttpTransportFactory { @Override @@ -57,7 +59,7 @@ public HttpTransport create() { } } - @Test(timeout = 5000) + @Test public void testWriteChannelWithConnectionPool() throws IOException { TransportOptions transportOptions = HttpTransportOptions.newBuilder() @@ -65,7 +67,7 @@ public void testWriteChannelWithConnectionPool() throws IOException { .build(); Storage storageWithPool = StorageOptions.http().setTransportOptions(transportOptions).build().getService(); - String blobName = "test-custom-pool-management"; + String blobName = generator.randomObjectName(); BlobInfo blob = BlobInfo.newBuilder(bucket.getName(), blobName).build(); byte[] stringBytes; try (WriteChannel writer = storageWithPool.writer(blob)) { diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/StorageITLeafRunner.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/StorageITLeafRunner.java index 0a509daa03..d763fcf7e6 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/StorageITLeafRunner.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/StorageITLeafRunner.java @@ -25,7 +25,12 @@ import java.lang.reflect.Modifier; import java.util.List; import java.util.Objects; +import java.util.logging.Logger; import org.checkerframework.checker.nullness.qual.Nullable; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; @@ -81,6 +86,31 @@ protected void validateFields(List errors) { } } + @Override + protected void validateTestMethods(List errors) { + TestClass testClass = getTestClass(); + if (testClass != null) { + boolean anyTestWithTimeout = + testClass.getAnnotatedMethods(Test.class).stream() + .anyMatch(fm -> fm.getAnnotation(Test.class).timeout() > 0); + + boolean timeoutRule = + testClass.getAnnotatedFields(Rule.class).stream() + .anyMatch(ff -> ff.getType().isAssignableFrom(Timeout.class)); + + boolean timeoutClassRule = + testClass.getAnnotatedFields(ClassRule.class).stream() + .anyMatch(ff -> ff.getType().isAssignableFrom(Timeout.class)); + + if (anyTestWithTimeout || timeoutRule || timeoutClassRule) { + String msg = + "Using @Test(timeout = 1), @Rule Timeout or @ClassRule Timeout can break multi-thread and Fixture support of StorageITRunner. Please refactor your test to detect a timeout in the test itself."; + Logger.getLogger(StorageITRunner.class.getName()).warning(msg); + } + } + super.validateTestMethods(errors); + } + @Override protected String getName() { if (name == null) { From faebdf0dd0f33cd2ffd4f564a1cb0ebfcc5fb4c0 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Thu, 22 Dec 2022 16:09:30 -0500 Subject: [PATCH 9/9] chore: use Generator#randomObjectName() for all object names in ITBatchTest Rewrite testBatchRequestManyOperations to be more straightforward. Rather than generating 100 different operations which are then bucketed, define the 5 high level operations we're verifying. --- .../google/cloud/storage/it/ITBatchTest.java | 111 ++++++++---------- 1 file changed, 50 insertions(+), 61 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBatchTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBatchTest.java index ed19da58c7..d1d1c97a58 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBatchTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBatchTest.java @@ -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; @@ -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; @@ -38,8 +38,8 @@ 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; @@ -47,7 +47,6 @@ @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 @@ -55,6 +54,7 @@ public class ITBatchTest { public Storage storage; @Inject public BucketInfo bucket; + @Inject public Generator generator; private String bucketName; @@ -108,67 +108,56 @@ public void testBatchRequest() { } @Test - public void testBatchRequestManyOperations() { - List> deleteResults = - Lists.newArrayListWithCapacity(MAX_BATCH_SIZE); - List> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2); - List> 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 ka = ImmutableMap.of("k", "a"); + ImmutableMap 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 getResult = batch.get(bucketName, sourceBlobName1); - StorageBatchResult updateResult = batch.update(updatedBlob2); + StorageBatchResult get1Success = batch.get(id1); + StorageBatchResult update2Success = + batch.update( + obj2.toBuilder().setMetadata(kB).build(), BlobTargetOption.metagenerationMatch()); + StorageBatchResult delete3Success = batch.delete(id3); + StorageBatchResult get4Error = batch.get(id4); + StorageBatchResult delete5Error = batch.delete(id5); + // submit the batch batch.submit(); - // Check deletes - for (StorageBatchResult failedDeleteResult : deleteResults) { - assertFalse(failedDeleteResult.get()); - } - - // Check gets - for (StorageBatchResult failedGetResult : getResults) { - assertNull(failedGetResult.get()); - } - Blob remoteBlob1 = getResult.get(); - assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket()); - assertEquals(sourceBlob1.getName(), remoteBlob1.getName()); - - // Check updates - for (StorageBatchResult 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