Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.simprints.infra.sync.config.usecase

import com.simprints.infra.config.store.models.ProjectConfiguration
import com.simprints.infra.enrolment.records.repository.EnrolmentRecordRepository
import com.simprints.infra.eventsync.EventSyncManager
import com.simprints.infra.sync.SyncOrchestrator
import javax.inject.Inject

internal class ResetLocalRecordsIfConfigChangedUseCase @Inject constructor(
private val syncOrchestrator: SyncOrchestrator,
private val eventSyncManager: EventSyncManager,
private val enrolmentRecordRepository: EnrolmentRecordRepository,
) {
suspend operator fun invoke(
oldConfig: ProjectConfiguration,
newConfig: ProjectConfiguration,
) {
if (hasPartitionTypeChanged(oldConfig, newConfig)) {
syncOrchestrator.cancelEventSync()
eventSyncManager.resetDownSyncInfo()
enrolmentRecordRepository.deleteAll()
syncOrchestrator.rescheduleEventSync()
}
}

private fun hasPartitionTypeChanged(
oldConfig: ProjectConfiguration,
newConfig: ProjectConfiguration,
) = oldConfig.synchronization.down.partitionType != newConfig.synchronization.down.partitionType
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.sync.ConfigManager
import com.simprints.infra.sync.config.usecase.HandleProjectStateUseCase
import com.simprints.infra.sync.config.usecase.RescheduleWorkersIfConfigChangedUseCase
import com.simprints.infra.sync.config.usecase.ResetLocalRecordsIfConfigChangedUseCase
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineDispatcher
Expand All @@ -22,6 +23,7 @@ internal class ProjectConfigDownSyncWorker @AssistedInject constructor(
private val configManager: ConfigManager,
private val handleProjectState: HandleProjectStateUseCase,
private val rescheduleWorkersIfConfigChanged: RescheduleWorkersIfConfigChangedUseCase,
private val resetLocalRecordsIfConfigChanged: ResetLocalRecordsIfConfigChangedUseCase,
@DispatcherBG private val dispatcher: CoroutineDispatcher,
) : SimCoroutineWorker(context, params) {
override val tag = "ProjectConfigDownSync"
Expand All @@ -40,7 +42,7 @@ internal class ProjectConfigDownSyncWorker @AssistedInject constructor(
val (project, config) = configManager.refreshProject(projectId)
handleProjectState(project.state)
rescheduleWorkersIfConfigChanged(oldConfig, config)

resetLocalRecordsIfConfigChanged(oldConfig, config)
success()
}
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.simprints.infra.sync.config.usecase

import com.simprints.infra.config.store.models.DownSynchronizationConfiguration
import com.simprints.infra.enrolment.records.repository.EnrolmentRecordRepository
import com.simprints.infra.eventsync.EventSyncManager
import com.simprints.infra.sync.SyncOrchestrator
import com.simprints.infra.sync.config.testtools.projectConfiguration
import com.simprints.infra.sync.config.testtools.synchronizationConfiguration
import io.mockk.*
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test

class ResetLocalRecordsIfConfigChangedUseCaseTest {
@MockK
private lateinit var syncOrchestrator: SyncOrchestrator

@MockK
private lateinit var eventSyncManager: EventSyncManager

@MockK
private lateinit var enrolmentRecordRepository: EnrolmentRecordRepository

private lateinit var useCase: ResetLocalRecordsIfConfigChangedUseCase

@Before
fun setUp() {
MockKAnnotations.init(this, relaxed = true)

useCase = ResetLocalRecordsIfConfigChangedUseCase(
syncOrchestrator = syncOrchestrator,
eventSyncManager = eventSyncManager,
enrolmentRecordRepository = enrolmentRecordRepository,
)
}

@Test
fun `should not reset local records when partition type not changes`() = runTest {
useCase(
projectConfiguration.copy(
synchronization = synchronizationConfiguration.copy(
down = synchronizationConfiguration.down.copy(
partitionType = DownSynchronizationConfiguration.PartitionType.MODULE,
),
),
),
projectConfiguration.copy(
synchronization = synchronizationConfiguration.copy(
down = synchronizationConfiguration.down.copy(
partitionType = DownSynchronizationConfiguration.PartitionType.MODULE,
),
),
),
)

coVerify(exactly = 0) {
syncOrchestrator.cancelEventSync()
syncOrchestrator.rescheduleEventSync()
eventSyncManager.resetDownSyncInfo()
enrolmentRecordRepository.deleteAll()
}
}

@Test
fun `should reset local records when partition type not changes`() = runTest {
useCase(
projectConfiguration.copy(
synchronization = synchronizationConfiguration.copy(
down = synchronizationConfiguration.down.copy(
partitionType = DownSynchronizationConfiguration.PartitionType.PROJECT,
),
),
),
projectConfiguration.copy(
synchronization = synchronizationConfiguration.copy(
down = synchronizationConfiguration.down.copy(
partitionType = DownSynchronizationConfiguration.PartitionType.MODULE,
),
),
),
)

coVerify {
syncOrchestrator.cancelEventSync()
syncOrchestrator.rescheduleEventSync()
eventSyncManager.resetDownSyncInfo()
enrolmentRecordRepository.deleteAll()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.simprints.infra.sync.config.testtools.project
import com.simprints.infra.sync.config.testtools.projectConfiguration
import com.simprints.infra.sync.config.usecase.HandleProjectStateUseCase
import com.simprints.infra.sync.config.usecase.RescheduleWorkersIfConfigChangedUseCase
import com.simprints.infra.sync.config.usecase.ResetLocalRecordsIfConfigChangedUseCase
import com.simprints.testtools.common.coroutines.TestCoroutineRule
import io.mockk.MockKAnnotations
import io.mockk.coEvery
Expand Down Expand Up @@ -37,6 +38,9 @@ class ProjectConfigDownSyncWorkerTest {
@MockK
private lateinit var rescheduleWorkersIfConfigChangedUseCase: RescheduleWorkersIfConfigChangedUseCase

@MockK
private lateinit var resetLocalRecordsIfConfigChangedUseCase: ResetLocalRecordsIfConfigChangedUseCase

private lateinit var projectConfigDownSyncWorker: ProjectConfigDownSyncWorker

@Before
Expand All @@ -50,6 +54,7 @@ class ProjectConfigDownSyncWorkerTest {
configManager = configManager,
handleProjectState = handleProjectStateUseCase,
rescheduleWorkersIfConfigChanged = rescheduleWorkersIfConfigChangedUseCase,
resetLocalRecordsIfConfigChanged = resetLocalRecordsIfConfigChangedUseCase,
dispatcher = testCoroutineRule.testCoroutineDispatcher,
)
}
Expand Down Expand Up @@ -85,6 +90,7 @@ class ProjectConfigDownSyncWorkerTest {
coVerify {
handleProjectStateUseCase.invoke(any())
rescheduleWorkersIfConfigChangedUseCase.invoke(any(), any())
resetLocalRecordsIfConfigChangedUseCase.invoke(any(), any())
}
}

Expand Down