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());
}
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());
}
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);
}
Aggregations