FormSubmissionService { public void processSubmissions(List<FormSubmission> formSubmissions) { for (FormSubmission submission : formSubmissions) { if (!formDataRepository.submissionExists(submission.instanceId())) { try { ziggyService.saveForm(getParams(submission), submission.instance()); } catch (Exception e) { logError(format("Form submission processing failed, with instanceId: {0}. Exception: {1}, StackTrace: {2}", submission.instanceId(), e.getMessage(), ExceptionUtils.getStackTrace(e))); } } formDataRepository.updateServerVersion(submission.instanceId(), submission.serverVersion()); allSettings.savePreviousFormSyncIndex(submission.serverVersion()); } } FormSubmissionService(ZiggyService ziggyService, FormDataRepository formDataRepository, AllSettings allSettings); void processSubmissions(List<FormSubmission> formSubmissions);  }
@Test public void shouldDelegateProcessingToZiggyServiceAndMarkAsSynced() throws Exception { List<FormSubmission> submissions = asList(create().withInstanceId("instance id 1").withVersion("122").build(), create().withInstanceId("instance id 2").withVersion("123").build()); service.processSubmissions(submissions); String paramsForFirstSubmission = new Gson().toJson( create("instanceId", "instance id 1") .put("entityId", "entity id 1") .put("formName", "form name 1") .put("version", "122") .put("sync_status", SYNCED.value()) .map()); String paramsForSecondSubmission = new Gson().toJson( create("instanceId", "instance id 2") .put("entityId", "entity id 1") .put("formName", "form name 1") .put("sync_status", SYNCED.value()) .put("version", "123") .map()); InOrder inOrder = inOrder(ziggyService, allSettings, formDataRepository); inOrder.verify(ziggyService).saveForm(paramsForFirstSubmission, "{}"); inOrder.verify(formDataRepository).updateServerVersion("instance id 1", "0"); inOrder.verify(allSettings).savePreviousFormSyncIndex("0"); inOrder.verify(ziggyService).saveForm(paramsForSecondSubmission, "{}"); inOrder.verify(formDataRepository).updateServerVersion("instance id 2", "0"); inOrder.verify(allSettings).savePreviousFormSyncIndex("0"); }
@Test public void shouldNotDelegateProcessingToZiggyServiceForProcessedSubmissions() throws Exception { FormSubmission firstFormSubmission = create().withInstanceId("instance id 1").withVersion("122").build(); FormSubmission secondFormSubmission = create().withInstanceId("instance id 2").withVersion("123").withServerVersion("1").build(); List<FormSubmission> submissions = asList(firstFormSubmission, secondFormSubmission); when(formDataRepository.submissionExists("instance id 1")).thenReturn(true); when(formDataRepository.submissionExists("instance id 2")).thenReturn(false); service.processSubmissions(submissions); String paramsForFirstSubmission = new Gson().toJson( create("instanceId", "instance id 1") .put("entityId", "entity id 1") .put("formName", "form name 1") .put("version", "122") .put("sync_status", SYNCED.value()) .map()); String paramsForSecondSubmission = new Gson().toJson( create("instanceId", "instance id 2") .put("entityId", "entity id 1") .put("formName", "form name 1") .put("version", "123") .put("sync_status", SYNCED.value()) .map()); InOrder inOrder = inOrder(ziggyService, allSettings, formDataRepository); inOrder.verify(ziggyService, times(0)).saveForm(paramsForFirstSubmission, "{}"); inOrder.verify(formDataRepository).updateServerVersion("instance id 1", "0"); inOrder.verify(allSettings).savePreviousFormSyncIndex("0"); inOrder.verify(ziggyService).saveForm(paramsForSecondSubmission, "{}"); inOrder.verify(formDataRepository).updateServerVersion("instance id 2", "1"); inOrder.verify(allSettings).savePreviousFormSyncIndex("1"); }