Skip to content

Commit

Permalink
Merge pull request #628 from OpenSRP/rkareko-tt-24
Browse files Browse the repository at this point in the history
SyncIntentService Tests
  • Loading branch information
Rkareko authored Aug 18, 2020
2 parents e1f4043 + 19a4908 commit 24d58ed
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +49,12 @@ public class SyncIntentServiceTest extends BaseRobolectricUnitTest {
@Captor
private ArgumentCaptor<Intent> intentArgumentCaptor;

@Captor
private ArgumentCaptor<String> stringArgumentCaptor;

@Mock
private HTTPAgent httpAgent;

private Context context = RuntimeEnvironment.application;

private SyncIntentService syncIntentService;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 24d58ed

Please sign in to comment.