Search in sources :

Example 1 with StorageException

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);
}
Also used : StorageGetUrlOptions(com.amplifyframework.storage.options.StorageGetUrlOptions) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 2 with StorageException

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);
}
Also used : StorageException(com.amplifyframework.storage.StorageException) StorageUploadFileOptions(com.amplifyframework.storage.options.StorageUploadFileOptions) Test(org.junit.Test)

Example 3 with StorageException

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);
}
Also used : StorageDownloadFileOptions(com.amplifyframework.storage.options.StorageDownloadFileOptions) StorageException(com.amplifyframework.storage.StorageException) Test(org.junit.Test)

Example 4 with StorageException

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);
}
Also used : JSONException(org.json.JSONException) Region(com.amazonaws.regions.Region) StorageException(com.amplifyframework.storage.StorageException)

Example 5 with StorageException

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);
    });
}
Also used : StorageException(com.amplifyframework.storage.StorageException) URL(java.net.URL) StorageException(com.amplifyframework.storage.StorageException) SuppressLint(android.annotation.SuppressLint)

Aggregations

StorageException (com.amplifyframework.storage.StorageException)19 Test (org.junit.Test)15 RandomString (com.amplifyframework.testutils.random.RandomString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 TransferListener (com.amazonaws.mobileconnectors.s3.transferutility.TransferListener)6 TransferObserver (com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)6 File (java.io.File)5 RandomTempFile (com.amplifyframework.testutils.random.RandomTempFile)4 SuppressLint (android.annotation.SuppressLint)3 InputStream (java.io.InputStream)3 StorageDownloadFileResult (com.amplifyframework.storage.result.StorageDownloadFileResult)2 StorageUploadFileResult (com.amplifyframework.storage.result.StorageUploadFileResult)2 StorageUploadInputStreamResult (com.amplifyframework.storage.result.StorageUploadInputStreamResult)2 ServerSideEncryption (com.amplifyframework.storage.s3.ServerSideEncryption)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 Region (com.amazonaws.regions.Region)1 StorageItem (com.amplifyframework.storage.StorageItem)1 StorageDownloadFileOptions (com.amplifyframework.storage.options.StorageDownloadFileOptions)1