use of com.amplifyframework.storage.result.StorageUploadFileResult in project amplify-android by aws-amplify.
the class RxStorageBindingTest method uploadFileReturnsResult.
/**
* When {@link StorageCategoryBehavior#uploadFile(String, File, Consumer, Consumer)} returns
* a {@link StorageUploadFileResult}, then the {@link Single} returned by
* {@link RxStorageCategoryBehavior#uploadFile(String, File)} should emit that result.
* @throws InterruptedException Not expected.
*/
@Test
public void uploadFileReturnsResult() throws InterruptedException {
StorageUploadFileResult result = StorageUploadFileResult.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<StorageUploadFileResult> 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(StorageUploadFileOperation.class);
}).when(delegate).uploadFile(eq(remoteKey), eq(localFile), any(StorageUploadFileOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
RxStorageBinding.RxProgressAwareSingleOperation<StorageUploadFileResult> rxOperation = rxStorage.uploadFile(remoteKey, localFile);
TestObserver<StorageUploadFileResult> 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);
}
use of com.amplifyframework.storage.result.StorageUploadFileResult in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadFileGetsKey.
/**
* Test that calling upload file 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 testUploadFileGetsKey() throws Exception {
final String toRemoteKey = RandomString.string();
final File fromLocalFile = new RandomTempFile(FILE_SIZE);
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadFile(anyString(), any(File.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));
StorageUploadFileResult result = Await.<StorageUploadFileResult, StorageException>result((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(toRemoteKey, result.getKey());
}
use of com.amplifyframework.storage.result.StorageUploadFileResult in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadFileError.
/**
* Test that calling upload file method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the upload file cannot be created
*/
@Test
public void testUploadFileError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
final String toRemoteKey = RandomString.string();
final File fromLocalFile = new RandomTempFile(FILE_SIZE);
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadFile(anyString(), any(File.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.<StorageUploadFileResult, StorageException>error((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(testError, error.getCause());
}
Aggregations