FormEventListener { @MotechListener(subjects = DrishtiFormScheduler.SUBJECT) public void fetchForms(MotechEvent event) { if (!lock.tryLock()) { logger.warn("Not fetching forms from Message Queue. It is already in progress."); return; } try { logger.info("Fetching Forms"); long version = getVersion(); List<FormSubmissionDTO> formSubmissionDTOs = formSubmissionService.fetch(version); if (formSubmissionDTOs.isEmpty()) { logger.info("No new forms found. Export token: " + version); return; } logger.info(format("Fetched {0} new forms found. Export token: {1}", formSubmissionDTOs.size(), version)); List<FormSubmission> formSubmissions = with(formSubmissionDTOs) .convert(new Converter<FormSubmissionDTO, FormSubmission>() { @Override public FormSubmission convert(FormSubmissionDTO submission) { return FormSubmissionConverter.toFormSubmissionWithVersion(submission); } }); formEntityService.process(formSubmissions); } catch (Exception e) { logger.error(MessageFormat.format("{0} occurred while trying to fetch forms. Message: {1} with stack trace {2}", e.toString(), e.getMessage(), getFullStackTrace(e))); } finally { lock.unlock(); } } @Autowired  FormEventListener(FormSubmissionService formSubmissionService, FormEntityService formEntityService, AllFormExportTokens allFormExportTokens); @MotechListener(subjects = FormSubmissionEvent.SUBJECT) void submitForms(MotechEvent event); @MotechListener(subjects = DrishtiFormScheduler.SUBJECT) void fetchForms(MotechEvent event);  }
@Test public void shouldFetchFormSubmissionsFromSubmissionService() throws Exception { List<FormSubmissionDTO> formSubmissions = asList(new FormSubmissionDTO("anm id 1", "instance id 1", "entity id 1", "form name", null, "0", "1").withServerVersion("0"), new FormSubmissionDTO("anm id 2", "instance id 2", "entity id 2", "form name", null, "0", "1").withServerVersion("0")); when(formExportTokens.getAll()).thenReturn(asList(new FormExportToken(1L))); when(formSubmissionService.fetch(1L)).thenReturn(formSubmissions); listener.fetchForms(new MotechEvent("SUBJECT", null)); verify(formEntityService).process(asList(new FormSubmission("anm id 1", "instance id 1", "form name", "entity id 1", 0L, "1", null, 0L), new FormSubmission("anm id 2", "instance id 2", "form name", "entity id 2", 0L, "1", null, 0L))); }
@Test public void shouldCreateFormExportTokenIfItDoesNotExists() throws Exception { List<FormSubmissionDTO> formSubmissions = asList(new FormSubmissionDTO("anm id 1", "instance id 1", "entity id 1", "form name", null, "0", "1").withServerVersion("0"), new FormSubmissionDTO("anm id 2", "instance id 2", "entity id 2", "form name", null, "0", "1").withServerVersion("0")); when(formExportTokens.getAll()).thenReturn(Collections.EMPTY_LIST); when(formSubmissionService.fetch(1L)).thenReturn(formSubmissions); listener.fetchForms(new MotechEvent("SUBJECT", null)); verify(formExportTokens).add(new FormExportToken(0L)); }
@Test public void shouldNotDoAnythingIfFetchFromSubmissionServiceReturnsEmptyList() throws Exception { when(formExportTokens.getAll()).thenReturn(asList(new FormExportToken(1L))); when(formSubmissionService.fetch(1L)).thenReturn(Collections.EMPTY_LIST); listener.fetchForms(new MotechEvent("SUBJECT", null)); verifyZeroInteractions(formEntityService); }