-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 61 additions & 28 deletions
89
opensrp-core/src/test/java/org/smartregister/job/DuplicateCleanerWorkerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,118 @@ | ||
package org.smartregister.job; | ||
|
||
import android.content.SharedPreferences; | ||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import androidx.work.Configuration; | ||
import androidx.work.ListenableWorker; | ||
import androidx.work.WorkInfo; | ||
import androidx.work.WorkManager; | ||
import androidx.work.testing.SynchronousExecutor; | ||
import androidx.work.testing.TestWorkerBuilder; | ||
import androidx.work.testing.WorkManagerTestInitHelper; | ||
|
||
import junit.framework.TestCase; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.BaseRobolectricUnitTest; | ||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.domain.DuplicateZeirIdStatus; | ||
import org.smartregister.repository.AllSharedPreferences; | ||
import org.smartregister.repository.EventClientRepository; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - [email protected] on 02-11-2022. | ||
*/ | ||
@RunWith(RobolectricTestRunner.class) | ||
@Config(sdk = 27) | ||
public class DuplicateCleanerWorkerTest extends TestCase { | ||
|
||
public class DuplicateCleanerWorkerTest extends BaseRobolectricUnitTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
//super.setUp(); | ||
initCoreLibrary(); | ||
initializeWorkManager(); | ||
} | ||
|
||
@Test | ||
public void shouldScheduleShouldReturnCallDuplicateIdsFixed() { | ||
public void shouldScheduleShouldReturnFalseAndCallDuplicateIdsFixed() { | ||
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context().userService(), "allSharedPreferences", allSharedPreferences); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences); | ||
|
||
Mockito.doReturn(true).when(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed"); | ||
|
||
Assert.assertFalse(DuplicateCleanerWorker.shouldSchedule()); | ||
|
||
Mockito.verify(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed") | ||
Mockito.verify(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed"); | ||
} | ||
|
||
@Test | ||
public void schedulePeriodicallyShouldScheduleJobInWorkManager() { | ||
Context context = ; | ||
public void schedulePeriodicallyShouldScheduleJobInWorkManager() throws ExecutionException, InterruptedException { | ||
Context context = RuntimeEnvironment.application; | ||
DuplicateCleanerWorker.schedulePeriodically(context, 15); | ||
|
||
WorkManager.getInstance(context) | ||
.che | ||
ListenableFuture<List<WorkInfo>> listenableFuture = WorkManager.getInstance(context) | ||
.getWorkInfosForUniqueWork(DuplicateCleanerWorker.TAG); | ||
|
||
Assert.assertEquals(1, listenableFuture.get().size()); | ||
} | ||
|
||
@Test | ||
public void doWorkShouldReturnSuccess() { | ||
DuplicateCleanerWorker duplicateCleanerWorker = new DuplicateCleanerWorker(); | ||
DuplicateCleanerWorker duplicateCleanerWorker = TestWorkerBuilder.from(RuntimeEnvironment.application, DuplicateCleanerWorker.class) | ||
.build(); | ||
Assert.assertEquals(ListenableWorker.Result.success(), duplicateCleanerWorker.doWork()); | ||
} | ||
|
||
@Test | ||
public void doWorkShouldCallCleanDuplicateUniqueZeirIdsWhenDuplicateIdsFixedPreferenceIsFalse() { | ||
public void doWorkShouldCallCleanDuplicateUniqueZeirIdsWhenDuplicateIdsFixedPreferenceIsFalse() throws Exception { | ||
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context().userService(), "allSharedPreferences", allSharedPreferences); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences); | ||
Mockito.doReturn(false).when(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed"); | ||
|
||
EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().userService().getEventClientRepository()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context().userService(), "eventClientRepository", eventClientRepository); | ||
EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository); | ||
Mockito.doReturn(DuplicateZeirIdStatus.PENDING).when(eventClientRepository).cleanDuplicateMotherIds(); | ||
|
||
DuplicateCleanerWorker duplicateCleanerWorker = new DuplicateCleanerWorker(); | ||
DuplicateCleanerWorker duplicateCleanerWorker = TestWorkerBuilder.from(RuntimeEnvironment.application, DuplicateCleanerWorker.class) | ||
.build(); | ||
Assert.assertEquals(ListenableWorker.Result.success(), duplicateCleanerWorker.doWork()); | ||
|
||
Mockito.verify(eventClientRepository).cleanDuplicateMotherIds(); | ||
Mockito.verify(allSharedPreferences, Mockito.times(0)).saveBooleanPreference("duplicate-ids-fixed", true); | ||
} | ||
|
||
private void initializeWorkManager() { | ||
Configuration config = new Configuration.Builder() | ||
.setMinimumLoggingLevel(Log.DEBUG) | ||
.setExecutor(new SynchronousExecutor()) | ||
.build(); | ||
|
||
// Initialize WorkManager for instrumentation tests. | ||
WorkManagerTestInitHelper.initializeTestWorkManager( | ||
RuntimeEnvironment.application, config); | ||
} | ||
|
||
@Test | ||
public void doWorkShouldSetDuplicateIdsFixedPreferenceTrueWhenCleanUniqueZeirIdsReturnsCleaned() { | ||
public void doWorkShouldSetDuplicateIdsFixedPreferenceTrueWhenCleanUniqueZeirIdsReturnsCleaned() throws Exception { | ||
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context().userService(), "allSharedPreferences", allSharedPreferences); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences); | ||
Mockito.doReturn(false).when(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed"); | ||
|
||
EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().userService().getEventClientRepository()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context().userService(), "eventClientRepository", eventClientRepository); | ||
Mockito.doReturn(DuplicateZeirIdStatus.PENDING).when(eventClientRepository).cleanDuplicateMotherIds(); | ||
EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository); | ||
Mockito.doReturn(DuplicateZeirIdStatus.CLEANED).when(eventClientRepository).cleanDuplicateMotherIds(); | ||
|
||
DuplicateCleanerWorker duplicateCleanerWorker = new DuplicateCleanerWorker(); | ||
//WorkManagerInitializer | ||
DuplicateCleanerWorker duplicateCleanerWorker = TestWorkerBuilder.from(RuntimeEnvironment.application, DuplicateCleanerWorker.class) | ||
.build(); | ||
Assert.assertEquals(ListenableWorker.Result.success(), duplicateCleanerWorker.doWork()); | ||
|
||
Mockito.verify(eventClientRepository).cleanDuplicateMotherIds(); | ||
|