Skip to content

Commit

Permalink
Merge pull request #624 from OpenSRP/kigamba-tt-11-08-2020
Browse files Browse the repository at this point in the history
Add tests for SettingsSyncedCheck
  • Loading branch information
githengi authored Aug 24, 2020
2 parents de49325 + db83ea5 commit 82dde40
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 22 deletions.
Empty file added opensrp-app/profile-pig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.smartregister.sync.P2PClassifier;
import org.smartregister.view.activity.DrishtiApplication;


import static org.mockito.Mockito.mock;

/**
Expand All @@ -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
}
Expand Down
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
}
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}
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();
}

}
Loading

0 comments on commit 82dde40

Please sign in to comment.