diff --git a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/id/QualifiedId.kt b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/id/QualifiedId.kt index a8f19d888f1..d6996ecb13f 100644 --- a/data/src/commonMain/kotlin/com/wire/kalium/logic/data/id/QualifiedId.kt +++ b/data/src/commonMain/kotlin/com/wire/kalium/logic/data/id/QualifiedId.kt @@ -33,7 +33,8 @@ data class QualifiedID( @SerialName("domain") val domain: String ) { - override fun toString(): String = if (domain.isEmpty()) value else "$value$VALUE_DOMAIN_SEPARATOR$domain" + override fun toString(): String = + if (domain.isEmpty()) value else "$value$VALUE_DOMAIN_SEPARATOR$domain" fun toLogString(): String = if (domain.isEmpty()) { value.obfuscateId() @@ -43,6 +44,16 @@ data class QualifiedID( fun toPlainID(): PlainId = PlainId(value) + /** + * This checks if the domain in either instances is blank. If it is, it will compare only the value. + * To be used when when of the instance do not have domain due to the API limitations. + */ + fun equalsIgnoringBlankDomain(other: QualifiedID): Boolean { + if (domain.isBlank() || other.domain.isBlank()) { + return value == other.value + } + return this == other + } } const val VALUE_DOMAIN_SEPARATOR = '@' diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/id/QualifiedIdTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/id/QualifiedIdTest.kt new file mode 100644 index 00000000000..15ddc78f92e --- /dev/null +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/id/QualifiedIdTest.kt @@ -0,0 +1,72 @@ +import com.wire.kalium.logic.data.id.QualifiedID +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + + +class QualifiedIdTest { + @Test + fun givenIdsWithoutDomains_whenEqualsIgnoringBlankDomain_thenReturnsTrue() { + // Given + val qualifiedId1 = QualifiedID("id1", "") + val qualifiedId2 = QualifiedID("id1", "") + + // When + val result = qualifiedId1.equalsIgnoringBlankDomain(qualifiedId2) + + // Then + assertTrue(result) + } + + @Test + fun givenOneIdWithoutDomain_whenEqualsIgnoringBlankDomain_thenReturnsTrue() { + // Given + val qualifiedId1 = QualifiedID("id1", "") + val qualifiedId2 = QualifiedID("id1", "domain") + + // When + val result = qualifiedId1.equalsIgnoringBlankDomain(qualifiedId2) + + // Then + assertTrue(result) + } + + @Test + fun givenIdsWithSameDomains_whenEqualsIgnoringBlankDomain_thenReturnsTrue() { + // Given + val qualifiedId1 = QualifiedID("id1", "domain") + val qualifiedId2 = QualifiedID("id1", "domain") + + // When + val result = qualifiedId1.equalsIgnoringBlankDomain(qualifiedId2) + + // Then + assertTrue(result) + } + + @Test + fun givenIdsWithDifferentDomains_whenEqualsIgnoringBlankDomain_thenReturnsFalse() { + // Given + val qualifiedId1 = QualifiedID("id1", "domain1") + val qualifiedId2 = QualifiedID("id1", "domain2") + + // When + val result = qualifiedId1.equalsIgnoringBlankDomain(qualifiedId2) + + // Then + assertTrue(!result) + } + + @Test + fun givenIdsWithDifferentValues_whenEqualsIgnoringBlankDomain_thenReturnsFalse() { + // Given + val qualifiedId1 = QualifiedID("id1", "") + val qualifiedId2 = QualifiedID("id2", "") + + // When + val result = qualifiedId1.equalsIgnoringBlankDomain(qualifiedId2) + + // Then + assertFalse(result) + } +} \ No newline at end of file