use of com.amplifyframework.storage.result.StorageDownloadFileResult in project amplify-android by aws-amplify.
the class StorageComponentTest method testDownloadToFileGetsFile.
/**
* Test that calling download file method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageDownloadFileResult} with correct file path.
*
* @throws Exception when an error is encountered while downloading
*/
@Test
public void testDownloadToFileGetsFile() throws Exception {
final String fromRemoteKey = RandomString.string();
final File toLocalFile = new RandomTempFile();
// Since we use a mock StorageService, it will return a null
// result by default. We need a non-null transfer observer.
// One option is to mock that, too.
TransferObserver observer = mock(TransferObserver.class);
when(storageService.downloadToFile(anyString(), any(File.class))).thenReturn(observer);
// Since we use a mock TransferObserver, it has no internal logic
// to know to call back the listener! So, we simulate the success
// callback, as part of our "happy path" test.
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageDownloadFileResult result = Await.<StorageDownloadFileResult, StorageException>result((onResult, onError) -> storage.downloadFile(fromRemoteKey, toLocalFile, onResult, onError));
assertEquals(toLocalFile.getAbsolutePath(), result.getFile().toString());
}
use of com.amplifyframework.storage.result.StorageDownloadFileResult in project amplify-android by aws-amplify.
the class StorageComponentTest method testDownloadError.
/**
* Test that calling download file method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the temporary file cannot be created
*/
@Test
public void testDownloadError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
final String fromRemoteKey = RandomString.string();
final File toLocalFile = new RandomTempFile();
TransferObserver observer = mock(TransferObserver.class);
when(storageService.downloadToFile(anyString(), any(File.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.<StorageDownloadFileResult, StorageException>error((onResult, onError) -> storage.downloadFile(fromRemoteKey, toLocalFile, onResult, onError));
assertEquals(testError, error.getCause());
}
use of com.amplifyframework.storage.result.StorageDownloadFileResult in project amplify-android by aws-amplify.
the class RxStorageBindingTest method downloadFileReturnsResult.
/**
* When {@link StorageCategoryBehavior#downloadFile(String, File, StorageDownloadFileOptions,
* Consumer, Consumer, Consumer)} invokes its success callback, the {@link StorageDownloadFileResult}
* should propagate via the {@link Single} returned by
* {@link RxStorageBinding.RxProgressAwareSingleOperation#observeResult()}.
* @throws InterruptedException not expected.
*/
@Test
public void downloadFileReturnsResult() throws InterruptedException {
StorageDownloadFileResult result = StorageDownloadFileResult.fromFile(mock(File.class));
doAnswer(invocation -> {
// 0 key, 1 local, 2 options, 3 onProgress 4 onResult, 5 onError
final int indexOfProgressConsumer = 3;
final int indexOfResultConsumer = 4;
Consumer<StorageTransferProgress> progressConsumer = invocation.getArgument(indexOfProgressConsumer);
Consumer<StorageDownloadFileResult> resultConsumer = invocation.getArgument(indexOfResultConsumer);
Observable.interval(100, 100, TimeUnit.MILLISECONDS).take(5).doOnNext(aLong -> progressConsumer.accept(new StorageTransferProgress(aLong, 500))).doOnComplete(() -> resultConsumer.accept(result)).subscribe();
return mock(StorageDownloadFileOperation.class);
}).when(delegate).downloadFile(eq(remoteKey), eq(localFile), any(StorageDownloadFileOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
RxStorageBinding.RxProgressAwareSingleOperation<StorageDownloadFileResult> rxOperation = rxStorage.downloadFile(remoteKey, localFile, StorageDownloadFileOptions.defaultInstance());
TestObserver<StorageDownloadFileResult> 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