-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
258 additions
and
5 deletions.
There are no files selected for viewing
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
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
63 changes: 63 additions & 0 deletions
63
...p/src/test/java/org/smartregister/sync/intent/DocumentConfigurationIntentServiceTest.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,63 @@ | ||
package org.smartregister.sync.intent; | ||
|
||
import org.json.JSONException; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.BaseRobolectricUnitTest; | ||
import org.smartregister.Context; | ||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.exception.NoHttpResponseException; | ||
import org.smartregister.service.DocumentConfigurationService; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020. | ||
*/ | ||
public class DocumentConfigurationIntentServiceTest extends BaseRobolectricUnitTest { | ||
|
||
private DocumentConfigurationIntentService documentConfigurationIntentService; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
documentConfigurationIntentService = Mockito.spy(Robolectric.buildIntentService(DocumentConfigurationIntentService.class) | ||
.create() | ||
.get()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
ReflectionHelpers.setStaticField(Context.class, "context", null); | ||
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); | ||
} | ||
|
||
@Test | ||
public void onStartCommandShouldInstantiateVariables() { | ||
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "httpAgent")); | ||
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "manifestRepository")); | ||
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "clientFormRepository")); | ||
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "configuration")); | ||
|
||
documentConfigurationIntentService.onStartCommand(null, 0, 900); | ||
|
||
|
||
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "httpAgent")); | ||
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "manifestRepository")); | ||
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "clientFormRepository")); | ||
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "configuration")); | ||
} | ||
|
||
@Test | ||
public void onHandleIntentShouldCallDocumentConfigurationServiceFetchManifest() throws JSONException, NoHttpResponseException { | ||
DocumentConfigurationService documentConfigurationService = Mockito.mock(DocumentConfigurationService.class); | ||
Mockito.doReturn(documentConfigurationService).when(documentConfigurationIntentService).getDocumentConfigurationService(); | ||
|
||
documentConfigurationIntentService.onHandleIntent(null); | ||
|
||
Mockito.verify(documentConfigurationService).fetchManifest(); | ||
Mockito.verify(documentConfigurationIntentService).getDocumentConfigurationService(); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
opensrp-app/src/test/java/org/smartregister/sync/intent/P2pProcessRecordsServiceTest.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,108 @@ | ||
package org.smartregister.sync.intent; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mockito; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.BaseRobolectricUnitTest; | ||
import org.smartregister.Context; | ||
import org.smartregister.CoreLibrary; | ||
import org.smartregister.TestP2pApplication; | ||
import org.smartregister.domain.FetchStatus; | ||
import org.smartregister.domain.db.EventClient; | ||
import org.smartregister.repository.AllSharedPreferences; | ||
import org.smartregister.repository.EventClientRepository; | ||
import org.smartregister.sync.ClientProcessorForJava; | ||
import org.smartregister.view.activity.DrishtiApplication; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020. | ||
*/ | ||
@Config(application = TestP2pApplication.class) | ||
public class P2pProcessRecordsServiceTest extends BaseRobolectricUnitTest { | ||
|
||
private P2pProcessRecordsService p2pProcessRecordsService; | ||
|
||
|
||
private EventClientRepository eventClientRepository; | ||
private ClientProcessorForJava clientProcessorForJava; | ||
private AllSharedPreferences allSharedPreferences; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
p2pProcessRecordsService = Mockito.spy(Robolectric.buildService(P2pProcessRecordsService.class) | ||
.create() | ||
.get()); | ||
|
||
eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository); | ||
|
||
clientProcessorForJava = Mockito.spy(DrishtiApplication.getInstance().getClientProcessor()); | ||
ReflectionHelpers.setStaticField(ClientProcessorForJava.class, "instance", clientProcessorForJava); | ||
|
||
|
||
allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().allSharedPreferences()); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", null); | ||
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", null); | ||
ReflectionHelpers.setStaticField(ClientProcessorForJava.class, "instance", null); | ||
ReflectionHelpers.setStaticField(Context.class, "context", null); | ||
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null); | ||
} | ||
|
||
@Test | ||
public void onHandleIntentShouldNotProcessEventsIfPeerToPeerUnprocessedEventsReturnsFalse() throws Exception { | ||
// Mock dependencies | ||
int maxEventClientRowId = 20; | ||
CoreLibrary.getInstance().context().allSharedPreferences().setLastPeerToPeerSyncProcessedEvent(maxEventClientRowId); | ||
List<EventClient> eventClientList = new ArrayList<>(); | ||
eventClientList.add(new EventClient(null, null)); | ||
Mockito.doReturn(new P2pProcessRecordsService.EventClientQueryResult(maxEventClientRowId, eventClientList)).when(eventClientRepository).fetchEventClientsByRowId(maxEventClientRowId); | ||
Mockito.doReturn(maxEventClientRowId).when(eventClientRepository).getMaxRowId(EventClientRepository.Table.event); | ||
Mockito.doNothing().when(clientProcessorForJava).processClient(eventClientList); | ||
|
||
// Call method under test | ||
p2pProcessRecordsService.onHandleIntent(null); | ||
|
||
// Verifications and assertions | ||
Mockito.verify(eventClientRepository, Mockito.times(1)).getMaxRowId(EventClientRepository.Table.event); | ||
Mockito.verify(eventClientRepository, Mockito.times(1)).fetchEventClientsByRowId(maxEventClientRowId); | ||
Mockito.verify(clientProcessorForJava, Mockito.times(1)).processClient(eventClientList); | ||
Mockito.verify(allSharedPreferences, Mockito.times(1)).resetLastPeerToPeerSyncProcessedEvent(); | ||
Assert.assertTrue(CoreLibrary.getInstance().isPeerToPeerProcessing()); | ||
Mockito.verify(p2pProcessRecordsService, Mockito.times(1)).sendSyncStatusBroadcastMessage(FetchStatus.fetched); | ||
} | ||
|
||
|
||
@Test | ||
public void onHandleIntentShouldProcessEventsIfPeerToPeerUnprocessedEventsReturnsTrue() throws Exception { | ||
|
||
p2pProcessRecordsService.onHandleIntent(null); | ||
|
||
Mockito.verify(eventClientRepository, Mockito.never()).getMaxRowId(Mockito.any(EventClientRepository.Table.class)); | ||
Mockito.verify(clientProcessorForJava, Mockito.never()).processClient(ArgumentMatchers.<List< EventClient>>any()); | ||
} | ||
|
||
@Test | ||
public void onDestroyShouldSetPeerProcessingToFalse() { | ||
CoreLibrary.getInstance().setPeerToPeerProcessing(true); | ||
Assert.assertTrue(CoreLibrary.getInstance().isPeerToPeerProcessing()); | ||
|
||
// Call the method | ||
p2pProcessRecordsService.onDestroy(); | ||
|
||
Assert.assertFalse(CoreLibrary.getInstance().isPeerToPeerProcessing()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
opensrp-app/src/test/java/org/smartregister/sync/intent/SettingsSyncIntentServiceTest.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,71 @@ | ||
package org.smartregister.sync.intent; | ||
|
||
import android.content.Intent; | ||
|
||
import org.json.JSONException; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.robolectric.Robolectric; | ||
import org.smartregister.AllConstants; | ||
import org.smartregister.BaseRobolectricUnitTest; | ||
|
||
|
||
/** | ||
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020. | ||
*/ | ||
public class SettingsSyncIntentServiceTest extends BaseRobolectricUnitTest { | ||
|
||
private SettingsSyncIntentService settingsSyncIntentService; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
settingsSyncIntentService = Robolectric.buildIntentService(SettingsSyncIntentService.class) | ||
.create() | ||
.get(); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void onHandleIntent() { | ||
// TODO: Implement this in the next round of tests | ||
Assert.assertEquals(2, 1 + 1); | ||
} | ||
|
||
@Test | ||
public void processSettingsShouldReturnTrueWhenIntentIsNull() throws JSONException { | ||
settingsSyncIntentService.syncSettingsServiceHelper = Mockito.spy(settingsSyncIntentService.syncSettingsServiceHelper); | ||
Assert.assertTrue(settingsSyncIntentService.processSettings(null)); | ||
|
||
Mockito.verify(settingsSyncIntentService.syncSettingsServiceHelper, Mockito.never()).processIntent(); | ||
} | ||
|
||
@Test | ||
public void processSettingsShouldReturnTrueAndCallProcessIntentWhenIntentIsNotNull() throws JSONException { | ||
settingsSyncIntentService.syncSettingsServiceHelper = Mockito.spy(settingsSyncIntentService.syncSettingsServiceHelper); | ||
Mockito.doReturn(30).when(settingsSyncIntentService.syncSettingsServiceHelper).processIntent(); | ||
|
||
Intent intent = new Intent(); | ||
|
||
Assert.assertTrue(settingsSyncIntentService.processSettings(intent)); | ||
Mockito.verify(settingsSyncIntentService.syncSettingsServiceHelper, Mockito.times(1)).processIntent(); | ||
|
||
Assert.assertEquals(30, intent.getIntExtra(AllConstants.INTENT_KEY.SYNC_TOTAL_RECORDS, 0)); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void onCreate() { | ||
// TODO: Implement this in the next round of tests | ||
Assert.assertEquals(2, 1 + 1); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void onDestroy() { | ||
// TODO: Implement this in the next round of tests | ||
Assert.assertEquals(2, 1 + 1); | ||
} | ||
} |