SyncAllDataAsyncTask extends SynchronisationAsyncTask<T> { protected void sync() throws JSONException, IOException, HttpException { List<T> recordsToUpload = repository.toBeSynced(); List<String> idsToDownload; Boolean isBlacklisted = deviceService.isBlacklisted(); if(isBlacklisted){ sendRecordsToServer(recordsToUpload); if (repository.toBeSynced().isEmpty()) { deviceService.wipeData(); } } else { idsToDownload = recordSyncService.getIdsToDownload(); setProgressBarParameters(idsToDownload, recordsToUpload); setProgressAndNotify(context.getString(R.string.synchronize_step_1), 0); sendRecordsToServer(recordsToUpload); downloadRecordsFromServer(idsToDownload, numberOfUploadedRecords(recordsToUpload)); } repository.close(); } @Inject  SyncAllDataAsyncTask(FormService formService, SyncService<T> recordService,
                                DeviceService deviceService, Repository<T> recordRepository,
                                User user);   }
@Test public void shouldSyncFormsAndChildren() throws Exception { Child child1 = mock(Child.class); Child child2 = mock(Child.class); given(childRepository.toBeSynced()).willReturn(newArrayList(child1, child2)); syncAllDataAsyncTask.setContext(rapidFtrActivity); syncAllDataAsyncTask.execute(); verify(formService).downloadPublishedFormSections(); verify(childSyncService).sync(child1, currentUser); verify(childSyncService).sync(child2, currentUser); }
@Test public void shouldNotSyncChildrenIfCancelled() throws Exception { Child child1 = mock(Child.class); Child child2 = mock(Child.class); given(childRepository.toBeSynced()).willReturn(newArrayList(child1, child2)); syncAllDataAsyncTask.setContext(rapidFtrActivity); syncAllDataAsyncTask = spy(syncAllDataAsyncTask); doReturn(true).when(syncAllDataAsyncTask).isCancelled(); syncAllDataAsyncTask.onPreExecute(); syncAllDataAsyncTask.doInBackground(); verify(childSyncService, never()).sync(child1, currentUser); verify(childSyncService, never()).sync(child2, currentUser); }
@Test public void shouldNotGetIncomingChildrenFromServerIfBlacklisted() throws Exception { syncAllDataAsyncTask.setContext(rapidFtrActivity); Child child1 = mock(Child.class); ArrayList<Child> childList = new ArrayList<Child>(); childList.add(child1); given(childRepository.toBeSynced()).willReturn(childList); given(deviceService.isBlacklisted()).willReturn(true); syncAllDataAsyncTask.execute(); verify(childSyncService).sync(child1, currentUser); verify(childSyncService, never()).getRecord(any(String.class)); verify(childSyncService, never()).getIdsToDownload(); }
@Test public void shouldCompareAndRetrieveIdsToBeDownloadedFromServer() throws JSONException, IOException, HttpException { SharedPreferences sharedPreferences = RapidFtrApplication.getApplicationInstance().getSharedPreferences(); sharedPreferences.edit().putLong(RapidFtrApplication.LAST_CHILD_SYNC, 0).commit(); Child child1 = mock(Child.class); Child child2 = mock(Child.class); given(childRepository.toBeSynced()).willReturn(newArrayList(child1, child2)); given(childSyncService.getIdsToDownload()).willReturn(Arrays.asList("qwerty0987", "abcd1234")); given(childSyncService.getRecord("qwerty0987")).willReturn(mock(Child.class)); given(childSyncService.getRecord("abcd1234")).willReturn(mock(Child.class)); syncAllDataAsyncTask.setContext(rapidFtrActivity); syncAllDataAsyncTask.execute(); verify(formService).downloadPublishedFormSections(); verify(childSyncService).sync(child1, currentUser); verify(childSyncService).sync(child2, currentUser); verify(childSyncService).getIdsToDownload(); verify(childSyncService).getRecord("qwerty0987"); verify(childSyncService).getRecord("abcd1234"); }