@Test public void testCreateEmptyBlob() { String blobName = "test-create-empty-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(new byte[0], readBytes); }
private void compareBlobs(BlobInfo expected, BlobInfo value) { assertEquals(expected, value); assertEquals(expected.getBucket(), value.getBucket()); assertEquals(expected.getName(), value.getName()); assertEquals(expected.getAcl(), value.getAcl()); assertEquals(expected.getComponentCount(), value.getComponentCount()); assertEquals(expected.getContentType(), value.getContentType()); assertEquals(expected.getCacheControl(), value.getCacheControl()); assertEquals(expected.getContentDisposition(), value.getContentDisposition()); assertEquals(expected.getContentEncoding(), value.getContentEncoding()); assertEquals(expected.getContentLanguage(), value.getContentLanguage()); assertEquals(expected.getCustomerEncryption(), value.getCustomerEncryption()); assertEquals(expected.getCrc32c(), value.getCrc32c()); assertEquals(expected.getCreateTime(), value.getCreateTime()); assertEquals(expected.getDeleteTime(), value.getDeleteTime()); assertEquals(expected.getEtag(), value.getEtag()); assertEquals(expected.getGeneration(), value.getGeneration()); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getMd5(), value.getMd5()); assertEquals(expected.getMediaLink(), value.getMediaLink()); assertEquals(expected.getMetadata(), value.getMetadata()); assertEquals(expected.getMetageneration(), value.getMetageneration()); assertEquals(expected.getOwner(), value.getOwner()); assertEquals(expected.getSelfLink(), value.getSelfLink()); assertEquals(expected.getSize(), value.getSize()); assertEquals(expected.getUpdateTime(), value.getUpdateTime()); assertEquals(expected.getStorageClass(), value.getStorageClass()); assertEquals(expected.getKmsKeyName(), value.getKmsKeyName()); assertEquals(expected.getEventBasedHold(), value.getEventBasedHold()); assertEquals(expected.getTemporaryHold(), value.getTemporaryHold());
@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); }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("bucket", getBucket()) .add("name", getName()) .add("generation", getGeneration()) .add("size", getSize()) .add("content-type", getContentType()) .add("metadata", getMetadata()) .toString(); }
checkArgument(blobInfo.getMd5() != null, "Blob is missing a value for md5"); signatureInfoBuilder.setContentMd5(blobInfo.getMd5()); checkArgument(blobInfo.getContentType() != null, "Blob is missing a value for content-type"); signatureInfoBuilder.setContentType(blobInfo.getContentType());
@Test public void testGetBlobsFail() { String sourceBlobName1 = "test-get-blobs-fail-1"; String sourceBlobName2 = "test-get-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Blob> remoteBlobs = storage.get(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertEquals(sourceBlob1.getBucket(), remoteBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), remoteBlobs.get(0).getName()); assertNull(remoteBlobs.get(1)); }
@Test public void testCreateFromStreamNoContentType() 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}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent); assertEquals(expectedBlob, blob); }
@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(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 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 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 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 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); }
@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 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 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 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 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)); }