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,34 @@
package org.dhis2.mobile.sync.data

import org.hisp.dhis.android.core.D2

internal class SyncEventRepositoryImpl(
private val d2: D2,
) : SyncEventRepository {
override suspend fun uploadEvent(eventUid: String) {
d2
.eventModule()
.events()
.byUid()
.eq(eventUid)
.blockingUpload()
}

override suspend fun downloadEvent(eventUid: String) {
d2
.eventModule()
.eventDownloader()
.byUid()
.eq(eventUid)
.blockingDownload()
}

override suspend fun downloadFileResources(eventUid: String) {
d2
.fileResourceModule()
.fileResourceDownloader()
.byEventUid()
.eq(eventUid)
.blockingDownload()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.dhis2.mobile.sync.data

internal interface SyncEventRepository {
suspend fun uploadEvent(eventUid: String)

suspend fun downloadEvent(eventUid: String)

suspend fun downloadFileResources(eventUid: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.dhis2.mobile.sync.domain

import org.dhis2.mobile.commons.domain.UseCase
import org.dhis2.mobile.sync.data.SyncEventRepository

internal typealias EventUid = String

internal class SyncEvent(
private val syncEventRepository: SyncEventRepository,
) : UseCase<EventUid, Unit> {
override suspend fun invoke(input: EventUid): Result<Unit> =
try {
syncEventRepository.downloadEvent(input)
syncEventRepository.uploadEvent(input)
syncEventRepository.downloadFileResources(input)
Result.success(Unit)
} catch (e: Exception) {
Result.failure(e)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.dhis2.mobile.sync.domain

import kotlinx.coroutines.runBlocking
import org.dhis2.mobile.sync.data.SyncEventRepository
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import kotlin.test.assertTrue

class SyncEventTest {
private val repository: SyncEventRepository = mock()
private val useCase = SyncEvent(repository)

@Test
fun `should return success and call all repository methods in order`() =
runBlocking {
val eventUid = "eventUid"

val result = useCase(eventUid)

assertTrue(result.isSuccess)
verify(repository).downloadEvent(eventUid)
verify(repository).uploadEvent(eventUid)
verify(repository).downloadFileResources(eventUid)
}

@Test
fun `should return failure when downloadEvent throws`() =
runBlocking {
val eventUid = "eventUid"
whenever(repository.downloadEvent(eventUid)).thenThrow(RuntimeException("download error"))

val result = useCase(eventUid)

assertTrue(result.isFailure)
}

@Test
fun `should return failure when uploadEvent throws`() =
runBlocking {
val eventUid = "eventUid"
whenever(repository.uploadEvent(eventUid)).thenThrow(RuntimeException("upload error"))

val result = useCase(eventUid)

assertTrue(result.isFailure)
}

@Test
fun `should return failure when downloadFileResources throws`() =
runBlocking {
val eventUid = "eventUid"
whenever(repository.downloadFileResources(eventUid))
.thenThrow(RuntimeException("file resources error"))

val result = useCase(eventUid)

assertTrue(result.isFailure)
}
}