@Test public void testCreateNoContentType() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, content)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", content); assertEquals(expectedBlob, blob); }
@Test public void testGetBlobWithOptionsFromBlobId() { EasyMock.expect(storageRpcMock.get(BLOB_INFO1.getBlobId().toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get( BLOB_INFO1.getBlobId(), BLOB_GET_METAGENERATION, BLOB_GET_GENERATION_FROM_BLOB_ID); assertEquals(expectedBlob1, blob); }
@Test public void testForceDeleteNoTimeout() { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)).anyTimes(); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true); EasyMock.replay(storageMock, blob1, blob2); RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME); EasyMock.verify(storageMock); }
@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 testReloadNull() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), new Storage.BlobGetOption[0])).andReturn(null); replay(storage); initializeBlob(); Blob reloadedBlob = blob.reload(); assertNull(reloadedBlob); }
@Test public void testWriteChannelExistingBlob() throws IOException { String blobName = "test-write-channel-existing-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); storage.create(blob); byte[] stringBytes; try (WriteChannel writer = storage.writer(blob)) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(stringBytes)); } assertArrayEquals(stringBytes, storage.readAllBytes(blob.getBlobId())); assertTrue(storage.delete(BUCKET, blobName)); }
@Test public void testListBucketsEmpty() { EasyMock.expect(storageRpcMock.list(EMPTY_RPC_OPTIONS)) .andReturn( Tuple.<String, Iterable<com.google.api.services.storage.model.Bucket>>of(null, null)); EasyMock.replay(storageRpcMock); initializeService(); Page<Bucket> page = storage.list(); assertNull(page.getNextPageToken()); assertArrayEquals( ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Bucket.class)); }
@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 testReadAllBytesFromBlobIdWithOptions() { EasyMock.expect(storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes( BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION); assertArrayEquals(BLOB_CONTENT, readBytes); }
@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(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testDeleteBlobWithOptionsFromBlobId() { EasyMock.expect(storageRpcMock.delete(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue( storage.delete( BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION)); }
@Test public void testWriterWithOptions() { BlobInfo info = BLOB_INFO1.toBuilder().setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); EasyMock.expect(storageRpcMock.open(info.toPb(), BLOB_TARGET_OPTIONS_CREATE)) .andReturn("upload-id"); EasyMock.replay(storageRpcMock); initializeService(); WriteChannel channel = storage.writer( info, BLOB_WRITE_METAGENERATION, BLOB_WRITE_NOT_EXIST, BLOB_WRITE_PREDEFINED_ACL, BLOB_WRITE_CRC2C, BLOB_WRITE_MD5_HASH); assertNotNull(channel); assertTrue(channel.isOpen()); }
@Test public void testCopy() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.getBlobId(), BLOB_INFO2.getBlobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); }
@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 testReaderWithOptions() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( storageRpcMock.read(BLOB_INFO2.toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader( BUCKET_NAME1, BLOB_NAME2, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); }
@Test public void testExists_True() throws Exception { initializeExpectedBucket(4); Storage.BucketGetOption[] expectedOptions = {Storage.BucketGetOption.fields()}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BUCKET_INFO.getName(), expectedOptions)).andReturn(expectedBucket); replay(storage); initializeBucket(); assertTrue(bucket.exists()); }
@Test public void testCreateBucketWithOptions() { EasyMock.expect(storageRpcMock.create(BUCKET_INFO1.toPb(), BUCKET_TARGET_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.create(BUCKET_INFO1, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_PREDEFINED_ACL); assertEquals(expectedBucket1, bucket); }
@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 testGet() throws Exception { initializeExpectedBucket(5); Blob expectedBlob = new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n").build())); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BlobId.of(expectedBucket.getName(), "n"), new Storage.BlobGetOption[0])) .andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.get("n"); assertEquals(expectedBlob, blob); }