Skip to content

Commit

Permalink
Add DuplicateCleanerWorkerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Nov 3, 2022
1 parent d15c031 commit d5fd7ee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
1 change: 1 addition & 0 deletions opensrp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ dependencies {

def work_version = "2.7.1"
implementation "androidx.work:work-runtime:$work_version"
testImplementation "androidx.work:work-testing:$work_version"

// Add the dependency for the Performance Monitoring library
implementation 'com.google.firebase:firebase-perf:19.0.7'
Expand Down
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();
Expand Down

0 comments on commit d5fd7ee

Please sign in to comment.