From ddba45622c1ec934b18a8ba9f240816322d6106b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 2 Jan 2023 14:48:31 +0100 Subject: [PATCH] DataSyncWorkerTest --- plugins/sync/build.gradle | 1 + .../sync/nsclientV3/workers/DataSyncWorker.kt | 2 + .../nsclientV3/workers/DataSyncWorkerTest.kt | 63 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorkerTest.kt diff --git a/plugins/sync/build.gradle b/plugins/sync/build.gradle index 17613b56ae..0d225d45d9 100644 --- a/plugins/sync/build.gradle +++ b/plugins/sync/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation project(':core:utils') implementation project(':core:validators') + testImplementation "androidx.work:work-testing:$work_version" testImplementation project(':implementation') // NSClient, Tidepool diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorker.kt index dcd0f6dcef..b81a7b4055 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorker.kt @@ -2,11 +2,13 @@ package info.nightscout.plugins.sync.nsclientV3.workers import android.content.Context import androidx.work.WorkerParameters +import info.nightscout.androidaps.annotations.OpenForTesting import info.nightscout.core.utils.worker.LoggingWorker import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.sync.DataSyncSelector import javax.inject.Inject +@OpenForTesting class DataSyncWorker( context: Context, params: WorkerParameters ) : LoggingWorker(context, params) { diff --git a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorkerTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorkerTest.kt new file mode 100644 index 0000000000..059a83566c --- /dev/null +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/workers/DataSyncWorkerTest.kt @@ -0,0 +1,63 @@ +package info.nightscout.plugins.sync.nsclientV3.workers + +import android.content.Context +import androidx.work.ListenableWorker.Result.Success +import androidx.work.testing.TestListenableWorkerBuilder +import dagger.android.AndroidInjector +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.TestBase +import info.nightscout.core.utils.fabric.FabricPrivacy +import info.nightscout.interfaces.plugin.ActivePlugin +import info.nightscout.interfaces.sync.DataSyncSelector +import info.nightscout.interfaces.sync.NsClient +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.`when` + +internal class DataSyncWorkerTest : TestBase() { + + abstract class ContextWithInjector : Context(), HasAndroidInjector + + @Mock lateinit var fabricPrivacy: FabricPrivacy + @Mock lateinit var dataSyncSelector: DataSyncSelector + @Mock lateinit var context: ContextWithInjector + @Mock lateinit var activePlugin: ActivePlugin + @Mock lateinit var nsClient: NsClient + + private lateinit var sut: DataSyncWorker + + private val injector = HasAndroidInjector { + AndroidInjector { + if (it is DataSyncWorker) { + it.aapsLogger = aapsLogger + it.fabricPrivacy = fabricPrivacy + it.dataSyncSelector = dataSyncSelector + it.activePlugin = activePlugin + } + } + } + + @BeforeEach + fun prepare() { + `when`(context.applicationContext).thenReturn(context) + `when`(context.androidInjector()).thenReturn(injector.androidInjector()) + `when`(activePlugin.activeNsClient).thenReturn(nsClient) + } + + @Test + fun doWorkAndLog() { + sut = TestListenableWorkerBuilder(context).build() + `when`(nsClient.hasWritePermission).thenReturn(false) + sut.doWorkAndLog() + Mockito.verify(dataSyncSelector, Mockito.times(0)).doUpload() + + `when`(nsClient.hasWritePermission).thenReturn(true) + val result = sut.doWorkAndLog() + Mockito.verify(dataSyncSelector, Mockito.times(1)).doUpload() + Assertions.assertTrue(result is Success) + + } +} \ No newline at end of file