diff --git a/opensrp-app/src/main/java/org/smartregister/sync/intent/SyncIntentService.java b/opensrp-app/src/main/java/org/smartregister/sync/intent/SyncIntentService.java index 86f5b8f33..bd45fbd7e 100644 --- a/opensrp-app/src/main/java/org/smartregister/sync/intent/SyncIntentService.java +++ b/opensrp-app/src/main/java/org/smartregister/sync/intent/SyncIntentService.java @@ -130,6 +130,7 @@ private synchronized void fetchRetry(final int count, boolean returnCount) { if (httpAgent == null) { complete(FetchStatus.fetchedFailed); + return; } String url = baseUrl + SYNC_URL; @@ -156,6 +157,7 @@ private synchronized void fetchRetry(final int count, boolean returnCount) { if (resp.isTimeoutError()) { FetchStatus.fetchedFailed.setDisplayValue(resp.status().displayValue()); complete(FetchStatus.fetchedFailed); + return; } if (resp.isFailure() && !resp.isUrlError() && !resp.isTimeoutError()) { diff --git a/opensrp-app/src/test/java/org/smartregister/sync/intent/SyncIntentServiceTest.java b/opensrp-app/src/test/java/org/smartregister/sync/intent/SyncIntentServiceTest.java index 76a22cff5..d1f5dca51 100644 --- a/opensrp-app/src/test/java/org/smartregister/sync/intent/SyncIntentServiceTest.java +++ b/opensrp-app/src/test/java/org/smartregister/sync/intent/SyncIntentServiceTest.java @@ -9,19 +9,26 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.reflect.Whitebox; import org.robolectric.RuntimeEnvironment; +import org.smartregister.AllConstants; import org.smartregister.BaseRobolectricUnitTest; import org.smartregister.CoreLibrary; import org.smartregister.SyncConfiguration; import org.smartregister.SyncFilter; import org.smartregister.domain.FetchStatus; +import org.smartregister.domain.Response; +import org.smartregister.domain.ResponseErrorStatus; +import org.smartregister.domain.ResponseStatus; import org.smartregister.receiver.SyncStatusBroadcastReceiver; +import org.smartregister.service.HTTPAgent; import org.smartregister.util.SyncUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -42,6 +49,12 @@ public class SyncIntentServiceTest extends BaseRobolectricUnitTest { @Captor private ArgumentCaptor intentArgumentCaptor; + @Captor + private ArgumentCaptor stringArgumentCaptor; + + @Mock + private HTTPAgent httpAgent; + private Context context = RuntimeEnvironment.application; private SyncIntentService syncIntentService; @@ -50,6 +63,7 @@ public class SyncIntentServiceTest extends BaseRobolectricUnitTest { public void setUp() { MockitoAnnotations.initMocks(this); Whitebox.setInternalState(CoreLibrary.getInstance(), "syncConfiguration", syncConfiguration); + CoreLibrary.getInstance().context().allSharedPreferences().savePreference(AllConstants.DRISHTI_BASE_URL, "https://sample-stage.smartregister.org/opensrp"); syncIntentService = new SyncIntentService(); syncIntentService.init(context); Whitebox.setInternalState(syncIntentService, "mBase", RuntimeEnvironment.application); @@ -127,6 +141,93 @@ public void testPullEcFromServerWhenSyncFilterValueIsNull() throws PackageManage } + @Test + public void testPullEcFromServerWhenHttpAgentIsNull() throws PackageManager.NameNotFoundException { + syncIntentService = spy(syncIntentService); + + Whitebox.setInternalState(syncIntentService, "httpAgent", (Object[]) null); + assertNull(Whitebox.getInternalState(syncIntentService, "httpAgent")); + + when(syncConfiguration.getSyncFilterParam()).thenReturn(SyncFilter.LOCATION); + when(syncConfiguration.getSyncFilterValue()).thenReturn("location-1"); + syncIntentService.pullECFromServer(); + verify(syncIntentService).sendBroadcast(intentArgumentCaptor.capture()); + + // sync fetch failed broadcast sent + assertEquals(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS, intentArgumentCaptor.getValue().getAction()); + assertEquals(FetchStatus.fetchedFailed, intentArgumentCaptor.getValue().getSerializableExtra(SyncStatusBroadcastReceiver.EXTRA_FETCH_STATUS)); + + } + + @Test + public void testPullEcFromServerUsingPOSTUsesCorrectURLAndParams() throws PackageManager.NameNotFoundException { + syncIntentService = spy(syncIntentService); + + initMocksForPullECFromServerUsingPOST(); + ResponseStatus responseStatus = ResponseStatus.failure; + responseStatus.setDisplayValue(ResponseErrorStatus.malformed_url.name()); + Mockito.doReturn(new Response<>(responseStatus, null)) + .when(httpAgent).postWithJsonResponse(stringArgumentCaptor.capture(), stringArgumentCaptor.capture()); + + syncIntentService.pullECFromServer(); + verify(syncIntentService).sendBroadcast(intentArgumentCaptor.capture()); + + String syncUrl = stringArgumentCaptor.getAllValues().get(0); + assertEquals("https://sample-stage.smartregister.org/opensrp/rest/event/sync", syncUrl); + String requestString = stringArgumentCaptor.getAllValues().get(1); + assertEquals("{\"locationId\":\"location-1\",\"serverVersion\":0,\"limit\":250,\"return_count\":true}", requestString); + + } + + @Test + public void testPullEcFromServerWithURLError() throws PackageManager.NameNotFoundException { + syncIntentService = spy(syncIntentService); + + initMocksForPullECFromServerUsingPOST(); + ResponseStatus responseStatus = ResponseStatus.failure; + responseStatus.setDisplayValue(ResponseErrorStatus.malformed_url.name()); + Mockito.doReturn(new Response<>(responseStatus, null)) + .when(httpAgent).postWithJsonResponse(stringArgumentCaptor.capture(), stringArgumentCaptor.capture()); + + syncIntentService.pullECFromServer(); + verify(syncIntentService).sendBroadcast(intentArgumentCaptor.capture()); + + // sync fetch failed broadcast sent + assertEquals(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS, intentArgumentCaptor.getValue().getAction()); + FetchStatus actualFetchStatus = (FetchStatus) intentArgumentCaptor.getValue().getSerializableExtra(SyncStatusBroadcastReceiver.EXTRA_FETCH_STATUS); + assertEquals(FetchStatus.fetchedFailed, actualFetchStatus); + assertEquals("malformed_url", actualFetchStatus.displayValue()); + + } + + @Test + public void testPullEcFromServerWithTimeoutError() throws PackageManager.NameNotFoundException { + syncIntentService = spy(syncIntentService); + + initMocksForPullECFromServerUsingPOST(); + ResponseStatus responseStatus = ResponseStatus.failure; + responseStatus.setDisplayValue(ResponseErrorStatus.timeout.name()); + Mockito.doReturn(new Response<>(responseStatus, null)) + .when(httpAgent).postWithJsonResponse(stringArgumentCaptor.capture(), stringArgumentCaptor.capture()); + + syncIntentService.pullECFromServer(); + verify(syncIntentService).sendBroadcast(intentArgumentCaptor.capture()); + + // sync fetch failed broadcast sent + assertEquals(SyncStatusBroadcastReceiver.ACTION_SYNC_STATUS, intentArgumentCaptor.getValue().getAction()); + FetchStatus actualFetchStatus = (FetchStatus) intentArgumentCaptor.getValue().getSerializableExtra(SyncStatusBroadcastReceiver.EXTRA_FETCH_STATUS); + assertEquals(FetchStatus.fetchedFailed, actualFetchStatus); + assertEquals("timeout", actualFetchStatus.displayValue()); + + } + + private void initMocksForPullECFromServerUsingPOST() { + Whitebox.setInternalState(syncIntentService, "httpAgent", httpAgent); + when(syncConfiguration.isSyncUsingPost()).thenReturn(true); + when(syncConfiguration.getSyncFilterParam()).thenReturn(SyncFilter.LOCATION); + when(syncConfiguration.getSyncFilterValue()).thenReturn("location-1"); + } + private void initMocksForPullECFromServer() throws PackageManager.NameNotFoundException { syncIntentService = spy(syncIntentService); Whitebox.setInternalState(syncIntentService, "syncUtils", syncUtils);