-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4101 from element-hq/feature/bma/unifiedPushGatew…
…ayResolverImprovement Unified push gateway resolver improvement
- Loading branch information
Showing
8 changed files
with
244 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...n/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayUrlResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.unifiedpush | ||
|
||
import com.squareup.anvil.annotations.ContributesBinding | ||
import io.element.android.libraries.di.AppScope | ||
import javax.inject.Inject | ||
|
||
interface UnifiedPushGatewayUrlResolver { | ||
fun resolve( | ||
gatewayResult: UnifiedPushGatewayResolverResult, | ||
instance: String, | ||
): String | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class DefaultUnifiedPushGatewayUrlResolver @Inject constructor( | ||
private val unifiedPushStore: UnifiedPushStore, | ||
) : UnifiedPushGatewayUrlResolver { | ||
override fun resolve( | ||
gatewayResult: UnifiedPushGatewayResolverResult, | ||
instance: String, | ||
): String { | ||
return when (gatewayResult) { | ||
is UnifiedPushGatewayResolverResult.Error -> { | ||
// Use previous gateway if any, or the provided one | ||
unifiedPushStore.getPushGateway(instance) ?: gatewayResult.gateway | ||
} | ||
UnifiedPushGatewayResolverResult.ErrorInvalidUrl, | ||
UnifiedPushGatewayResolverResult.NoMatrixGateway -> { | ||
UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL | ||
} | ||
is UnifiedPushGatewayResolverResult.Success -> { | ||
gatewayResult.gateway | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...t/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayUrlResolverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.unifiedpush | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
class DefaultUnifiedPushGatewayUrlResolverTest { | ||
@Test | ||
fun `resolve ErrorInvalidUrl returns the default gateway`() { | ||
val sut = createDefaultUnifiedPushGatewayUrlResolver() | ||
val result = sut.resolve( | ||
gatewayResult = UnifiedPushGatewayResolverResult.ErrorInvalidUrl, | ||
instance = "", | ||
) | ||
assertThat(result).isEqualTo(UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL) | ||
} | ||
|
||
@Test | ||
fun `resolve NoMatrixGateway returns the default gateway`() { | ||
val sut = createDefaultUnifiedPushGatewayUrlResolver() | ||
val result = sut.resolve( | ||
gatewayResult = UnifiedPushGatewayResolverResult.NoMatrixGateway, | ||
instance = "", | ||
) | ||
assertThat(result).isEqualTo(UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL) | ||
} | ||
|
||
@Test | ||
fun `resolve Success returns the url`() { | ||
val sut = createDefaultUnifiedPushGatewayUrlResolver() | ||
val result = sut.resolve( | ||
gatewayResult = UnifiedPushGatewayResolverResult.Success("aUrl"), | ||
instance = "", | ||
) | ||
assertThat(result).isEqualTo("aUrl") | ||
} | ||
|
||
@Test | ||
fun `resolve Error returns the current url when available`() { | ||
val sut = createDefaultUnifiedPushGatewayUrlResolver( | ||
unifiedPushStore = FakeUnifiedPushStore( | ||
getPushGatewayResult = { instance -> | ||
assertThat(instance).isEqualTo("instance") | ||
"aCurrentUrl" | ||
}, | ||
) | ||
) | ||
val result = sut.resolve( | ||
gatewayResult = UnifiedPushGatewayResolverResult.Error("aUrl"), | ||
instance = "instance", | ||
) | ||
assertThat(result).isEqualTo("aCurrentUrl") | ||
} | ||
|
||
@Test | ||
fun `resolve Error returns the url if no current url is available`() { | ||
val sut = createDefaultUnifiedPushGatewayUrlResolver( | ||
unifiedPushStore = FakeUnifiedPushStore( | ||
getPushGatewayResult = { instance -> | ||
assertThat(instance).isEqualTo("instance") | ||
null | ||
}, | ||
) | ||
) | ||
val result = sut.resolve( | ||
gatewayResult = UnifiedPushGatewayResolverResult.Error("aUrl"), | ||
instance = "instance", | ||
) | ||
assertThat(result).isEqualTo("aUrl") | ||
} | ||
|
||
private fun createDefaultUnifiedPushGatewayUrlResolver( | ||
unifiedPushStore: UnifiedPushStore = FakeUnifiedPushStore(), | ||
) = DefaultUnifiedPushGatewayUrlResolver( | ||
unifiedPushStore = unifiedPushStore, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
.../element/android/libraries/pushproviders/unifiedpush/FakeUnifiedPushGatewayUrlResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2024 New Vector Ltd. | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Please see LICENSE in the repository root for full details. | ||
*/ | ||
|
||
package io.element.android.libraries.pushproviders.unifiedpush | ||
|
||
import io.element.android.tests.testutils.lambda.lambdaError | ||
|
||
class FakeUnifiedPushGatewayUrlResolver( | ||
private val resolveResult: (UnifiedPushGatewayResolverResult, String) -> String = { _, _ -> lambdaError() }, | ||
) : UnifiedPushGatewayUrlResolver { | ||
override fun resolve(gatewayResult: UnifiedPushGatewayResolverResult, instance: String): String { | ||
return resolveResult(gatewayResult, instance) | ||
} | ||
} |
Oops, something went wrong.