/
BlobTest.java
706 lines (665 loc) · 27.8 KB
/
BlobTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
* Copyright 2015 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.getCurrentArguments;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.api.core.ApiClock;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.ReadChannel;
import com.google.cloud.storage.Acl.Project;
import com.google.cloud.storage.Acl.Project.ProjectRole;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.Blob.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobWriteOption;
import com.google.cloud.storage.Storage.CopyRequest;
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import java.io.File;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.security.Key;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.crypto.spec.SecretKeySpec;
import org.easymock.Capture;
import org.easymock.IAnswer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BlobTest {
private static final Acl ACL = Acl.of(User.ofAllAuthenticatedUsers(), Role.OWNER);
private static final Acl OTHER_ACL = Acl.of(new Project(ProjectRole.OWNERS, "p"), Role.READER);
private static final List<Acl> ACLS = ImmutableList.of(ACL, OTHER_ACL);
private static final Integer COMPONENT_COUNT = 2;
private static final String CONTENT_TYPE = "text/html";
private static final String CACHE_CONTROL = "cache";
private static final String CONTENT_DISPOSITION = "content-disposition";
private static final String CONTENT_ENCODING = "UTF-8";
private static final String CONTENT_LANGUAGE = "En";
private static final String CRC32 = "FF00";
private static final String CRC32_HEX_STRING = "145d34";
private static final Long DELETE_TIME = System.currentTimeMillis();
private static final String ETAG = "0xFF00";
private static final Long GENERATION = 1L;
private static final String GENERATED_ID = "B/N:1";
private static final String MD5 = "FF00";
private static final String MD5_HEX_STRING = "145d34";
private static final String MEDIA_LINK = "http://media/b/n";
private static final Map<String, String> METADATA = ImmutableMap.of("n1", "v1", "n2", "v2");
private static final Long META_GENERATION = 10L;
private static final User OWNER = new User("user@gmail.com");
private static final String SELF_LINK = "http://storage/b/n";
private static final Long SIZE = 1024L;
private static final Long UPDATE_TIME = DELETE_TIME - 1L;
private static final Long CREATE_TIME = UPDATE_TIME - 1L;
private static final Long CUSTOM_TIME = CREATE_TIME - 1L;
private static final StorageClass STORAGE_CLASS = StorageClass.COLDLINE;
private static final Long TIME_STORAGE_CLASS_UPDATED = CREATE_TIME;
private static final String ENCRYPTION_ALGORITHM = "AES256";
private static final String KEY_SHA256 = "keySha";
private static final BlobInfo.CustomerEncryption CUSTOMER_ENCRYPTION =
new BlobInfo.CustomerEncryption(ENCRYPTION_ALGORITHM, KEY_SHA256);
private static final String KMS_KEY_NAME =
"projects/p/locations/kr-loc/keyRings/kr/cryptoKeys/key";
private static final Boolean EVENT_BASED_HOLD = true;
private static final Boolean TEMPORARY_HOLD = true;
private static final Long RETENTION_EXPIRATION_TIME = 10L;
private static final BlobInfo FULL_BLOB_INFO =
BlobInfo.newBuilder("b", "n", GENERATION)
.setAcl(ACLS)
.setComponentCount(COMPONENT_COUNT)
.setContentType(CONTENT_TYPE)
.setCacheControl(CACHE_CONTROL)
.setContentDisposition(CONTENT_DISPOSITION)
.setContentEncoding(CONTENT_ENCODING)
.setContentLanguage(CONTENT_LANGUAGE)
.setCrc32c(CRC32)
.setDeleteTime(DELETE_TIME)
.setEtag(ETAG)
.setGeneratedId(GENERATED_ID)
.setMd5(MD5)
.setMediaLink(MEDIA_LINK)
.setMetadata(METADATA)
.setMetageneration(META_GENERATION)
.setOwner(OWNER)
.setSelfLink(SELF_LINK)
.setSize(SIZE)
.setUpdateTime(UPDATE_TIME)
.setCreateTime(CREATE_TIME)
.setCustomTime(CUSTOM_TIME)
.setStorageClass(STORAGE_CLASS)
.setTimeStorageClassUpdated(TIME_STORAGE_CLASS_UPDATED)
.setCustomerEncryption(CUSTOMER_ENCRYPTION)
.setKmsKeyName(KMS_KEY_NAME)
.setEventBasedHold(EVENT_BASED_HOLD)
.setTemporaryHold(TEMPORARY_HOLD)
.setRetentionExpirationTime(RETENTION_EXPIRATION_TIME)
.build();
private static final BlobInfo BLOB_INFO =
BlobInfo.newBuilder("b", "n", 12345678L).setMetageneration(42L).build();
private static final BlobInfo BLOB_INFO_NO_GENERATION =
BlobInfo.newBuilder(BLOB_INFO.getBucket(), BLOB_INFO.getName())
.setMetageneration(42L)
.build();
private static final BlobInfo DIRECTORY_INFO =
BlobInfo.newBuilder("b", "n/").setSize(0L).setIsDirectory(true).build();
private static final String BASE64_KEY = "JVzfVl8NLD9FjedFuStegjRfES5ll5zc59CIXw572OA=";
private static final Key KEY =
new SecretKeySpec(BaseEncoding.base64().decode(BASE64_KEY), "AES256");
// This retrying setting is used by test testDownloadWithRetries. This unit test is setup
// to write one byte and then throw retryable exception, it then writes another bytes on
// second call succeeds.
private static final RetrySettings RETRY_SETTINGS =
RetrySettings.newBuilder().setMaxAttempts(2).build();
private static final ApiClock API_CLOCK =
new ApiClock() {
@Override
public long nanoTime() {
return 42_000_000_000L;
}
@Override
public long millisTime() {
return 42_000L;
}
};
private Storage storage;
private Blob blob;
private Blob expectedBlob;
private Storage serviceMockReturnsOptions = createMock(Storage.class);
private StorageOptions mockOptions = createMock(StorageOptions.class);
@Before
public void setUp() {
storage = createStrictMock(Storage.class);
}
@After
public void tearDown() throws Exception {
verify(storage);
}
private void initializeExpectedBlob(int optionsCalls) {
expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls);
replay(serviceMockReturnsOptions);
expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BLOB_INFO));
}
private void initializeBlob() {
blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
}
@Test
public void testExists_True() throws Exception {
initializeExpectedBlob(1);
Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()};
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.get(expectedBlob.getBlobId(), expectedOptions)).andReturn(expectedBlob);
replay(storage);
initializeBlob();
assertTrue(blob.exists());
}
@Test
public void testExists_False() throws Exception {
Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()};
expect(storage.getOptions()).andReturn(null);
expect(storage.get(BLOB_INFO.getBlobId(), expectedOptions)).andReturn(null);
replay(storage);
initializeBlob();
assertFalse(blob.exists());
}
@Test
public void testContent() throws Exception {
initializeExpectedBlob(2);
byte[] content = {1, 2};
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.readAllBytes(BLOB_INFO.getBlobId())).andReturn(content);
replay(storage);
initializeBlob();
assertArrayEquals(content, blob.getContent());
}
@Test
public void testContentWithDecryptionKey() throws Exception {
initializeExpectedBlob(2);
byte[] content = {1, 2};
expect(storage.getOptions()).andReturn(mockOptions);
expect(
storage.readAllBytes(
BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY)))
.andReturn(content)
.times(2);
replay(storage);
initializeBlob();
assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(BASE64_KEY)));
assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(KEY)));
}
@Test
public void testReload() throws Exception {
initializeExpectedBlob(2);
Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.get(BLOB_INFO_NO_GENERATION.getBlobId(), new Storage.BlobGetOption[0]))
.andReturn(expectedReloadedBlob);
replay(storage);
initializeBlob();
Blob updatedBlob = blob.reload();
assertEquals(expectedReloadedBlob, updatedBlob);
}
@Test
public void testReloadNull() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.get(BLOB_INFO_NO_GENERATION.getBlobId(), new Storage.BlobGetOption[0]))
.andReturn(null);
replay(storage);
initializeBlob();
Blob reloadedBlob = blob.reload();
assertNull(reloadedBlob);
}
@Test
public void testReloadWithOptions() throws Exception {
initializeExpectedBlob(2);
Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)};
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.get(BLOB_INFO_NO_GENERATION.getBlobId(), options))
.andReturn(expectedReloadedBlob);
replay(storage);
initializeBlob();
Blob updatedBlob = blob.reload(BlobSourceOption.metagenerationMatch());
assertEquals(expectedReloadedBlob, updatedBlob);
}
@Test
public void testUpdate() throws Exception {
initializeExpectedBlob(2);
Blob expectedUpdatedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
expect(storage.getOptions()).andReturn(mockOptions).times(2);
expect(storage.update(eq(expectedUpdatedBlob), new Storage.BlobTargetOption[0]))
.andReturn(expectedUpdatedBlob);
replay(storage);
initializeBlob();
Blob updatedBlob = new Blob(storage, new BlobInfo.BuilderImpl(expectedUpdatedBlob));
Blob actualUpdatedBlob = updatedBlob.update();
assertEquals(expectedUpdatedBlob, actualUpdatedBlob);
}
@Test
public void testDelete() throws Exception {
initializeExpectedBlob(2);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.delete(BLOB_INFO.getBlobId(), new Storage.BlobSourceOption[0])).andReturn(true);
replay(storage);
initializeBlob();
assertTrue(blob.delete());
}
@Test
public void testCopyToBucket() throws Exception {
initializeExpectedBlob(2);
BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "n")).build();
CopyWriter copyWriter = createMock(CopyWriter.class);
Capture<CopyRequest> capturedCopyRequest = Capture.newInstance();
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter);
replay(storage);
initializeBlob();
CopyWriter returnedCopyWriter = blob.copyTo("bt");
assertEquals(copyWriter, returnedCopyWriter);
assertEquals(BLOB_INFO_NO_GENERATION.getBlobId(), capturedCopyRequest.getValue().getSource());
assertEquals(target, capturedCopyRequest.getValue().getTarget());
assertFalse(capturedCopyRequest.getValue().overrideInfo());
assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty());
assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty());
}
@Test
public void testCopyTo() throws Exception {
initializeExpectedBlob(2);
BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build();
CopyWriter copyWriter = createMock(CopyWriter.class);
Capture<CopyRequest> capturedCopyRequest = Capture.newInstance();
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter);
replay(storage);
initializeBlob();
CopyWriter returnedCopyWriter = blob.copyTo("bt", "nt");
assertEquals(copyWriter, returnedCopyWriter);
assertEquals(BLOB_INFO_NO_GENERATION.getBlobId(), capturedCopyRequest.getValue().getSource());
assertEquals(target, capturedCopyRequest.getValue().getTarget());
assertFalse(capturedCopyRequest.getValue().overrideInfo());
assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty());
assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty());
}
@Test
public void testCopyToBlobId() throws Exception {
initializeExpectedBlob(2);
BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build();
BlobId targetId = BlobId.of("bt", "nt");
CopyWriter copyWriter = createMock(CopyWriter.class);
Capture<CopyRequest> capturedCopyRequest = Capture.newInstance();
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter);
replay(storage);
initializeBlob();
CopyWriter returnedCopyWriter = blob.copyTo(targetId);
assertEquals(copyWriter, returnedCopyWriter);
assertEquals(BLOB_INFO_NO_GENERATION.getBlobId(), capturedCopyRequest.getValue().getSource());
assertEquals(target, capturedCopyRequest.getValue().getTarget());
assertFalse(capturedCopyRequest.getValue().overrideInfo());
assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty());
assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty());
}
@Test
public void testReader() throws Exception {
initializeExpectedBlob(2);
ReadChannel channel = createMock(ReadChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.reader(BLOB_INFO.getBlobId())).andReturn(channel);
replay(storage);
initializeBlob();
assertSame(channel, blob.reader());
}
@Test
public void testReaderWithDecryptionKey() throws Exception {
initializeExpectedBlob(2);
ReadChannel channel = createMock(ReadChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(
storage.reader(
BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY)))
.andReturn(channel)
.times(2);
replay(storage);
initializeBlob();
assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(BASE64_KEY)));
assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(KEY)));
}
@Test
public void testWriter() throws Exception {
initializeExpectedBlob(2);
BlobWriteChannel channel = createMock(BlobWriteChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.writer(eq(expectedBlob))).andReturn(channel);
replay(storage);
initializeBlob();
assertSame(channel, blob.writer());
}
@Test
public void testWriterWithEncryptionKey() throws Exception {
initializeExpectedBlob(2);
BlobWriteChannel channel = createMock(BlobWriteChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.encryptionKey(BASE64_KEY))))
.andReturn(channel)
.times(2);
replay(storage);
initializeBlob();
assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(BASE64_KEY)));
assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(KEY)));
}
@Test
public void testWriterWithKmsKeyName() throws Exception {
initializeExpectedBlob(2);
BlobWriteChannel channel = createMock(BlobWriteChannel.class);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.kmsKeyName(KMS_KEY_NAME))))
.andReturn(channel);
replay(storage);
initializeBlob();
assertSame(channel, blob.writer(BlobWriteOption.kmsKeyName(KMS_KEY_NAME)));
}
@Test
public void testSignUrl() throws Exception {
initializeExpectedBlob(2);
URL url = new URL("http://localhost:123/bla");
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.signUrl(expectedBlob, 100, TimeUnit.SECONDS)).andReturn(url);
replay(storage);
initializeBlob();
assertEquals(url, blob.signUrl(100, TimeUnit.SECONDS));
}
@Test
public void testGetAcl() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.getAcl(BLOB_INFO.getBlobId(), User.ofAllAuthenticatedUsers())).andReturn(ACL);
replay(storage);
initializeBlob();
assertEquals(ACL, blob.getAcl(User.ofAllAuthenticatedUsers()));
}
@Test
public void testDeleteAcl() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.deleteAcl(BLOB_INFO.getBlobId(), User.ofAllAuthenticatedUsers()))
.andReturn(true);
replay(storage);
initializeBlob();
assertTrue(blob.deleteAcl(User.ofAllAuthenticatedUsers()));
}
@Test
public void testCreateAcl() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build();
expect(storage.createAcl(BLOB_INFO.getBlobId(), ACL)).andReturn(returnedAcl);
replay(storage);
initializeBlob();
assertEquals(returnedAcl, blob.createAcl(ACL));
}
@Test
public void testUpdateAcl() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build();
expect(storage.updateAcl(BLOB_INFO.getBlobId(), ACL)).andReturn(returnedAcl);
replay(storage);
initializeBlob();
assertEquals(returnedAcl, blob.updateAcl(ACL));
}
@Test
public void testListAcls() throws Exception {
initializeExpectedBlob(1);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.listAcls(BLOB_INFO.getBlobId())).andReturn(ACLS);
replay(storage);
initializeBlob();
assertEquals(ACLS, blob.listAcls());
}
@Test
public void testToBuilder() {
expect(storage.getOptions()).andReturn(mockOptions).times(6);
replay(storage);
Blob fullBlob = new Blob(storage, new BlobInfo.BuilderImpl(FULL_BLOB_INFO));
assertEquals(fullBlob, fullBlob.toBuilder().build());
Blob simpleBlob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
assertEquals(simpleBlob, simpleBlob.toBuilder().build());
Blob directory = new Blob(storage, new BlobInfo.BuilderImpl(DIRECTORY_INFO));
assertEquals(directory, directory.toBuilder().build());
}
@Test
public void testBuilder() {
initializeExpectedBlob(4);
expect(storage.getOptions()).andReturn(mockOptions).times(6);
replay(storage);
Blob.Builder builder = new Blob.Builder(new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO)));
Blob blob =
builder
.setAcl(ACLS)
.setComponentCount(COMPONENT_COUNT)
.setContentType(CONTENT_TYPE)
.setCacheControl(CACHE_CONTROL)
.setContentDisposition(CONTENT_DISPOSITION)
.setContentEncoding(CONTENT_ENCODING)
.setContentLanguage(CONTENT_LANGUAGE)
.setCrc32c(CRC32)
.setCreateTime(CREATE_TIME)
.setCustomTime(CUSTOM_TIME)
.setStorageClass(STORAGE_CLASS)
.setTimeStorageClassUpdated(TIME_STORAGE_CLASS_UPDATED)
.setCustomerEncryption(CUSTOMER_ENCRYPTION)
.setKmsKeyName(KMS_KEY_NAME)
.setEventBasedHold(EVENT_BASED_HOLD)
.setTemporaryHold(TEMPORARY_HOLD)
.setRetentionExpirationTime(RETENTION_EXPIRATION_TIME)
.setDeleteTime(DELETE_TIME)
.setEtag(ETAG)
.setGeneratedId(GENERATED_ID)
.setMd5(MD5)
.setMediaLink(MEDIA_LINK)
.setMetadata(METADATA)
.setMetageneration(META_GENERATION)
.setOwner(OWNER)
.setSelfLink(SELF_LINK)
.setSize(SIZE)
.setUpdateTime(UPDATE_TIME)
.build();
assertEquals("b", blob.getBucket());
assertEquals("n", blob.getName());
assertEquals(ACLS, blob.getAcl());
assertEquals(COMPONENT_COUNT, blob.getComponentCount());
assertEquals(CONTENT_TYPE, blob.getContentType());
assertEquals(CACHE_CONTROL, blob.getCacheControl());
assertEquals(CONTENT_DISPOSITION, blob.getContentDisposition());
assertEquals(CONTENT_ENCODING, blob.getContentEncoding());
assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage());
assertEquals(CRC32, blob.getCrc32c());
assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString());
assertEquals(CREATE_TIME, blob.getCreateTime());
assertEquals(CUSTOM_TIME, blob.getCustomTime());
assertEquals(STORAGE_CLASS, blob.getStorageClass());
assertEquals(TIME_STORAGE_CLASS_UPDATED, blob.getTimeStorageClassUpdated());
assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption());
assertEquals(KMS_KEY_NAME, blob.getKmsKeyName());
assertEquals(EVENT_BASED_HOLD, blob.getEventBasedHold());
assertEquals(TEMPORARY_HOLD, blob.getTemporaryHold());
assertEquals(RETENTION_EXPIRATION_TIME, blob.getRetentionExpirationTime());
assertEquals(DELETE_TIME, blob.getDeleteTime());
assertEquals(ETAG, blob.getEtag());
assertEquals(GENERATED_ID, blob.getGeneratedId());
assertEquals(MD5, blob.getMd5());
assertEquals(MD5_HEX_STRING, blob.getMd5ToHexString());
assertEquals(MEDIA_LINK, blob.getMediaLink());
assertEquals(METADATA, blob.getMetadata());
assertEquals(META_GENERATION, blob.getMetageneration());
assertEquals(OWNER, blob.getOwner());
assertEquals(SELF_LINK, blob.getSelfLink());
assertEquals(SIZE, blob.getSize());
assertEquals(UPDATE_TIME, blob.getUpdateTime());
assertEquals(storage.getOptions(), blob.getStorage().getOptions());
assertFalse(blob.isDirectory());
builder = new Blob.Builder(new Blob(storage, new BlobInfo.BuilderImpl(DIRECTORY_INFO)));
blob = builder.setBlobId(BlobId.of("b", "n/")).setIsDirectory(true).setSize(0L).build();
assertEquals("b", blob.getBucket());
assertEquals("n/", blob.getName());
assertNull(blob.getAcl());
assertNull(blob.getComponentCount());
assertNull(blob.getContentType());
assertNull(blob.getCacheControl());
assertNull(blob.getContentDisposition());
assertNull(blob.getContentEncoding());
assertNull(blob.getContentLanguage());
assertNull(blob.getCrc32c());
assertNull(blob.getCrc32cToHexString());
assertNull(blob.getCreateTime());
assertNull(blob.getStorageClass());
assertNull(blob.getTimeStorageClassUpdated());
assertNull(blob.getCustomerEncryption());
assertNull(blob.getKmsKeyName());
assertNull(blob.getEventBasedHold());
assertNull(blob.getTemporaryHold());
assertNull(blob.getRetentionExpirationTime());
assertNull(blob.getDeleteTime());
assertNull(blob.getEtag());
assertNull(blob.getGeneratedId());
assertNull(blob.getMd5());
assertNull(blob.getMd5ToHexString());
assertNull(blob.getMediaLink());
assertNull(blob.getMetadata());
assertNull(blob.getMetageneration());
assertNull(blob.getOwner());
assertNull(blob.getSelfLink());
assertEquals(0L, (long) blob.getSize());
assertNull(blob.getUpdateTime());
assertNull(blob.getCustomTime());
assertTrue(blob.isDirectory());
}
private StorageRpc prepareForDownload() {
StorageRpc mockStorageRpc = createNiceMock(StorageRpc.class);
expect(storage.getOptions()).andReturn(mockOptions);
replay(storage);
expect(mockOptions.getStorageRpcV1()).andReturn(mockStorageRpc);
expect(mockOptions.getRetrySettings()).andReturn(RETRY_SETTINGS);
expect(mockOptions.getClock()).andReturn(API_CLOCK);
replay(mockOptions);
blob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
return mockStorageRpc;
}
@Test
public void testDownloadTo() throws Exception {
final byte[] expected = {1, 2};
StorageRpc mockStorageRpc = prepareForDownload();
expect(
mockStorageRpc.read(
anyObject(StorageObject.class),
anyObject(Map.class),
eq(0l),
anyObject(OutputStream.class)))
.andAnswer(
new IAnswer<Long>() {
@Override
public Long answer() throws Throwable {
((OutputStream) getCurrentArguments()[3]).write(expected);
return 2l;
}
});
replay(mockStorageRpc);
File file = File.createTempFile("blob", ".tmp");
blob.downloadTo(file.toPath());
byte actual[] = Files.readAllBytes(file.toPath());
assertArrayEquals(expected, actual);
}
@Test
public void testDownloadToWithRetries() throws Exception {
final byte[] expected = {1, 2};
StorageRpc mockStorageRpc = prepareForDownload();
expect(
mockStorageRpc.read(
anyObject(StorageObject.class),
anyObject(Map.class),
eq(0l),
anyObject(OutputStream.class)))
.andAnswer(
new IAnswer<Long>() {
@Override
public Long answer() throws Throwable {
((OutputStream) getCurrentArguments()[3]).write(expected[0]);
throw new StorageException(504, "error");
}
});
expect(
mockStorageRpc.read(
anyObject(StorageObject.class),
anyObject(Map.class),
eq(1l),
anyObject(OutputStream.class)))
.andAnswer(
new IAnswer<Long>() {
@Override
public Long answer() throws Throwable {
((OutputStream) getCurrentArguments()[3]).write(expected[1]);
return 1l;
}
});
replay(mockStorageRpc);
File file = File.createTempFile("blob", ".tmp");
blob.downloadTo(file.toPath());
byte actual[] = Files.readAllBytes(file.toPath());
assertArrayEquals(expected, actual);
}
@Test
public void testDownloadToWithException() throws Exception {
StorageRpc mockStorageRpc = prepareForDownload();
Exception exception = new IllegalStateException("test");
expect(
mockStorageRpc.read(
anyObject(StorageObject.class),
anyObject(Map.class),
eq(0l),
anyObject(OutputStream.class)))
.andThrow(exception);
replay(mockStorageRpc);
File file = File.createTempFile("blob", ".tmp");
try {
blob.downloadTo(file.toPath());
fail();
} catch (StorageException e) {
assertSame(exception, e.getCause());
}
}
}