3131import com .google .cloud .storage .BlobInfo ;
3232import com .google .cloud .storage .BucketInfo ;
3333import com .google .cloud .storage .Storage ;
34+ import com .google .cloud .storage .Storage .BlobListOption ;
3435import com .google .cloud .storage .StorageException ;
3536import com .google .cloud .storage .testing .RemoteStorageHelper ;
3637import com .google .common .collect .ImmutableMap ;
3738import com .google .common .collect .Sets ;
3839
40+ import org .junit .After ;
3941import org .junit .AfterClass ;
42+ import org .junit .Before ;
4043import org .junit .BeforeClass ;
4144import org .junit .Rule ;
4245import org .junit .Test ;
4649import java .io .InputStream ;
4750import java .net .URL ;
4851import java .net .URLConnection ;
52+ import java .util .Iterator ;
4953import java .util .Set ;
5054import java .util .concurrent .ExecutionException ;
5155import java .util .concurrent .TimeUnit ;
@@ -71,7 +75,6 @@ public static void beforeClass() {
7175 RemoteStorageHelper helper = RemoteStorageHelper .create ();
7276 storage = helper .getOptions ().getService ();
7377 storage .create (BucketInfo .of (BUCKET ));
74- blob = storage .create (BlobInfo .newBuilder (BUCKET , BLOB ).build ());
7578 }
7679
7780 @ AfterClass
@@ -84,6 +87,18 @@ public static void afterClass() throws ExecutionException, InterruptedException
8487 }
8588 }
8689
90+ @ Before
91+ public void before () {
92+ blob = storage .create (BlobInfo .newBuilder (BUCKET , BLOB ).build ());
93+ }
94+
95+ @ After
96+ public void after () {
97+ for (BlobInfo info : storage .list (BUCKET , BlobListOption .versions (true )).getValues ()) {
98+ storage .delete (info .getBlobId ());
99+ }
100+ }
101+
87102 @ Test
88103 public void testBlob () throws IOException {
89104 BlobSnippets blobSnippets = new BlobSnippets (blob );
@@ -137,4 +152,21 @@ public void testBlob() throws IOException {
137152 assertNull (blobSnippets .getAcl ());
138153 storage .delete (BlobId .of (BUCKET , BLOB ));
139154 }
155+
156+ @ Test
157+ public void testMoveBlob () throws IOException {
158+ BlobSnippets blobSnippets = new BlobSnippets (blob );
159+
160+ Blob movedBlob = blobSnippets .moveTo (BUCKET , "moveBlob" );
161+ assertNotNull (movedBlob );
162+
163+ // Assert that the destination blob exists
164+ Iterator <Blob > blobs = storage .list (BUCKET ).iterateAll ();
165+ Blob moveBlob = blobs .next ();
166+ assertEquals (BUCKET , moveBlob .getBucket ());
167+ assertEquals ("moveBlob" , moveBlob .getName ());
168+
169+ // Assert that the old blob doesn't exist
170+ assertFalse (blobs .hasNext ());
171+ }
140172}
0 commit comments