From b7c911feeefd7a8653ce069106ecdb8a4d5e470f Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 22 Sep 2021 16:17:39 +0100 Subject: [PATCH] adding test cases for when adding a pusher fails and when it already exists --- .../pushers/DefaultAddPusherTaskTest.kt | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/pushers/DefaultAddPusherTaskTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/pushers/DefaultAddPusherTaskTest.kt index 47730eba49..c3be7c613e 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/pushers/DefaultAddPusherTaskTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/pushers/DefaultAddPusherTaskTest.kt @@ -28,6 +28,7 @@ import io.realm.RealmModel import io.realm.RealmQuery import io.realm.kotlin.where import kotlinx.coroutines.runBlocking +import org.amshove.kluent.internal.assertFailsWith import org.amshove.kluent.shouldBeEqualTo import org.junit.Test import org.matrix.android.sdk.api.failure.GlobalError @@ -35,6 +36,7 @@ import org.matrix.android.sdk.api.session.pushers.PusherState import org.matrix.android.sdk.internal.database.model.PusherEntity import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.util.awaitTransaction +import java.net.SocketException private val A_JSON_PUSHER = JsonPusher( pushKey = "push-key", @@ -60,10 +62,10 @@ class DefaultAddPusherTaskTest { ) @Test - fun `given no persisted pusher when running task then fetches from api and inserts result with Registered state`() = runBlocking { + fun `given no persisted pusher when adding Pusher then updates api and inserts result with Registered state`() { monarchy.givenWhereReturns(result = null) - addPusherTask.execute(AddPusherTask.Params(A_JSON_PUSHER)) + runBlocking { addPusherTask.execute(AddPusherTask.Params(A_JSON_PUSHER)) } pushersAPI.verifySetPusher(A_JSON_PUSHER) monarchy.verifyInsertOrUpdate { @@ -72,6 +74,42 @@ class DefaultAddPusherTaskTest { } } } + + @Test + fun `given a persisted pusher when adding Pusher then updates api and mutates persisted result with Registered state`() { + val realmResult = PusherEntity(appDisplayName = null) + monarchy.givenWhereReturns(result = realmResult) + + runBlocking { addPusherTask.execute(AddPusherTask.Params(A_JSON_PUSHER)) } + + pushersAPI.verifySetPusher(A_JSON_PUSHER) + + realmResult.appDisplayName shouldBeEqualTo A_JSON_PUSHER.appDisplayName + realmResult.state shouldBeEqualTo PusherState.REGISTERED + } + + @Test + fun `given a persisted push entity and SetPush API fails when adding Pusher then mutates persisted result with Failed registration state and rethrows error`() { + val realmResult = PusherEntity() + monarchy.givenWhereReturns(result = realmResult) + pushersAPI.givenSetPusherErrors(SocketException()) + + assertFailsWith { + runBlocking { addPusherTask.execute(AddPusherTask.Params(A_JSON_PUSHER)) } + } + + realmResult.state shouldBeEqualTo PusherState.FAILED_TO_REGISTER + } + + @Test + fun `given no persisted push entity and SetPush API fails when adding Pusher then rethrows error`() { + monarchy.givenWhereReturns(result = null) + pushersAPI.givenSetPusherErrors(SocketException()) + + assertFailsWith { + runBlocking { addPusherTask.execute(AddPusherTask.Params(A_JSON_PUSHER)) } + } + } } internal class FakePushersAPI : PushersAPI {