Search in sources :

Example 6 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryImplTest method testWriter.

@Test
public void testWriter() {
    WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.of(TABLE_ID);
    EasyMock.expect(bigqueryRpcMock.open(WriteChannelConfiguration.of(TABLE_ID_WITH_PROJECT).toPb())).andReturn("upload-id");
    EasyMock.replay(bigqueryRpcMock);
    bigquery = options.getService();
    WriteChannel channel = bigquery.writer(writeChannelConfiguration);
    assertNotNull(channel);
    assertTrue(channel.isOpen());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 7 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testWriterWithOptions.

@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());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 8 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testReadAndWriteChannels.

@Test
public void testReadAndWriteChannels() throws IOException {
    String blobName = "test-read-and-write-channels-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    byte[] stringBytes;
    try (WriteChannel writer = storage.writer(blob)) {
        stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
        writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
        writer.write(ByteBuffer.wrap(stringBytes));
    }
    ByteBuffer readBytes;
    ByteBuffer readStringBytes;
    try (ReadChannel reader = storage.reader(blob.getBlobId())) {
        readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
        readStringBytes = ByteBuffer.allocate(stringBytes.length);
        reader.read(readBytes);
        reader.read(readStringBytes);
    }
    assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
    assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
    assertTrue(storage.delete(BUCKET, blobName));
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 9 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testWriter.

@Test
public void testWriter() {
    BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder();
    BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build();
    BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build();
    EasyMock.expect(storageRpcMock.open(infoWithoutHashes.toPb(), EMPTY_RPC_OPTIONS)).andReturn("upload-id");
    EasyMock.replay(storageRpcMock);
    initializeService();
    WriteChannel channel = storage.writer(infoWithHashes);
    assertNotNull(channel);
    assertTrue(channel.isOpen());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 10 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testWriteChannelFail.

@Test
public void testWriteChannelFail() throws IOException {
    String blobName = "test-write-channel-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build();
    try {
        try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.generationMatch())) {
            writer.write(ByteBuffer.allocate(42));
        }
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) Test(org.junit.Test)

Aggregations

WriteChannel (com.google.cloud.WriteChannel)18 Test (org.junit.Test)17 BlobInfo (com.google.cloud.storage.BlobInfo)8 ByteBuffer (java.nio.ByteBuffer)7 ReadChannel (com.google.cloud.ReadChannel)5 StorageException (com.google.cloud.storage.StorageException)3 Random (java.util.Random)2 EasyMock.captureLong (org.easymock.EasyMock.captureLong)2 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1