use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class RxStorageBindingTest method getUrlEmitsFailure.
/**
* When the delegate emits a failure from the
* {@link StorageCategoryBehavior#getUrl(String, StorageGetUrlOptions, Consumer, Consumer)},
* the binding should emit a failure to its single observer.
*/
@Test
public void getUrlEmitsFailure() {
StorageException expectedException = new StorageException("oh", "boy!");
doAnswer(invocation -> {
int indexOfErrorConsumer = 3;
Consumer<StorageException> onError = invocation.getArgument(indexOfErrorConsumer);
onError.accept(expectedException);
return mock(StorageGetUrlOperation.class);
}).when(delegate).getUrl(eq(remoteKey), any(StorageGetUrlOptions.class), anyConsumer(), anyConsumer());
rxStorage.getUrl(remoteKey, StorageGetUrlOptions.defaultInstance()).test().awaitDone(TIMEOUT_MS, TimeUnit.MILLISECONDS).assertError(expectedException);
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class RxStorageBindingTest method uploadFileReturnsError.
/**
* When {@link StorageCategoryBehavior#uploadFile(String, File, Consumer, Consumer)} returns
* an {@link StorageException}, then the {@link Single} returned by
* {@link RxStorageCategoryBehavior#uploadFile(String, File)} should emit a {@link StorageException}.
*/
@Test
public void uploadFileReturnsError() {
StorageException error = new StorageException("Error uploading.", "Expected.");
doAnswer(invocation -> {
// 0 key, 1 local, 2 options, 3 onProgress 4 onResult, 5 onError
final int indexOfResultConsumer = 5;
Consumer<StorageException> errorConsumer = invocation.getArgument(indexOfResultConsumer);
errorConsumer.accept(error);
return mock(StorageUploadFileOperation.class);
}).when(delegate).uploadFile(eq(remoteKey), eq(localFile), any(StorageUploadFileOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
rxStorage.uploadFile(remoteKey, localFile).observeResult().test().assertError(error);
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class RxStorageBindingTest method downloadFileReturnsError.
/**
* When {@link StorageCategoryBehavior#downloadFile(String, File, Consumer, Consumer)} invokes
* its error callback, the {@link StorageException} is communicated via the {@link Single}
* returned by {@link RxStorageCategoryBehavior#downloadFile(String, File)}.
*/
@Test
public void downloadFileReturnsError() {
StorageException downloadError = new StorageException("Test exception.", "It is expected.");
doAnswer(invocation -> {
// 0 key, 1 local, 2 options, 3 onProgress 4 onResult, 5 onError
final int indexOfErrorConsumer = 5;
Consumer<StorageException> resultConsumer = invocation.getArgument(indexOfErrorConsumer);
resultConsumer.accept(downloadError);
return mock(StorageDownloadFileOperation.class);
}).when(delegate).downloadFile(eq(remoteKey), eq(localFile), any(StorageDownloadFileOptions.class), anyConsumer(), anyConsumer(), anyConsumer());
rxStorage.downloadFile(remoteKey, localFile).observeResult().test().assertError(downloadError);
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class AWSS3StoragePlugin method configure.
@Override
// TODO: Remove once default values are moved to configuration
@SuppressWarnings("MagicNumber")
public void configure(JSONObject pluginConfiguration, @NonNull Context context) throws StorageException {
String regionStr;
String bucket;
try {
regionStr = pluginConfiguration.getString(JsonKeys.REGION.getConfigurationKey());
} catch (JSONException error) {
throw new StorageException("Missing or malformed value for Region in " + AWS_S3_STORAGE_PLUGIN_KEY + "configuration.", error, "Check the attached error to see where the parsing issue took place.");
} catch (NullPointerException error) {
throw new StorageException("Missing configuration for " + AWS_S3_STORAGE_PLUGIN_KEY, "Check amplifyconfiguration.json to make sure that there is a section for " + AWS_S3_STORAGE_PLUGIN_KEY + " under the storage category.");
}
Region region = Region.getRegion(regionStr);
if (region == null) {
throw new StorageException("Invalid region provided", "Make sure the region you have configured for the AWS S3 Storage plugin is a value we support.");
}
try {
bucket = pluginConfiguration.getString(JsonKeys.BUCKET.getConfigurationKey());
} catch (JSONException error) {
throw new StorageException("Missing or malformed value for bucket in " + AWS_S3_STORAGE_PLUGIN_KEY + "configuration.", error, "Check the attached error to see where the parsing issue took place.");
}
try {
this.storageService = storageServiceFactory.create(context, region, bucket);
} catch (RuntimeException exception) {
throw new StorageException("Failed to create storage service.", exception, "Did you make sure to add AWSCognitoAuthPlugin to Amplify? " + "Check the attached exception for more details.");
}
// TODO: Integrate into config + options
this.defaultAccessLevel = StorageAccessLevel.PUBLIC;
this.defaultUrlExpiration = (int) TimeUnit.DAYS.toSeconds(7);
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class AWSS3StorageGetPresignedUrlOperation method start.
@SuppressLint("SyntheticAccessor")
@Override
public void start() {
executorService.submit(() -> {
awsS3StoragePluginConfiguration.getAWSS3PluginPrefixResolver(cognitoAuthProvider).resolvePrefix(getRequest().getAccessLevel(), getRequest().getTargetIdentityId(), prefix -> {
try {
String serviceKey = prefix.concat(getRequest().getKey());
URL url = storageService.getPresignedUrl(serviceKey, getRequest().getExpires());
onSuccess.accept(StorageGetUrlResult.fromUrl(url));
} catch (Exception exception) {
onError.accept(new StorageException("Encountered an issue while generating pre-signed URL", exception, "See included exception for more details and suggestions to fix."));
}
}, onError);
});
}
Aggregations