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

v5: Fix tests coverage #907

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Android CI with Gradle

on:
push:
branches: [ master ]
branches: [ master, v5 ]
pull_request:
branches: [ master ]
branches: [ master, v5 ]

jobs:
unit_tests:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ opensrp-path/fabric.properties
opensrp-path/reports/
opensrp-core/reports/
opensrp-core/jacoco.exec
robolectric-deps/
14 changes: 14 additions & 0 deletions download-robolectric-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
mkdir robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/10-robolectric-5803371/android-all-10-robolectric-5803371.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/8.1.0-robolectric-4611349/android-all-8.1.0-robolectric-4611349.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/8.0.0_r4-robolectric-r1/android-all-8.0.0_r4-robolectric-r1.jar -P robolectric-deps

wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/6.0.1_r3-robolectric-r1/android-all-6.0.1_r3-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/7.0.0_r1-robolectric-r1/android-all-7.0.0_r1-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/7.1.0_r7-robolectric-r1/android-all-7.1.0_r7-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/11-robolectric-6757853/android-all-11-robolectric-6757853.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/5.1.1_r9-robolectric-r2/android-all-5.1.1_r9-robolectric-r2.jar -P robolectric-deps

cp robolectric-deps.properties opensrp-core/src/test/resources
4 changes: 3 additions & 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.9-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand All @@ -11,7 +11,9 @@ POM_SETTING_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_SETTING_LICENCE_DIST=repo
POM_SETTING_DEVELOPER_ID=opensrp
POM_SETTING_DEVELOPER_NAME=OpenSRP Onadev

android.useAndroidX=true
android.enableJetifier=true
android.jetifier.blacklist=shadows

org.gradle.jvmargs=-Xmx2048m
Binary file removed opensrp-app/jacoco.exec
Binary file not shown.
28 changes: 24 additions & 4 deletions opensrp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ apply plugin: 'com.android.library'
apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'
apply plugin: 'maven-publish'
apply plugin: 'kotlin-android'
jacoco {
toolVersion = "0.8.5"
}
Expand Down Expand Up @@ -91,10 +92,11 @@ android {
targetSdkVersion androidTargetSdkVersion
versionCode Integer.parseInt(project.VERSION_CODE)
versionName project.VERSION_NAME
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
buildConfigField "long", "BUILD_TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "int", "DB_ENCRYPTION_VERSION", '1'
}
/*

sourceSets {
main {
Expand All @@ -105,8 +107,12 @@ android {
jniLibs.srcDirs 'src/main/native'
resources.srcDir 'resources'
}
test {
java.srcDir 'tests'
}
androidTest.setRoot('src/androidTest')
}
*/

aaptOptions {
noCompress 'apk'
Expand All @@ -123,6 +129,7 @@ android {
includeAndroidResources = true
}
}

}

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
Expand All @@ -133,13 +140,23 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
testLogging {
// Enables easier debugging of tests in Github Actions CI
events TestLogEvent.FAILED
events TestLogEvent.FAILED, TestLogEvent.SKIPPED, TestLogEvent.PASSED
exceptionFormat TestExceptionFormat.FULL
showExceptions true
showCauses true
showStackTraces true
}
maxHeapSize = "4g"

test {
beforeTest { testDescriptor ->
println "${testDescriptor.className} > ${testDescriptor.name} STARTED"
}
afterTest { testDescriptor ->
println "${testDescriptor.className} > ${testDescriptor.name} ENDED"

}
}
}

