From 66f5fb9596cb4487b0f28db2e8c5d9462b020a7c Mon Sep 17 00:00:00 2001 From: bennsimon Date: Thu, 19 Nov 2020 11:01:42 +0300 Subject: [PATCH 1/7] call checkPlatformMigrations on the last init method --- opensrp-app/src/main/java/org/smartregister/CoreLibrary.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java index b582a6ed2..22eaa8ce5 100644 --- a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java +++ b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java @@ -73,7 +73,6 @@ public static void init(Context context, SyncConfiguration syncConfiguration, lo if (instance == null) { instance = new CoreLibrary(context, syncConfiguration, options); buildTimeStamp = buildTimestamp; - checkPlatformMigrations(); } } @@ -105,6 +104,8 @@ protected CoreLibrary(Context contextArg, SyncConfiguration syncConfiguration, @ if (syncConfiguration != null && syncConfiguration.runPlanEvaluationOnClientProcessing()) { PathEvaluatorLibrary.init(new LocationDaoImpl(), new ClientDaoImpl(), new TaskDaoImpl(new TaskNotesRepository()), new EventDaoImpl()); } + + checkPlatformMigrations(); } public void initP2pLibrary(@Nullable String username) { From 831c1e964163757f738fcf888d2a9c3d50875ca6 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Thu, 19 Nov 2020 11:44:33 +0300 Subject: [PATCH 2/7] update init methods --- gradle.properties | 2 +- .../java/org/smartregister/CoreLibrary.java | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5ba4447a2..f54fe1f1b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.2.1-SNAPSHOT +VERSION_NAME=3.2.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java index 22eaa8ce5..c832f8896 100644 --- a/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java +++ b/opensrp-app/src/main/java/org/smartregister/CoreLibrary.java @@ -4,10 +4,10 @@ import android.accounts.AccountManager; import android.accounts.OnAccountsUpdateListener; import android.content.Intent; -import androidx.annotation.NonNull; +import android.text.TextUtils; + import androidx.annotation.Nullable; import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import android.text.TextUtils; import org.apache.commons.lang3.StringUtils; import org.smartregister.account.AccountAuthenticatorXml; @@ -57,22 +57,18 @@ public static void init(Context context) { } public static void init(Context context, SyncConfiguration syncConfiguration) { - if (instance == null) { - instance = new CoreLibrary(context, syncConfiguration, null); - } + init(context, syncConfiguration, BuildConfig.BUILD_TIMESTAMP); } public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp) { - if (instance == null) { - instance = new CoreLibrary(context, syncConfiguration, null); - buildTimeStamp = buildTimestamp; - } + init(context, syncConfiguration, buildTimestamp, null); } - public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @NonNull P2POptions options) { + public static void init(Context context, SyncConfiguration syncConfiguration, long buildTimestamp, @Nullable P2POptions options) { if (instance == null) { instance = new CoreLibrary(context, syncConfiguration, options); buildTimeStamp = buildTimestamp; + checkPlatformMigrations(); } } @@ -104,8 +100,6 @@ protected CoreLibrary(Context contextArg, SyncConfiguration syncConfiguration, @ if (syncConfiguration != null && syncConfiguration.runPlanEvaluationOnClientProcessing()) { PathEvaluatorLibrary.init(new LocationDaoImpl(), new ClientDaoImpl(), new TaskDaoImpl(new TaskNotesRepository()), new EventDaoImpl()); } - - checkPlatformMigrations(); } public void initP2pLibrary(@Nullable String username) { From 9a12d10fc6c3e13ea06ea015ba2a927575012036 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Thu, 19 Nov 2020 15:30:06 +0300 Subject: [PATCH 3/7] update tests --- .../sync/ClientProcessorTest.java | 16 +++++++++--- .../util/OpenSRPImageLoaderTest.java | 20 +++++++++----- .../org/smartregister/util/SyncUtilsTest.java | 26 ++++++++++++++----- .../view/activity/DrishtiApplicationTest.java | 6 +++-- .../view/contract/FPClientTest.java | 14 +++++++--- .../fragment/BaseRegisterFragmentTest.java | 18 ++++++++++--- .../PNCClientPreProcessorTest.java | 17 ++++++++++-- 7 files changed, 91 insertions(+), 26 deletions(-) diff --git a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java index 81a85d902..356126043 100644 --- a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java @@ -5,6 +5,7 @@ import org.json.JSONArray; import org.json.JSONObject; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -18,6 +19,7 @@ import org.powermock.modules.junit4.rule.PowerMockRule; import org.powermock.reflect.Whitebox; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; @@ -65,10 +67,13 @@ public class ClientProcessorTest extends BaseUnitTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - CoreLibrary.init(context, new TestSyncConfiguration()); - when(context.detailsRepository()).thenReturn(detailsRepository); - when(coreLibrary.context()).thenReturn(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(new TestSyncConfiguration()).when(coreLibrary).getSyncConfiguration(); + doReturn(context).when(coreLibrary).context(); + doReturn("ec_client_fields.json").when(coreLibrary).getEcClientFieldsFile(); + + when(context.detailsRepository()).thenReturn(detailsRepository); when(context.commonrepository(Mockito.anyString())).thenReturn(cr); when(cr.executeInsertStatement(Mockito.any(ContentValues.class), Mockito.anyString())).thenReturn(1l); context.updateApplicationContext(RuntimeEnvironment.application); @@ -217,4 +222,9 @@ public void processCaseModelReturnsNotNUll() throws Exception { Assert.assertEquals(Boolean.TRUE, b); } + + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } diff --git a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java index df6ab7a05..df658ccc9 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java @@ -2,7 +2,6 @@ import android.app.Service; import android.content.Intent; -import android.content.res.Resources; import com.android.volley.RequestQueue; import com.android.volley.toolbox.HurlStack; @@ -10,6 +9,7 @@ import junit.framework.Assert; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -22,11 +22,13 @@ import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; import org.robolectric.android.controller.ActivityController; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.util.mock.OpenSRPImageLoaderTestActivity; -import org.smartregister.view.activity.DrishtiApplication; + +import static org.mockito.Mockito.doReturn; /** * Created by kaderchowdhury on 14/11/17. @@ -39,12 +41,13 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest { public PowerMockRule rule = new PowerMockRule(); private OpenSRPImageLoaderTestActivity activity; + @Mock private Context context; + @Mock - Resources res; - @Mock - private DrishtiApplication drishtiApplication; + private CoreLibrary coreLibrary; + private ActivityController controller; @Before @@ -53,7 +56,8 @@ public void setUp() throws Exception { Intent intent = new Intent(RuntimeEnvironment.application, OpenSRPImageLoaderTestActivity.class); controller = Robolectric.buildActivity(OpenSRPImageLoaderTestActivity.class, intent); activity = controller.get(); - CoreLibrary.init(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(context).when(coreLibrary).context(); controller.setup(); } @@ -82,4 +86,8 @@ public void assertServiceConstructorInitializationNotNull() throws Exception { Assert.assertNotNull(openSRPImageLoader); } + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } diff --git a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java index 3f756cffc..04ab3a912 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java @@ -3,13 +3,16 @@ import android.content.Context; import android.content.pm.PackageManager; +import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.rule.PowerMockRule; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.BaseUnitTest; import org.smartregister.CoreLibrary; import org.smartregister.domain.Setting; import org.smartregister.repository.AllSettings; @@ -28,9 +31,8 @@ * Created by Vincent Karuri on 10/03/2020 */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({Utils.class}) -public class SyncUtilsTest { +@PrepareForTest(Utils.class) +public class SyncUtilsTest extends BaseUnitTest { @Mock private Context context; @@ -43,10 +45,17 @@ public class SyncUtilsTest { private SyncUtils syncUtils; + @Rule + public PowerMockRule rule = new PowerMockRule(); + + @Mock + private CoreLibrary coreLibrary; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - CoreLibrary.init(opensrpContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(opensrpContext).when(coreLibrary).context(); syncUtils = new SyncUtils(context); doReturn(settingsRepository).when(opensrpContext).allSettings(); } @@ -109,4 +118,9 @@ private String getMinAppVersionSetting(int minVersion) { return setting; } + + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } diff --git a/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java b/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java index 7896ec8f2..1f7132787 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/activity/DrishtiApplicationTest.java @@ -2,6 +2,8 @@ import android.os.Build; +import com.evernote.android.job.ShadowJobManager; + import net.sqlcipher.database.SQLiteDatabase; import org.junit.Assert; @@ -16,11 +18,11 @@ import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.TestSyncConfiguration; import org.smartregister.customshadows.FontTextViewShadow; import org.smartregister.repository.Repository; import org.smartregister.shadows.ShadowAppDatabase; import org.smartregister.shadows.ShadowDrawableResourcesImpl; -import com.evernote.android.job.ShadowJobManager; import org.smartregister.shadows.ShadowSQLiteDatabase; import org.smartregister.util.CredentialsHelper; @@ -115,7 +117,7 @@ public void onCreate() { context = Context.getInstance(); context.updateApplicationContext(getApplicationContext()); - CoreLibrary.init(context, null, 1588062490000l); + CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l); } } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java index 9af1064d1..5abbb209e 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java @@ -1,5 +1,6 @@ package org.smartregister.view.contract; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -10,6 +11,7 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; @@ -17,6 +19,8 @@ import java.util.Arrays; +import static org.mockito.Mockito.doReturn; + @RunWith(PowerMockRunner.class) @PrepareForTest(CoreLibrary.class) public class FPClientTest { @@ -32,10 +36,9 @@ public class FPClientTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - CoreLibrary.init(context); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(context).when(coreLibrary).context(); - PowerMockito.mockStatic(CoreLibrary.class); - PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary); Mockito.doReturn(context).when(coreLibrary).context(); fpClient = new FPClient("entity id 1", "woman name", "husband name", "village name", "ec no 1"); @@ -162,4 +165,9 @@ public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndN Assert.assertEquals(expectedFPClient.refillFollowUps(), fpClient.refillFollowUps()); } + + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java index 7c99c9f53..0a055b89a 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java @@ -18,6 +18,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.loader.app.LoaderManager; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -119,6 +120,9 @@ public class BaseRegisterFragmentTest extends BaseUnitTest { @Mock private LoaderManager loaderManager; + @Mock + private CoreLibrary coreLibrary; + @Before public void setUp() { @@ -138,8 +142,9 @@ public void setUp() { AppCompatActivity activitySpy = Mockito.spy(activity); doReturn(activitySpy).when(baseRegisterFragment).getActivity(); - ReflectionHelpers.setStaticField(CoreLibrary.class,"instance",null); - CoreLibrary.init(opensrpContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(opensrpContext).when(coreLibrary).context(); + doReturn(appProperties).when(opensrpContext).getAppProperties(); doReturn(opensrpContext).when(baseRegisterFragment).context(); @@ -266,7 +271,7 @@ public void assertOnQRCodeSucessfullyScannedInvokesFilterWithCorrectParams() { doReturn(activity).when(baseRegisterFragment).getActivity(); doReturn(loaderManager).when(baseRegisterFragment).getLoaderManager(); - doReturn(null).when(loaderManager).restartLoader(anyInt(),any(),any()); + doReturn(null).when(loaderManager).restartLoader(anyInt(), any(), any()); baseRegisterFragment.onQRCodeSucessfullyScanned(OPENSRP_ID); @@ -296,7 +301,7 @@ public void assertOnQRCodeSucessfullyScannedInvokessetUniqueIDWithCorrectParams( doReturn(activity).when(baseRegisterFragment).getActivity(); doReturn(loaderManager).when(baseRegisterFragment).getLoaderManager(); - doReturn(null).when(loaderManager).restartLoader(anyInt(),any(),any()); + doReturn(null).when(loaderManager).restartLoader(anyInt(), any(), any()); baseRegisterFragment.onQRCodeSucessfullyScanned(OPENSRP_ID); @@ -536,4 +541,9 @@ public void testRefreshSyncStatusViewsWithSyncingFalseFetchStatusNull() { baseRegisterFragment.refreshSyncStatusViews(null); Mockito.verify(baseRegisterFragment).refreshSyncProgressSpinner(); } + + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java index 33af1c121..7e3815209 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java @@ -1,9 +1,11 @@ package org.smartregister.view.preProcessor; import org.joda.time.LocalDate; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.R; @@ -23,17 +25,22 @@ import java.util.List; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; public class PNCClientPreProcessorTest { @Mock - Context mockedContext; + private Context mockedContext; + + @Mock + private CoreLibrary coreLibrary; @Before public void setUp() throws Exception { initMocks(this); - CoreLibrary.init(mockedContext); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); + doReturn(mockedContext).when(coreLibrary).context(); when(mockedContext.getStringResource(R.string.str_pnc_circle_type_expected)).thenReturn("expected"); when(mockedContext.getStringResource(R.string.str_pnc_circle_type_actual)).thenReturn("actual"); } @@ -312,4 +319,10 @@ public void shouldPopulateRecentlyProvidedServices() throws Exception { assertEquals(1, processedClient.recentlyProvidedServices().size()); } + + + @After + public void tearDown() { + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + } } \ No newline at end of file From 3eacf35ca537d9d822551986b9c818be9dffc042 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Fri, 20 Nov 2020 15:23:35 +0300 Subject: [PATCH 4/7] update snapshot verison --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 0103686b7..39246ac47 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.3.0-SNAPSHOT +VERSION_NAME=3.3.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application From 09fb0c5e54165643af18f0df00db8d5be0ba8b97 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Fri, 20 Nov 2020 18:10:38 +0300 Subject: [PATCH 5/7] remove @After set CoreLibrary.class to null --- .../sync/ClientProcessorTest.java | 6 ------ .../util/OpenSRPImageLoaderTest.java | 6 ------ .../org/smartregister/util/SyncUtilsTest.java | 6 ------ .../view/contract/FPClientTest.java | 21 +++++++------------ .../fragment/BaseRegisterFragmentTest.java | 6 ------ .../PNCClientPreProcessorTest.java | 7 ------- 6 files changed, 7 insertions(+), 45 deletions(-) diff --git a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java index 356126043..98df62040 100644 --- a/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/sync/ClientProcessorTest.java @@ -5,7 +5,6 @@ import org.json.JSONArray; import org.json.JSONObject; -import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -222,9 +221,4 @@ public void processCaseModelReturnsNotNUll() throws Exception { Assert.assertEquals(Boolean.TRUE, b); } - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } diff --git a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java index df658ccc9..264ed2329 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java @@ -9,7 +9,6 @@ import junit.framework.Assert; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -85,9 +84,4 @@ public void assertServiceConstructorInitializationNotNull() throws Exception { OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(Mockito.mock(Service.class), -1); Assert.assertNotNull(openSRPImageLoader); } - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } diff --git a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java index 04ab3a912..2c9af2739 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/SyncUtilsTest.java @@ -3,7 +3,6 @@ import android.content.Context; import android.content.pm.PackageManager; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -118,9 +117,4 @@ private String getMinAppVersionSetting(int minVersion) { return setting; } - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } diff --git a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java index 5abbb209e..3b38dbe6d 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/contract/FPClientTest.java @@ -1,6 +1,5 @@ package org.smartregister.view.contract; -import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -12,7 +11,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.robolectric.util.ReflectionHelpers; -import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; import org.smartregister.R; @@ -23,7 +21,7 @@ @RunWith(PowerMockRunner.class) @PrepareForTest(CoreLibrary.class) -public class FPClientTest { +public class FPClientTest { @Mock private Context context; @@ -34,7 +32,7 @@ public class FPClientTest { CoreLibrary coreLibrary; @Before - public void setUp() { + public void setUp() { MockitoAnnotations.initMocks(this); ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary); doReturn(context).when(coreLibrary).context(); @@ -45,7 +43,7 @@ public void setUp() { } @Test - public void shouldReturnTheReferralFollowUpAlert() { + public void shouldReturnTheReferralFollowUpAlert() { fpClient.withAlerts(Arrays.asList(new AlertDTO("FP Referral Followup", "normal", "2013-02-02") , new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02") @@ -72,7 +70,7 @@ public void shouldReturnTheReferralFollowUpAlert() { } @Test - public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() { + public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExist() { fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("FP Followup", "normal", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) @@ -92,7 +90,7 @@ public void shouldSetFPFollowupDataIfAFPFollowupExistsAndReferralAlertDoesNotExi } @Test - public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() { + public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAlertExitsAndReferralDataAndFPFollowUpIsNotSpecified() { fpClient.withAlerts(Arrays.asList(new AlertDTO("OCP Refill", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) .withFPMethod("female_sterilization"); @@ -111,7 +109,7 @@ public void shouldSetFemaleSterilizationFollowUpDataWhenAFemaleSterilizationAler } @Test - public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() { + public void shouldOnlySetFemaleSterilizationFollowUpDataWhenFPMethodIsAlsoFemaleSterilization() { fpClient.withAlerts(Arrays.asList(new AlertDTO("Male Sterilization Followup", "urgent", "2013-02-02") , new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02"))) .withFPMethod("female_sterilization"); @@ -148,7 +146,7 @@ public void shouldSetCondomRefillDataOnlyIfFPMethodIsAlsoCondom() { } @Test - public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() { + public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndNotAnyOfSterilizationMethods() { fpClient.withAlerts(Arrays.asList(new AlertDTO("Female sterilization Followup 1", "urgent", "2013-02-02") , new AlertDTO("Condom Refill", "urgent", "2013-02-02"))) .withFPMethod("condom"); @@ -165,9 +163,4 @@ public void shouldSetCondomRefillDataOverSterilisationDataIfFPMethodIsCondomAndN Assert.assertEquals(expectedFPClient.refillFollowUps(), fpClient.refillFollowUps()); } - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java index 0a055b89a..04c064c86 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/fragment/BaseRegisterFragmentTest.java @@ -18,7 +18,6 @@ import androidx.appcompat.widget.Toolbar; import androidx.loader.app.LoaderManager; -import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -541,9 +540,4 @@ public void testRefreshSyncStatusViewsWithSyncingFalseFetchStatusNull() { baseRegisterFragment.refreshSyncStatusViews(null); Mockito.verify(baseRegisterFragment).refreshSyncProgressSpinner(); } - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java index 7e3815209..6134a9bcc 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/preProcessor/PNCClientPreProcessorTest.java @@ -1,7 +1,6 @@ package org.smartregister.view.preProcessor; import org.joda.time.LocalDate; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -319,10 +318,4 @@ public void shouldPopulateRecentlyProvidedServices() throws Exception { assertEquals(1, processedClient.recentlyProvidedServices().size()); } - - - @After - public void tearDown() { - ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); - } } \ No newline at end of file From 65ca54a0e1e21bd1fc9d012c5930913fcabf2548 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Tue, 24 Nov 2020 13:02:59 +0300 Subject: [PATCH 6/7] add test --- .../org/smartregister/util/UtilsTest.java | 21 +++++++++++++++++++ 1 file changed, 21 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 671d1e3b0..9cf599778 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -31,6 +31,7 @@ import org.smartregister.domain.jsonmapping.util.TeamMember; import org.smartregister.receiver.SyncStatusBroadcastReceiver; import org.smartregister.repository.AllSharedPreferences; +import org.smartregister.service.UserService; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -46,6 +47,13 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +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; @@ -449,5 +457,18 @@ public void testGetAppIdShouldReturnAppId() { public void testGetAppVersionShouldReturnAppVersion() { assertNull(Utils.getAppVersion(RuntimeEnvironment.application)); } + + @Test + public void testLogoutUserShouldInvokeRequiredMethods() { + org.smartregister.Context opensrpContext = spy(CoreLibrary.getInstance().context()); + Context context = spy(opensrpContext.applicationContext()); + doReturn(context).when(opensrpContext).applicationContext(); + UserService mockUserService = mock(UserService.class); + doReturn(mockUserService).when(opensrpContext).userService(); + Utils.logoutUser(opensrpContext, "logged out"); + verify(mockUserService, times(1)).forceRemoteLogin(anyString()); + verify(mockUserService, times(1)).logoutSession(); + verify(context, times(1)).startActivity(any(Intent.class)); + } } From 9593623c4699bd595b0e5e57e6bc7233c48ad571 Mon Sep 17 00:00:00 2001 From: bennsimon Date: Tue, 24 Nov 2020 15:56:12 +0300 Subject: [PATCH 7/7] add test --- .../java/org/smartregister/util/Utils.java | 2 +- .../org/smartregister/util/UtilsTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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 147186fb6..3145d555c 100644 --- a/opensrp-app/src/main/java/org/smartregister/util/Utils.java +++ b/opensrp-app/src/main/java/org/smartregister/util/Utils.java @@ -546,7 +546,7 @@ public static void hideKeyboard(Context context, View view) { inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); } } catch (Exception e) { - logError("Error encountered while hiding keyboard " + e); + Timber.e(e, "Error encountered while hiding keyboard"); } } 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 9cf599778..9ad262b03 100644 --- a/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-app/src/test/java/org/smartregister/util/UtilsTest.java @@ -1,8 +1,11 @@ package org.smartregister.util; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.view.View; +import android.view.inputmethod.InputMethodManager; import android.widget.TableRow; import androidx.test.core.app.ApplicationProvider; @@ -49,8 +52,11 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; 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; @@ -470,5 +476,21 @@ public void testLogoutUserShouldInvokeRequiredMethods() { verify(mockUserService, times(1)).logoutSession(); verify(context, times(1)).startActivity(any(Intent.class)); } + + @Test + public void testHideKeyboardShouldInvokeRequireMethods() { + Activity activity = mock(Activity.class); + + View view = mock(View.class); + doReturn(view).when(activity).getCurrentFocus(); + + InputMethodManager keyboard = mock(InputMethodManager.class); + + doReturn(keyboard).when(activity).getSystemService(Context.INPUT_METHOD_SERVICE); + + Utils.hideKeyboard(activity); + + verify(keyboard, only()).hideSoftInputFromWindow(isNull(), eq(0)); + } }