Search in sources :

Example 1 with StorageUploadFileResult

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);
}
Also used : StorageUploadFileResult(com.amplifyframework.storage.result.StorageUploadFileResult) StorageUploadFileOptions(com.amplifyframework.storage.options.StorageUploadFileOptions) StorageTransferProgress(com.amplifyframework.storage.result.StorageTransferProgress) Test(org.junit.Test)

Example 2 with StorageUploadFileResult

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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadFileResult(com.amplifyframework.storage.result.StorageUploadFileResult) 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) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 3 with StorageUploadFileResult

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());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadFileResult(com.amplifyframework.storage.result.StorageUploadFileResult) 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) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.junit.Test)

Aggregations

StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)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 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)2 File (java.io.File)2 Mockito.anyString (org.mockito.Mockito.anyString)2 StorageUploadFileOptions (com.amplifyframework.storage.options.StorageUploadFileOptions)1 StorageTransferProgress (com.amplifyframework.storage.result.StorageTransferProgress)1