Search in sources :

Example 6 with TransferObserver

use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver in project amplify-android by aws-amplify.

the class StorageComponentTest method testUploadInputStreamGetsKey.

/**
 * Test that calling upload inputStream 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 testUploadInputStreamGetsKey() throws Exception {
    final String toRemoteKey = RandomString.string();
    final InputStream inputStream = new ByteArrayInputStream(RandomBytes.bytes());
    TransferObserver observer = mock(TransferObserver.class);
    when(storageService.uploadInputStream(anyString(), any(InputStream.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));
    StorageUploadInputStreamResult result = Await.<StorageUploadInputStreamResult, StorageException>result((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
    assertEquals(toRemoteKey, result.getKey());
}
Also used : TransferListener(com.amazonaws.mobileconnectors.s3.transferutility.TransferListener) StorageUploadInputStreamResult(com.amplifyframework.storage.result.StorageUploadInputStreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TransferObserver(com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 7 with TransferObserver

use of com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver 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)

Aggregations

TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)6 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)6 StorageException (com.amplifyframework.storage.StorageException)6 RandomString (com.amplifyframework.testutils.random.RandomString)6 Test (org.junit.Test)6 Mockito.anyString (org.mockito.Mockito.anyString)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)4 File (java.io.File)4 StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)2 StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)2 StorageUploadInputStreamResult (com.amplifyframework.storage.result.StorageUploadInputStreamResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NonNull (androidx.annotation.NonNull)1 UploadOptions (com.amazonaws.mobileconnectors.s3.transferutility.UploadOptions)1