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());
}
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());
}
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());
}
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());
}
Aggregations