|
43 | 43 | import com.example.storage.object.GenerateEncryptionKey; |
44 | 44 | import com.example.storage.object.GenerateV4GetObjectSignedUrl; |
45 | 45 | import com.example.storage.object.GenerateV4PutObjectSignedUrl; |
| 46 | +import com.example.storage.object.GetObjectContexts; |
46 | 47 | import com.example.storage.object.GetObjectMetadata; |
| 48 | +import com.example.storage.object.ListObjectContexts; |
47 | 49 | import com.example.storage.object.ListObjects; |
48 | 50 | import com.example.storage.object.ListObjectsWithOldVersions; |
49 | 51 | import com.example.storage.object.ListObjectsWithPrefix; |
|
52 | 54 | import com.example.storage.object.MakeObjectPublic; |
53 | 55 | import com.example.storage.object.RestoreSoftDeletedObject; |
54 | 56 | import com.example.storage.object.RotateObjectEncryptionKey; |
| 57 | +import com.example.storage.object.SetObjectContexts; |
55 | 58 | import com.example.storage.object.SetObjectMetadata; |
56 | 59 | import com.example.storage.object.SetObjectRetentionPolicy; |
57 | 60 | import com.example.storage.object.StreamObjectDownload; |
|
65 | 68 | import com.google.cloud.storage.Blob; |
66 | 69 | import com.google.cloud.storage.BlobId; |
67 | 70 | import com.google.cloud.storage.BlobInfo; |
| 71 | +import com.google.cloud.storage.BlobInfo.ObjectContexts; |
| 72 | +import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload; |
68 | 73 | import com.google.cloud.storage.Bucket; |
69 | 74 | import com.google.cloud.storage.BucketInfo; |
70 | 75 | import com.google.cloud.storage.BucketInfo.IamConfiguration; |
|
79 | 84 | import com.google.cloud.storage.it.runner.annotations.Inject; |
80 | 85 | import com.google.cloud.storage.it.runner.registry.KmsFixture; |
81 | 86 | import com.google.common.collect.ImmutableMap; |
| 87 | +import com.google.common.collect.Maps; |
82 | 88 | import com.google.common.io.BaseEncoding; |
83 | 89 | import java.io.File; |
84 | 90 | import java.io.IOException; |
|
89 | 95 | import java.nio.file.Path; |
90 | 96 | import java.time.Duration; |
91 | 97 | import java.util.Date; |
| 98 | +import java.util.Map; |
92 | 99 | import java.util.Random; |
93 | 100 | import javax.net.ssl.HttpsURLConnection; |
94 | 101 | import org.junit.Assert; |
@@ -633,4 +640,73 @@ public void testRestoreSoftDeletedObject() throws Exception { |
633 | 640 | assertNotNull(storage.get(BlobId.of(bucketName, blob))); |
634 | 641 | } |
635 | 642 | } |
| 643 | + |
| 644 | + @Test |
| 645 | + public void testSetObjectContexts() throws Exception { |
| 646 | + String blobName = generator.randomObjectName(); |
| 647 | + String key = "test-key-get"; |
| 648 | + String value = "test-value-get"; |
| 649 | + |
| 650 | + Blob initialBlob = storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist()); |
| 651 | + |
| 652 | + SetObjectContexts.setObjectContexts( |
| 653 | + GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName, key, value); |
| 654 | + String setOutput = stdOut.getCapturedOutputAsUtf8String(); |
| 655 | + assertThat(setOutput).contains("Updated custom contexts for object " + blobName); |
| 656 | + |
| 657 | + Blob updatedBlob = storage.get(bucket.getName(), blobName); |
| 658 | + assertThat(updatedBlob.getContexts().getCustom().get(key).getValue()).isEqualTo(value); |
| 659 | + } |
| 660 | + |
| 661 | + @Test |
| 662 | + public void testGetObjectContexts() throws Exception { |
| 663 | + String blobName = generator.randomObjectName(); |
| 664 | + String key = "test-key-get"; |
| 665 | + String value = "test-value-get"; |
| 666 | + |
| 667 | + storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist()); |
| 668 | + |
| 669 | + ObjectCustomContextPayload payload = |
| 670 | + ObjectCustomContextPayload.newBuilder().setValue(value).build(); |
| 671 | + Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap(); |
| 672 | + custom.put(key, payload); |
| 673 | + ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build(); |
| 674 | + BlobInfo pendingUpdate = |
| 675 | + storage.get(bucket.getName(), blobName).toBuilder().setContexts(contexts).build(); |
| 676 | + storage.update(pendingUpdate); |
| 677 | + |
| 678 | + GetObjectContexts.getObjectContexts(GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName); |
| 679 | + |
| 680 | + String getOutput = stdOut.getCapturedOutputAsUtf8String(); |
| 681 | + |
| 682 | + assertThat(getOutput).contains("Custom Contexts:"); |
| 683 | + assertThat(getOutput).contains(key + "=ObjectCustomContextPayload{"); |
| 684 | + assertThat(getOutput).contains("value=" + value); |
| 685 | + } |
| 686 | + |
| 687 | + @Test |
| 688 | + public void testListObjectContexts() throws Exception { |
| 689 | + String blobName = generator.randomObjectName(); |
| 690 | + String key = "test-key-list"; |
| 691 | + String value = "test-value-list"; |
| 692 | + |
| 693 | + storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist()); |
| 694 | + |
| 695 | + ObjectCustomContextPayload payload = |
| 696 | + ObjectCustomContextPayload.newBuilder().setValue(value).build(); |
| 697 | + Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap(); |
| 698 | + custom.put(key, payload); |
| 699 | + ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build(); |
| 700 | + BlobInfo pendingUpdate = |
| 701 | + storage.get(bucket.getName(), blobName).toBuilder().setContexts(contexts).build(); |
| 702 | + storage.update(pendingUpdate); |
| 703 | + |
| 704 | + ListObjectContexts.listObjectContexts(GOOGLE_CLOUD_PROJECT, bucket.getName(), key); |
| 705 | + String listOutput = stdOut.getCapturedOutputAsUtf8String(); |
| 706 | + |
| 707 | + assertThat(listOutput).contains("gs://" + bucket.getName() + "/" + blobName); |
| 708 | + |
| 709 | + assertThat(listOutput) |
| 710 | + .contains("Listing objects for bucket: " + bucket.getName() + "with context key: " + key); |
| 711 | + } |
636 | 712 | } |
0 commit comments