2525import static org .junit .Assert .fail ;
2626
2727import com .google .common .collect .ImmutableList ;
28+ import com .google .common .collect .ImmutableMap ;
2829import com .google .gcloud .RestorableState ;
2930import com .google .gcloud .storage .testing .RemoteGcsHelper ;
3031
3637import java .net .URLConnection ;
3738import java .nio .ByteBuffer ;
3839import java .util .Arrays ;
40+ import java .util .HashMap ;
3941import java .util .Iterator ;
4042import java .util .List ;
43+ import java .util .Map ;
4144import java .util .concurrent .ExecutionException ;
4245import java .util .concurrent .TimeUnit ;
4346import java .util .concurrent .TimeoutException ;
@@ -95,8 +98,7 @@ public void testCreateBlob() {
9598 BlobInfo blob = BlobInfo .builder (bucket , blobName ).build ();
9699 BlobInfo remoteBlob = storage .create (blob , BLOB_BYTE_CONTENT );
97100 assertNotNull (remoteBlob );
98- assertEquals (blob .bucket (), remoteBlob .bucket ());
99- assertEquals (blob .name (), remoteBlob .name ());
101+ assertEquals (blob .blobId (), remoteBlob .blobId ());
100102 byte [] readBytes = storage .readAllBytes (bucket , blobName );
101103 assertArrayEquals (BLOB_BYTE_CONTENT , readBytes );
102104 assertTrue (storage .delete (bucket , blobName ));
@@ -108,8 +110,7 @@ public void testCreateEmptyBlob() {
108110 BlobInfo blob = BlobInfo .builder (bucket , blobName ).build ();
109111 BlobInfo remoteBlob = storage .create (blob );
110112 assertNotNull (remoteBlob );
111- assertEquals (blob .bucket (), remoteBlob .bucket ());
112- assertEquals (blob .name (), remoteBlob .name ());
113+ assertEquals (blob .blobId (), remoteBlob .blobId ());
113114 byte [] readBytes = storage .readAllBytes (bucket , blobName );
114115 assertArrayEquals (new byte [0 ], readBytes );
115116 assertTrue (storage .delete (bucket , blobName ));
@@ -122,8 +123,7 @@ public void testCreateBlobStream() throws UnsupportedEncodingException {
122123 ByteArrayInputStream stream = new ByteArrayInputStream (BLOB_STRING_CONTENT .getBytes (UTF_8 ));
123124 BlobInfo remoteBlob = storage .create (blob , stream );
124125 assertNotNull (remoteBlob );
125- assertEquals (blob .bucket (), remoteBlob .bucket ());
126- assertEquals (blob .name (), remoteBlob .name ());
126+ assertEquals (blob .blobId (), remoteBlob .blobId ());
127127 assertEquals (blob .contentType (), remoteBlob .contentType ());
128128 byte [] readBytes = storage .readAllBytes (bucket , blobName );
129129 assertEquals (BLOB_STRING_CONTENT , new String (readBytes , UTF_8 ));
@@ -168,12 +168,68 @@ public void testUpdateBlob() {
168168 assertNotNull (storage .create (blob ));
169169 BlobInfo updatedBlob = storage .update (blob .toBuilder ().contentType (CONTENT_TYPE ).build ());
170170 assertNotNull (updatedBlob );
171- assertEquals (blob .bucket (), updatedBlob .bucket ());
172- assertEquals (blob .name (), updatedBlob .name ());
171+ assertEquals (blob .blobId (), updatedBlob .blobId ());
173172 assertEquals (CONTENT_TYPE , updatedBlob .contentType ());
174173 assertTrue (storage .delete (bucket , blobName ));
175174 }
176175
176+ @ Test
177+ public void testUpdateBlobReplaceMetadata () {
178+ String blobName = "test-update-blob-replace-metadata" ;
179+ ImmutableMap <String , String > metadata = ImmutableMap .of ("k1" , "a" );
180+ ImmutableMap <String , String > newMetadata = ImmutableMap .of ("k2" , "b" );
181+ BlobInfo blob = BlobInfo .builder (bucket , blobName )
182+ .contentType (CONTENT_TYPE )
183+ .metadata (metadata )
184+ .build ();
185+ assertNotNull (storage .create (blob ));
186+ BlobInfo updatedBlob = storage .update (blob .toBuilder ().metadata (null ).build ());
187+ assertNotNull (updatedBlob );
188+ assertNull (updatedBlob .metadata ());
189+ updatedBlob = storage .update (blob .toBuilder ().metadata (newMetadata ).build ());
190+ assertEquals (blob .blobId (), updatedBlob .blobId ());
191+ assertEquals (newMetadata , updatedBlob .metadata ());
192+ assertTrue (storage .delete (bucket , blobName ));
193+ }
194+
195+ @ Test
196+ public void testUpdateBlobMergeMetadata () {
197+ String blobName = "test-update-blob-merge-metadata" ;
198+ ImmutableMap <String , String > metadata = ImmutableMap .of ("k1" , "a" );
199+ ImmutableMap <String , String > newMetadata = ImmutableMap .of ("k2" , "b" );
200+ ImmutableMap <String , String > expectedMetadata = ImmutableMap .of ("k1" , "a" , "k2" , "b" );
201+ BlobInfo blob = BlobInfo .builder (bucket , blobName )
202+ .contentType (CONTENT_TYPE )
203+ .metadata (metadata )
204+ .build ();
205+ assertNotNull (storage .create (blob ));
206+ BlobInfo updatedBlob = storage .update (blob .toBuilder ().metadata (newMetadata ).build ());
207+ assertNotNull (updatedBlob );
208+ assertEquals (blob .blobId (), updatedBlob .blobId ());
209+ assertEquals (expectedMetadata , updatedBlob .metadata ());
210+ assertTrue (storage .delete (bucket , blobName ));
211+ }
212+
213+ @ Test
214+ public void testUpdateBlobUnsetMetadata () {
215+ String blobName = "test-update-blob-unset-metadata" ;
216+ ImmutableMap <String , String > metadata = ImmutableMap .of ("k1" , "a" , "k2" , "b" );
217+ Map <String , String > newMetadata = new HashMap <>();
218+ newMetadata .put ("k1" , "a" );
219+ newMetadata .put ("k2" , null );
220+ ImmutableMap <String , String > expectedMetadata = ImmutableMap .of ("k1" , "a" );
221+ BlobInfo blob = BlobInfo .builder (bucket , blobName )
222+ .contentType (CONTENT_TYPE )
223+ .metadata (metadata )
224+ .build ();
225+ assertNotNull (storage .create (blob ));
226+ BlobInfo updatedBlob = storage .update (blob .toBuilder ().metadata (newMetadata ).build ());
227+ assertNotNull (updatedBlob );
228+ assertEquals (blob .blobId (), updatedBlob .blobId ());
229+ assertEquals (expectedMetadata , updatedBlob .metadata ());
230+ assertTrue (storage .delete (bucket , blobName ));
231+ }
232+
177233 @ Test
178234 public void testUpdateBlobFail () {
179235 String blobName = "test-update-blob-fail" ;
@@ -223,8 +279,7 @@ public void testComposeBlob() {
223279 Storage .ComposeRequest .of (ImmutableList .of (sourceBlobName1 , sourceBlobName2 ), targetBlob );
224280 BlobInfo remoteBlob = storage .compose (req );
225281 assertNotNull (remoteBlob );
226- assertEquals (bucket , remoteBlob .bucket ());
227- assertEquals (targetBlobName , remoteBlob .name ());
282+ assertEquals (targetBlob .blobId (), remoteBlob .blobId ());
228283 byte [] readBytes = storage .readAllBytes (bucket , targetBlobName );
229284 byte [] composedBytes = Arrays .copyOf (BLOB_BYTE_CONTENT , BLOB_BYTE_CONTENT .length * 2 );
230285 System .arraycopy (BLOB_BYTE_CONTENT , 0 , composedBytes , BLOB_BYTE_CONTENT .length ,
@@ -288,8 +343,7 @@ public void testCopyBlobUpdateMetadata() {
288343 Storage .CopyRequest req = Storage .CopyRequest .of (bucket , sourceBlobName , targetBlob );
289344 BlobInfo remoteBlob = storage .copy (req );
290345 assertNotNull (remoteBlob );
291- assertEquals (bucket , remoteBlob .bucket ());
292- assertEquals (targetBlobName , remoteBlob .name ());
346+ assertEquals (targetBlob .blobId (), remoteBlob .blobId ());
293347 assertEquals (CONTENT_TYPE , remoteBlob .contentType ());
294348 assertTrue (storage .delete (bucket , sourceBlobName ));
295349 assertTrue (storage .delete (bucket , targetBlobName ));
@@ -337,10 +391,8 @@ public void testBatchRequest() {
337391 assertEquals (0 , updateResponse .gets ().size ());
338392 BlobInfo remoteUpdatedBlob1 = updateResponse .updates ().get (0 ).get ();
339393 BlobInfo remoteUpdatedBlob2 = updateResponse .updates ().get (1 ).get ();
340- assertEquals (bucket , remoteUpdatedBlob1 .bucket ());
341- assertEquals (bucket , remoteUpdatedBlob2 .bucket ());
342- assertEquals (updatedBlob1 .name (), remoteUpdatedBlob1 .name ());
343- assertEquals (updatedBlob2 .name (), remoteUpdatedBlob2 .name ());
394+ assertEquals (sourceBlob1 .blobId (), remoteUpdatedBlob1 .blobId ());
395+ assertEquals (sourceBlob2 .blobId (), remoteUpdatedBlob2 .blobId ());
344396 assertEquals (updatedBlob1 .contentType (), remoteUpdatedBlob1 .contentType ());
345397 assertEquals (updatedBlob2 .contentType (), remoteUpdatedBlob2 .contentType ());
346398
@@ -515,8 +567,7 @@ public void testPostSignedUrl() throws IOException {
515567 connection .connect ();
516568 BlobInfo remoteBlob = storage .get (bucket , blobName );
517569 assertNotNull (remoteBlob );
518- assertEquals (bucket , remoteBlob .bucket ());
519- assertEquals (blob .name (), remoteBlob .name ());
570+ assertEquals (blob .blobId (), remoteBlob .blobId ());
520571 assertTrue (storage .delete (bucket , blobName ));
521572 }
522573
@@ -528,11 +579,9 @@ public void testGetBlobs() {
528579 BlobInfo sourceBlob2 = BlobInfo .builder (bucket , sourceBlobName2 ).build ();
529580 assertNotNull (storage .create (sourceBlob1 ));
530581 assertNotNull (storage .create (sourceBlob2 ));
531- List <BlobInfo > remoteInfos = storage .get (sourceBlob1 .blobId (), sourceBlob2 .blobId ());
532- assertEquals (sourceBlob1 .bucket (), remoteInfos .get (0 ).bucket ());
533- assertEquals (sourceBlob1 .name (), remoteInfos .get (0 ).name ());
534- assertEquals (sourceBlob2 .bucket (), remoteInfos .get (1 ).bucket ());
535- assertEquals (sourceBlob2 .name (), remoteInfos .get (1 ).name ());
582+ List <BlobInfo > remoteBlobs = storage .get (sourceBlob1 .blobId (), sourceBlob2 .blobId ());
583+ assertEquals (sourceBlob1 .blobId (), remoteBlobs .get (0 ).blobId ());
584+ assertEquals (sourceBlob2 .blobId (), remoteBlobs .get (1 ).blobId ());
536585 assertTrue (storage .delete (bucket , sourceBlobName1 ));
537586 assertTrue (storage .delete (bucket , sourceBlobName2 ));
538587 }
@@ -545,8 +594,7 @@ public void testGetBlobsFail() {
545594 BlobInfo sourceBlob2 = BlobInfo .builder (bucket , sourceBlobName2 ).build ();
546595 assertNotNull (storage .create (sourceBlob1 ));
547596 List <BlobInfo > remoteBlobs = storage .get (sourceBlob1 .blobId (), sourceBlob2 .blobId ());
548- assertEquals (sourceBlob1 .bucket (), remoteBlobs .get (0 ).bucket ());
549- assertEquals (sourceBlob1 .name (), remoteBlobs .get (0 ).name ());
597+ assertEquals (sourceBlob1 .blobId (), remoteBlobs .get (0 ).blobId ());
550598 assertNull (remoteBlobs .get (1 ));
551599 assertTrue (storage .delete (bucket , sourceBlobName1 ));
552600 }
@@ -589,11 +637,9 @@ public void testUpdateBlobs() {
589637 List <BlobInfo > updatedBlobs = storage .update (
590638 remoteBlob1 .toBuilder ().contentType (CONTENT_TYPE ).build (),
591639 remoteBlob2 .toBuilder ().contentType (CONTENT_TYPE ).build ());
592- assertEquals (sourceBlob1 .bucket (), updatedBlobs .get (0 ).bucket ());
593- assertEquals (sourceBlob1 .name (), updatedBlobs .get (0 ).name ());
640+ assertEquals (sourceBlob1 .blobId (), updatedBlobs .get (0 ).blobId ());
594641 assertEquals (CONTENT_TYPE , updatedBlobs .get (0 ).contentType ());
595- assertEquals (sourceBlob2 .bucket (), updatedBlobs .get (1 ).bucket ());
596- assertEquals (sourceBlob2 .name (), updatedBlobs .get (1 ).name ());
642+ assertEquals (sourceBlob2 .blobId (), updatedBlobs .get (1 ).blobId ());
597643 assertEquals (CONTENT_TYPE , updatedBlobs .get (1 ).contentType ());
598644 assertTrue (storage .delete (bucket , sourceBlobName1 ));
599645 assertTrue (storage .delete (bucket , sourceBlobName2 ));
@@ -610,8 +656,7 @@ public void testUpdateBlobsFail() {
610656 List <BlobInfo > updatedBlobs = storage .update (
611657 remoteBlob1 .toBuilder ().contentType (CONTENT_TYPE ).build (),
612658 sourceBlob2 .toBuilder ().contentType (CONTENT_TYPE ).build ());
613- assertEquals (sourceBlob1 .bucket (), updatedBlobs .get (0 ).bucket ());
614- assertEquals (sourceBlob1 .name (), updatedBlobs .get (0 ).name ());
659+ assertEquals (sourceBlob1 .blobId (), updatedBlobs .get (0 ).blobId ());
615660 assertEquals (CONTENT_TYPE , updatedBlobs .get (0 ).contentType ());
616661 assertNull (updatedBlobs .get (1 ));
617662 assertTrue (storage .delete (bucket , sourceBlobName1 ));
0 commit comments