FormSubmissionSyncService { public FetchStatus pullFromServer() { FetchStatus dataStatus = nothingFetched; String anmId = allSharedPreferences.fetchRegisteredANM(); int downloadBatchSize = configuration.syncDownloadBatchSize(); String baseURL = configuration.dristhiBaseURL(); while (true) { String uri = format("{0}/{1}?anm-id={2}&timestamp={3}&batch-size={4}", baseURL, FORM_SUBMISSIONS_PATH, anmId, allSettings.fetchPreviousFormSyncIndex(), downloadBatchSize); Response<String> response = httpAgent.fetch(uri); if (response.isFailure()) { logError(format("Form submissions pull failed.")); return fetchedFailed; } List<FormSubmissionDTO> formSubmissions = new Gson().fromJson(response.payload(), new TypeToken<List<FormSubmissionDTO>>() { }.getType()); if (formSubmissions.isEmpty()) { return dataStatus; } else { formSubmissionService.processSubmissions(toDomain(formSubmissions)); dataStatus = fetched; } } } FormSubmissionSyncService(FormSubmissionService formSubmissionService, HTTPAgent httpAgent,
                                     FormDataRepository formDataRepository, AllSettings allSettings,
                                     AllSharedPreferences allSharedPreferences, DristhiConfiguration configuration); FetchStatus sync(); void pushToServer(); FetchStatus pullFromServer(); static final String FORM_SUBMISSIONS_PATH; }
@Test public void shouldPullFormSubmissionsFromServerInBatchesAndDelegateToProcessing() throws Exception { this.expectedFormSubmissionsDto = asList(new FormSubmissionDTO( "anm id 1", "id 1", "entity id 1", "form name", formInstanceJSON, "123", "1")); List<FormSubmission> expectedFormSubmissions = asList(new FormSubmission("id 1", "entity id 1", "form name", formInstanceJSON, "123", SYNCED, "1")); when(configuration.syncDownloadBatchSize()).thenReturn(1); when(allSettings.fetchPreviousFormSyncIndex()).thenReturn("122"); when(httpAgent.fetch("http: .thenReturn(new Response<String>(success, new Gson().toJson(this.expectedFormSubmissionsDto)), new Response<String>(success, new Gson().toJson(Collections.emptyList()))); FetchStatus fetchStatus = service.pullFromServer(); assertEquals(fetched, fetchStatus); verify(httpAgent, times(2)) .fetch("http: verify(formSubmissionService).processSubmissions(expectedFormSubmissions); }
@Test public void shouldReturnNothingFetchedStatusWhenNoFormSubmissionsAreGotFromServer() throws Exception { when(configuration.syncDownloadBatchSize()).thenReturn(1); when(allSettings.fetchPreviousFormSyncIndex()).thenReturn("122"); when(httpAgent.fetch("http: .thenReturn(new Response<String>(success, new Gson().toJson(Collections.emptyList()))); FetchStatus fetchStatus = service.pullFromServer(); assertEquals(nothingFetched, fetchStatus); verify(httpAgent).fetch("http: verifyZeroInteractions(formSubmissionService); }
@Test public void shouldNotDelegateToProcessingIfPullFails() throws Exception { when(configuration.syncDownloadBatchSize()).thenReturn(1); when(allSettings.fetchPreviousFormSyncIndex()).thenReturn("122"); when(httpAgent.fetch("http: .thenReturn(new Response<String>(failure, null)); FetchStatus fetchStatus = service.pullFromServer(); assertEquals(FetchStatus.fetchedFailed, fetchStatus); verifyZeroInteractions(formSubmissionService); }