ReportingEventListener { @MotechListener(subjects = SUBJECT) public void fetchANMReports(MotechEvent event) throws Exception { logger.info("Fetching ANM reports..."); anmReportService.reportFromEntityData(); HttpResponse response = httpAgent.getWithSocketTimeout(url + "/" + FETCH_REPORTS_FOR_ALL_ANMS_ACTION); if (!response.isSuccess()) { logger.error("ANM Reports fetch failed. URL: " + url + "/" + FETCH_REPORTS_FOR_ALL_ANMS_ACTION + ". Response body: " + response.body()); return; } List<ANMReport> anmReports = new Gson().fromJson(response.body(), new TypeToken<List<ANMReport>>() { }.getType()); if (anmReports.isEmpty()) { logger.info("No ANM Reports fetched"); return; } anmReportService.processReports(anmReports); logger.info("Done fetching ANM reports."); } @Autowired  ReportingEventListener(ANMReportingService anmReportService, HttpAgent httpAgent,
                                  @Value("#{drishti['drishti.reporting.url']}") String url); @MotechListener(subjects = ReportEvent.SUBJECT) void submitReportingData(MotechEvent event); @MotechListener(subjects = ReportUpdateEvent.SUBJECT) void updateReportingData(MotechEvent event); @MotechListener(subjects = ReportDeleteEvent.SUBJECT) void deleteReportingData(MotechEvent event); @MotechListener(subjects = SUBJECT) void fetchANMReports(MotechEvent event); static final String FETCH_REPORTS_FOR_ALL_ANMS_ACTION; static final String SUBMIT_REPORT_ACTION; static final String UPDATE_REPORT_ACTION; static final String DELETE_REPORT_ACTION; }
@Test public void shouldFetchANMReportingDataAndPassDataToANMReportService() throws Exception { Map<String, Object> data = new HashMap<>(); List<ANMReport> anmReports = new ArrayList<>(); anmReports.add(new ANMReport("ANM X", asList(createSummaryForIUD()))); when(agent.getWithSocketTimeout(eq("http: listener.fetchANMReports(new MotechEvent("SUBJECT", data)); verify(agent).getWithSocketTimeout("http: verify(anmReportingService).processReports(anmReports); }
@Test public void shouldAddANMReportsThatAreGeneratedFromEntities() throws Exception { Map<String, Object> data = new HashMap<>(); when(agent.getWithSocketTimeout(eq("http: listener.fetchANMReports(new MotechEvent("SUBJECT", data)); verify(anmReportingService).reportFromEntityData(); }
@Test public void shouldNotPassDataToANMReportServiceIfRequestFailed() throws Exception { Map<String, Object> data = new HashMap<>(); when(agent.getWithSocketTimeout(eq("http: listener.fetchANMReports(new MotechEvent("SUBJECT", data)); verify(agent).getWithSocketTimeout("http: verify(anmReportingService, times(0)).processReports(anyList()); }
@Test public void shouldNotPassDataToANMReportServiceIfNoReportIsFetched() throws Exception { Map<String, Object> data = new HashMap<>(); when(agent.getWithSocketTimeout(eq("http: listener.fetchANMReports(new MotechEvent("SUBJECT", data)); verify(agent).getWithSocketTimeout("http: verify(anmReportingService, times(0)).processReports(anyList()); }