Search in sources :

Example 1 with StorageGetUrlResult

use of com.amplifyframework.storage.result.StorageGetUrlResult in project amplify-android by aws-amplify.

the class RxStorageBindingTest method getUrlReturnsResult.

/**
 * When the delegate returns a result from the
 * {@link StorageCategoryBehavior#getUrl(String, StorageGetUrlOptions, Consumer, Consumer)},
 * the binding should emit the result via the single.
 * @throws MalformedURLException Not expected; it's part of the URL constructor signature, though
 */
@Test
public void getUrlReturnsResult() throws MalformedURLException {
    URL someRandomUrl = new URL("https://bogus.tld/foo");
    StorageGetUrlResult expectedResult = StorageGetUrlResult.fromUrl(someRandomUrl);
    doAnswer(invocation -> {
        int indexOfResultConsumer = 2;
        Consumer<StorageGetUrlResult> onResult = invocation.getArgument(indexOfResultConsumer);
        onResult.accept(expectedResult);
        return mock(StorageGetUrlOperation.class);
    }).when(delegate).getUrl(eq(remoteKey), any(StorageGetUrlOptions.class), anyConsumer(), anyConsumer());
    rxStorage.getUrl(remoteKey, StorageGetUrlOptions.defaultInstance()).test().awaitCount(1).assertNoErrors().assertValue(expectedResult);
}
Also used : StorageGetUrlOptions(com.amplifyframework.storage.options.StorageGetUrlOptions) URL(java.net.URL) StorageGetUrlResult(com.amplifyframework.storage.result.StorageGetUrlResult) Test(org.junit.Test)

Example 2 with StorageGetUrlResult

use of com.amplifyframework.storage.result.StorageGetUrlResult in project amplify-android by aws-amplify.

the class StorageComponentTest method testGenerateUrlGetsPresignedUrl.

/**
 * Test that calling get URL method from Storage category correctly invokes
 * the registered AWSS3StoragePlugin instance and returns a {@link URL}
 * instance for that download.
 *
 * @throws StorageException when an error is encountered while generating
 *                          URL from storage service
 */
@Test
public void testGenerateUrlGetsPresignedUrl() throws StorageException {
    final String fromRemoteKey = RandomString.string();
    final URL urlFromRemoteKey;
    try {
        // URL instance cannot be mocked so just make one
        // https://{random-host}:0/{fromRemoteKey}
        urlFromRemoteKey = new URL("https", RandomString.string(), 0, fromRemoteKey, null);
    } catch (MalformedURLException exception) {
        throw new RuntimeException(exception);
    }
    // Allow mock StorageService instance to return a non-null
    // URL instance.
    when(storageService.getPresignedUrl(anyString(), anyInt())).thenReturn(urlFromRemoteKey);
    // Let Storage category invoke getUrl on mock Storage Service.
    StorageGetUrlResult result = Await.<StorageGetUrlResult, StorageException>result((onResult, onError) -> storage.getUrl(fromRemoteKey, onResult, onError));
    assertEquals(urlFromRemoteKey, result.getUrl());
}
Also used : MalformedURLException(java.net.MalformedURLException) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) URL(java.net.URL) StorageGetUrlResult(com.amplifyframework.storage.result.StorageGetUrlResult) Test(org.junit.Test)

Aggregations

StorageGetUrlResult (com.amplifyframework.storage.result.StorageGetUrlResult)2 URL (java.net.URL)2 Test (org.junit.Test)2 StorageException (com.amplifyframework.storage.StorageException)1 StorageGetUrlOptions (com.amplifyframework.storage.options.StorageGetUrlOptions)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 MalformedURLException (java.net.MalformedURLException)1 Mockito.anyString (org.mockito.Mockito.anyString)1