Skip to content

Commit cceee4e

Browse files
committed
chore: add tests for object contexts samples
1 parent e94c4e6 commit cceee4e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.example.storage.object.GenerateV4GetObjectSignedUrl;
4545
import com.example.storage.object.GenerateV4PutObjectSignedUrl;
4646
import com.example.storage.object.GetObjectMetadata;
47+
import com.example.storage.object.GetObjectContexts;
48+
import com.example.storage.object.ListObjectContexts;
4749
import com.example.storage.object.ListObjects;
4850
import com.example.storage.object.ListObjectsWithOldVersions;
4951
import com.example.storage.object.ListObjectsWithPrefix;
@@ -52,6 +54,7 @@
5254
import com.example.storage.object.MakeObjectPublic;
5355
import com.example.storage.object.RestoreSoftDeletedObject;
5456
import com.example.storage.object.RotateObjectEncryptionKey;
57+
import com.example.storage.object.SetObjectContexts;
5558
import com.example.storage.object.SetObjectMetadata;
5659
import com.example.storage.object.SetObjectRetentionPolicy;
5760
import com.example.storage.object.StreamObjectDownload;
@@ -65,6 +68,8 @@
6568
import com.google.cloud.storage.Blob;
6669
import com.google.cloud.storage.BlobId;
6770
import com.google.cloud.storage.BlobInfo;
71+
import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;
72+
import com.google.cloud.storage.BlobInfo.ObjectContexts;
6873
import com.google.cloud.storage.Bucket;
6974
import com.google.cloud.storage.BucketInfo;
7075
import com.google.cloud.storage.BucketInfo.IamConfiguration;
@@ -79,6 +84,7 @@
7984
import com.google.cloud.storage.it.runner.annotations.Inject;
8085
import com.google.cloud.storage.it.runner.registry.KmsFixture;
8186
import com.google.common.collect.ImmutableMap;
87+
import com.google.common.collect.Maps;
8288
import com.google.common.io.BaseEncoding;
8389
import java.io.File;
8490
import java.io.IOException;
@@ -89,6 +95,7 @@
8995
import java.nio.file.Path;
9096
import java.time.Duration;
9197
import java.util.Date;
98+
import java.util.Map;
9299
import java.util.Random;
93100
import javax.net.ssl.HttpsURLConnection;
94101
import org.junit.Assert;
@@ -630,4 +637,70 @@ public void testRestoreSoftDeletedObject() throws Exception {
630637
assertNotNull(storage.get(BlobId.of(bucketName, blob)));
631638
}
632639
}
640+
641+
@Test
642+
public void testSetObjectContexts() throws Exception {
643+
String blobName = generator.randomObjectName();
644+
String key = "test-key-get";
645+
String value = "test-value-get";
646+
647+
Blob initialBlob = storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist());
648+
649+
SetObjectContexts.setObjectContexts(
650+
GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName, key, value);
651+
String setOutput = stdOut.getCapturedOutputAsUtf8String();
652+
assertThat(setOutput).contains("Updated custom contexts for object " + blobName);
653+
654+
Blob updatedBlob = storage.get(bucket.getName(), blobName);
655+
assertThat(updatedBlob.getContexts().getCustom().get(key).getValue()).isEqualTo(value);
656+
}
657+
658+
@Test
659+
public void testGetObjectContexts() throws Exception {
660+
String blobName = generator.randomObjectName();
661+
String key = "test-key-get";
662+
String value = "test-value-get";
663+
664+
storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist());
665+
666+
ObjectCustomContextPayload payload =
667+
ObjectCustomContextPayload.newBuilder().setValue(value).build();
668+
Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();
669+
custom.put(key, payload);
670+
ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();
671+
BlobInfo pendingUpdate = storage.get(bucket.getName(), blobName).toBuilder().setContexts(contexts).build();
672+
storage.update(pendingUpdate);
673+
674+
GetObjectContexts.getObjectContexts(GOOGLE_CLOUD_PROJECT, bucket.getName(), blobName);
675+
676+
String getOutput = stdOut.getCapturedOutputAsUtf8String();
677+
678+
assertThat(getOutput).contains("Custom Contexts:");
679+
assertThat(getOutput).contains(key + "=ObjectCustomContextPayload{");
680+
assertThat(getOutput).contains("value=" + value);
681+
}
682+
683+
@Test
684+
public void testListObjectContexts() throws Exception {
685+
String blobName = generator.randomObjectName();
686+
String key = "test-key-list";
687+
String value = "test-value-list";
688+
689+
storage.create(info(blobName), CONTENT, BlobTargetOption.doesNotExist());
690+
691+
ObjectCustomContextPayload payload =
692+
ObjectCustomContextPayload.newBuilder().setValue(value).build();
693+
Map<String, ObjectCustomContextPayload> custom = Maps.newHashMap();
694+
custom.put(key, payload);
695+
ObjectContexts contexts = ObjectContexts.newBuilder().setCustom(custom).build();
696+
BlobInfo pendingUpdate = storage.get(bucket.getName(), blobName).toBuilder().setContexts(contexts).build();
697+
storage.update(pendingUpdate);
698+
699+
ListObjectContexts.listObjectContexts(GOOGLE_CLOUD_PROJECT, bucket.getName(), key);
700+
String listOutput = stdOut.getCapturedOutputAsUtf8String();
701+
702+
assertThat(listOutput).contains("gs://" + bucket.getName() + "/" + blobName);
703+
704+
assertThat(listOutput).contains("Listing objects for bucket: " + bucket.getName() + "with context key: " + key);
705+
}
633706
}

0 commit comments

Comments
 (0)