FormSubmissionSyncService { public void pushToServer() { List<FormSubmission> pendingFormSubmissions = formDataRepository.getPendingFormSubmissions(); if (pendingFormSubmissions.isEmpty()) { return; } String jsonPayload = mapToFormSubmissionDTO(pendingFormSubmissions); Response<String> response = httpAgent.post( format("{0}/{1}", configuration.dristhiBaseURL(), FORM_SUBMISSIONS_PATH), jsonPayload); if (response.isFailure()) { logError(format("Form submissions sync failed. Submissions: {0}", pendingFormSubmissions)); return; } formDataRepository.markFormSubmissionsAsSynced(pendingFormSubmissions); logInfo(format("Form submissions sync successfully. Submissions: {0}", pendingFormSubmissions)); } 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 shouldPushPendingFormSubmissionsToServerAndMarkThemAsSynced() throws Exception { when(httpAgent.post("http: .thenReturn(new Response<String>(success, null)); service.pushToServer(); inOrder(allSettings, httpAgent, repository); verify(allSharedPreferences).fetchRegisteredANM(); verify(httpAgent).post("http: verify(repository).markFormSubmissionsAsSynced(submissions); }
@Test public void shouldNotMarkPendingSubmissionsAsSyncedIfPostFails() throws Exception { when(httpAgent.post("http: .thenReturn(new Response<String>(ResponseStatus.failure, null)); service.pushToServer(); verify(repository).getPendingFormSubmissions(); verifyNoMoreInteractions(repository); }
@Test public void shouldNotPushIfThereAreNoPendingSubmissions() throws Exception { when(repository.getPendingFormSubmissions()).thenReturn(Collections.<FormSubmission>emptyList()); service.pushToServer(); verify(repository).getPendingFormSubmissions(); verifyNoMoreInteractions(repository); verifyZeroInteractions(allSettings); verifyZeroInteractions(httpAgent); }