Search in sources :

Example 11 with RandomTempFile

use of com.amplifyframework.testutils.random.RandomTempFile in project amplify-android by aws-amplify.

the class AWSS3StorageUploadTest method testUploadFileIsCancelable.

/**
 * Tests that file upload operation can be canceled while the
 * transfer hasn't completed yet.
 *
 * @throws Exception if upload is not canceled successfully
 *         before timeout
 */
@SuppressWarnings("unchecked")
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadFileIsCancelable() throws Exception {
    final CountDownLatch canceled = new CountDownLatch(1);
    final AtomicReference<Cancelable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Create a file large enough that transfer won't finish before being canceled
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    // Listen to Hub events for cancel
    SubscriptionToken cancelToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
        if (StorageChannelEventName.UPLOAD_STATE.toString().equals(hubEvent.getName())) {
            HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
            TransferState state = TransferState.getState(stateEvent.getData());
            if (TransferState.CANCELED.equals(state)) {
                canceled.countDown();
            }
        }
    });
    subscriptions.add(cancelToken);
    // Begin uploading a large file
    StorageUploadFileOperation<?> op = storageCategory.uploadFile(uploadFile.getName(), uploadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0) {
            opContainer.get().cancel();
        }
    }, result -> errorContainer.set(new RuntimeException("Upload completed without canceling.")), errorContainer::set);
    opContainer.set(op);
    // Assert that the required conditions have been met
    assertTrue(canceled.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNull(errorContainer.get());
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) HubEvent(com.amplifyframework.hub.HubEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransferState(com.amazonaws.mobileconnectors.s3.transferutility.TransferState) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) Cancelable(com.amplifyframework.core.async.Cancelable) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 12 with RandomTempFile

use of com.amplifyframework.testutils.random.RandomTempFile 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 13 with RandomTempFile

use of com.amplifyframework.testutils.random.RandomTempFile 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)

Example 14 with RandomTempFile

use of com.amplifyframework.testutils.random.RandomTempFile 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

RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)14 File (java.io.File)9 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)4 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)4 StorageException (com.amplifyframework.storage.StorageException)4 RandomString (com.amplifyframework.testutils.random.RandomString)4 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4 Mockito.anyString (org.mockito.Mockito.anyString)4 StorageUploadFileOptions (com.amplifyframework.storage.options.StorageUploadFileOptions)3 Before (org.junit.Before)3 TransferState (com.amazonaws.mobileconnectors.s3.transferutility.TransferState)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 HubEvent (com.amplifyframework.hub.HubEvent)2 SubscriptionToken (com.amplifyframework.hub.SubscriptionToken)2 StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)2 StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Context (android.content.Context)1