Search in sources :

Example 16 with StorageException

use of com.amplifyframework.storage.StorageException 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 17 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class RxStorageBindingTest method uploadInputStreamReturnsError.

/**
 * When {@link StorageCategoryBehavior#uploadInputStream(String, InputStream, Consumer, Consumer)} returns
 * an {@link StorageException}, then the {@link Single} returned by
 * {@link RxStorageCategoryBehavior#uploadInputStream(String, InputStream)} should emit a {@link StorageException}.
 */
@Test
public void uploadInputStreamReturnsError() {
    StorageException error = new StorageException("Error uploading.", "Expected.");
    doAnswer(invocation -> {
        // 0 key, 1 local, 2 options, 3 onProgress 4 onResult, 5 onError
        final int indexOfResultConsumer = 5;
        Consumer<StorageException> errorConsumer = invocation.getArgument(indexOfResultConsumer);
        errorConsumer.accept(error);
        return mock(StorageUploadInputStreamOperation.class);
    }).when(delegate).uploadInputStream(eq(remoteKey), eq(localInputStream), any(StorageUploadInputStreamOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
    rxStorage.uploadInputStream(remoteKey, localInputStream).observeResult().test().assertError(error);
}
Also used : StorageUploadInputStreamOptions(com.amplifyframework.storage.options.StorageUploadInputStreamOptions) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 18 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class RxStorageBindingTest method listReturnsError.

/**
 * When the {@link StorageCategoryBehavior#list(String, Consumer, Consumer)} emits an error,
 * the {@link Single} returned by {@link RxStorageCategoryBehavior#list(String)} should emit an
 * {@link StorageException}.
 */
@Test
public void listReturnsError() {
    StorageException error = new StorageException("Error removing item.", "Expected.");
    doAnswer(invocation -> {
        // 0 localPath, 1 onResult, 2 onError
        final int indexOfErrorConsumer = 2;
        Consumer<StorageException> errorConsumer = invocation.getArgument(indexOfErrorConsumer);
        errorConsumer.accept(error);
        return mock(StorageListOperation.class);
    }).when(delegate).list(eq(remoteKey), anyConsumer(), anyConsumer());
    rxStorage.list(remoteKey).test().assertError(error);
}
Also used : StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 19 with StorageException

use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.

the class RxStorageBindingTest method removeReturnsError.

/**
 * When {@link StorageCategoryBehavior#remove(String, Consumer, Consumer)} calls its
 * error consumer, then the {@link Single} returned by {@link RxStorageCategoryBehavior#remove(String)}
 * should emit an error.
 */
@Test
public void removeReturnsError() {
    StorageException error = new StorageException("Error removing item.", "Expected.");
    doAnswer(invocation -> {
        // 0 remoteKey, 1 onResult, 2 onError
        final int indexOfErrorConsumer = 2;
        Consumer<StorageException> errorConsumer = invocation.getArgument(indexOfErrorConsumer);
        errorConsumer.accept(error);
        return mock(StorageRemoveOperation.class);
    }).when(delegate).remove(eq(remoteKey), anyConsumer(), anyConsumer());
    rxStorage.remove(remoteKey).test().assertError(error);
}
Also used : StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Aggregations

StorageException (com.amplifyframework.storage.StorageException)19 Test (org.junit.Test)15 RandomString (com.amplifyframework.testutils.random.RandomString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)6 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)6 File (java.io.File)5 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)4 SuppressLint (android.annotation.SuppressLint)3 InputStream (java.io.InputStream)3 StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)2 StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)2 StorageUploadInputStreamResult (com.amplifyframework.storage.result.StorageUploadInputStreamResult)2 ServerSideEncryption (com.amplifyframework.storage.s3.ServerSideEncryption)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 Region (com.amazonaws.regions.Region)1 StorageItem (com.amplifyframework.storage.StorageItem)1 StorageDownloadFileOptions (com.amplifyframework.storage.options.StorageDownloadFileOptions)1