Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 5e1470ed authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

refactor(architecture): change IdFactory to proper factory pattern

parent dfab3f97
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ class DefaultAccountProfileLocalDataSourceTest {
    @Test
    fun `getById should return account profile`() = runTest {
        // arrange
        val accountId = AccountIdFactory.new()
        val accountId = AccountIdFactory.create()
        val legacyAccount = createLegacyAccount(accountId)
        val accountProfile = createAccountProfile(accountId)
        val testSubject = createTestSubject(legacyAccount)
@@ -41,7 +41,7 @@ class DefaultAccountProfileLocalDataSourceTest {
    @Test
    fun `getById should return null when account is not found`() = runTest {
        // arrange
        val accountId = AccountIdFactory.new()
        val accountId = AccountIdFactory.create()
        val testSubject = createTestSubject(null)

        // act & assert
@@ -53,7 +53,7 @@ class DefaultAccountProfileLocalDataSourceTest {
    @Test
    fun `update should save account profile`() = runTest {
        // arrange
        val accountId = AccountIdFactory.new()
        val accountId = AccountIdFactory.create()
        val legacyAccount = createLegacyAccount(accountId)
        val accountProfile = createAccountProfile(accountId)

+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ open class LegacyAccount(
) : Account, BaseAccount {

    // [Account]
    override val id: AccountId = AccountIdFactory.create(uuid)
    override val id: AccountId = AccountIdFactory.of(uuid)

    // [BaseAccount]
    @get:Synchronized
+2 −2
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import kotlin.uuid.Uuid
 */
@OptIn(ExperimentalUuidApi::class)
abstract class BaseIdFactory<T> : IdFactory<T> {
    override fun create(raw: String): Id<T> = Id(Uuid.parse(raw))
    override fun of(raw: String): Id<T> = Id(Uuid.parse(raw))

    override fun new(): Id<T> = Id(Uuid.random())
    override fun create(): Id<T> = Id(Uuid.random())
}
+5 −5
Original line number Diff line number Diff line
@@ -6,17 +6,17 @@ package net.thunderbird.core.architecture.model
interface IdFactory<T> {

    /**
     * Creates an ID from a raw string representation.
     * Creates an [Id] from a raw string representation.
     *
     * @param raw The raw string representation of the ID.
     * @return An instance of [Id] representing the ID.
     */
    fun create(raw: String): Id<T>
    fun of(raw: String): Id<T>

    /**
     * Generates a new ID.
     * Creates a new [Id].
     *
     * @return A new instance of [Id] representing the generated ID.
     * @return A new instance of [Id] representing the created ID.
     */
    fun new(): Id<T>
    fun create(): Id<T>
}
+4 −4
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ class AccountIdFactoryTest {
    fun `create should return AccountId with the same id`() {
        val id = "123e4567-e89b-12d3-a456-426614174000"

        val result = AccountIdFactory.create(id)
        val result = AccountIdFactory.of(id)

        assertThat(result.asRaw()).isEqualTo(id)
    }
@@ -27,7 +27,7 @@ class AccountIdFactoryTest {
        val id = "invalid"

        val result = assertFailure {
            AccountIdFactory.create(id)
            AccountIdFactory.of(id)
        }

        result.hasMessage(
@@ -39,14 +39,14 @@ class AccountIdFactoryTest {

    @Test
    fun `new should return AccountId with a uuid`() {
        val result = AccountIdFactory.new()
        val result = AccountIdFactory.create()

        assertThat(result.asRaw()).isUuid()
    }

    @Test
    fun `create should return AccountId with unique ids`() {
        val ids = List(10) { AccountIdFactory.new().asRaw() }
        val ids = List(10) { AccountIdFactory.create().asRaw() }

        ids.forEachIndexed { index, id ->
            ids.drop(index + 1).forEach { otherId ->
Loading