Search in sources :

Example 1 with StorageDownloadFileResult

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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageDownloadFileResult(com.amplifyframework.storage.result.StorageDownloadFileResult) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 2 with StorageDownloadFileResult

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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageDownloadFileResult(com.amplifyframework.storage.result.StorageDownloadFileResult) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Test(org.junit.Test)

Example 3 with StorageDownloadFileResult

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);
}
Also used : StorageDownloadFileResult(com.amplifyframework.storage.result.StorageDownloadFileResult) StorageDownloadFileOptions(com.amplifyframework.storage.options.StorageDownloadFileOptions) File(java.io.File) StorageTransferProgress(com.amplifyframework.storage.result.StorageTransferProgress) Test(org.junit.Test)

Aggregations

StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)3 File (java.io.File)3 Test (org.junit.Test)3 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)2 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)2 StorageException (com.amplifyframework.storage.StorageException)2 RandomString (com.amplifyframework.testutils.random.RandomString)2 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)2 Mockito.anyString (org.mockito.Mockito.anyString)2 StorageDownloadFileOptions (com.amplifyframework.storage.options.StorageDownloadFileOptions)1 StorageTransferProgress (com.amplifyframework.storage.result.StorageTransferProgress)1