Search in sources :

Example 1 with StorageUploadInputStreamResult

use of com.amplifyframework.storage.result.StorageUploadInputStreamResult in project amplify-android by aws-amplify.

the class StorageComponentTest method testInputStreamError.

/**
 * Test that calling upload inputStream method from Storage category fails
 * successfully when {@link TransferListener} emits an error.
 *
 * @throws IOException when the upload file cannot be created
 */
@Test
public void testInputStreamError() throws IOException {
    final StorageException testError = new StorageException("Test error message", "Test recovery message");
    final String toRemoteKey = RandomString.string();
    final InputStream inputStream = new ByteArrayInputStream(RandomBytes.bytes());
    TransferObserver observer = mock(TransferObserver.class);
    when(storageService.uploadInputStream(anyString(), any(InputStream.class), any(ObjectMetadata.class))).thenReturn(observer);
    doAnswer(invocation -> {
        TransferListener listener = invocation.getArgument(0);
        listener.onError(0, testError);
        return null;
    }).when(observer).setTransferListener(any(TransferListener.class));
    StorageException error = Await.<StorageUploadInputStreamResult, StorageException>error((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
    assertEquals(testError, error.getCause());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadInputStreamResult(com.amplifyframework.storage.result.StorageUploadInputStreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.junit.Test)

Example 2 with StorageUploadInputStreamResult

use of com.amplifyframework.storage.result.StorageUploadInputStreamResult in project amplify-android by aws-amplify.

the class StorageComponentTest method testUploadInputStreamGetsKey.

/**
 * Test that calling upload inputStream method from Storage category correctly
 * invokes the registered AWSS3StoragePlugin instance and returns a
 * {@link StorageUploadFileResult} with correct remote key.
 *
 * @throws Exception when an error is encountered while uploading
 */
@Test
public void testUploadInputStreamGetsKey() throws Exception {
    final String toRemoteKey = RandomString.string();
    final InputStream inputStream = new ByteArrayInputStream(RandomBytes.bytes());
    TransferObserver observer = mock(TransferObserver.class);
    when(storageService.uploadInputStream(anyString(), any(InputStream.class), any(ObjectMetadata.class))).thenReturn(observer);
    doAnswer(invocation -> {
        TransferListener listener = invocation.getArgument(0);
        listener.onStateChanged(0, TransferState.COMPLETED);
        return null;
    }).when(observer).setTransferListener(any(TransferListener.class));
    StorageUploadInputStreamResult result = Await.<StorageUploadInputStreamResult, StorageException>result((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
    assertEquals(toRemoteKey, result.getKey());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadInputStreamResult(com.amplifyframework.storage.result.StorageUploadInputStreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 3 with StorageUploadInputStreamResult

use of com.amplifyframework.storage.result.StorageUploadInputStreamResult in project amplify-android by aws-amplify.

the class RxStorageBindingTest method uploadInputStreamReturnsResult.

/**
 * When {@link StorageCategoryBehavior#uploadInputStream(String, InputStream, Consumer, Consumer)} returns
 * a {@link StorageUploadInputStreamResult}, then the {@link Single} returned by
 * {@link RxStorageCategoryBehavior#uploadInputStream(String, InputStream)} should emit that result.
 * @throws InterruptedException Not expected.
 */
@Test
public void uploadInputStreamReturnsResult() throws InterruptedException {
    StorageUploadInputStreamResult result = StorageUploadInputStreamResult.fromKey(remoteKey);
    doAnswer(invocation -> {
        // 0 key, 1 local, 2 options, 3 onProgress, 4 onResult, 5 onError
        final int indexOfResultConsumer = 4;
        final int indexOfProgressConsumer = 3;
        Consumer<StorageUploadInputStreamResult> resultConsumer = invocation.getArgument(indexOfResultConsumer);
        Consumer<StorageTransferProgress> progressConsumer = invocation.getArgument(indexOfProgressConsumer);
        Observable.interval(100, 100, TimeUnit.MILLISECONDS).take(5).doOnNext(aLong -> progressConsumer.accept(new StorageTransferProgress(aLong, 500))).doOnComplete(() -> resultConsumer.accept(result)).subscribe();
        return mock(StorageUploadInputStreamOperation.class);
    }).when(delegate).uploadInputStream(eq(remoteKey), eq(localInputStream), any(StorageUploadInputStreamOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
    RxStorageBinding.RxProgressAwareSingleOperation<StorageUploadInputStreamResult> rxOperation = rxStorage.uploadInputStream(remoteKey, localInputStream);
    TestObserver<StorageUploadInputStreamResult> testObserver = rxOperation.observeResult().test();
    TestObserver<StorageTransferProgress> testProgressObserver = rxOperation.observeProgress().test();
    testObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    testObserver.assertValues(result);
    testProgressObserver.awaitCount(5);
    testProgressObserver.assertValueCount(5);
}
Also used : StorageUploadInputStreamResult(com.amplifyframework.storage.result.StorageUploadInputStreamResult) StorageUploadInputStreamOptions(com.amplifyframework.storage.options.StorageUploadInputStreamOptions) StorageTransferProgress(com.amplifyframework.storage.result.StorageTransferProgress) Test(org.junit.Test)

Aggregations

StorageUploadInputStreamResult (com.amplifyframework.storage.result.StorageUploadInputStreamResult)3 Test (org.junit.Test)3 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)2 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 StorageException (com.amplifyframework.storage.StorageException)2 RandomString (com.amplifyframework.testutils.random.RandomString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Mockito.anyString (org.mockito.Mockito.anyString)2 StorageUploadInputStreamOptions (com.amplifyframework.storage.options.StorageUploadInputStreamOptions)1 StorageTransferProgress (com.amplifyframework.storage.result.StorageTransferProgress)1