Search in sources :

Example 1 with RandomTempFile

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

the class AWSS3StorageListAccessLevelTest method uploadTestFiles.

private static void uploadTestFiles() throws Exception {
    // Create a temporary file to upload
    File uploadFile = new RandomTempFile(UPLOAD_SIZE);
    String uploadName = uploadFile.getName();
    String uploadPath = uploadFile.getAbsolutePath();
    uploadKey = TEST_DIR_NAME + "/" + uploadName;
    StorageUploadFileOptions options;
    // Upload as GUEST
    mobileClient.signOut();
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PUBLIC).build();
    storage.uploadFile(uploadKey, uploadFile, options);
    // Upload as user one
    mobileClient.signOut();
    mobileClient.signIn(userOne.getUsername(), userOne.getPassword());
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PROTECTED).build();
    storage.uploadFile(uploadKey, uploadFile, options);
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PRIVATE).build();
    storage.uploadFile(uploadKey, uploadFile, options);
    // Upload as user two
    mobileClient.signOut();
    mobileClient.signIn(userTwo.getUsername(), userTwo.getPassword());
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PROTECTED).build();
    storage.uploadFile(uploadKey, uploadFile, options);
    options = StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PRIVATE).build();
    storage.uploadFile(uploadKey, uploadFile, options);
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) StorageUploadFileOptions(com.amplifyframework.storage.options.StorageUploadFileOptions)

Example 2 with RandomTempFile

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

the class AWSS3StorageUploadTest method testUploadSmallFile.

/**
 * Tests that small file (single-part) uploads successfully.
 *
 * @throws Exception if upload fails
 */
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadSmallFile() throws Exception {
    File uploadFile = new RandomTempFile(SMALL_FILE_SIZE);
    String fileName = uploadFile.getName();
    synchronousStorage.uploadFile(fileName, uploadFile, options);
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 3 with RandomTempFile

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

the class AWSS3StorageUploadTest method testUploadLargeFile.

/**
 * Tests that large file (multi-part) uploads successfully.
 *
 * @throws Exception if upload fails
 */
@Ignore("Contains test which either hang themselves, or hang the suite overall.")
public void testUploadLargeFile() throws Exception {
    File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
    String fileName = uploadFile.getName();
    synchronousStorage.uploadFile(fileName, uploadFile, options, EXTENDED_TIMEOUT_MS);
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Ignore(org.junit.Ignore)

Example 4 with RandomTempFile

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

the class StorageComponentTest method testUploadFileGetsKey.

/**
 * Test that calling upload file 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 testUploadFileGetsKey() throws Exception {
    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.onStateChanged(0, TransferState.COMPLETED);
        return null;
    }).when(observer).setTransferListener(any(TransferListener.class));
    StorageUploadFileResult result = Await.<StorageUploadFileResult, StorageException>result((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
    assertEquals(toRemoteKey, result.getKey());
}
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) File(java.io.File) RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 5 with RandomTempFile

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

the class AWSS3StorageDownloadAccessLevelTest method setUp.

/**
 * Signs out by default and sets up download file destination.
 *
 * @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 download destination
    downloadFile = new RandomTempFile();
    // Override this per test-case
    downloadOptions = StorageDownloadFileOptions.defaultInstance();
}
Also used : RandomTempFile(com.amplifyframework.testutils.random.RandomTempFile) Before(org.junit.Before)

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