@@ -239,12 +239,13 @@ public void testCreateBlobWithEncryptionKey() {
239239 @ Test
240240 public void testCreateBlobWithKmsKeyName () {
241241 String blobName = "test-create-with-kms-key-name-blob" ;
242- BlobInfo blob = BlobInfo .newBuilder (BUCKET , blobName ).setKmsKeyName ( KMS_KEY_NAME_1 ). build ();
243- Blob remoteBlob = storage .create (blob , BLOB_BYTE_CONTENT );
242+ BlobInfo blob = BlobInfo .newBuilder (BUCKET , blobName ).build ();
243+ Blob remoteBlob = storage .create (blob , BLOB_BYTE_CONTENT , Storage . BlobTargetOption . kmsKeyName ( KMS_KEY_NAME_1 ) );
244244 assertNotNull (remoteBlob );
245245 assertEquals (blob .getBucket (), remoteBlob .getBucket ());
246246 assertEquals (blob .getName (), remoteBlob .getName ());
247- assertTrue (remoteBlob .getKmsKeyName ().startsWith (blob .getKmsKeyName ()));
247+ assertNotNull (remoteBlob .getKmsKeyName ());
248+ assertTrue (remoteBlob .getKmsKeyName ().startsWith (KMS_KEY_NAME_1 ));
248249 byte [] readBytes = storage .readAllBytes (BUCKET , blobName );
249250 assertArrayEquals (BLOB_BYTE_CONTENT , readBytes );
250251 }
@@ -253,8 +254,9 @@ public void testCreateBlobWithKmsKeyName() {
253254 public void testCreateBlobWithKmsKeyNameAndCustomerSuppliedKey () {
254255 try {
255256 String blobName = "test-create-with-kms-key-name-blob" ;
256- BlobInfo blob = BlobInfo .newBuilder (BUCKET , blobName ).setKmsKeyName (KMS_KEY_NAME_1 ).build ();
257- storage .create (blob , BLOB_BYTE_CONTENT , Storage .BlobTargetOption .encryptionKey (KEY ));
257+ BlobInfo blob = BlobInfo .newBuilder (BUCKET , blobName ).build ();
258+ storage .create (blob , BLOB_BYTE_CONTENT , Storage .BlobTargetOption .encryptionKey (KEY ),
259+ Storage .BlobTargetOption .kmsKeyName (KMS_KEY_NAME_1 ));
258260 fail ("StorageException was expected" ); // can't supply both.
259261 } catch (StorageException ex ) {
260262 // expected
@@ -367,6 +369,20 @@ public void testGetBlobSelectedFields() {
367369 assertNull (remoteBlob .getContentType ());
368370 }
369371
372+ @ Test
373+ public void testGetBlobKmsKeyNameField () {
374+ String blobName = "test-get-selected-kms-key-name-field-blob" ;
375+ BlobInfo blob = BlobInfo .newBuilder (BUCKET , blobName )
376+ .setContentType (CONTENT_TYPE )
377+ .build ();
378+ assertNotNull (storage .create (blob , Storage .BlobTargetOption .kmsKeyName (KMS_KEY_NAME_1 )));
379+ Blob remoteBlob = storage .get (blob .getBlobId (), Storage .BlobGetOption .fields (
380+ BlobField .KMS_KEY_NAME ));
381+ assertEquals (blob .getBlobId (), remoteBlob .getBlobId ());
382+ assertTrue (remoteBlob .getKmsKeyName ().startsWith (KMS_KEY_NAME_1 ));
383+ assertNull (remoteBlob .getContentType ());
384+ }
385+
370386 @ Test
371387 public void testGetBlobAllSelectedFields () {
372388 String blobName = "test-get-all-selected-fields-blob" ;
@@ -459,14 +475,12 @@ public void testListBlobsKmsKeySelectedFields() throws InterruptedException {
459475 "test-list-blobs-selected-field-kms-key-name-blob2" };
460476 BlobInfo blob1 = BlobInfo .newBuilder (BUCKET , blobNames [0 ])
461477 .setContentType (CONTENT_TYPE )
462- .setKmsKeyName (KMS_KEY_NAME_1 )
463478 .build ();
464479 BlobInfo blob2 = BlobInfo .newBuilder (BUCKET , blobNames [1 ])
465480 .setContentType (CONTENT_TYPE )
466- .setKmsKeyName (KMS_KEY_NAME_1 )
467481 .build ();
468- Blob remoteBlob1 = storage .create (blob1 );
469- Blob remoteBlob2 = storage .create (blob2 );
482+ Blob remoteBlob1 = storage .create (blob1 , Storage . BlobTargetOption . kmsKeyName ( KMS_KEY_NAME_1 ) );
483+ Blob remoteBlob2 = storage .create (blob2 , Storage . BlobTargetOption . kmsKeyName ( KMS_KEY_NAME_1 ) );
470484 assertNotNull (remoteBlob1 );
471485 assertNotNull (remoteBlob2 );
472486 Page <Blob > page = storage .list (BUCKET ,
@@ -936,12 +950,11 @@ public void testRotateFromCustomerEncryptionToKmsKey() {
936950 BlobInfo target = BlobInfo .newBuilder (BUCKET , targetBlobName )
937951 .setContentType (CONTENT_TYPE )
938952 .setMetadata (metadata )
939- .setKmsKeyName (KMS_KEY_NAME_1 )
940953 .build ();
941954 Storage .CopyRequest req = Storage .CopyRequest .newBuilder ()
942955 .setSource (source )
943956 .setSourceOptions (Storage .BlobSourceOption .decryptionKey (BASE64_KEY ))
944- .setTarget (target )
957+ .setTarget (target , Storage . BlobTargetOption . kmsKeyName ( KMS_KEY_NAME_1 ) )
945958 .build ();
946959 CopyWriter copyWriter = storage .copy (req );
947960 assertEquals (BUCKET , copyWriter .getResult ().getBucket ());
@@ -967,13 +980,13 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncrytion() {
967980 BlobInfo target = BlobInfo .newBuilder (BUCKET , targetBlobName )
968981 .setContentType (CONTENT_TYPE )
969982 .setMetadata (metadata )
970- .setKmsKeyName (KMS_KEY_NAME_1 )
971983 .build ();
972984 try {
973985 Storage .CopyRequest req = Storage .CopyRequest .newBuilder ()
974986 .setSource (source )
975987 .setSourceOptions (Storage .BlobSourceOption .decryptionKey (BASE64_KEY ))
976- .setTarget (target , Storage .BlobTargetOption .encryptionKey (KEY ))
988+ .setTarget (target , Storage .BlobTargetOption .encryptionKey (KEY ),
989+ Storage .BlobTargetOption .kmsKeyName (KMS_KEY_NAME_1 ))
977990 .build ();
978991 storage .copy (req );
979992 fail ("StorageException was expected" );
@@ -982,7 +995,6 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncrytion() {
982995 }
983996 }
984997
985-
986998 @ Test
987999 public void testCopyBlobUpdateMetadata () {
9881000 String sourceBlobName = "test-copy-blob-update-metadata-source" ;
@@ -1847,6 +1859,29 @@ public void testListBucketRequesterPaysFails() throws InterruptedException {
18471859 }
18481860 }
18491861
1862+ @ Test
1863+ public void testListBucketDefaultKmsKeyName () throws InterruptedException {
1864+ Bucket remoteBucket = storage .get (BUCKET , Storage .BucketGetOption .fields (BucketField .ENCRYPTION ));
1865+ assertNull (remoteBucket .getDefaultKmsKeyName ());
1866+ remoteBucket = remoteBucket .toBuilder ().setDefaultKmsKeyName (KMS_KEY_NAME_1 ).build ().update ();
1867+ assertTrue (remoteBucket .getDefaultKmsKeyName ().startsWith (KMS_KEY_NAME_1 ));
1868+ Iterator <Bucket > bucketIterator = storage .list (Storage .BucketListOption .prefix (BUCKET ),
1869+ Storage .BucketListOption .fields (BucketField .ENCRYPTION )).iterateAll ().iterator ();
1870+ while (!bucketIterator .hasNext ()) {
1871+ Thread .sleep (500 );
1872+ bucketIterator = storage .list (Storage .BucketListOption .prefix (BUCKET ),
1873+ Storage .BucketListOption .fields (BucketField .ENCRYPTION )).iterateAll ().iterator ();
1874+ }
1875+ while (bucketIterator .hasNext ()) {
1876+ Bucket bucket = bucketIterator .next ();
1877+ assertTrue (bucket .getName ().startsWith (BUCKET ));
1878+ assertNotNull (bucket .getDefaultKmsKeyName ());
1879+ assertTrue (bucket .getDefaultKmsKeyName ().startsWith (KMS_KEY_NAME_1 ));
1880+ assertNull (bucket .getCreateTime ());
1881+ assertNull (bucket .getSelfLink ());
1882+ }
1883+ }
1884+
18501885 @ Test
18511886 public void testGetServiceAccount () throws InterruptedException {
18521887 String projectId = remoteStorageHelper .getOptions ().getProjectId ();
0 commit comments