-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from OpenSRP/kigamba-tt-11-08-2020
Add tests for SettingsSyncedCheck
- Loading branch information
Showing
10 changed files
with
285 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
opensrp-app/src/test/java/org/smartregister/TestP2pApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 - [email protected] 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 | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
opensrp-app/src/test/java/org/smartregister/multitenant/check/SettingsSyncedCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 - [email protected] 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 - [email protected] 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<String, Object> 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); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
opensrp-app/src/test/java/org/smartregister/shadows/ShadowSyncSettingsServiceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 - [email protected] 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(); | ||
} | ||
|
||
} |
Oops, something went wrong.