MCTSReportEventListener { @MotechListener(subjects = MCTSReportScheduler.SUBJECT) public void fetchReports(MotechEvent event) { if (!lock.tryLock()) { logger.warn("Not fetching MCTS Reports. It is already in progress."); return; } try { logger.info("Fetching MCTS Reports"); List<MCTSReport> reports = mctsSMSReportService.fetch(DateUtil.today().toString(DEFAULT_DATE_FORMAT)); if (reports.isEmpty()) { logger.info("No new MCTS report found."); return; } sendSMS(reports); logger.info(format("Sent {0} new MCTS found.", reports.size())); } 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  MCTSReportEventListener(MCTSSMSService mctssmsService, MCTSSMSReportService mctsSMSReportService); @MotechListener(subjects = MCTSReportScheduler.SUBJECT) void fetchReports(MotechEvent event);  }
@Test public void shouldFetchMCTSReport() throws Exception { DateUtil.fakeIt(LocalDate.parse("2014-01-01")); Map<String, Object> data = new HashMap<>(); listener.fetchReports(new MotechEvent("SUBJECT", data)); verify(mctsSMSReportService).fetch("2014-01-01"); }
@Test public void shouldSendSMSForMCTSReportsAndMarkReportsAsClosed() throws Exception { DateUtil.fakeIt(LocalDate.parse("2014-01-01")); Map<String, Object> data = new HashMap<>(); MCTSReport mctsReport = new MCTSReport().withCaseId("case id 1").withEntityId("entity id 1").withSendDate("2014-01-01").withEntityRegistrationDate("2014-01-01").withReportText("report text 1").withReportSent("false"); MCTSReport anotherMCTSReport = new MCTSReport().withCaseId("case id 2").withEntityId("entity id 2").withSendDate("2014-01-01").withEntityRegistrationDate("2014-01-01").withReportText("report text 2").withReportSent("false"); when(mctsSMSReportService.fetch("2014-01-01")).thenReturn(asList(mctsReport, anotherMCTSReport)); listener.fetchReports(new MotechEvent("SUBJECT", data)); InOrder inOrder = inOrder(mctsSMSService); inOrder.verify(mctsSMSService).send("report text 1"); inOrder.verify(mctsSMSService).send("report text 2"); inOrder = inOrder(mctsSMSReportService); inOrder.verify(mctsSMSReportService).markReportAsSent(mctsReport); inOrder.verify(mctsSMSReportService).markReportAsSent(anotherMCTSReport); }