From 3e3cd4898cb09e70de9a8865d0e09a92adc0a0dc Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 11:11:31 +0300 Subject: [PATCH 1/7] Add tests for 4 Utils class methods --- .../java/org/smartregister/util/Utils.java | 1 + .../org/smartregister/util/UtilsTest.java | 60 +++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/opensrp-app/src/main/java/org/smartregister/util/Utils.java b/opensrp-app/src/main/java/org/smartregister/util/Utils.java index 3145d555c..d3282c74a 100644 --- a/opensrp-app/src/main/java/org/smartregister/util/Utils.java +++ b/opensrp-app/src/main/java/org/smartregister/util/Utils.java @@ -107,6 +107,7 @@ * Class containing some static utility methods. */ public class Utils { + private static final SimpleDateFormat UI_DF = new SimpleDateFormat("dd-MM-yyyy", Utils.getDefaultLocale()); private static final SimpleDateFormat UI_DTF = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Utils.getDefaultLocale()); diff --git a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java index aa48cc999..52e872cd1 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -65,16 +65,15 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; - +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.only; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.smartregister.TestUtils.getContext; import static org.smartregister.util.Utils.getDefaultLocale; - -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; +import static org.smartregister.util.Utils.getUserDefaultTeamId; /** * Created by kaderchowdhury on 12/11/17. @@ -635,5 +634,58 @@ public void convert() { assertEquals(client.getCaseId(), actualClient.getCaseId()); assertEquals(details.size(), actualClient.getColumnmaps().size()); } + + @Test + public void getUserDefaultTeamIdShouldReturnNullWhenUserInfoDetailsAreNull() { + Assert.assertNull(getUserDefaultTeamId(null)); + + LoginResponseData loginData = new LoginResponseData(); + Assert.assertNull(getUserDefaultTeamId(loginData)); + + loginData.team = new TeamMember(); + Assert.assertNull(getUserDefaultTeamId(loginData)); + } + + @Ignore + @Test + public void getPreferredNameShouldHandleNull() { + + } + + @Ignore + @Test + public void getDurationShouldReturnValidDurationString() { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + + Calendar calendar = Calendar.getInstance(); + calendar.add(1, Calendar.DAY_OF_MONTH); + + Assert.assertEquals("1d", Utils.getDuration(dateFormat.format(calendar.getTime()))); + + calendar.add(5, Calendar.WEEK_OF_YEAR); + Assert.assertEquals("5w 1d", Utils.getDuration(calendar.getTime().toString())); + + calendar.add(-1, Calendar.DAY_OF_MONTH); + Assert.assertEquals("5w", Utils.getDuration(calendar.getTime().toString())); + } + + + @Test + public void getDurationShouldReturnEmptyString() { + Calendar calendar = Calendar.getInstance(); + calendar.add(1, Calendar.DAY_OF_MONTH); + + Assert.assertEquals("", Utils.getDuration("")); + } + + @Test + public void dobStringToDateTimeShouldHandleNull() { + Assert.assertNull(Utils.dobStringToDateTime(null)); + } + + @Test + public void dobStringToDateTimeShouldHandleInvalidDateTimeStrings() { + Assert.assertNull(Utils.dobStringToDateTime("opensrp")); + } } From bc810e2921f291bb8b59b4de93d8a4b28259925f Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 11:15:55 +0300 Subject: [PATCH 2/7] Add test for Utils#getProperties --- .../test/java/org/smartregister/util/UtilsTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java index 52e872cd1..df3b4ef83 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -687,5 +687,16 @@ public void dobStringToDateTimeShouldHandleNull() { public void dobStringToDateTimeShouldHandleInvalidDateTimeStrings() { Assert.assertNull(Utils.dobStringToDateTime("opensrp")); } + + @Test + public void getPropertiesShouldLoadPropertiesInPropertiesFile() { + AppProperties appProperties = Utils.getProperties(RuntimeEnvironment.application); + + Assert.assertEquals(5, appProperties.size()); + Assert.assertEquals("", appProperties.getProperty("DRISHTI_BASE_URL")); + Assert.assertEquals("false", appProperties.getProperty("SHOULD_VERIFY_CERTIFICATE")); + Assert.assertEquals("false", appProperties.getProperty("system.toaster.centered")); + Assert.assertEquals("10", appProperties.getProperty("SYNC_DOWNLOAD_BATCH_SIZE")); + } } From 277e26d070c0675b1942bb7dc35a18e948570156 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 11:18:27 +0300 Subject: [PATCH 3/7] Add test for Utils#safeArrayToString --- .../src/test/java/org/smartregister/util/UtilsTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java index df3b4ef83..49c144171 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -698,5 +698,11 @@ public void getPropertiesShouldLoadPropertiesInPropertiesFile() { Assert.assertEquals("false", appProperties.getProperty("system.toaster.centered")); Assert.assertEquals("10", appProperties.getProperty("SYNC_DOWNLOAD_BATCH_SIZE")); } + + @Test + public void safeArrayToString() { + Assert.assertEquals("OpenSRP", Utils.safeArrayToString(new char[]{'O', 'p', 'e', 'n', 'S', 'R', 'P'})); + + } } From 441902c5d561d9046ea01346350e9e78fa5fe4fb Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 12:03:18 +0300 Subject: [PATCH 4/7] Add test app.properties file to fix failing test --- opensrp-app/src/test/assets/app.properties | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 opensrp-app/src/test/assets/app.properties diff --git a/opensrp-app/src/test/assets/app.properties b/opensrp-app/src/test/assets/app.properties new file mode 100644 index 000000000..d97e65efa --- /dev/null +++ b/opensrp-app/src/test/assets/app.properties @@ -0,0 +1,5 @@ +DRISHTI_BASE_URL= +PORT=-1 +SHOULD_VERIFY_CERTIFICATE=false +SYNC_DOWNLOAD_BATCH_SIZE=10 +system.toaster.centered=false \ No newline at end of file From 49037e68ad2f1bdc8776c001523a2d53905f8706 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 12:13:54 +0300 Subject: [PATCH 5/7] Fix codacy issue on imports --- .../org/smartregister/util/UtilsTest.java | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java index 49c144171..e6e9b5821 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -117,7 +117,7 @@ public void assertConvertDateFormatReturnsDate() throws Exception { @Test public void assertConvertDateTimeFormatReturnsDate() throws Exception { assertEquals("24-07-1985 00:00:00", Utils.convertDateTimeFormat("1985-07-24T00:00:00.000Z", true)); -// org.junit.Assert.assertEquals("", Utils.convertDateTimeFormat("19850724", true)); +// assertEquals("", Utils.convertDateTimeFormat("19850724", true)); } @Test(expected = RuntimeException.class) @@ -637,19 +637,13 @@ public void convert() { @Test public void getUserDefaultTeamIdShouldReturnNullWhenUserInfoDetailsAreNull() { - Assert.assertNull(getUserDefaultTeamId(null)); + assertNull(getUserDefaultTeamId(null)); LoginResponseData loginData = new LoginResponseData(); - Assert.assertNull(getUserDefaultTeamId(loginData)); + assertNull(getUserDefaultTeamId(loginData)); loginData.team = new TeamMember(); - Assert.assertNull(getUserDefaultTeamId(loginData)); - } - - @Ignore - @Test - public void getPreferredNameShouldHandleNull() { - + assertNull(getUserDefaultTeamId(loginData)); } @Ignore @@ -660,13 +654,13 @@ public void getDurationShouldReturnValidDurationString() { Calendar calendar = Calendar.getInstance(); calendar.add(1, Calendar.DAY_OF_MONTH); - Assert.assertEquals("1d", Utils.getDuration(dateFormat.format(calendar.getTime()))); + assertEquals("1d", Utils.getDuration(dateFormat.format(calendar.getTime()))); calendar.add(5, Calendar.WEEK_OF_YEAR); - Assert.assertEquals("5w 1d", Utils.getDuration(calendar.getTime().toString())); + assertEquals("5w 1d", Utils.getDuration(calendar.getTime().toString())); calendar.add(-1, Calendar.DAY_OF_MONTH); - Assert.assertEquals("5w", Utils.getDuration(calendar.getTime().toString())); + assertEquals("5w", Utils.getDuration(calendar.getTime().toString())); } @@ -675,34 +669,33 @@ public void getDurationShouldReturnEmptyString() { Calendar calendar = Calendar.getInstance(); calendar.add(1, Calendar.DAY_OF_MONTH); - Assert.assertEquals("", Utils.getDuration("")); + assertEquals("", Utils.getDuration("")); } @Test public void dobStringToDateTimeShouldHandleNull() { - Assert.assertNull(Utils.dobStringToDateTime(null)); + assertNull(Utils.dobStringToDateTime(null)); } @Test public void dobStringToDateTimeShouldHandleInvalidDateTimeStrings() { - Assert.assertNull(Utils.dobStringToDateTime("opensrp")); + assertNull(Utils.dobStringToDateTime("opensrp")); } @Test public void getPropertiesShouldLoadPropertiesInPropertiesFile() { AppProperties appProperties = Utils.getProperties(RuntimeEnvironment.application); - Assert.assertEquals(5, appProperties.size()); - Assert.assertEquals("", appProperties.getProperty("DRISHTI_BASE_URL")); - Assert.assertEquals("false", appProperties.getProperty("SHOULD_VERIFY_CERTIFICATE")); - Assert.assertEquals("false", appProperties.getProperty("system.toaster.centered")); - Assert.assertEquals("10", appProperties.getProperty("SYNC_DOWNLOAD_BATCH_SIZE")); + assertEquals(5, appProperties.size()); + assertEquals("", appProperties.getProperty("DRISHTI_BASE_URL")); + assertEquals("false", appProperties.getProperty("SHOULD_VERIFY_CERTIFICATE")); + assertEquals("false", appProperties.getProperty("system.toaster.centered")); + assertEquals("10", appProperties.getProperty("SYNC_DOWNLOAD_BATCH_SIZE")); } @Test public void safeArrayToString() { - Assert.assertEquals("OpenSRP", Utils.safeArrayToString(new char[]{'O', 'p', 'e', 'n', 'S', 'R', 'P'})); - + assertEquals("OpenSRP", Utils.safeArrayToString(new char[]{'O', 'p', 'e', 'n', 'S', 'R', 'P'})); } } From 1e0ac08d0fb2e196eafc520daaa7e27f2a5bf5f5 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 12:34:35 +0300 Subject: [PATCH 6/7] Add test for SyncUtils#getLogoutUserIntent --- .../org/smartregister/util/SyncUtils.java | 14 ++-- .../util/SyncUtilsRobolectricTest.java | 78 +++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java diff --git a/opensrp-app/src/main/java/org/smartregister/util/SyncUtils.java b/opensrp-app/src/main/java/org/smartregister/util/SyncUtils.java index 7baf104f7..1d0289153 100644 --- a/opensrp-app/src/main/java/org/smartregister/util/SyncUtils.java +++ b/opensrp-app/src/main/java/org/smartregister/util/SyncUtils.java @@ -12,6 +12,7 @@ import androidx.annotation.NonNull; import androidx.annotation.StringRes; +import androidx.annotation.VisibleForTesting; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; @@ -43,7 +44,7 @@ */ public class SyncUtils { - private org.smartregister.Context opensrpContent = CoreLibrary.getInstance().context(); + private org.smartregister.Context opensrpContext = CoreLibrary.getInstance().context(); private Context context; @@ -61,21 +62,22 @@ public void logoutUser() throws AuthenticatorException, OperationCanceledExcepti public void logoutUser(@StringRes int logoutMessage) throws AuthenticatorException, OperationCanceledException, IOException { //force remote login - opensrpContent.userService().forceRemoteLogin(opensrpContent.allSharedPreferences().fetchRegisteredANM()); + opensrpContext.userService().forceRemoteLogin(opensrpContext.allSharedPreferences().fetchRegisteredANM()); Intent logoutUserIntent = getLogoutUserIntent(logoutMessage); - AccountManagerFuture reAuthenticateFuture = AccountHelper.reAuthenticateUserAfterSessionExpired(opensrpContent.allSharedPreferences().fetchRegisteredANM(), CoreLibrary.getInstance().getAccountAuthenticatorXml().getAccountType(), AccountHelper.TOKEN_TYPE.PROVIDER); + AccountManagerFuture reAuthenticateFuture = AccountHelper.reAuthenticateUserAfterSessionExpired(opensrpContext.allSharedPreferences().fetchRegisteredANM(), CoreLibrary.getInstance().getAccountAuthenticatorXml().getAccountType(), AccountHelper.TOKEN_TYPE.PROVIDER); Intent accountAuthenticatorIntent = reAuthenticateFuture.getResult().getParcelable(AccountManager.KEY_INTENT); accountAuthenticatorIntent.putExtras(logoutUserIntent); context.startActivity(logoutUserIntent); //logoff opensrp session - opensrpContent.userService().logoutSession(); + opensrpContext.userService().logoutSession(); } + @VisibleForTesting @NonNull - private Intent getLogoutUserIntent(@StringRes int logoutMessage) { + protected Intent getLogoutUserIntent(@StringRes int logoutMessage) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); @@ -98,7 +100,7 @@ public boolean isAppVersionAllowed() { try { // see if setting was synced - AllSettings settingsRepository = opensrpContent.allSettings(); + AllSettings settingsRepository = opensrpContext.allSettings(); Setting rawMinAllowedAppVersionSetting; try { rawMinAllowedAppVersionSetting = settingsRepository.getSetting(MIN_ALLOWED_APP_VERSION_SETTING); diff --git a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java new file mode 100644 index 000000000..6f06438fd --- /dev/null +++ b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java @@ -0,0 +1,78 @@ +package org.smartregister.util; + +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; +import android.content.Intent; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.BaseRobolectricUnitTest; +import org.smartregister.Context; +import org.smartregister.CoreLibrary; +import org.smartregister.R; +import org.smartregister.service.UserService; + +import java.io.IOException; + + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 09-02-2021. + */ +public class SyncUtilsRobolectricTest extends BaseRobolectricUnitTest { + + private SyncUtils syncUtils; + private Context opensrpContext; + private android.content.Context context; + private UserService userService; + + @Before + public void setUp() { + opensrpContext = Mockito.spy(CoreLibrary.getInstance().context()); + userService = Mockito.spy(opensrpContext.userService()); + Mockito.doReturn(userService).when(opensrpContext).userService(); + + context = Mockito.spy(RuntimeEnvironment.application); + + syncUtils = new SyncUtils(context); + ReflectionHelpers.setField(syncUtils, "opensrpContext", opensrpContext); + } + + @Ignore + @Test + public void logoutUser() throws AuthenticatorException, OperationCanceledException, IOException { + ArgumentCaptor intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); + Mockito.doNothing().when(context).startActivity(intentArgumentCaptor.capture()); + + syncUtils.logoutUser(R.string.logout_text); + + Mockito.verify(userService).forceRemoteLogin(Mockito.anyString()); + Mockito.verify(userService).logoutSession(); + + Intent intent = intentArgumentCaptor.getValue(); + Assert.assertEquals(Intent.ACTION_MAIN, intent.getAction()); + Assert.assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)); + Assert.assertEquals("org.smartregister", intent.getPackage()); + + Mockito.verify(context).startActivity(Mockito.any(Intent.class)); + + } + + @Test + public void getLogoutUserIntentShouldProvideAnonymouseIntentWhenLauncherActivityIsUnavailable() { + ArgumentCaptor intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); + Mockito.doNothing().when(context).startActivity(intentArgumentCaptor.capture()); + + Intent intent = syncUtils.getLogoutUserIntent(R.string.logout_text); + + Assert.assertEquals(Intent.ACTION_MAIN, intent.getAction()); + Assert.assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)); + Assert.assertEquals("org.smartregister.test", intent.getPackage()); + + } +} From 5e2965732a98721e1163325d99d020794bcf4398 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Tue, 9 Feb 2021 13:42:59 +0300 Subject: [PATCH 7/7] Code cleanup --- .../java/org/smartregister/util/SyncUtilsRobolectricTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java index 6f06438fd..de83a28d0 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsRobolectricTest.java @@ -27,13 +27,12 @@ public class SyncUtilsRobolectricTest extends BaseRobolectricUnitTest { private SyncUtils syncUtils; - private Context opensrpContext; private android.content.Context context; private UserService userService; @Before public void setUp() { - opensrpContext = Mockito.spy(CoreLibrary.getInstance().context()); + Context opensrpContext = Mockito.spy(CoreLibrary.getInstance().context()); userService = Mockito.spy(opensrpContext.userService()); Mockito.doReturn(userService).when(opensrpContext).userService();