Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -47,73 +45,41 @@ class GeoCanaryTest {
}
}

private val syncAuth = SynchronousAuth.delegatingToAmplify()
private val syncGeo = SynchronousGeo.delegatingToAmplify()

@After
fun tearDown() {
signOutFromCognito()
}

@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<Context>()
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()
}
}
Loading