Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicates for other client identifiers #893

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=5.0.7-SNAPSHOT
VERSION_NAME=5.0.8-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
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
Expand Up @@ -449,6 +449,7 @@ public static class BARCODE {

public static class PREF_KEY {
public static final String SETTINGS = "settings";
public static final String DUPLICATE_IDS_FIXED = "duplicate-ids-fixed";
}

public static class PeerToPeer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,66 @@
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.domain.DuplicateZeirIdStatus;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.util.AppHealthUtils;

import java.util.concurrent.TimeUnit;

import timber.log.Timber;

public class DuplicateCleanerWorker extends Worker {
private Context mContext;

public static final String TAG = "DuplicateCleanerWorker";

public DuplicateCleanerWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
mContext = context;
}

public static boolean shouldSchedule() {
AllSharedPreferences allSharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences();
return !allSharedPreferences.getBooleanPreference(AllConstants.PREF_KEY.DUPLICATE_IDS_FIXED);
}

/**
* Schedule this job to run periodically
*
* @param context
* @param mins - Duration after which the job repeatedly runs. This should be at least 15 mins
*/
public static void schedulePeriodically(Context context, int mins) {
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(DuplicateCleanerWorker.class, mins, TimeUnit.MINUTES)
.build();

WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
}

@NonNull
@Override
public Result doWork() {
DuplicateZeirIdStatus duplicateZeirIdStatus = AppHealthUtils.cleanUniqueZeirIds();
Timber.i("Doing some cleaning work");
if (duplicateZeirIdStatus != null && duplicateZeirIdStatus.equals(DuplicateZeirIdStatus.CLEANED))
AllSharedPreferences allSharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences();

if (!allSharedPreferences.getBooleanPreference(AllConstants.PREF_KEY.DUPLICATE_IDS_FIXED)) {
DuplicateZeirIdStatus duplicateZeirIdStatus = AppHealthUtils.cleanUniqueZeirIds();
Timber.i("Started doing duplicate client-identifier cleanup");
if (duplicateZeirIdStatus != null && duplicateZeirIdStatus.equals(DuplicateZeirIdStatus.CLEANED)) {
allSharedPreferences.saveBooleanPreference(AllConstants.PREF_KEY.DUPLICATE_IDS_FIXED, true);
WorkManager.getInstance(mContext).cancelWorkById(this.getId());
}
} else {
WorkManager.getInstance(mContext).cancelWorkById(this.getId());
}

return Result.success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ public boolean getBooleanPreference(String key) {
return preferences.getBoolean(key, false);
}

public boolean saveBooleanPreference(String key, boolean value) {
return preferences.edit().putBoolean(key, value).commit();
}

public void updateUrl(String baseUrl) {
try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -2404,7 +2405,12 @@ public DuplicateZeirIdStatus cleanDuplicateMotherIds() throws Exception {
String clientType = clientJson.getString(AllConstants.CLIENT_TYPE);

if (AllConstants.CHILD_TYPE.equals(clientType)) {
identifiers.put(ZEIR_ID, newZeirId.replaceAll("-", ""));
String identifierLabel = ZEIR_ID;
if (!identifiers.has(ZEIR_ID)) {
identifierLabel = ZEIR_ID.toLowerCase(Locale.ROOT);
}

identifiers.put(identifierLabel, newZeirId.replaceAll("-", ""));
} else if (AllConstants.Entity.MOTHER.equals(clientType)) {
identifiers.put(M_ZEIR_ID, newZeirId);
eventType = AllConstants.EventType.NEW_WOMAN_REGISTRATION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ZeirIdCleanupRepository extends BaseRepository {
private static final String DUPLICATES_SQL =
"WITH duplicates AS ( " +
" WITH clients AS ( " +
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID')) zeir_id " +
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID'), json_extract(json, '$.identifiers.zeir_id'), json_extract(json, '$.identifiers.ANC_ID')) zeir_id " +
" FROM client " +
" ) " +
" SELECT b.* FROM (SELECT baseEntityId, zeir_id FROM clients GROUP BY zeir_id HAVING count(zeir_id) > 1) a " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.smartregister.job;

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 com.google.common.util.concurrent.ListenableFuture;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
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.
*/

public class DuplicateCleanerWorkerTest extends BaseRobolectricUnitTest {

@Before
public void setUp() throws Exception {
//super.setUp();
initCoreLibrary();
initializeWorkManager();
}

@Test
public void shouldScheduleShouldReturnFalseAndCallDuplicateIdsFixed() {
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences());
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");
}

@Test
public void schedulePeriodicallyShouldScheduleJobInWorkManager() throws ExecutionException, InterruptedException {
Context context = RuntimeEnvironment.application;
DuplicateCleanerWorker.schedulePeriodically(context, 15);

ListenableFuture<List<WorkInfo>> listenableFuture = WorkManager.getInstance(context)
.getWorkInfosForUniqueWork(DuplicateCleanerWorker.TAG);

Assert.assertEquals(1, listenableFuture.get().size());
}

@Test
public void doWorkShouldReturnSuccess() {
DuplicateCleanerWorker duplicateCleanerWorker = TestWorkerBuilder.from(RuntimeEnvironment.application, DuplicateCleanerWorker.class)
.build();
Assert.assertEquals(ListenableWorker.Result.success(), duplicateCleanerWorker.doWork());
}

@Test
public void doWorkShouldCallCleanDuplicateUniqueZeirIdsWhenDuplicateIdsFixedPreferenceIsFalse() throws Exception {
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences);
Mockito.doReturn(false).when(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed");

EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository);
Mockito.doReturn(DuplicateZeirIdStatus.PENDING).when(eventClientRepository).cleanDuplicateMotherIds();

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() throws Exception {
AllSharedPreferences allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().userService().getAllSharedPreferences());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences);
Mockito.doReturn(false).when(allSharedPreferences).getBooleanPreference("duplicate-ids-fixed");

EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository);
Mockito.doReturn(DuplicateZeirIdStatus.CLEANED).when(eventClientRepository).cleanDuplicateMotherIds();

//WorkManagerInitializer
DuplicateCleanerWorker duplicateCleanerWorker = TestWorkerBuilder.from(RuntimeEnvironment.application, DuplicateCleanerWorker.class)
.build();
Assert.assertEquals(ListenableWorker.Result.success(), duplicateCleanerWorker.doWork());

Mockito.verify(eventClientRepository).cleanDuplicateMotherIds();
Mockito.verify(allSharedPreferences).saveBooleanPreference("duplicate-ids-fixed", true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,17 @@ public void testFetchTransactionsKilledFlag() {

assertTrue(allSharedPreferences.fetchTransactionsKilledFlag());
}

@Test
public void saveBooleanPreferenceShouldCallSharedPreferencesEdit() {
SharedPreferences.Editor editor = Mockito.mock(SharedPreferences.Editor.class);
Mockito.doReturn(editor).when(preferences).edit();
Mockito.doReturn(true).when(editor).commit();
Mockito.doReturn(editor).when(editor).putBoolean(Mockito.anyString(), Mockito.anyBoolean());

allSharedPreferences.saveBooleanPreference("is-logged-in", true);

Mockito.verify(preferences, Mockito.times(1)).edit();
Mockito.verify(editor).putBoolean("is-logged-in", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public MatrixCursor getCursorMaxRowId() {
public void testCleanDuplicateMotherIdsShouldFixAndMarkDuplicateClientsUnSynced() throws Exception {
String DUPLICATES_SQL = "WITH duplicates AS ( " +
" WITH clients AS ( " +
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID')) zeir_id " +
" SELECT baseEntityId, COALESCE(json_extract(json, '$.identifiers.ZEIR_ID'), json_extract(json, '$.identifiers.M_ZEIR_ID'), json_extract(json, '$.identifiers.zeir_id'), json_extract(json, '$.identifiers.ANC_ID')) zeir_id " +
" FROM client " +
" ) " +
" SELECT b.* FROM (SELECT baseEntityId, zeir_id FROM clients GROUP BY zeir_id HAVING count(zeir_id) > 1) a " +
Expand All @@ -766,6 +766,7 @@ public void testCleanDuplicateMotherIdsShouldFixAndMarkDuplicateClientsUnSynced(
DuplicateZeirIdStatus duplicateZeirIdStatus = eventClientRepository.cleanDuplicateMotherIds();
Assert.assertEquals(DuplicateZeirIdStatus.CLEANED, duplicateZeirIdStatus);
verify(sqliteDatabase, times(1)).rawQuery(eq(DUPLICATES_SQL), any());
verify(sqliteDatabase, times(2)).rawQuery(eq("SELECT COUNT (*) FROM unique_ids WHERE status=?"), any());
verify(sqliteDatabase, times(1)).insert(eq("client"), eq(null), any());
}

Expand Down
5 changes: 5 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ repositories {
apply plugin: 'com.android.application'
apply plugin: 'org.smartregister.gradle.jarjar'


configurations.all {
all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}

android {

compileSdkVersion androidCompileSdkVersion
Expand Down