Skip to content

Commit ae12eef

Browse files
committed
Add UnifiedAccountId
1 parent b82ffdd commit ae12eef

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.thunderbird.feature.account
2+
3+
import kotlin.uuid.ExperimentalUuidApi
4+
import kotlin.uuid.Uuid
5+
import net.thunderbird.core.architecture.model.Id
6+
7+
/**
8+
* Constant for the unified account ID.
9+
*
10+
* This ID is used to identify the unified account, which is a special account for aggregation purposes.
11+
*
12+
* The unified account ID is represented by a nil UUID (all zeros).
13+
*
14+
* See [RFC 4122 Section 4.1.7](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.7) for more details on nil UUIDs.
15+
*/
16+
@OptIn(ExperimentalUuidApi::class)
17+
val UnifiedAccountId: AccountId = Id(Uuid.NIL)
18+
19+
/**
20+
* Extension property to check if an [AccountId] is the unified account ID.
21+
*/
22+
val AccountId.isUnified: Boolean
23+
get() = this == UnifiedAccountId
24+
25+
/**
26+
* Ensures that the [AccountId] is not the unified account ID.
27+
*/
28+
fun AccountId.requireReal(): AccountId {
29+
check(!isUnified) { "Operation not allowed on unified account" }
30+
return this
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package net.thunderbird.feature.account
2+
3+
import assertk.assertFailure
4+
import assertk.assertThat
5+
import assertk.assertions.hasMessage
6+
import assertk.assertions.isEqualTo
7+
import assertk.assertions.isFalse
8+
import assertk.assertions.isTrue
9+
import kotlin.test.Test
10+
11+
class UnifiedAccountIdTest {
12+
13+
@Test
14+
fun `unified account id is nil uuid`() {
15+
assertThat(UnifiedAccountId.asRaw()).isEqualTo("00000000-0000-0000-0000-000000000000")
16+
}
17+
18+
@Test
19+
fun `isUnified returns true for unified account id`() {
20+
assertThat(UnifiedAccountId.isUnified).isTrue()
21+
}
22+
23+
@Test
24+
fun `isUnified returns false for non-unified account id`() {
25+
val nonUnifiedAccountId = AccountIdFactory.of("123e4567-e89b-12d3-a456-426614174000")
26+
assertThat(nonUnifiedAccountId.isUnified).isFalse()
27+
}
28+
29+
@Test
30+
fun `requireReal returns the same id if not unified`() {
31+
val nonUnifiedAccountId = AccountIdFactory.of("123e4567-e89b-12d3-a456-426614174000")
32+
assertThat(nonUnifiedAccountId.requireReal()).isEqualTo(nonUnifiedAccountId)
33+
}
34+
35+
@Test
36+
fun `requireReal throws exception if unified`() {
37+
assertFailure {
38+
UnifiedAccountId.requireReal()
39+
}.hasMessage("Operation not allowed on unified account")
40+
}
41+
}

0 commit comments

Comments
 (0)