From c2f15ada3aef1e1732dec974dfc2296f56edf248 Mon Sep 17 00:00:00 2001 From: richard Date: Tue, 18 Aug 2020 10:43:46 +0300 Subject: [PATCH 1/3] Add pullEcFromServer when HttpAgent is null test --- .../sync/intent/SyncIntentService.java | 1 + .../sync/intent/SyncIntentServiceTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) 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..1da1a660e 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; 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..d6ce173b9 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 @@ -12,6 +12,7 @@ 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; @@ -22,6 +23,7 @@ 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; @@ -50,6 +52,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 +130,24 @@ 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)); + + } + private void initMocksForPullECFromServer() throws PackageManager.NameNotFoundException { syncIntentService = spy(syncIntentService); Whitebox.setInternalState(syncIntentService, "syncUtils", syncUtils); From 60bae2af278f54cf6ffecb46ddeb1dbadd009354 Mon Sep 17 00:00:00 2001 From: richard Date: Tue, 18 Aug 2020 11:32:09 +0300 Subject: [PATCH 2/3] Added pull EC from server sync using POST tests --- .../sync/intent/SyncIntentServiceTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) 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 d6ce173b9..cb12a32cd 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,6 +9,7 @@ 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; @@ -18,7 +19,11 @@ 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; @@ -44,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; @@ -148,6 +159,54 @@ public void testPullEcFromServerWhenHttpAgentIsNull() throws PackageManager.Name } + @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()); + + } + + 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); From 19a4908a23bf382cded37ff463345192dc557487 Mon Sep 17 00:00:00 2001 From: richard Date: Tue, 18 Aug 2020 11:42:01 +0300 Subject: [PATCH 3/3] Added pull EC from server with timeout error test --- .../sync/intent/SyncIntentService.java | 1 + .../sync/intent/SyncIntentServiceTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) 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 1da1a660e..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 @@ -157,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 cb12a32cd..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 @@ -200,6 +200,27 @@ public void testPullEcFromServerWithURLError() throws PackageManager.NameNotFoun } + @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);