From 757f4bb5a019d58685e5036322872faecb3fe394 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 16:09:40 +0300 Subject: [PATCH 01/11] :recycle: Move update null event Ids to Utils class --- .../chw/util/RepositoryUtils.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index 5deb177ca4..f3ec78198d 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -1,10 +1,20 @@ package org.smartregister.chw.util; +import static org.smartregister.repository.BaseRepository.TYPE_Synced; +import static org.smartregister.repository.BaseRepository.TYPE_Valid; + +import android.database.Cursor; import net.sqlcipher.database.SQLiteDatabase; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; +import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.application.CoreChwApplication; import org.smartregister.chw.core.utils.CoreConstants; +import org.smartregister.domain.Event; import org.smartregister.family.util.DBConstants; +import org.smartregister.repository.EventClientRepository; +import org.smartregister.sync.helper.ECSyncHelper; import org.smartregister.util.DatabaseMigrationUtils; import java.util.ArrayList; @@ -14,6 +24,9 @@ public interface RepositoryUtils { + String EVENT_ID = "id"; + String _ID = "_id"; + String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; @@ -50,4 +63,73 @@ static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { } } + static void updateNullEventIds(SQLiteDatabase db) { + List events = new ArrayList<>(); + String eventTableName = EventClientRepository.Table.event.name(); + String eventIdCol = EventClientRepository.event_column.eventId.name(); + String eventSyncStatusCol = EventClientRepository.event_column.syncStatus.name(); + String eventValidCol = EventClientRepository.event_column.validationStatus.name(); + String jsonCol = EventClientRepository.event_column.json.name(); + String formSubmissionCol = EventClientRepository.event_column.formSubmissionId.name(); + + Cursor cursor; + String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?"; + try { + cursor = db.query(eventTableName, new String[]{jsonCol}, + selection, new String[]{TYPE_Valid}, null, null, null); + events = readEvents(cursor); + } catch (Exception ex) { + Timber.e(ex); + } + String updateSQL; + for (Event event : events) { + updateSQL = String.format("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';", eventTableName, + eventIdCol, event.getEventId(), eventSyncStatusCol, TYPE_Synced, formSubmissionCol, event.getFormSubmissionId()); + try { + db.execSQL(updateSQL); + } catch (Exception e) { + Timber.e(e, "UpdateNullEventIds "); + } + } + } + + static List readEvents(Cursor cursor) { + List events = new ArrayList<>(); + ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); + try { + if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { + while (!cursor.isAfterLast()) { + String json = cursor.getString(cursor.getColumnIndex("json")); + Event event = syncHelper.convert(new JSONObject(json), Event.class); + event.setEventId(getEventId(json)); + events.add(event); + cursor.moveToNext(); + } + } + } catch (Exception e) { + Timber.e(e); + } finally { + cursor.close(); + } + return events; + } + + static String getEventId(String jsonString) { + JSONObject jsonObject; + String eventId = null; + if (StringUtils.isNotEmpty(jsonString)) { + try { + jsonObject = new JSONObject(jsonString); + if (jsonObject.has(EVENT_ID)) { + eventId = jsonObject.getString(EVENT_ID); + } else if (jsonObject.has(_ID)) { + eventId = jsonObject.getString(_ID); + } + } catch (Exception ex) { + Timber.e(ex); + } + } + return eventId; + } + } From e79dc5a8b73b56305ef4bfd58a491219f56bc222 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 16:11:16 +0300 Subject: [PATCH 02/11] :recycle: Refactor migration to update null event ids Use functionality introduced in Utils class --- .../chw/repository/ChwRepositoryFlv.java | 24 +++--- .../chw/repository/ChwRepositoryFlv.java | 80 +------------------ .../chw/repository/ChwRepositoryFlv.java | 79 +----------------- 3 files changed, 18 insertions(+), 165 deletions(-) diff --git a/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index dcefb40ea5..a7075c42af 100644 --- a/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/drc/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -45,6 +45,9 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, case 11: upgradeToVersion11(db); break; + case 12: + upgradeToVersion12(db); + break; default: break; } @@ -52,15 +55,6 @@ public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, } } - private static void upgradeToVersion11(SQLiteDatabase db) { - try { - db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;"); - } catch (Exception e) { - Timber.e(e, "upgradeToVersion11"); - } - } - - private static void upgradeToVersion2(Context context, SQLiteDatabase db) { try { db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_EVENT_ID_COL); @@ -159,4 +153,16 @@ private static void upgradeToVersion10(Context context, SQLiteDatabase db) { Timber.e(e); } } + + private static void upgradeToVersion11(SQLiteDatabase db) { + try { + db.execSQL("ALTER TABLE ec_family_member ADD COLUMN marital_status VARCHAR;"); + } catch (Exception e) { + Timber.e(e, "upgradeToVersion11"); + } + } + + private static void upgradeToVersion12(SQLiteDatabase db) { + RepositoryUtils.updateNullEventIds(db); + } } diff --git a/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index 539e9cb2cb..d0b52a8cc2 100644 --- a/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/liberia/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,19 +1,11 @@ package org.smartregister.chw.repository; -import static org.smartregister.repository.BaseRepository.TYPE_Synced; -import static org.smartregister.repository.BaseRepository.TYPE_Valid; - import android.content.Context; -import net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; -import org.apache.commons.lang3.StringUtils; -import org.json.JSONObject; import org.smartregister.chw.anc.repository.VisitRepository; -import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.util.RepositoryUtils; -import org.smartregister.domain.Event; import org.smartregister.domain.db.Column; import org.smartregister.immunization.repository.RecurringServiceRecordRepository; import org.smartregister.immunization.repository.VaccineRepository; @@ -21,18 +13,13 @@ import org.smartregister.reporting.ReportingLibrary; import org.smartregister.repository.AlertRepository; import org.smartregister.repository.EventClientRepository; -import org.smartregister.sync.helper.ECSyncHelper; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.List; import timber.log.Timber; public class ChwRepositoryFlv { - private static final String EVENT_ID = "id"; - private static final String _ID = "_id"; public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) { Timber.w(ChwRepository.class.getName(), @@ -165,71 +152,6 @@ private static void upgradeToVersion7(SQLiteDatabase db) { } private static void upgradeToVersion8(SQLiteDatabase db) { - List events = new ArrayList<>(); - String eventTableName = EventClientRepository.Table.event.name(); - String eventIdCol = EventClientRepository.event_column.eventId.name(); - String eventSyncStatusCol = EventClientRepository.event_column.syncStatus.name(); - String eventValidCol = EventClientRepository.event_column.validationStatus.name(); - String jsonCol = EventClientRepository.event_column.json.name(); - String formSubmissionCol = EventClientRepository.event_column.formSubmissionId.name(); - - Cursor cursor; - String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?"; - try { - cursor = db.query(eventTableName, new String[]{jsonCol}, - selection, new String[]{TYPE_Valid}, null, null, null); - events = readEvents(cursor); - } catch (Exception ex) { - Timber.e(ex); - } - String updateSQL; - for (Event event : events) { - updateSQL = String.format("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';", eventTableName, - eventIdCol, event.getEventId(), eventSyncStatusCol, TYPE_Synced, formSubmissionCol, event.getFormSubmissionId()); - try { - db.execSQL(updateSQL); - } catch (Exception e) { - Timber.e(e, "upgradeToVersion21 "); - } - } - } - - private static List readEvents(Cursor cursor) { - List events = new ArrayList<>(); - ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); - try { - if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - String json = cursor.getString(cursor.getColumnIndex("json")); - Event event = syncHelper.convert(new JSONObject(json), Event.class); - event.setEventId(getEventId(json)); - events.add(event); - cursor.moveToNext(); - } - } - } catch (Exception e) { - Timber.e(e); - } finally { - cursor.close(); - } - return events; - } - - private static String getEventId(String jsonString) { - JSONObject jsonObject; - String eventId = null; - if (StringUtils.isNotEmpty(jsonString)) { - try { - jsonObject = new JSONObject(jsonString); - if (jsonObject.has(EVENT_ID)) { - eventId = jsonObject.getString(EVENT_ID); - } else if (jsonObject.has(_ID)) { - eventId = jsonObject.getString(_ID); - } - } catch (Exception ex) { - Timber.e(ex); - } - } - return eventId; + RepositoryUtils.updateNullEventIds(db); } } \ No newline at end of file diff --git a/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index 7dcee515f0..e7fbba4475 100644 --- a/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/togo/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,23 +1,17 @@ package org.smartregister.chw.repository; -import static org.smartregister.repository.BaseRepository.TYPE_Synced; -import static org.smartregister.repository.BaseRepository.TYPE_Valid; - import android.content.Context; import android.database.Cursor; import net.sqlcipher.database.SQLiteDatabase; -import org.apache.commons.lang3.StringUtils; import org.joda.time.format.DateTimeFormat; -import org.json.JSONObject; import org.smartregister.chw.anc.AncLibrary; import org.smartregister.chw.anc.domain.Visit; import org.smartregister.chw.anc.domain.VisitDetail; import org.smartregister.chw.anc.repository.VisitDetailsRepository; import org.smartregister.chw.anc.repository.VisitRepository; import org.smartregister.chw.anc.util.NCUtils; -import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.core.application.CoreChwApplication; import org.smartregister.chw.core.repository.ScheduleRepository; import org.smartregister.chw.core.rule.PNCHealthFacilityVisitRule; @@ -42,7 +36,6 @@ import org.smartregister.reporting.ReportingLibrary; import org.smartregister.repository.AlertRepository; import org.smartregister.repository.EventClientRepository; -import org.smartregister.sync.helper.ECSyncHelper; import org.smartregister.util.DatabaseMigrationUtils; import java.util.ArrayList; @@ -55,9 +48,6 @@ public class ChwRepositoryFlv { - private static final String EVENT_ID = "id"; - private static final String _ID = "_id"; - public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) { Timber.w(ChwRepository.class.getName(), "Upgrading database from version " + oldVersion + " to " @@ -319,7 +309,7 @@ private static List getEvents(SQLiteDatabase db, String[] selectionArgs) try { String[] myStringArray = {"json"}; cursor = db.query(EventClientRepository.Table.event.name(), myStringArray, " eventType = ? ", selectionArgs, null, null, " eventDate ASC ", null); - events = readEvents(cursor); + events = RepositoryUtils.readEvents(cursor); } catch (Exception e) { Timber.e(e); } finally { @@ -330,45 +320,6 @@ private static List getEvents(SQLiteDatabase db, String[] selectionArgs) return events; } - private static List readEvents(Cursor cursor) { - List events = new ArrayList<>(); - ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); - try { - if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - String json = cursor.getString(cursor.getColumnIndex("json")); - Event event = syncHelper.convert(new JSONObject(json), Event.class); - event.setEventId(getEventId(json)); - events.add(event); - cursor.moveToNext(); - } - } - } catch (Exception e) { - Timber.e(e); - } finally { - cursor.close(); - } - return events; - } - - private static String getEventId(String jsonString) { - JSONObject jsonObject; - String eventId = null; - if (StringUtils.isNotEmpty(jsonString)) { - try { - jsonObject = new JSONObject(jsonString); - if (jsonObject.has(EVENT_ID)) { - eventId = jsonObject.getString(EVENT_ID); - }else if (jsonObject.has(_ID)) { - eventId = jsonObject.getString(_ID); - } - } catch (Exception ex) { - Timber.e(ex); - } - } - return eventId; - } - private static void processHFNextVisitDateObs(List events, SQLiteDatabase sqLiteDatabase) { // Save missing PNC Health Facility visit (next visit) details for (Event event : events) { @@ -471,32 +422,6 @@ private static void upgradeToVersion20(SQLiteDatabase db) { } private static void upgradeToVersion21(SQLiteDatabase db) { - List events = new ArrayList<>(); - String eventTableName = EventClientRepository.Table.event.name(); - String eventIdCol = EventClientRepository.event_column.eventId.name(); - String eventSyncStatusCol = EventClientRepository.event_column.syncStatus.name(); - String eventValidCol = EventClientRepository.event_column.validationStatus.name(); - String jsonCol = EventClientRepository.event_column.json.name(); - String formSubmissionCol = EventClientRepository.event_column.formSubmissionId.name(); - - Cursor cursor; - String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?"; - try { - cursor = db.query(eventTableName, new String[]{jsonCol}, - selection, new String[]{TYPE_Valid}, null, null, null); - events = readEvents(cursor); - } catch (Exception ex) { - Timber.e(ex); - } - String updateSQL; - for (Event event : events) { - updateSQL = String.format("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';", eventTableName, - eventIdCol, event.getEventId(), eventSyncStatusCol, TYPE_Synced, formSubmissionCol, event.getFormSubmissionId()); - try { - db.execSQL(updateSQL); - } catch (Exception e) { - Timber.e(e, "upgradeToVersion21 "); - } - } + RepositoryUtils.updateNullEventIds(db); } } From 41b2b20ad80913906e7a371dcf9c9f5a8988b01d Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 16:12:15 +0300 Subject: [PATCH 03/11] :camera_flash: Update DRC to v1.0.16 and DB to v12 --- opensrp-chw/build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle index ed5abd487a..2c9b7dd756 100644 --- a/opensrp-chw/build.gradle +++ b/opensrp-chw/build.gradle @@ -255,8 +255,8 @@ android { resConfigs "en", "fr" dimension = 'baseDimension' applicationIdSuffix ".drc" - versionCode 10 - versionName "1.0.15" + versionCode 11 + versionName "1.0.16" buildConfigField "String", 'opensrp_url', '"https://wcaro-cd.smartregister.org/opensrp/"' buildConfigField "String", 'guidebooks_url', '"https://opensrp.s3.amazonaws.com/media/drc/"' buildConfigField "String", 'opensrp_url_preview', '"https://wcaro-cd-preview.smartregister.org/opensrp/"' @@ -268,7 +268,7 @@ android { buildConfigField "String", 'DEFAULT_LOCATION', '"VILLAGE/COMMUNAUTE"' buildConfigField "int", "MAX_CONNECTION_TIMEOUT", '5' buildConfigField "int", "MAX_READ_TIMEOUT", '5' - buildConfigField "int", "DATABASE_VERSION", '11' + buildConfigField "int", "DATABASE_VERSION", '12' } guinea { dimension = 'baseDimension' From 1bed3f10e9c8f77b620e982af8f05a5fb4a87792 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 16:13:42 +0300 Subject: [PATCH 04/11] :recycle: Refactor update null event Id CHAD migration Reuse functionality in Utils --- .../chw/repository/ChwRepositoryFlv.java | 81 +------------------ 1 file changed, 1 insertion(+), 80 deletions(-) diff --git a/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java b/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java index 16879b2df8..f50fdad831 100644 --- a/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java +++ b/opensrp-chw/src/chad/java/org/smartregister/chw/repository/ChwRepositoryFlv.java @@ -1,20 +1,12 @@ package org.smartregister.chw.repository; -import static org.smartregister.repository.BaseRepository.TYPE_Synced; -import static org.smartregister.repository.BaseRepository.TYPE_Valid; - import android.content.Context; -import net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; -import org.apache.commons.lang3.StringUtils; -import org.json.JSONObject; import org.smartregister.chw.anc.repository.VisitRepository; -import org.smartregister.chw.application.ChwApplication; import org.smartregister.chw.util.ChildDBConstants; import org.smartregister.chw.util.RepositoryUtils; -import org.smartregister.domain.Event; import org.smartregister.domain.db.Column; import org.smartregister.immunization.repository.RecurringServiceRecordRepository; import org.smartregister.immunization.repository.VaccineRepository; @@ -22,20 +14,15 @@ import org.smartregister.reporting.ReportingLibrary; import org.smartregister.repository.AlertRepository; import org.smartregister.repository.EventClientRepository; -import org.smartregister.sync.helper.ECSyncHelper; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.List; import timber.log.Timber; public class ChwRepositoryFlv { private static final String TAG = ChwRepositoryFlv.class.getCanonicalName(); - private static final String EVENT_ID = "id"; - private static final String _ID = "_id"; public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) { Timber.w(ChwRepository.class.getName(), @@ -98,7 +85,6 @@ private static void upgradeToVersion14(SQLiteDatabase db) { } } - private static void upgradeToVersion2(Context context, SQLiteDatabase db) { try { db.execSQL(VaccineRepository.UPDATE_TABLE_ADD_EVENT_ID_COL); @@ -225,72 +211,7 @@ private static void upgradeToVersion11(Context context, SQLiteDatabase db) { } private static void upgradeToVersion15(SQLiteDatabase db) { - List events = new ArrayList<>(); - String eventTableName = EventClientRepository.Table.event.name(); - String eventIdCol = EventClientRepository.event_column.eventId.name(); - String eventSyncStatusCol = EventClientRepository.event_column.syncStatus.name(); - String eventValidCol = EventClientRepository.event_column.validationStatus.name(); - String jsonCol = EventClientRepository.event_column.json.name(); - String formSubmissionCol = EventClientRepository.event_column.formSubmissionId.name(); - - Cursor cursor; - String selection = eventIdCol + " IS NULL AND " + eventValidCol + " = ?"; - try { - cursor = db.query(eventTableName, new String[]{jsonCol}, - selection, new String[]{TYPE_Valid}, null, null, null); - events = readEvents(cursor); - } catch (Exception ex) { - Timber.e(ex); - } - String updateSQL; - for (Event event : events) { - updateSQL = String.format("UPDATE %s SET %s = '%s', %s = '%s' WHERE %s = '%s';", eventTableName, - eventIdCol, event.getEventId(), eventSyncStatusCol, TYPE_Synced, formSubmissionCol, event.getFormSubmissionId()); - try { - db.execSQL(updateSQL); - } catch (Exception e) { - Timber.e(e, "upgradeToVersion21 "); - } - } - } - - private static List readEvents(Cursor cursor) { - List events = new ArrayList<>(); - ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); - try { - if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - String json = cursor.getString(cursor.getColumnIndex("json")); - Event event = syncHelper.convert(new JSONObject(json), Event.class); - event.setEventId(getEventId(json)); - events.add(event); - cursor.moveToNext(); - } - } - } catch (Exception e) { - Timber.e(e); - } finally { - cursor.close(); - } - return events; - } - - private static String getEventId(String jsonString) { - JSONObject jsonObject; - String eventId = null; - if (StringUtils.isNotEmpty(jsonString)) { - try { - jsonObject = new JSONObject(jsonString); - if (jsonObject.has(EVENT_ID)) { - eventId = jsonObject.getString(EVENT_ID); - } else if (jsonObject.has(_ID)) { - eventId = jsonObject.getString(_ID); - } - } catch (Exception ex) { - Timber.e(ex); - } - } - return eventId; + RepositoryUtils.updateNullEventIds(db); } private static void initializeIndicatorDefinitions(ReportingLibrary reportingLibrary, SQLiteDatabase sqLiteDatabase) { From cd92f363728b475dc364fcddb9765bb91f8fe60a Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 18:55:58 +0300 Subject: [PATCH 05/11] :white_check_mark: Add RepositoryUtils updateNullEventIds test --- .../chw/util/RepositoryUtils.java | 4 +- .../chw/util/RepositoryUtilsTest.java | 134 ++++++++++++++++++ 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index f3ec78198d..abcb2e487f 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -79,7 +79,7 @@ static void updateNullEventIds(SQLiteDatabase db) { selection, new String[]{TYPE_Valid}, null, null, null); events = readEvents(cursor); } catch (Exception ex) { - Timber.e(ex); + Timber.e(ex, "Problem getting events "); } String updateSQL; for (Event event : events) { @@ -88,7 +88,7 @@ static void updateNullEventIds(SQLiteDatabase db) { try { db.execSQL(updateSQL); } catch (Exception e) { - Timber.e(e, "UpdateNullEventIds "); + Timber.e(e, "Problem executing update "); } } } diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java new file mode 100644 index 0000000000..7b6b3a65a6 --- /dev/null +++ b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java @@ -0,0 +1,134 @@ +package org.smartregister.chw.util; + +import net.sqlcipher.MatrixCursor; +import net.sqlcipher.database.SQLiteDatabase; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.smartregister.chw.application.ChwApplication; +import org.smartregister.repository.EventClientRepository; + +@RunWith(RobolectricTestRunner.class) +@Config(application = ChwApplication.class) +public class RepositoryUtilsTest { + + @Mock + private SQLiteDatabase database; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void updateNullEventIdsUpdatesCorrectEvents() { + MatrixCursor matrixCursor = new MatrixCursor(new String[]{"json"}); + matrixCursor.addRow(new Object[]{getSampleEventJSONString()}); + Mockito.doReturn(matrixCursor).when(database).query(ArgumentMatchers.eq(EventClientRepository.Table.event.name()), + ArgumentMatchers.any(String[].class), ArgumentMatchers.eq("eventId IS NULL AND validationStatus = ?"), + ArgumentMatchers.any(String[].class), ArgumentMatchers.isNull(), ArgumentMatchers.isNull(), ArgumentMatchers.isNull()); + RepositoryUtils.updateNullEventIds(database); + Mockito.verify(database).execSQL("UPDATE event SET eventId = '3b598b80-13ee-4a9a-8cd1-8e66fa76bbe9', " + + "syncStatus = 'Synced' WHERE formSubmissionId = '45a294f5-ec2f-4233-847a-6f7910a6e63f';"); + } + + private static String getSampleEventJSONString() { + return "{\n" + + "\n" + + " baseEntityId: \"158cb2d0-a78f-4087-9ccc-0a0de9724548\",\n" + + " childLocationId: \"Nairobi\",\n" + + " duration: 0,\n" + + " entityType: \"ec_family_member\",\n" + + " eventDate: \"2020-12-20T00:00:00.000Z\",\n" + + " eventType: \"Family Member Registration\",\n" + + " formSubmissionId: \"45a294f5-ec2f-4233-847a-6f7910a6e63f\",\n" + + " locationId: \"bd657bc4-fad6-40b1-bdc4-38a8f1ddfdc4\",\n" + + " obs: [\n" + + " {\n" + + " fieldCode: \"\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"surname\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"Jeff\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"fam_name\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"Koinange\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"dob_unknown\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"formsubmissionField\",\n" + + " formSubmissionField: \"dob_unknown\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"true\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"last_interacted_with\",\n" + + " fieldDataType: \"text\",\n" + + " fieldType: \"formsubmissionField\",\n" + + " formSubmissionField: \"last_interacted_with\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"1608491344580\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\n" + + " fieldDataType: \"start\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"start\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"2020-12-20 19:08:16\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " fieldCode: \"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\n" + + " fieldDataType: \"end\",\n" + + " fieldType: \"concept\",\n" + + " formSubmissionField: \"end\",\n" + + " humanReadableValues: [],\n" + + " parentCode: \"\",\n" + + " values: [\n" + + " \"2020-12-20 19:09:04\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + " providerId: \"onatester\",\n" + + " team: \"ONA\",\n" + + " teamId: \"127edf8b-d021-41b9-a74a-71896397ed42\",\n" + + " version: 1608491344583,\n" + + " clientApplicationVersion: 13,\n" + + " clientDatabaseVersion: 15,\n" + + " dateCreated: \"2020-12-20T19:09:04.583Z\",\n" + + " type: \"Event\",\n" + + "_id: \"3b598b80-13ee-4a9a-8cd1-8e66fa76bbe9\",\n" + + "_rev: \"v1\"" + + "}"; + } + +} From abff799ecde0dc603e9cee745e1e2dc11bb78b21 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 20:26:47 +0300 Subject: [PATCH 06/11] :white_check_mark: Update RepositoryUtilsTest Add sdk version config --- .../java/org/smartregister/chw/util/RepositoryUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java index 7b6b3a65a6..598fbb6163 100644 --- a/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java +++ b/opensrp-chw/src/test/java/org/smartregister/chw/util/RepositoryUtilsTest.java @@ -16,7 +16,7 @@ import org.smartregister.repository.EventClientRepository; @RunWith(RobolectricTestRunner.class) -@Config(application = ChwApplication.class) +@Config(application = ChwApplication.class, sdk = 22) public class RepositoryUtilsTest { @Mock From 5987677321548c5401245558f7aa51a1fafbeeae Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 21:42:53 +0300 Subject: [PATCH 07/11] :recycle: Refactor RepositoryUtils interface Converted it to a class --- .../chw/util/RepositoryUtils.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index abcb2e487f..35ca8fc63f 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -22,32 +22,32 @@ import timber.log.Timber; -public interface RepositoryUtils { +public class RepositoryUtils { - String EVENT_ID = "id"; - String _ID = "_id"; + public static final String EVENT_ID = "id"; + public static final String _ID = "_id"; - String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; - String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; - String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; + public static String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; + public static String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; + public static String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; - String[] UPDATE_REPOSITORY_TYPES = { + public static String[] UPDATE_REPOSITORY_TYPES = { "UPDATE recurring_service_types SET service_group = 'woman' WHERE type = 'IPTp-SP';", "UPDATE recurring_service_types SET service_group = 'child' WHERE type != 'IPTp-SP';", }; - String[] UPGRADE_V10 = { + public static String[] UPGRADE_V10 = { "ALTER TABLE ec_child ADD COLUMN mother_entity_id VARCHAR;", "ALTER TABLE ec_child ADD COLUMN entry_point VARCHAR;" }; - String DELETE_DUPLICATE_SCHEDULES = "delete from schedule_service where id not in ( " + + public static String DELETE_DUPLICATE_SCHEDULES = "delete from schedule_service where id not in ( " + "select max(id) from schedule_service " + "group by base_entity_id , schedule_group_name , schedule_name " + "having count(*) > 1 " + ")"; - static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { + public static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { try { db.execSQL("ALTER TABLE ec_family ADD COLUMN entity_type VARCHAR; " + @@ -63,7 +63,7 @@ static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { } } - static void updateNullEventIds(SQLiteDatabase db) { + public static void updateNullEventIds(SQLiteDatabase db) { List events = new ArrayList<>(); String eventTableName = EventClientRepository.Table.event.name(); String eventIdCol = EventClientRepository.event_column.eventId.name(); From f87916ab35f707752983c4fee39ed15ff48f9a50 Mon Sep 17 00:00:00 2001 From: Allan O Date: Thu, 7 Oct 2021 21:53:27 +0300 Subject: [PATCH 08/11] :white_check_mark: Fix failing test --- .../main/java/org/smartregister/chw/util/RepositoryUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index 35ca8fc63f..22799c66e6 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -93,7 +93,7 @@ public static void updateNullEventIds(SQLiteDatabase db) { } } - static List readEvents(Cursor cursor) { + public static List readEvents(Cursor cursor) { List events = new ArrayList<>(); ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); try { From dbfbfaf5bc06fd216bb2bfb798c00871eac0e6b0 Mon Sep 17 00:00:00 2001 From: Allan O Date: Fri, 8 Oct 2021 05:06:45 +0300 Subject: [PATCH 09/11] :white_check_mark: Add tests --- .../chw/util/RepositoryUtils.java | 24 +++---- ...JobAidsDashboardFragmentPresenterTest.java | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java diff --git a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java index 22799c66e6..abcb2e487f 100644 --- a/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java +++ b/opensrp-chw/src/main/java/org/smartregister/chw/util/RepositoryUtils.java @@ -22,32 +22,32 @@ import timber.log.Timber; -public class RepositoryUtils { +public interface RepositoryUtils { - public static final String EVENT_ID = "id"; - public static final String _ID = "_id"; + String EVENT_ID = "id"; + String _ID = "_id"; - public static String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; - public static String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; - public static String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; + String ADD_MISSING_REPORTING_COLUMN = "ALTER TABLE 'indicator_queries' ADD COLUMN expected_indicators TEXT NULL;"; + String FAMILY_MEMBER_ADD_REASON_FOR_REGISTRATION = "ALTER TABLE 'ec_family_member' ADD COLUMN reasons_for_registration TEXT NULL;"; + String EC_REFERRAL_ADD_FP_METHOD_COLUMN = "ALTER TABLE 'ec_referral' ADD COLUMN fp_method_accepted_referral TEXT NULL;"; - public static String[] UPDATE_REPOSITORY_TYPES = { + String[] UPDATE_REPOSITORY_TYPES = { "UPDATE recurring_service_types SET service_group = 'woman' WHERE type = 'IPTp-SP';", "UPDATE recurring_service_types SET service_group = 'child' WHERE type != 'IPTp-SP';", }; - public static String[] UPGRADE_V10 = { + String[] UPGRADE_V10 = { "ALTER TABLE ec_child ADD COLUMN mother_entity_id VARCHAR;", "ALTER TABLE ec_child ADD COLUMN entry_point VARCHAR;" }; - public static String DELETE_DUPLICATE_SCHEDULES = "delete from schedule_service where id not in ( " + + String DELETE_DUPLICATE_SCHEDULES = "delete from schedule_service where id not in ( " + "select max(id) from schedule_service " + "group by base_entity_id , schedule_group_name , schedule_name " + "having count(*) > 1 " + ")"; - public static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { + static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { try { db.execSQL("ALTER TABLE ec_family ADD COLUMN entity_type VARCHAR; " + @@ -63,7 +63,7 @@ public static void addDetailsColumnToFamilySearchTable(SQLiteDatabase db) { } } - public static void updateNullEventIds(SQLiteDatabase db) { + static void updateNullEventIds(SQLiteDatabase db) { List events = new ArrayList<>(); String eventTableName = EventClientRepository.Table.event.name(); String eventIdCol = EventClientRepository.event_column.eventId.name(); @@ -93,7 +93,7 @@ public static void updateNullEventIds(SQLiteDatabase db) { } } - public static List readEvents(Cursor cursor) { + static List readEvents(Cursor cursor) { List events = new ArrayList<>(); ECSyncHelper syncHelper = ChwApplication.getInstance().getEcSyncHelper(); try { diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java new file mode 100644 index 0000000000..398fbd432b --- /dev/null +++ b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java @@ -0,0 +1,68 @@ +package org.smartregister.chw.presenter; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.Context; +import org.smartregister.commonregistry.CommonFtsObject; +import org.smartregister.reporting.ReportingLibrary; +import org.smartregister.reporting.contract.ReportContract; +import org.smartregister.reporting.domain.IndicatorQuery; +import org.smartregister.reporting.domain.ReportIndicator; +import org.smartregister.repository.Repository; + +import java.util.ArrayList; +import java.util.List; + +public class JobAidsDashboardFragmentPresenterTest { + + private JobAidsDashboardFragmentPresenter presenter; + + @Mock + ReportContract.View view; + + @Mock + ReportContract.Model model; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + ReportingLibrary.init(Mockito.mock(Context.class), Mockito.mock(Repository.class), + Mockito.mock(CommonFtsObject.class), 1, 1); + presenter = new JobAidsDashboardFragmentPresenter(view); + ReflectionHelpers.setField(presenter, "model", model); + } + + @Test + public void fetchDailyTalliesCallsModelGetDailyTallies() { + presenter.fetchIndicatorsDailytallies(); + Mockito.verify(model).getIndicatorsDailyTallies(); + } + + @Test + public void canAddAListOfIndicators() { + List indicators = new ArrayList<>(); + indicators.add(Mockito.mock(ReportIndicator.class)); + presenter.addIndicators(indicators); + Mockito.verify(model, Mockito.atLeast(1)).addIndicator(Mockito.any(ReportIndicator.class)); + } + + @Test + public void canAddIndicatorQueries() { + List indicatorQueries = new ArrayList<>(); + indicatorQueries.add(Mockito.mock(IndicatorQuery.class)); + presenter.addIndicatorQueries(indicatorQueries); + Mockito.verify(model, Mockito.atLeast(1)).addIndicatorQuery(Mockito.any(IndicatorQuery.class)); + } + + @Test + public void canGetView() { + Assert.assertTrue(presenter.getView() instanceof ReportContract.View); + } +} From 1621c3f82b29769f2138b212ba3146a3095cec80 Mon Sep 17 00:00:00 2001 From: Allan O Date: Fri, 8 Oct 2021 05:14:06 +0300 Subject: [PATCH 10/11] :recycle: Remove unused imports --- .../chw/presenter/JobAidsDashboardFragmentPresenterTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java index 398fbd432b..a7bf8efe18 100644 --- a/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java +++ b/opensrp-chw/src/test/java/org/smartregister/chw/presenter/JobAidsDashboardFragmentPresenterTest.java @@ -6,8 +6,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.robolectric.Robolectric; -import org.robolectric.RuntimeEnvironment; import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.commonregistry.CommonFtsObject; From b478bcbdeedaa61479ecb5a0c75ec6b5032e2ffc Mon Sep 17 00:00:00 2001 From: Simon Njoroge Date: Fri, 15 Oct 2021 16:07:12 +0300 Subject: [PATCH 11/11] Update hellocharts dependency --- opensrp-chw/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-chw/build.gradle b/opensrp-chw/build.gradle index 2c9b7dd756..2c1704d343 100644 --- a/opensrp-chw/build.gradle +++ b/opensrp-chw/build.gradle @@ -382,7 +382,7 @@ dependencies { //For viewing PDFs in the app implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1' - implementation 'com.github.lecho:hellocharts-android:v1.5.8@aar' + implementation 'com.github.lecho:hellocharts-android:1.5.8@aar' testImplementation "org.koin:koin-test:2.0.1" testImplementation 'junit:junit:4.13'