Skip to content

Commit a1b0087

Browse files
author
Frank Natividad
committed
Adding unit tests
1 parent 4da3796 commit a1b0087

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ public void testWriterWithEncryptionKey() throws Exception {
367367
assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(KEY)));
368368
}
369369

370+
@Test
371+
public void testWriterWithKmsKeyName() throws Exception {
372+
initializeExpectedBlob(2);
373+
BlobWriteChannel channel = createMock(BlobWriteChannel.class);
374+
expect(storage.getOptions()).andReturn(mockOptions);
375+
expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.kmsKeyName(KMS_KEY_NAME))))
376+
.andReturn(channel);
377+
replay(storage);
378+
initializeBlob();
379+
assertSame(channel, blob.writer(BlobWriteOption.kmsKeyName(KMS_KEY_NAME)));
380+
}
381+
370382
@Test
371383
public void testSignUrl() throws Exception {
372384
initializeExpectedBlob(2);

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ public void testCreateWithEncryptionKey() throws Exception {
366366
assertEquals(expectedBlob, blob);
367367
}
368368

369+
@Test
370+
public void testCreateWithKmsKeyName() throws Exception {
371+
initializeExpectedBucket(5);
372+
BlobInfo info = BlobInfo.newBuilder(BlobId.of("b", "n")).setContentType(CONTENT_TYPE).build();
373+
Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info));
374+
byte[] content = {0xD, 0xE, 0xA, 0xD};
375+
expect(storage.getOptions()).andReturn(mockOptions);
376+
expect(storage.create(info, content, Storage.BlobTargetOption.kmsKeyName(DEFAULT_KMS_KEY_NAME)))
377+
.andReturn(expectedBlob);
378+
replay(storage);
379+
initializeBucket();
380+
Blob blob =
381+
bucket.create("n", content, CONTENT_TYPE, Bucket.BlobTargetOption.kmsKeyName(DEFAULT_KMS_KEY_NAME));
382+
assertEquals(expectedBlob, blob);
383+
}
384+
369385
@Test
370386
public void testCreateNotExists() throws Exception {
371387
initializeExpectedBucket(5);

google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,40 @@ public void testCreateBlobWithEncryptionKey() throws IOException {
566566
assertEquals(-1, byteStream.read(streamBytes));
567567
}
568568

569+
@Test
570+
public void testCreateBlobWithKmsKeyName() throws IOException {
571+
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();
572+
EasyMock.expect(
573+
storageRpcMock.create(
574+
EasyMock.eq(
575+
BLOB_INFO1
576+
.toBuilder()
577+
.setMd5(CONTENT_MD5)
578+
.setCrc32c(CONTENT_CRC32C)
579+
.build()
580+
.toPb()),
581+
EasyMock.capture(capturedStream),
582+
EasyMock.eq(KMS_KEY_NAME_OPTIONS)))
583+
.andReturn(BLOB_INFO1.toPb())
584+
.times(2);
585+
EasyMock.replay(storageRpcMock);
586+
initializeService();
587+
Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT, BlobTargetOption.kmsKeyName(KMS_KEY_NAME));
588+
assertEquals(expectedBlob1, blob);
589+
ByteArrayInputStream byteStream = capturedStream.getValue();
590+
byte[] streamBytes = new byte[BLOB_CONTENT.length];
591+
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
592+
assertArrayEquals(BLOB_CONTENT, streamBytes);
593+
assertEquals(-1, byteStream.read(streamBytes));
594+
blob = storage.create(BLOB_INFO1, BLOB_CONTENT, BlobTargetOption.kmsKeyName(KMS_KEY_NAME));
595+
assertEquals(expectedBlob1, blob);
596+
byteStream = capturedStream.getValue();
597+
streamBytes = new byte[BLOB_CONTENT.length];
598+
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
599+
assertArrayEquals(BLOB_CONTENT, streamBytes);
600+
assertEquals(-1, byteStream.read(streamBytes));
601+
}
602+
569603
@Test
570604
public void testCreateBlobFromStream() throws IOException {
571605
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();
@@ -1243,6 +1277,43 @@ public void testCopyWithEncryptionKey() {
12431277
assertTrue(!writer.isDone());
12441278
}
12451279

1280+
@Test
1281+
public void testCopyFromEncryptionKeyToKmsKeyName() {
1282+
CopyRequest request =
1283+
Storage.CopyRequest.newBuilder()
1284+
.setSource(BLOB_INFO2.getBlobId())
1285+
.setSourceOptions(BlobSourceOption.decryptionKey(KEY))
1286+
.setTarget(BLOB_INFO1, BlobTargetOption.kmsKeyName(KMS_KEY_NAME))
1287+
.build();
1288+
StorageRpc.RewriteRequest rpcRequest =
1289+
new StorageRpc.RewriteRequest(
1290+
request.getSource().toPb(),
1291+
ENCRYPTION_KEY_OPTIONS,
1292+
true,
1293+
request.getTarget().toPb(),
1294+
KMS_KEY_NAME_OPTIONS,
1295+
null);
1296+
StorageRpc.RewriteResponse rpcResponse =
1297+
new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L);
1298+
EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse).times(2);
1299+
EasyMock.replay(storageRpcMock);
1300+
initializeService();
1301+
CopyWriter writer = storage.copy(request);
1302+
assertEquals(42L, writer.getBlobSize());
1303+
assertEquals(21L, writer.getTotalBytesCopied());
1304+
assertTrue(!writer.isDone());
1305+
request =
1306+
Storage.CopyRequest.newBuilder()
1307+
.setSource(BLOB_INFO2.getBlobId())
1308+
.setSourceOptions(BlobSourceOption.decryptionKey(BASE64_KEY))
1309+
.setTarget(BLOB_INFO1, BlobTargetOption.kmsKeyName(KMS_KEY_NAME))
1310+
.build();
1311+
writer = storage.copy(request);
1312+
assertEquals(42L, writer.getBlobSize());
1313+
assertEquals(21L, writer.getTotalBytesCopied());
1314+
assertTrue(!writer.isDone());
1315+
}
1316+
12461317
@Test
12471318
public void testCopyWithOptionsFromBlobId() {
12481319
CopyRequest request =
@@ -1494,6 +1565,22 @@ public void testWriterWithEncryptionKey() {
14941565
assertTrue(channel.isOpen());
14951566
}
14961567

1568+
@Test
1569+
public void testWriterWithKmsKeyName() {
1570+
BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build();
1571+
EasyMock.expect(storageRpcMock.open(info.toPb(), KMS_KEY_NAME_OPTIONS))
1572+
.andReturn("upload-id")
1573+
.times(2);
1574+
EasyMock.replay(storageRpcMock);
1575+
initializeService();
1576+
WriteChannel channel = storage.writer(info, BlobWriteOption.kmsKeyName(KMS_KEY_NAME));
1577+
assertNotNull(channel);
1578+
assertTrue(channel.isOpen());
1579+
channel = storage.writer(info, BlobWriteOption.kmsKeyName(KMS_KEY_NAME));
1580+
assertNotNull(channel);
1581+
assertTrue(channel.isOpen());
1582+
}
1583+
14971584
@Test
14981585
public void testSignUrl()
14991586
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException,

0 commit comments

Comments
 (0)