diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt index 4f6ff0666..ac66b5eea 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt @@ -23,12 +23,10 @@ import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider import com.amplifyframework.testmodels.commentsblog.Post import com.amplifyframework.testmodels.commentsblog.PostStatus import com.amplifyframework.testutils.HubAccumulator +import com.amplifyframework.testutils.sync.SynchronousDataStore import java.util.UUID -import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import org.junit.After -import org.junit.Assert.assertTrue -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -53,45 +51,26 @@ class DatastoreCanaryTest { } } + val syncDatastore = SynchronousDataStore.delegatingTo(Amplify.DataStore) + @After fun teardown() { - val latch = CountDownLatch(1) - Amplify.DataStore.clear( - { latch.countDown() }, - { - latch.countDown() - Log.e(TAG, "Error clearing DataStore", it) - } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncDatastore.clear() } @Test fun save() { - val latch = CountDownLatch(1) val post = Post.builder() .title("Post" + UUID.randomUUID().toString()) .status(PostStatus.ACTIVE) .rating(3) .build() - - Amplify.DataStore.save( - post, - { latch.countDown() }, - { fail("Error creating post: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.save(post) } @Test fun query() { - val latch = CountDownLatch(1) - Amplify.DataStore.query( - Post::class.java, - { latch.countDown() }, - { fail("Error retrieving posts: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.query(Post::class.java) } @Test @@ -103,75 +82,33 @@ class DatastoreCanaryTest { .id(UUID.randomUUID().toString()) .build() - val saveLatch = CountDownLatch(1) val createHub = HubAccumulator.create( HubChannel.DATASTORE, DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id), 1 ).start() - Amplify.DataStore.save( - post, - { saveLatch.countDown() }, - { fail("Error creating post: $it") } - ) - saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncDatastore.save(post) createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS) - val deleteLatch = CountDownLatch(1) - Amplify.DataStore.delete( - post, - { deleteLatch.countDown() }, - { fail("Failed to delete post: $it") } - ) + syncDatastore.delete(post) + // Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617 Thread.sleep(1000) - assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) } @Test fun start() { - val latch = CountDownLatch(1) - Amplify.DataStore.start( - { latch.countDown() }, - { fail("Error starting DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) - } - - @Test - fun observe() { - val latch = CountDownLatch(1) - Amplify.DataStore.observe( - Post::class.java, - { latch.countDown() }, - { - val post = it.item() - Log.i(TAG, "Post: $post") - }, - { fail("Observation failed: $it") }, - { Log.i(TAG, "Observation complete") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.start() } @Test fun stop() { - val latch = CountDownLatch(1) - Amplify.DataStore.stop( - { latch.countDown() }, - { fail("Error stopping DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.stop() } @Test fun clear() { - val latch = CountDownLatch(1) - Amplify.DataStore.clear( - { latch.countDown() }, - { fail("Error clearing DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.clear() } } diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTestGen2.kt b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTestGen2.kt index 71bd0bcdc..cfe4518fe 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTestGen2.kt +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTestGen2.kt @@ -25,12 +25,10 @@ import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider import com.amplifyframework.testmodels.commentsblog.Post import com.amplifyframework.testmodels.commentsblog.PostStatus import com.amplifyframework.testutils.HubAccumulator +import com.amplifyframework.testutils.sync.SynchronousDataStore import java.util.UUID -import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import org.junit.After -import org.junit.Assert.assertTrue -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -55,45 +53,26 @@ class DatastoreCanaryTestGen2 { } } + val syncDatastore = SynchronousDataStore.delegatingTo(Amplify.DataStore) + @After fun teardown() { - val latch = CountDownLatch(1) - Amplify.DataStore.clear( - { latch.countDown() }, - { - latch.countDown() - Log.e(TAG, "Error clearing DataStore", it) - } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncDatastore.clear() } @Test fun save() { - val latch = CountDownLatch(1) val post = Post.builder() .title("Post" + UUID.randomUUID().toString()) .status(PostStatus.ACTIVE) .rating(3) .build() - - Amplify.DataStore.save( - post, - { latch.countDown() }, - { fail("Error creating post: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.save(post) } @Test fun query() { - val latch = CountDownLatch(1) - Amplify.DataStore.query( - Post::class.java, - { latch.countDown() }, - { fail("Error retrieving posts: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.query(Post::class.java) } @Test @@ -105,75 +84,33 @@ class DatastoreCanaryTestGen2 { .id(UUID.randomUUID().toString()) .build() - val saveLatch = CountDownLatch(1) val createHub = HubAccumulator.create( HubChannel.DATASTORE, DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id), 1 ).start() - Amplify.DataStore.save( - post, - { saveLatch.countDown() }, - { fail("Error creating post: $it") } - ) - saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncDatastore.save(post) createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS) - val deleteLatch = CountDownLatch(1) - Amplify.DataStore.delete( - post, - { deleteLatch.countDown() }, - { fail("Failed to delete post: $it") } - ) + syncDatastore.delete(post) + // Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617 Thread.sleep(1000) - assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) } @Test fun start() { - val latch = CountDownLatch(1) - Amplify.DataStore.start( - { latch.countDown() }, - { fail("Error starting DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) - } - - @Test - fun observe() { - val latch = CountDownLatch(1) - Amplify.DataStore.observe( - Post::class.java, - { latch.countDown() }, - { - val post = it.item() - Log.i(TAG, "Post: $post") - }, - { fail("Observation failed: $it") }, - { Log.i(TAG, "Observation complete") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.start() } @Test fun stop() { - val latch = CountDownLatch(1) - Amplify.DataStore.stop( - { latch.countDown() }, - { fail("Error stopping DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.stop() } @Test fun clear() { - val latch = CountDownLatch(1) - Amplify.DataStore.clear( - { latch.countDown() }, - { fail("Error clearing DataStore: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncDatastore.clear() } } diff --git a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTest.kt b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTest.kt index e94fba8fb..6fab22adb 100644 --- a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTest.kt +++ b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTest.kt @@ -21,11 +21,9 @@ import com.amplifyframework.AmplifyException import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin import com.amplifyframework.core.Amplify import com.amplifyframework.geo.models.Coordinates -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit +import com.amplifyframework.testutils.sync.SynchronousAuth +import com.amplifyframework.testutils.sync.SynchronousGeo import org.junit.After -import org.junit.Assert -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -47,6 +45,9 @@ class GeoCanaryTest { } } + private val syncAuth = SynchronousAuth.delegatingToAmplify() + private val syncGeo = SynchronousGeo.delegatingToAmplify() + @After fun tearDown() { signOutFromCognito() @@ -54,66 +55,31 @@ class GeoCanaryTest { @Test fun searchByText() { - val latch = CountDownLatch(1) signInWithCognito() val searchQuery = "Amazon Go" - try { - Amplify.Geo.searchByText( - searchQuery, - { - for (place in it.places) { - Log.i(TAG, place.toString()) - } - latch.countDown() - }, - { fail("Failed to search for $searchQuery: $it") } - ) - } catch (e: Exception) { - fail(e.toString()) + val result = syncGeo.searchByText(searchQuery) + for (place in result.places) { + Log.i(TAG, place.toString()) } - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) } @Test fun searchByCoordinates() { - val latch = CountDownLatch(1) signInWithCognito() val position = Coordinates(47.6153, -122.3384) - try { - Amplify.Geo.searchByCoordinates( - position, - { - for (place in it.places) { - Log.i(TAG, place.toString()) - } - latch.countDown() - }, - { fail("Failed to reverse geocode $position: $it") } - ) - } catch (e: Exception) { - fail(e.toString()) + val result = syncGeo.searchByCoordinates(position) + for (place in result.places) { + Log.i(TAG, place.toString()) } - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) } private fun signInWithCognito() { - val latch = CountDownLatch(1) val context = ApplicationProvider.getApplicationContext() val (username, password) = Credentials.load(context) - Amplify.Auth.signIn( - username, - password, - { latch.countDown() }, - { Log.e(TAG, "Failed to sign in", it) } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncAuth.signIn(username, password) } private fun signOutFromCognito() { - val latch = CountDownLatch(1) - Amplify.Auth.signOut { - latch.countDown() - } - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncAuth.signOut() } } diff --git a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTestGen2.kt b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTestGen2.kt index 09e1762eb..09f72bfed 100644 --- a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTestGen2.kt +++ b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/GeoCanaryTestGen2.kt @@ -23,11 +23,9 @@ import com.amplifyframework.core.Amplify import com.amplifyframework.core.configuration.AmplifyOutputs import com.amplifyframework.geo.location.test.R import com.amplifyframework.geo.models.Coordinates -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit +import com.amplifyframework.testutils.sync.SynchronousAuth +import com.amplifyframework.testutils.sync.SynchronousGeo import org.junit.After -import org.junit.Assert -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -49,6 +47,9 @@ class GeoCanaryTestGen2 { } } + private val syncAuth = SynchronousAuth.delegatingToAmplify() + private val syncGeo = SynchronousGeo.delegatingToAmplify() + @After fun tearDown() { signOutFromCognito() @@ -56,66 +57,31 @@ class GeoCanaryTestGen2 { @Test fun searchByText() { - val latch = CountDownLatch(1) signInWithCognito() val searchQuery = "Amazon Go" - try { - Amplify.Geo.searchByText( - searchQuery, - { - for (place in it.places) { - Log.i(TAG, place.toString()) - } - latch.countDown() - }, - { fail("Failed to search for $searchQuery: $it") } - ) - } catch (e: Exception) { - fail(e.toString()) + val result = syncGeo.searchByText(searchQuery) + for (place in result.places) { + Log.i(TAG, place.toString()) } - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) } @Test fun searchByCoordinates() { - val latch = CountDownLatch(1) signInWithCognito() val position = Coordinates(47.6153, -122.3384) - try { - Amplify.Geo.searchByCoordinates( - position, - { - for (place in it.places) { - Log.i(TAG, place.toString()) - } - latch.countDown() - }, - { fail("Failed to reverse geocode $position: $it") } - ) - } catch (e: Exception) { - fail(e.toString()) + val result = syncGeo.searchByCoordinates(position) + for (place in result.places) { + Log.i(TAG, place.toString()) } - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) } private fun signInWithCognito() { - val latch = CountDownLatch(1) val context = ApplicationProvider.getApplicationContext() val (username, password) = Credentials.load(context) - Amplify.Auth.signIn( - username, - password, - { latch.countDown() }, - { Log.e(TAG, "Failed to sign in", it) } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncAuth.signIn(username, password) } private fun signOutFromCognito() { - val latch = CountDownLatch(1) - Amplify.Auth.signOut { - latch.countDown() - } - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncAuth.signOut() } } diff --git a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/SearchApiTest.kt b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/SearchApiTest.kt index 24855b091..e9b4d9e2b 100644 --- a/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/SearchApiTest.kt +++ b/aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/SearchApiTest.kt @@ -96,7 +96,7 @@ class SearchApiTest { fun searchByTextReturnsResult() { signInWithCognito() val query = UUID.randomUUID().toString() - val result = geo?.searchByText(query, GeoSearchByTextOptions.defaults()) + val result = geo.searchByText(query, GeoSearchByTextOptions.defaults()) Assert.assertNotNull(result) Assert.assertNotNull(result!!.places) } @@ -115,7 +115,7 @@ class SearchApiTest { nextDouble(-90.0, 90.0), nextDouble(-180.0, 180.0) ) - val result = geo?.searchByCoordinates(coordinates, GeoSearchByCoordinatesOptions.defaults()) + val result = geo.searchByCoordinates(coordinates, GeoSearchByCoordinatesOptions.defaults()) Assert.assertNotNull(result) Assert.assertNotNull(result!!.places) diff --git a/aws-predictions/src/androidTest/java/com/amplifyframework/predictions/aws/PredictionsCanaryTest.kt b/aws-predictions/src/androidTest/java/com/amplifyframework/predictions/aws/PredictionsCanaryTest.kt index 2068e46d0..689f67ab6 100644 --- a/aws-predictions/src/androidTest/java/com/amplifyframework/predictions/aws/PredictionsCanaryTest.kt +++ b/aws-predictions/src/androidTest/java/com/amplifyframework/predictions/aws/PredictionsCanaryTest.kt @@ -29,10 +29,7 @@ import com.amplifyframework.predictions.result.IdentifyEntitiesResult import com.amplifyframework.predictions.result.IdentifyLabelsResult import com.amplifyframework.predictions.result.IdentifyTextResult import com.amplifyframework.testutils.Assets -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit -import org.junit.Assert.assertTrue -import org.junit.Assert.fail +import com.amplifyframework.testutils.sync.SynchronousPredictions import org.junit.BeforeClass import org.junit.Test @@ -54,149 +51,77 @@ class PredictionsCanaryTest { } } + val syncPredictions = SynchronousPredictions.delegatingTo(Amplify.Predictions) + @Test fun translateText() { - val latch = CountDownLatch(1) - Amplify.Predictions.translateText( + val result = syncPredictions.translateText( "I like to eat spaghetti", LanguageType.ENGLISH, - LanguageType.SPANISH, - { - Log.i(TAG, it.translatedText) - latch.countDown() - }, - { fail("Translation failed: $it") } + LanguageType.SPANISH ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + Log.i(TAG, result.translatedText) } @Test fun textToSpeech() { - val latch = CountDownLatch(1) - Amplify.Predictions.convertTextToSpeech( - "I like to eat spaghetti!", - { latch.countDown() }, - { fail("Failed to convert text to speech: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncPredictions.convertTextToSpeech("I like to eat spaghetti!") } @Test fun identifyTextInImage() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("sample-table.png") - Amplify.Predictions.identify( - TextFormatType.PLAIN, - image, - { result -> - val identifyResult = result as IdentifyTextResult - Log.i(TAG, identifyResult.fullText) - latch.countDown() - }, - { fail("Identify text failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(TextFormatType.PLAIN, image) + val identifyResult = result as IdentifyTextResult + Log.i(TAG, identifyResult.fullText) } @Test fun identifyTextInDocument() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("sample-table.png") - Amplify.Predictions.identify( - TextFormatType.FORM, - image, - { result -> - val identifyResult = result as IdentifyDocumentTextResult - Log.i(TAG, identifyResult.fullText) - latch.countDown() - }, - { fail("Identify text failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(TextFormatType.FORM, image) + val identifyResult = result as IdentifyDocumentTextResult + Log.i(TAG, identifyResult.fullText) } @Test fun identifyEntities() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("jeff_bezos.jpg") - Amplify.Predictions.identify( - IdentifyActionType.DETECT_ENTITIES, - image, - { result -> - val identifyResult = result as IdentifyEntitiesResult - val metadata = identifyResult.entities.firstOrNull() - Log.i(TAG, "${metadata?.box?.toShortString()}") - latch.countDown() - }, - { fail("Entity detection failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(IdentifyActionType.DETECT_ENTITIES, image) + val identifyResult = result as IdentifyEntitiesResult + val metadata = identifyResult.entities.firstOrNull() + Log.i(TAG, "${metadata?.box?.toShortString()}") } @Test fun identifyCelebrities() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("jeff_bezos.jpg") - Amplify.Predictions.identify( - IdentifyActionType.DETECT_CELEBRITIES, - image, - { result -> - val identifyResult = result as IdentifyCelebritiesResult - val metadata = identifyResult.celebrities.firstOrNull() - Log.i(TAG, "${metadata?.celebrity?.name}") - latch.countDown() - }, - { fail("Celebrity detection failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(IdentifyActionType.DETECT_CELEBRITIES, image) + val identifyResult = result as IdentifyCelebritiesResult + val metadata = identifyResult.celebrities.firstOrNull() + Log.i(TAG, "${metadata?.celebrity?.name}") } @Test fun identifyLabels() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("jeff_bezos.jpg") - Amplify.Predictions.identify( - LabelType.LABELS, - image, - { result -> - val identifyResult = result as IdentifyLabelsResult - val label = identifyResult.labels.firstOrNull() - Log.i(TAG, "${label?.name}") - latch.countDown() - }, - { fail("Label detection failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(LabelType.LABELS, image) + val identifyResult = result as IdentifyLabelsResult + val label = identifyResult.labels.firstOrNull() + Log.i(TAG, "${label?.name}") } @Test fun identifyModerationLabels() { - val latch = CountDownLatch(1) val image = Assets.readAsBitmap("jeff_bezos.jpg") - Amplify.Predictions.identify( - LabelType.MODERATION_LABELS, - image, - { result -> - val identifyResult = result as IdentifyLabelsResult - Log.i(TAG, "${identifyResult.isUnsafeContent}") - latch.countDown() - }, - { fail("Identify moderation labels failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.identify(LabelType.MODERATION_LABELS, image) + val identifyResult = result as IdentifyLabelsResult + Log.i(TAG, "${identifyResult.isUnsafeContent}") } @Test fun interpretSentiment() { - val latch = CountDownLatch(1) - Amplify.Predictions.interpret( - "I like to eat spaghetti", - { - Log.i(TAG, "${it.sentiment?.value}") - latch.countDown() - }, - { fail("Interpret failed: $it") } - ) - assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncPredictions.interpret("I like to eat spaghetti") + Log.i(TAG, "${result.sentiment?.value}") } } diff --git a/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTest.kt b/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTest.kt index 172a8c01c..b93487d01 100644 --- a/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTest.kt +++ b/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTest.kt @@ -21,17 +21,19 @@ import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin import com.amplifyframework.core.Amplify import com.amplifyframework.storage.StorageAccessLevel import com.amplifyframework.storage.operation.StorageUploadFileOperation +import com.amplifyframework.storage.options.StorageDownloadFileOptions +import com.amplifyframework.storage.options.StorageGetUrlOptions import com.amplifyframework.storage.options.StoragePagedListOptions +import com.amplifyframework.storage.options.StorageRemoveOptions import com.amplifyframework.storage.options.StorageUploadFileOptions +import com.amplifyframework.storage.options.StorageUploadInputStreamOptions +import com.amplifyframework.testutils.sync.SynchronousStorage import java.io.File import java.io.FileInputStream import java.io.RandomAccessFile import java.util.UUID -import java.util.concurrent.CountDownLatch +import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicReference -import org.junit.Assert -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -55,108 +57,71 @@ class StorageCanaryTest { } } + private val syncStorage = SynchronousStorage.delegatingToAmplify() + @Test fun uploadInputStream() { - val latch = CountDownLatch(1) val raf = createFile(1) val stream = FileInputStream(raf) val fileKey = "ExampleKey" - Amplify.Storage.uploadInputStream( - fileKey, - stream, - { latch.countDown() }, - { fail("Upload failed: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadInputStream(fileKey, stream, StorageUploadInputStreamOptions.defaultInstance()) removeFile(fileKey) } @Test fun uploadFile() { - val latch = CountDownLatch(1) val file = createFile(1) val fileKey = UUID.randomUUID().toString() - Amplify.Storage.uploadFile( - fileKey, - file, - { latch.countDown() }, - { fail("Upload failed: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadFile(fileKey, file, StorageUploadFileOptions.defaultInstance()) removeFile(fileKey) } @Test fun downloadFile() { - val uploadLatch = CountDownLatch(1) val file = createFile(1) val fileName = "ExampleKey${UUID.randomUUID()}" - Amplify.Storage.uploadFile( - fileName, - file, - { uploadLatch.countDown() }, - { fail("Upload failed: $it") } - ) - uploadLatch.await(TIMEOUT_S, TimeUnit.SECONDS) - - val downloadLatch = CountDownLatch(1) - Amplify.Storage.downloadFile( - fileName, - file, - { downloadLatch.countDown() }, - { fail("Download failed: $it") } - ) - Assert.assertTrue(downloadLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadFile(fileName, file, StorageUploadFileOptions.defaultInstance()) + syncStorage.downloadFile(fileName, file, StorageDownloadFileOptions.defaultInstance()) } @Test fun getUrl() { - val latch = CountDownLatch(1) - Amplify.Storage.getUrl( - "ExampleKey", - { - Log.i(TAG, "Successfully generated: ${it.url}") - latch.countDown() - }, - { fail("URL generation failure: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.getUrl("ExampleKey", StorageGetUrlOptions.defaultInstance()) + Log.i(TAG, "Successfully generated: ${result.url}") } @Test fun getTransfer() { - val uploadLatch = CountDownLatch(1) val file = createFile(100) val fileName = "ExampleKey${UUID.randomUUID()}" - val transferId = AtomicReference() - val opContainer = AtomicReference>() + + val opFuture = CompletableFuture>() + val paused = CompletableFuture() + val op = Amplify.Storage.uploadFile( fileName, file, StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PUBLIC).build(), { progress -> if (progress.currentBytes > 0) { - opContainer.get().pause() + // Block until operation is available + opFuture.get(TIMEOUT_S, TimeUnit.SECONDS).pause() } - uploadLatch.countDown() + paused.complete(true) }, - { Log.i(TAG, "Successfully uploaded: ${it.key}") }, - { fail("Upload failed: $it") } - ) - opContainer.set(op) - transferId.set(op.transferId) - uploadLatch.await(TIMEOUT_S, TimeUnit.SECONDS) - - val transferLatch = CountDownLatch(1) - Amplify.Storage.getTransfer( - transferId.get(), - { operation -> - Log.i(TAG, "Current State" + operation.transferState) - transferLatch.countDown() + { }, - { fail("Failed to query transfer: $it") } + { + paused.completeExceptionally(it) + } ) - Assert.assertTrue(transferLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) + opFuture.complete(op) + paused.get(TIMEOUT_S, TimeUnit.SECONDS) + + val operation = syncStorage.getTransfer(op.transferId) + Log.i(TAG, "Current State" + operation.transferState) + + op.cancel() } @Test @@ -164,33 +129,16 @@ class StorageCanaryTest { val options = StoragePagedListOptions.builder() .setPageSize(1000) .build() - val latch = CountDownLatch(1) - Amplify.Storage.list( - "", - options, - { result -> - result.items.forEach { item -> - Log.i(TAG, "Item: ${item.key}") - } - latch.countDown() - }, - { fail("Failed to list items: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.list("", options) + result.items.forEach { item -> + Log.i(TAG, "Item: ${item.key}") + } } @Test fun remove() { - val latch = CountDownLatch(1) - Amplify.Storage.remove( - "myUploadedFileName.txt", - { - Log.i(TAG, "Successfully removed: ${it.key}") - latch.countDown() - }, - { fail("Failed to remove file: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.remove("myUploadedFileName.txt", StorageRemoveOptions.defaultInstance()) + Log.i(TAG, "Successfully removed: ${result.key}") } private fun createFile(size: Int): File { @@ -204,12 +152,6 @@ class StorageCanaryTest { } private fun removeFile(fileKey: String) { - val latch = CountDownLatch(1) - Amplify.Storage.remove( - fileKey, - { latch.countDown() }, - { Log.e(TAG, "Failed to remove file", it) } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncStorage.remove(fileKey, StorageRemoveOptions.defaultInstance()) } } diff --git a/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTestGen2.kt b/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTestGen2.kt index 3efc05535..9b7691192 100644 --- a/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTestGen2.kt +++ b/aws-storage-s3/src/androidTest/java/com/amplifyframework/storage/s3/StorageCanaryTestGen2.kt @@ -22,18 +22,20 @@ import com.amplifyframework.core.Amplify import com.amplifyframework.core.configuration.AmplifyOutputs import com.amplifyframework.storage.StorageAccessLevel import com.amplifyframework.storage.operation.StorageUploadFileOperation +import com.amplifyframework.storage.options.StorageDownloadFileOptions +import com.amplifyframework.storage.options.StorageGetUrlOptions import com.amplifyframework.storage.options.StoragePagedListOptions +import com.amplifyframework.storage.options.StorageRemoveOptions import com.amplifyframework.storage.options.StorageUploadFileOptions +import com.amplifyframework.storage.options.StorageUploadInputStreamOptions import com.amplifyframework.storage.s3.test.R +import com.amplifyframework.testutils.sync.SynchronousStorage import java.io.File import java.io.FileInputStream import java.io.RandomAccessFile import java.util.UUID -import java.util.concurrent.CountDownLatch +import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicReference -import org.junit.Assert -import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test @@ -57,108 +59,74 @@ class StorageCanaryTestGen2 { } } + private val syncStorage = SynchronousStorage.delegatingToAmplify() + @Test fun uploadInputStream() { - val latch = CountDownLatch(1) val raf = createFile(1) val stream = FileInputStream(raf) val fileKey = "ExampleKey" - Amplify.Storage.uploadInputStream( - fileKey, - stream, - { latch.countDown() }, - { fail("Upload failed: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadInputStream(fileKey, stream, StorageUploadInputStreamOptions.defaultInstance()) removeFile(fileKey) } @Test fun uploadFile() { - val latch = CountDownLatch(1) val file = createFile(1) val fileKey = UUID.randomUUID().toString() - Amplify.Storage.uploadFile( - fileKey, - file, - { latch.countDown() }, - { fail("Upload failed: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadFile(fileKey, file, StorageUploadFileOptions.defaultInstance()) removeFile(fileKey) } @Test fun downloadFile() { - val uploadLatch = CountDownLatch(1) val file = createFile(1) val fileName = "ExampleKey${UUID.randomUUID()}" - Amplify.Storage.uploadFile( - fileName, - file, - { uploadLatch.countDown() }, - { fail("Upload failed: $it") } - ) - uploadLatch.await(TIMEOUT_S, TimeUnit.SECONDS) - - val downloadLatch = CountDownLatch(1) - Amplify.Storage.downloadFile( - fileName, - file, - { downloadLatch.countDown() }, - { fail("Download failed: $it") } - ) - Assert.assertTrue(downloadLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) + syncStorage.uploadFile(fileName, file, StorageUploadFileOptions.defaultInstance()) + syncStorage.downloadFile(fileName, file, StorageDownloadFileOptions.defaultInstance()) } @Test fun getUrl() { - val latch = CountDownLatch(1) - Amplify.Storage.getUrl( - "ExampleKey", - { - Log.i(TAG, "Successfully generated: ${it.url}") - latch.countDown() - }, - { fail("URL generation failure: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.getUrl("ExampleKey", StorageGetUrlOptions.defaultInstance()) + Log.i(TAG, "Successfully generated: ${result.url}") } @Test fun getTransfer() { - val uploadLatch = CountDownLatch(1) val file = createFile(100) val fileName = "ExampleKey${UUID.randomUUID()}" - val transferId = AtomicReference() - val opContainer = AtomicReference>() + + val opFuture = CompletableFuture>() + val uploadComplete = CompletableFuture() + val paused = CompletableFuture() + val op = Amplify.Storage.uploadFile( fileName, file, StorageUploadFileOptions.builder().accessLevel(StorageAccessLevel.PUBLIC).build(), { progress -> if (progress.currentBytes > 0) { - opContainer.get().pause() + // Block until operation is available + opFuture.get(TIMEOUT_S, TimeUnit.SECONDS).pause() } - uploadLatch.countDown() + paused.complete(true) }, - { Log.i(TAG, "Successfully uploaded: ${it.key}") }, - { fail("Upload failed: $it") } - ) - opContainer.set(op) - transferId.set(op.transferId) - uploadLatch.await(TIMEOUT_S, TimeUnit.SECONDS) - - val transferLatch = CountDownLatch(1) - Amplify.Storage.getTransfer( - transferId.get(), - { operation -> - Log.i(TAG, "Current State" + operation.transferState) - transferLatch.countDown() + { + uploadComplete.complete(true) }, - { fail("Failed to query transfer: $it") } + { + paused.completeExceptionally(it) + uploadComplete.complete(false) + } ) - Assert.assertTrue(transferLatch.await(TIMEOUT_S, TimeUnit.SECONDS)) + opFuture.complete(op) + paused.get(TIMEOUT_S, TimeUnit.SECONDS) + + val operation = syncStorage.getTransfer(op.transferId) + Log.i(TAG, "Current State" + operation.transferState) + + op.cancel() } @Test @@ -166,33 +134,16 @@ class StorageCanaryTestGen2 { val options = StoragePagedListOptions.builder() .setPageSize(1000) .build() - val latch = CountDownLatch(1) - Amplify.Storage.list( - "", - options, - { result -> - result.items.forEach { item -> - Log.i(TAG, "Item: ${item.key}") - } - latch.countDown() - }, - { fail("Failed to list items: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.list("", options) + result.items.forEach { item -> + Log.i(TAG, "Item: ${item.key}") + } } @Test fun remove() { - val latch = CountDownLatch(1) - Amplify.Storage.remove( - "myUploadedFileName.txt", - { - Log.i(TAG, "Successfully removed: ${it.key}") - latch.countDown() - }, - { fail("Failed to remove file: $it") } - ) - Assert.assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS)) + val result = syncStorage.remove("myUploadedFileName.txt", StorageRemoveOptions.defaultInstance()) + Log.i(TAG, "Successfully removed: ${result.key}") } private fun createFile(size: Int): File { @@ -206,12 +157,6 @@ class StorageCanaryTestGen2 { } private fun removeFile(fileKey: String) { - val latch = CountDownLatch(1) - Amplify.Storage.remove( - fileKey, - { latch.countDown() }, - { Log.e(TAG, "Failed to remove file", it) } - ) - latch.await(TIMEOUT_S, TimeUnit.SECONDS) + syncStorage.remove(fileKey, StorageRemoveOptions.defaultInstance()) } } diff --git a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousDataStore.java b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousDataStore.java index eb7a72090..511eb1457 100644 --- a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousDataStore.java +++ b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousDataStore.java @@ -128,6 +128,11 @@ public List list(@NonNull Class clazz) throws DataStoreE return Immutable.of(items); } + @NonNull + public Iterator query(@NonNull Class clazz) throws DataStoreException { + return awaitIterator((onResult, onError) -> asyncDelegate.query(clazz, onResult, onError)); + } + /** * Calls the start method of the underlying DataStore implementation. * @throws DataStoreException On failure to start data store. diff --git a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousGeo.java b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousGeo.java index 18fd5d043..5d5552e05 100644 --- a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousGeo.java +++ b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousGeo.java @@ -120,6 +120,11 @@ public GeoSearchResult searchByText( asyncDelegate.searchByText(query, options, onResult, onError)); } + public GeoSearchResult searchByText(String query) throws GeoException { + return Await.result(TIMEOUT, (onResult, onError) -> + asyncDelegate.searchByText(query, onResult, onError)); + } + /** * Fetches geocode of provided geo-spacial coordinates. * @@ -135,4 +140,11 @@ public GeoSearchResult searchByCoordinates( return Await.result(TIMEOUT, (onResult, onError) -> asyncDelegate.searchByCoordinates(coordinates, options, onResult, onError)); } + + public GeoSearchResult searchByCoordinates( + Coordinates coordinates + ) throws GeoException { + return Await.result(TIMEOUT, (onResult, onError) -> + asyncDelegate.searchByCoordinates(coordinates, onResult, onError)); + } } diff --git a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousStorage.java b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousStorage.java index 26b0d8866..9ec0ee15b 100644 --- a/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousStorage.java +++ b/testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousStorage.java @@ -18,10 +18,12 @@ import androidx.annotation.NonNull; import com.amplifyframework.core.Amplify; +import com.amplifyframework.core.Consumer; import com.amplifyframework.storage.StorageCategory; import com.amplifyframework.storage.StorageCategoryBehavior; import com.amplifyframework.storage.StorageException; import com.amplifyframework.storage.StoragePath; +import com.amplifyframework.storage.operation.StorageTransferOperation; import com.amplifyframework.storage.options.StorageDownloadFileOptions; import com.amplifyframework.storage.options.StorageGetUrlOptions; import com.amplifyframework.storage.options.StorageListOptions; @@ -33,6 +35,8 @@ import com.amplifyframework.storage.result.StorageGetUrlResult; import com.amplifyframework.storage.result.StorageListResult; import com.amplifyframework.storage.result.StorageRemoveResult; +import com.amplifyframework.storage.result.StorageTransferProgress; +import com.amplifyframework.storage.result.StorageTransferResult; import com.amplifyframework.storage.result.StorageUploadFileResult; import com.amplifyframework.storage.result.StorageUploadInputStreamResult; import com.amplifyframework.testutils.Await; @@ -149,7 +153,6 @@ public StorageDownloadFileResult downloadFile( * @throws StorageException if download fails or times out */ @NonNull - @SuppressWarnings("deprecation") public StorageDownloadFileResult downloadFile( @NonNull StoragePath path, @NonNull File local, @@ -171,7 +174,6 @@ public StorageDownloadFileResult downloadFile( * @throws StorageException if upload fails or times out */ @NonNull - @SuppressWarnings("deprecation") public StorageUploadFileResult uploadFile( @NonNull String key, @NonNull File local, @@ -213,7 +215,6 @@ public StorageUploadFileResult uploadFile( * @throws StorageException if upload fails or times out */ @NonNull - @SuppressWarnings("deprecation") public StorageUploadFileResult uploadFile( @NonNull StoragePath path, @NonNull File local, @@ -417,6 +418,17 @@ public StorageGetUrlResult getUrl( ); } + @NonNull + @SuppressWarnings("deprecation") + public StorageGetUrlResult getUrl( + @NonNull String key, + @NonNull StorageGetUrlOptions options + ) throws StorageException { + return Await.result(STORAGE_OPERATION_TIMEOUT_MS, (onResult, onError) -> + asyncDelegate.getUrl(key, options, onResult, onError) + ); + } + /** * List the files in S3 bucket synchronously. * @@ -454,6 +466,14 @@ public StorageListResult list( ); } + @NonNull + public StorageListResult list( + @NonNull String path, + @NonNull StoragePagedListOptions options + ) throws StorageException { + return list(path, options, STORAGE_OPERATION_TIMEOUT_MS); + } + /** * List the files in S3 bucket synchronously. * @@ -492,4 +512,13 @@ public StorageListResult list( asyncDelegate.list(path, options, onResult, onError) ); } + + @NonNull + public StorageTransferOperation getTransfer( + @NonNull String transferId + ) throws StorageException { + return Await., StorageException>result(STORAGE_OPERATION_TIMEOUT_MS, (onResult, onError) -> + asyncDelegate.getTransfer(transferId, onResult, onError) + ); + } }