Search in sources :

Example 6 with RandomTempFile

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

the class AWSS3StorageDownloadAccessLevelTest method uploadTestFile.

private static void uploadTestFile() throws Exception {
    // Create a temporary file to upload
    uploadFile = new RandomTempFile(UPLOAD_NAME, UPLOAD_SIZE);
    final String key = UPLOAD_NAME;
    StorageUploadFileOptions options;
    // Upload as GUEST
    mobileClient.signOut();
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PUBLIC).build();
    storage.uploadFile(key, uploadFile, options);
    // Upload as user one
    mobileClient.signOut();
    mobileClient.signIn(userOne.getUsername(), userOne.getPassword());
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PROTECTED).build();
    storage.uploadFile(key, uploadFile, options);
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PRIVATE).build();
    storage.uploadFile(key, uploadFile, options);
    // Upload as user two
    mobileClient.signOut();
    mobileClient.signIn(userTwo.getUsername(), userTwo.getPassword());
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PROTECTED).build();
    storage.uploadFile(key, uploadFile, options);
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PRIVATE).build();
    storage.uploadFile(key, uploadFile, options);
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) StorageUploadFileOptions(com.amplifyframework.storage.options.StorageUploadFileOptions)

Example 7 with RandomTempFile

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

the class AWSS3StorageDownloadTest method setUpOnce.

/**
 * Initialize mobile client and configure the storage.
 * Upload the test files ahead of time.
 *
 * @throws Exception if mobile client initialization fails
 */
@BeforeClass
public static void setUpOnce() throws Exception {
    Context context = getApplicationContext();
    // Init auth stuff
    SynchronousMobileClient.instance().initialize();
    // Get a handle to storage
    storageCategory = TestStorageCategory.create(context, R.raw.amplifyconfiguration);
    synchronousStorage = SynchronousStorage.delegatingTo(storageCategory);
    // Upload to PUBLIC for consistency
    String key;
    StorageUploadFileOptions uploadOptions = StorageUploadFileOptions.builder().accessLevel(TESTING_ACCESS_LEVEL).build();
    // Upload large test file
    largeFile = new RandomTempFile(LARGE_FILE_NAME, LARGE_FILE_SIZE);
    key = LARGE_FILE_NAME;
    synchronousStorage.uploadFile(key, largeFile, uploadOptions, EXTENDED_TIMEOUT_MS);
    // Upload small test file
    smallFile = new RandomTempFile(SMALL_FILE_NAME, SMALL_FILE_SIZE);
    key = SMALL_FILE_NAME;
    synchronousStorage.uploadFile(key, smallFile, uploadOptions);
}
Also used : Context(android.content.Context) ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) StorageUploadFileOptions(com.amplifyframework.storage.options.StorageUploadFileOptions) BeforeClass(org.junit.BeforeClass)

Example 8 with RandomTempFile

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

the class AWSS3StorageDownloadTest method setUp.

/**
 * Sets up the options to use for transfer.
 *
 * @throws Exception if fails to create random temp file
 */
@Before
public void setUp() throws Exception {
    // Always interact with PUBLIC access for consistency
    options = StorageDownloadFileOptions.builder().accessLevel(TESTING_ACCESS_LEVEL).build();
    // Create a set to remember all the subscriptions
    subscriptions = new HashSet<>();
    // Create a file to download to
    downloadFile = new RandomTempFile();
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Before(org.junit.Before)

Example 9 with RandomTempFile

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

the class AWSS3StorageUploadAccessLevelTest method setUp.

/**
 * Signs out by default and sets up the file to test uploading.
 *
 * @throws Exception if an error is encountered while creating file
 */
@Before
public void setUp() throws Exception {
    // Start as a GUEST user
    mobileClient.signOut();
    // Create a new file to upload
    uploadFile = new RandomTempFile(UPLOAD_SIZE);
    remoteKey = uploadFile.getName();
    // Override this per test-case
    uploadOptions = StorageUploadFileOptions.defaultInstance();
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Before(org.junit.Before)

Example 10 with RandomTempFile

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

the class AWSS3StorageUploadTest method testUploadFileIsResumable.

/**
 * Tests that file upload operation can be paused and resumed
 * while the transfer hasn't completed yet.
 *
 * @throws Exception if upload is not paused, resumed, and
 *         completed successfully before timeout
 */
@SuppressWarnings("unchecked")
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadFileIsResumable() throws Exception {
    final CountDownLatch completed = new CountDownLatch(1);
    final CountDownLatch resumed = new CountDownLatch(1);
    final AtomicReference<Resumable> opContainer = new AtomicReference<>();
    final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
    // Create a file large enough that transfer won't finish before being paused
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    // Listen to Hub events to resume when operation has been paused
    SubscriptionToken resumeToken = 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.PAUSED.equals(state)) {
                opContainer.get().resume();
                resumed.countDown();
            }
        }
    });
    subscriptions.add(resumeToken);
    // Begin uploading a large file
    StorageUploadFileOperation<?> op = storageCategory.uploadFile(uploadFile.getName(), uploadFile, options, progress -> {
        if (progress.getCurrentBytes() > 0 && resumed.getCount() > 0) {
            opContainer.get().pause();
        }
    }, result -> completed.countDown(), errorContainer::set);
    opContainer.set(op);
    // Assert that all the required conditions have been met
    assertTrue(resumed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertTrue(completed.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) Resumable(com.amplifyframework.core.async.Resumable) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

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