def filesToCopy = copySpec {
Expand Down Expand Up @@ -258,6 +275,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 Expand Up @@ -285,6 +303,8 @@ dependencies {
exclude group: 'com.google.guava', module: 'guava'
}

testImplementation(project(":opensrp-core"))

// PowerMock
def powerMockVersion = '2.0.4'
testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
Expand Down Expand Up @@ -337,5 +357,5 @@ coveralls {
sourceDirs = ["$project.projectDir/src/main/java"]
}

apply from: '../maven.gradle'
apply plugin: 'kotlin-android'

apply from: '../maven.gradle'
Empty file modified opensrp-core/gradlew
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions opensrp-core/robolectric-deps.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
org.robolectric\:android-all\:6.0.1_r3-robolectric-r1=../../../../../../robolectric-deps/android-all-6.0.1_r3-robolectric-r1.jar
org.robolectric\:android-all\:7.0.0_r1-robolectric-r1=../../../../../../robolectric-deps/android-all-7.0.0_r1-robolectric-r1.jar
org.robolectric\:android-all\:7.1.0_r7-robolectric-r1=../../../../../../robolectric-deps/android-all-7.1.0_r7-robolectric-r1.jar
org.robolectric\:android-all\:8.0.0_r4-robolectric-r1=../../../../../../robolectric-deps/android-all-8.0.0_r4-robolectric-r1.jar
org.robolectric\:android-all\:8.1.0-robolectric-4611349=../../../../../../robolectric-deps/android-all-8.1.0-robolectric-4611349.jar
org.robolectric\:android-all\:9-robolectric-4913185-2=../../../../../../robolectric-deps/android-all-9-robolectric-4913185-2.jar
org.robolectric\:android-all\:10-robolectric-5803371=../../../../../../robolectric-deps/android-all-10-robolectric-5803371.jar
org.robolectric\:android-all\:11-robolectric-6757853=../../../../../../robolectric-deps/android-all-11-robolectric-6757853.jar
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 Expand Up @@ -598,4 +599,8 @@ public static class Entity {
public static final String MOTHER = "mother";
}

public static class WorkData {
public static final String EVENT_TYPES = "event-types";
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.smartregister.job;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.Data;
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 DuplicateZeirIdsCleanerWorker extends Worker {
private Context mContext;

public static final String TAG = "DuplicateZeirIdsCleanerWorker";

public DuplicateZeirIdsCleanerWorker(@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, String[] registrationEventTypes) {
Data data = new Data.Builder()
.putStringArray(AllConstants.WorkData.EVENT_TYPES, registrationEventTypes)
.build();

PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(DuplicateZeirIdsCleanerWorker.class, mins, TimeUnit.MINUTES)
.setInputData(data)
.build();

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

@NonNull
@Override
public Result doWork() {
AllSharedPreferences allSharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences();
String[] eventTypes = getInputData().getStringArray(AllConstants.WorkData.EVENT_TYPES);

if (!allSharedPreferences.getBooleanPreference(AllConstants.PREF_KEY.DUPLICATE_IDS_FIXED)) {
DuplicateZeirIdStatus duplicateZeirIdStatus = AppHealthUtils.cleanUniqueZeirIds(eventTypes);
Timber.i("Started cleaning duplicate client-identifiers");
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();
}
}
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 @@ -2357,7 +2358,7 @@ public List<Event> getEventsByTaskIds(Set<String> taskIds) {
+ " IN (" + StringUtils.repeat("?", ",", taskIds.size()) + ")", taskIds.toArray(new String[0]));
}

public DuplicateZeirIdStatus cleanDuplicateMotherIds() throws Exception {
public DuplicateZeirIdStatus cleanDuplicateMotherIds(String[] eventTypes) throws Exception {
String username = Context.getInstance().userService().getAllSharedPreferences().fetchRegisteredANM();

UniqueIdRepository uniqueIdRepository = Context.getInstance().getUniqueIdRepository();
Expand Down Expand Up @@ -2401,14 +2402,38 @@ public DuplicateZeirIdStatus cleanDuplicateMotherIds() throws Exception {
}

String eventType = AllConstants.EventType.BITRH_REGISTRATION;
String clientType = clientJson.getString(AllConstants.CLIENT_TYPE);
String clientType = clientJson.optString(AllConstants.CLIENT_TYPE);

List<String> eventTypesList = new ArrayList<>();

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;
} else {
String identifierLabel = ZEIR_ID.toLowerCase(Locale.ROOT);
if (identifiers.has(ZEIR_ID)) {
identifierLabel = ZEIR_ID;
} else if (identifiers.has("M_ZEIR_ID")) {
identifierLabel = "M_ZEIR_ID";
} else if (identifiers.has("ANC_ID")) {
identifierLabel = "ANC_ID";
}

identifiers.put(identifierLabel, newZeirId.replaceAll("-", ""));
}

eventTypesList.add(eventType);
if (eventTypes != null) {
Collections.addAll(eventTypesList, eventTypes);
}

clientJson.put(AllConstants.IDENTIFIERS, identifiers);

// Add events to process this
Expand All @@ -2418,7 +2443,7 @@ public DuplicateZeirIdStatus cleanDuplicateMotherIds() throws Exception {
List<EventClient> registrationEvent = getEvents(
Collections.singletonList(baseEntityId),
Collections.singletonList(BaseRepository.TYPE_Synced),
Collections.singletonList(eventType)
eventTypesList
);
Event event = null;
if (!registrationEvent.isEmpty())
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
Expand Up @@ -120,10 +120,10 @@ public static void refreshFileSystem(Context context, @VisibleForTesting boolean
}

@Nullable
public static DuplicateZeirIdStatus cleanUniqueZeirIds(){
public static DuplicateZeirIdStatus cleanUniqueZeirIds(String[] eventTypes){
try {
return CoreLibrary.getInstance().context().getEventClientRepository()
.cleanDuplicateMotherIds();
.cleanDuplicateMotherIds(eventTypes);
} catch (Exception e) {
Timber.e(e);
}
Expand Down
Loading