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