Skip to content

Commit e030050

Browse files
authored
chore: add tests for object contexts samples
2 parents cf628de + fd03bf2 commit e030050

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import com.example.storage.object.GenerateEncryptionKey;
4444
import com.example.storage.object.GenerateV4GetObjectSignedUrl;
4545
import com.example.storage.object.GenerateV4PutObjectSignedUrl;
46+
import com.example.storage.object.GetObjectContexts;
4647
import com.example.storage.object.GetObjectMetadata;
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.ObjectContexts;
72+
import com.google.cloud.storage.BlobInfo.ObjectCustomContextPayload;
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;
@@ -633,4 +640,73 @@ public void testRestoreSoftDeletedObject() throws Exception {
633640
assertNotNull(storage.get(BlobId.of(bucketName, blob)));
634641
}
635642
}
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+
}
636712
}

0 commit comments

Comments
 (0)