FormSubmissionService { public void submit(List<FormSubmissionDTO> formSubmissionsDTO) { List<FormSubmission> formSubmissions = with(formSubmissionsDTO).convert(new Converter<FormSubmissionDTO, FormSubmission>() { @Override public FormSubmission convert(FormSubmissionDTO submission) { return FormSubmissionConverter.toFormSubmission(submission); } }); sort(formSubmissions, timeStampComparator()); for (FormSubmission submission : formSubmissions) { if (allFormSubmissions.exists(submission.instanceId())) { logger.warn(format("Received form submission that already exists. Skipping. Submission: {0}", submission)); continue; } logger.info(format("Saving form {0} with instance Id: {1} and for entity Id: {2}", submission.formName(), submission.instanceId(), submission.entityId())); submission.setServerVersion(DateUtil.millis()); allFormSubmissions.add(submission); } } @Autowired  FormSubmissionService(AllFormSubmissions allFormSubmissions); List<FormSubmissionDTO> fetch(long formFetchToken); List<FormSubmission> getNewSubmissionsForANM(String anmIdentifier, Long version, Integer batchSize); List<FormSubmission> getAllSubmissions(Long version, Integer batchSize); void submit(List<FormSubmissionDTO> formSubmissionsDTO);  }
@Test public void shouldSortAllSubmissionsAndSaveEachOne() throws Exception { long baseTimeStamp = DateUtil.now().getMillis(); FormSubmissionDTO earlierFormSubmissionDTO = new FormSubmissionDTO("anm id 1", "instance id 1", "entity id 1", "form name 1", null, valueOf(baseTimeStamp), "1"); FormSubmissionDTO laterFormSubmissionDTO = new FormSubmissionDTO("anm id 2", "instance id 2", "entity id 2", "form name 1", null, valueOf(baseTimeStamp + 1), "1"); FormSubmissionDTO veryLateFormSubmissionDTO = new FormSubmissionDTO("anm id 2", "instance id 3", "entity id 3", "form name 1", null, valueOf(baseTimeStamp + 2), "1"); FormSubmission earlierFormSubmission = new FormSubmission("anm id 1", "instance id 1", "form name 1", "entity id 1", baseTimeStamp, "1", null, serverVersion); FormSubmission laterFormSubmission = new FormSubmission("anm id 2", "instance id 2", "form name 1", "entity id 2", baseTimeStamp + 1, "1", null, serverVersion); FormSubmission veryLateFormSubmission = new FormSubmission("anm id 2", "instance id 3", "form name 1", "entity id 3", baseTimeStamp + 2, "1", null, serverVersion); List<FormSubmissionDTO> formSubmissionsDTO = asList(laterFormSubmissionDTO, earlierFormSubmissionDTO, veryLateFormSubmissionDTO); when(allFormSubmissions.exists(anyString())).thenReturn(false); formSubmissionService.submit(formSubmissionsDTO); InOrder inOrder = inOrder(allFormSubmissions); inOrder.verify(allFormSubmissions).exists("instance id 1"); inOrder.verify(allFormSubmissions).add(earlierFormSubmission); inOrder.verify(allFormSubmissions).exists("instance id 2"); inOrder.verify(allFormSubmissions).add(laterFormSubmission); inOrder.verify(allFormSubmissions).exists("instance id 3"); inOrder.verify(allFormSubmissions).add(veryLateFormSubmission); verifyNoMoreInteractions(allFormSubmissions); }
@Test public void shouldNotDelegateFormSubmissionIfAlreadyExists() throws Exception { long baseTimeStamp = DateUtil.now().getMillis(); FormSubmissionDTO firstFormSubmissionDTO = new FormSubmissionDTO("anm id 1", "instance id 1", "entity id 1", "form name 1", null, valueOf(baseTimeStamp), "1"); FormSubmissionDTO secondFormSubmissionDTO = new FormSubmissionDTO("anm id 2", "instance id 2", "entity id 2", "form name 1", null, valueOf(baseTimeStamp + 1), "1"); FormSubmission firstFormSubmission = new FormSubmission("anm id 1", "instance id 1", "form name 1", "entity id 1", baseTimeStamp, "1", null, serverVersion); FormSubmission secondFormSubmission = new FormSubmission("anm id 2", "instance id 2", "form name 1", "entity id 2", baseTimeStamp + 1, "1", null, serverVersion); when(allFormSubmissions.exists("instance id 1")).thenReturn(true); when(allFormSubmissions.exists("instance id 2")).thenReturn(false); formSubmissionService.submit(asList(firstFormSubmissionDTO, secondFormSubmissionDTO)); InOrder inOrder = inOrder(allFormSubmissions); inOrder.verify(allFormSubmissions).exists("instance id 1"); inOrder.verify(allFormSubmissions, times(0)).add(firstFormSubmission); inOrder.verify(allFormSubmissions).exists("instance id 2"); inOrder.verify(allFormSubmissions).add(secondFormSubmission); verifyNoMoreInteractions(allFormSubmissions); }