diff --git a/opensrp-app/profile-pig.png b/opensrp-app/profile-pig.png new file mode 100644 index 000000000..e69de29bb diff --git a/opensrp-app/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java b/opensrp-app/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java index 4c8b49057..da55e5f0c 100644 --- a/opensrp-app/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java +++ b/opensrp-app/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java @@ -229,5 +229,17 @@ public String getPassword() { public void setPassword(String password) { this.password = password; } + + public void setHttpAgent(HTTPAgent httpAgent) { + this.httpAgent = httpAgent; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public void setSharedPreferences(AllSharedPreferences sharedPreferences) { + this.sharedPreferences = sharedPreferences; + } } diff --git a/opensrp-app/src/test/java/org/smartregister/TestApplication.java b/opensrp-app/src/test/java/org/smartregister/TestApplication.java index 855816fdd..b1fa18e84 100644 --- a/opensrp-app/src/test/java/org/smartregister/TestApplication.java +++ b/opensrp-app/src/test/java/org/smartregister/TestApplication.java @@ -7,7 +7,6 @@ import org.smartregister.sync.P2PClassifier; import org.smartregister.view.activity.DrishtiApplication; - import static org.mockito.Mockito.mock; /** @@ -22,7 +21,7 @@ public void onCreate() { mInstance = this; context = Context.getInstance(); context.updateApplicationContext(getApplicationContext()); - CoreLibrary.init(context, null, 1588062490000l); + CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l); setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat } diff --git a/opensrp-app/src/test/java/org/smartregister/TestP2pApplication.java b/opensrp-app/src/test/java/org/smartregister/TestP2pApplication.java new file mode 100644 index 000000000..aada09847 --- /dev/null +++ b/opensrp-app/src/test/java/org/smartregister/TestP2pApplication.java @@ -0,0 +1,27 @@ +package org.smartregister; + +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.repository.AllSharedPreferences; + +import static android.preference.PreferenceManager.getDefaultSharedPreferences; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 18-08-2020. + */ +public class TestP2pApplication extends TestApplication { + + @Override + public void onCreate() { + mInstance = this; + context = Context.getInstance(); + context.updateApplicationContext(getApplicationContext()); + + AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(context.applicationContext())); + allSharedPreferences.updateANMUserName("demo"); + ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); + CoreLibrary.init(context, null, 1588062490000l, new P2POptions(true)); + + + setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat + } +} diff --git a/opensrp-app/src/test/java/org/smartregister/multitenant/check/SettingsSyncedCheckTest.java b/opensrp-app/src/test/java/org/smartregister/multitenant/check/SettingsSyncedCheckTest.java new file mode 100644 index 000000000..8ccbfab48 --- /dev/null +++ b/opensrp-app/src/test/java/org/smartregister/multitenant/check/SettingsSyncedCheckTest.java @@ -0,0 +1,59 @@ +package org.smartregister.multitenant.check; + +import org.json.JSONException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.BaseRobolectricUnitTest; +import org.smartregister.DristhiConfiguration; +import org.smartregister.exception.PreResetAppOperationException; +import org.smartregister.repository.AllSettings; +import org.smartregister.shadows.ShadowSyncSettingsServiceHelper; +import org.smartregister.view.activity.DrishtiApplication; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 11-08-2020. + */ +public class SettingsSyncedCheckTest extends BaseRobolectricUnitTest { + + public SettingsSyncedCheck settingsSyncedCheck; + + @Before + public void setUp() throws Exception { + settingsSyncedCheck = Mockito.spy(new SettingsSyncedCheck()); + } + + @Test + public void isCheckOk() { + Mockito.doReturn(true).when(settingsSyncedCheck).isSettingsSynced(DrishtiApplication.getInstance()); + + settingsSyncedCheck.isCheckOk(DrishtiApplication.getInstance()); + + Mockito.verify(settingsSyncedCheck).isSettingsSynced(DrishtiApplication.getInstance()); + } + + @Config(shadows = {ShadowSyncSettingsServiceHelper.class}) + @Test + public void performPreResetAppOperations() throws PreResetAppOperationException, JSONException { + DristhiConfiguration dristhiConfiguration = Mockito.spy(DrishtiApplication.getInstance().getContext().configuration()); + Mockito.doReturn("https://someurl.com").when(dristhiConfiguration).dristhiBaseURL(); + ReflectionHelpers.setField(DrishtiApplication.getInstance().getContext(), "configuration", dristhiConfiguration); + + settingsSyncedCheck.performPreResetAppOperations(DrishtiApplication.getInstance()); + + Assert.assertEquals(1, ShadowSyncSettingsServiceHelper.processIntent); + } + + @Test + public void isSettingsSynced() { + AllSettings allSettings = Mockito.spy(DrishtiApplication.getInstance().getContext().allSettings()); + ReflectionHelpers.setField(DrishtiApplication.getInstance().getContext(), "allSettings", allSettings); + + Assert.assertTrue(settingsSyncedCheck.isSettingsSynced(DrishtiApplication.getInstance())); + + Mockito.verify(allSettings).getUnsyncedSettingsCount(); + } +} \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/multitenant/check/TaskSyncedCheckTest.java b/opensrp-app/src/test/java/org/smartregister/multitenant/check/TaskSyncedCheckTest.java index d35b7e9df..3096e378f 100644 --- a/opensrp-app/src/test/java/org/smartregister/multitenant/check/TaskSyncedCheckTest.java +++ b/opensrp-app/src/test/java/org/smartregister/multitenant/check/TaskSyncedCheckTest.java @@ -1,5 +1,6 @@ package org.smartregister.multitenant.check; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; @@ -24,6 +25,13 @@ public void setUp() throws Exception { taskSyncedCheck = Mockito.spy(new TaskSyncedCheck()); } + @After + public void tearDown() throws Exception { + // This fixes an issue where TaskServiceHelperTest fails due to the TaskServiceHelper being initialised and state + // changed from this tests onwards + ReflectionHelpers.setStaticField(TaskServiceHelper.class, "instance", null); + } + @Test public void isCheckOkShouldCallIsTaskSynced() { Mockito.doReturn(false).when(taskSyncedCheck).isTaskSynced(Mockito.eq(DrishtiApplication.getInstance())); diff --git a/opensrp-app/src/test/java/org/smartregister/repository/P2PSenderTransferDaoTest.java b/opensrp-app/src/test/java/org/smartregister/repository/P2PSenderTransferDaoTest.java index ff6e586e3..0a26622d2 100644 --- a/opensrp-app/src/test/java/org/smartregister/repository/P2PSenderTransferDaoTest.java +++ b/opensrp-app/src/test/java/org/smartregister/repository/P2PSenderTransferDaoTest.java @@ -4,19 +4,28 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.robolectric.annotation.Config; import org.robolectric.util.ReflectionHelpers; +import org.smartregister.AllConstants; import org.smartregister.BaseRobolectricUnitTest; import org.smartregister.CoreLibrary; import org.smartregister.TestApplication; +import org.smartregister.TestP2pApplication; +import org.smartregister.domain.SyncStatus; import org.smartregister.p2p.model.DataType; import org.smartregister.p2p.sync.data.JsonData; +import org.smartregister.p2p.sync.data.MultiMediaData; import org.smartregister.sync.P2PClassifier; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; import java.util.TreeSet; /** * Created by Ephraim Kigamba - nek.eam@gmail.com on 04-08-2020. */ +@Config(application = TestP2pApplication.class) public class P2PSenderTransferDaoTest extends BaseRobolectricUnitTest { private P2PSenderTransferDao p2PSenderTransferDao; @@ -83,6 +92,28 @@ public void getJsonDataShouldCallEventRepositoryGetClientsWhenDataTypeIsClient() Assert.assertEquals(jsonData, actualJsonData); } + @Test + public void getJsonDataShouldCallEventRepositoryGetClientsWithLocationIdWhenDataTypeIsClientAndP2pClassifierIsConfigured() { + EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository()); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository); + + ((TestApplication) TestApplication.getInstance()).setP2PClassifier(Mockito.mock(P2PClassifier.class)); + + int lastRecordId = 789; + int batchSize = 100; + + JsonData jsonData = Mockito.mock(JsonData.class); + Mockito.doReturn(jsonData).when(eventClientRepository).getClientsWithLastLocationID(lastRecordId, batchSize); + + // Call the method under test + JsonData actualJsonData = p2PSenderTransferDao.getJsonData(p2PSenderTransferDao.client, lastRecordId, batchSize); + + + // Verify that the repository was called + Mockito.verify(eventClientRepository).getClientsWithLastLocationID(lastRecordId, batchSize); + Assert.assertEquals(jsonData, actualJsonData); + } + @Test public void getJsonDataShouldCallStructureRepositoryGetStructuresWhenDataTypeIsStructure() { StructureRepository structureRepository = Mockito.spy(CoreLibrary.getInstance().context().getStructureRepository()); @@ -125,20 +156,109 @@ public void getJsonDataShouldCallTaskRepositoryGetTasksWhenDataTypeIsTask() { @Test public void getJsonDataShouldCallEventRepositoryGetClientsWhenContextHasForeignEventsAndDataTypeIsForeignClient() { ((TestApplication) TestApplication.getInstance()).setP2PClassifier(Mockito.mock(P2PClassifier.class)); - EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getForeignEventClientRepository()); - ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "foreignEventClientRepository", eventClientRepository); + EventClientRepository foreignEventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getForeignEventClientRepository()); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "foreignEventClientRepository", foreignEventClientRepository); int lastRecordId = 789; int batchSize = 100; JsonData jsonData = Mockito.mock(JsonData.class); - Mockito.doReturn(jsonData).when(eventClientRepository).getClients(lastRecordId, batchSize); + Mockito.doReturn(jsonData).when(foreignEventClientRepository).getClients(lastRecordId, batchSize); // Call the method under test JsonData actualJsonData = p2PSenderTransferDao.getJsonData(p2PSenderTransferDao.foreignClient, lastRecordId, batchSize); // Verify that the repository was called - Mockito.verify(eventClientRepository).getClients(lastRecordId, batchSize); + Mockito.verify(foreignEventClientRepository).getClients(lastRecordId, batchSize); Assert.assertEquals(jsonData, actualJsonData); } + + @Test + public void getJsonDataShouldCallEventRepositoryGetEventsWhenContextHasForeignEventsAndDataTypeIsForeignEvent() { + ((TestApplication) TestApplication.getInstance()).setP2PClassifier(Mockito.mock(P2PClassifier.class)); + EventClientRepository foreignEventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getForeignEventClientRepository()); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "foreignEventClientRepository", foreignEventClientRepository); + + int lastRecordId = 789; + int batchSize = 100; + + JsonData jsonData = Mockito.mock(JsonData.class); + Mockito.doReturn(jsonData).when(foreignEventClientRepository).getEvents(lastRecordId, batchSize); + + // Call the method under test + JsonData actualJsonData = p2PSenderTransferDao.getJsonData(p2PSenderTransferDao.foreignEvent, lastRecordId, batchSize); + + // Verify that the repository was called + Mockito.verify(foreignEventClientRepository).getEvents(lastRecordId, batchSize); + Assert.assertEquals(jsonData, actualJsonData); + } + + @Config(application = TestP2pApplication.class) + @Test + public void getJsonDataShouldReturnNullAndMakeNoCallsWhenContextHasForeignEventsAndDataTypeIsCoach() { + EventClientRepository foreignEventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getForeignEventClientRepository()); + EventClientRepository eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository()); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "foreignEventClientRepository", foreignEventClientRepository); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository); + + int lastRecordId = 789; + int batchSize = 100; + + JsonData jsonData = Mockito.mock(JsonData.class); + Mockito.doReturn(jsonData).when(foreignEventClientRepository).getEvents(lastRecordId, batchSize); + + DataType coachDataType = new DataType("coach", DataType.Type.NON_MEDIA, 99); + + // Call the method under test + JsonData actualJsonData = p2PSenderTransferDao.getJsonData(coachDataType, lastRecordId, batchSize); + + // Verify null is returned + Assert.assertNull(actualJsonData); + + // Verify that the repository was called + Mockito.verify(foreignEventClientRepository, Mockito.never()).getEvents(lastRecordId, batchSize); + Mockito.verify(foreignEventClientRepository, Mockito.never()).getClients(lastRecordId, batchSize); + Mockito.verify(eventClientRepository, Mockito.never()).getEvents(lastRecordId, batchSize); + Mockito.verify(eventClientRepository, Mockito.never()).getClients(lastRecordId, batchSize); + } + + + @Test + public void getMultiMediaDataShouldCallImageRepositoryAndReturnMultiMediaDataWhenDataTypeIsProfielPic() throws IOException { + ((TestApplication) TestApplication.getInstance()).setP2PClassifier(Mockito.mock(P2PClassifier.class)); + ImageRepository imageRepository = Mockito.spy(CoreLibrary.getInstance().context().imageRepository()); + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "imageRepository", imageRepository); + + int lastRecordId = 789; + String anmId = "90293-fsdawecSD"; + String imagePath = "profile-pig.png"; + + // Create the image + new File(imagePath).createNewFile(); + + HashMap imageDetails = new HashMap<>(); + imageDetails.put(ImageRepository.filepath_COLUMN, imagePath); + imageDetails.put(ImageRepository.syncStatus_COLUMN, SyncStatus.SYNCED.value()); + imageDetails.put(AllConstants.ROWID, 87L); + imageDetails.put(ImageRepository.filecategory_COLUMN, "profile-pic-male"); + imageDetails.put(ImageRepository.anm_ID_COLUMN, anmId); + imageDetails.put(ImageRepository.entityID_COLUMN, "entity-id"); + + Mockito.doReturn(imageDetails).when(imageRepository).getImage(lastRecordId); + + // Call the method under test + MultiMediaData actualJsonData = p2PSenderTransferDao.getMultiMediaData(p2PSenderTransferDao.profilePic, lastRecordId); + + // Verify that the repository was called + Mockito.verify(imageRepository).getImage(lastRecordId); + Assert.assertEquals("entity-id", actualJsonData.getMediaDetails().get(ImageRepository.entityID_COLUMN)); + Assert.assertEquals(anmId, actualJsonData.getMediaDetails().get(ImageRepository.anm_ID_COLUMN)); + Assert.assertEquals("profile-pic-male", actualJsonData.getMediaDetails().get(ImageRepository.filecategory_COLUMN)); + Assert.assertEquals(SyncStatus.SYNCED.value(), actualJsonData.getMediaDetails().get(ImageRepository.syncStatus_COLUMN)); + Assert.assertEquals(String.valueOf(87L), actualJsonData.getMediaDetails().get(AllConstants.ROWID)); + Assert.assertEquals(87L, actualJsonData.getRecordId()); + Assert.assertNotNull(actualJsonData.getFile()); + + Mockito.verify(imageRepository).getImage(lastRecordId); + } } \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/shadows/ShadowSyncSettingsServiceHelper.java b/opensrp-app/src/test/java/org/smartregister/shadows/ShadowSyncSettingsServiceHelper.java new file mode 100644 index 000000000..83d786f42 --- /dev/null +++ b/opensrp-app/src/test/java/org/smartregister/shadows/ShadowSyncSettingsServiceHelper.java @@ -0,0 +1,52 @@ +package org.smartregister.shadows; + +import org.json.JSONException; +import org.mockito.Mockito; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.RealObject; +import org.robolectric.shadow.api.Shadow; +import org.robolectric.util.ReflectionHelpers; +import org.smartregister.CoreLibrary; +import org.smartregister.repository.AllSharedPreferences; +import org.smartregister.service.HTTPAgent; +import org.smartregister.sync.helper.SyncSettingsServiceHelper; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 11-08-2020. + */ +@Implements(SyncSettingsServiceHelper.class) +public class ShadowSyncSettingsServiceHelper { + + private static SyncSettingsServiceHelper instance; + + @RealObject + private SyncSettingsServiceHelper realObject; + + public static int processIntent; + + @Implementation + public SyncSettingsServiceHelper __constructor__(String baseUrl, HTTPAgent httpAgent) { + + ReflectionHelpers.setField(realObject, "httpAgent", httpAgent); + ReflectionHelpers.setField(realObject, "baseUrl", baseUrl); + AllSharedPreferences sharedPreferences = CoreLibrary.getInstance().context().allSharedPreferences(); + ReflectionHelpers.setField(realObject, "sharedPreferences", sharedPreferences); + + realObject = Mockito.spy(realObject); + instance = realObject; + + return realObject; + } + + public static SyncSettingsServiceHelper getLastInstance() { + return instance; + } + + @Implementation + public int processIntent() throws JSONException { + processIntent++; + return Shadow.directlyOn(instance, SyncSettingsServiceHelper.class).processIntent(); + } + +} diff --git a/opensrp-app/src/test/java/org/smartregister/view/activity/SecuredActivityTest.java b/opensrp-app/src/test/java/org/smartregister/view/activity/SecuredActivityTest.java index 2b7f3bb7d..4bb52efaf 100644 --- a/opensrp-app/src/test/java/org/smartregister/view/activity/SecuredActivityTest.java +++ b/opensrp-app/src/test/java/org/smartregister/view/activity/SecuredActivityTest.java @@ -28,9 +28,8 @@ import org.smartregister.BaseRobolectricUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; -import org.smartregister.P2POptions; import org.smartregister.R; -import org.smartregister.TestApplication; +import org.smartregister.TestP2pApplication; import org.smartregister.broadcastreceivers.OpenSRPClientBroadCastReceiver; import org.smartregister.commonregistry.CommonRepositoryInformationHolder; import org.smartregister.event.Event; @@ -50,7 +49,7 @@ /** * Created by Ephraim Kigamba - nek.eam@gmail.com on 14-07-2020. */ -@Config(application = SecuredActivityTest.TestP2pApplication.class) +@Config(application = TestP2pApplication.class) public class SecuredActivityTest extends BaseRobolectricUnitTest { private SecuredActivity securedActivity; @@ -249,17 +248,4 @@ protected void onResumption() { } } - static class TestP2pApplication extends TestApplication { - - @Override - public void onCreate() { - mInstance = this; - context = Context.getInstance(); - context.updateApplicationContext(getApplicationContext()); - - CoreLibrary.init(context, null, 1588062490000l, new P2POptions(true)); - - setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat - } - } } \ No newline at end of file diff --git a/opensrp-app/user-profile.png b/opensrp-app/user-profile.png new file mode 100644 index 000000000..e69de29bb