AggregateReportsService { @Transactional("service_provided") public void sendReportsToAggregator() { if (!lock.tryLock()) { logger.warn("Not Aggregating reports. It is already in progress."); return; } try { Integer token = tokenRepository.getAggregateReportsToken(); logger.info(format("Trying to aggregate reports. Report Token: {0}", token)); List<ServiceProvidedReport> reports = servicesProvidedRepository.getNewReports(token, this.numberOfReportsSentInABatch); if (reports.isEmpty()) { logger.info("No new reports to aggregate."); return; } logger.info(format("Got reports to aggregate. Number of reports: {0}", reports.size())); update(reports); } catch (Exception e) { logger.error(MessageFormat.format("{0} occurred while trying to aggregate reports. Message: {1} with stack trace {2}", e.toString(), e.getMessage(), getFullStackTrace(e))); throw e; } finally { lock.unlock(); } } protected  AggregateReportsService(); @Autowired  AggregateReportsService(@Value("#{drishti['aggregator.dataset.url']}") String aggregatorDataSetUrl,
                                   @Value("#{drishti['aggregated.dataset.url']}") String aggregatedDataSetUrl,
                                   @Value("#{drishti['number.of.reports.sent.in.a.batch']}") int numberOfReportsSentInABatch,
                                   HttpAgent httpAgent, AllTokensRepository tokenRepository,
                                   ServicesProvidedRepository servicesProvidedRepository, ReportMonth reportMonth); @Transactional("service_provided") void sendReportsToAggregator(); List<AggregatorResponseDTO> getAggregatedReports(String anmIdentifier, int month, int year);  }
@Test public void shouldSendReportsWithNRHMReportingMonthToAggregator() throws Exception { when(tokenRepository.getAggregateReportsToken()).thenReturn(0); when(servicesProvidedRepository.getNewReports(0, batchSizeToUpdate)) .thenReturn(asList(new ServiceProvidedReport().withId(1).withDate(LocalDate.parse("2012-11-26").toDate()), new ServiceProvidedReport().withId(2).withDate(LocalDate.parse("2012-12-28").toDate()))); when(reportMonth.reportingMonth(LocalDate.parse("2012-11-26"))).thenReturn(12); when(reportMonth.reportingYear(LocalDate.parse("2012-11-26"))).thenReturn(2012); when(reportMonth.reportingMonth(LocalDate.parse("2012-12-28"))).thenReturn(1); when(reportMonth.reportingYear(LocalDate.parse("2012-12-28"))).thenReturn(2013); String reportJson = new Gson().toJson(asList( new ServiceProvidedReportDTO().withDate(LocalDate.parse("2012-11-26")).withNRHMReportMonth(12).withNRHMReportYear(2012).withId(1), new ServiceProvidedReportDTO().withDate(LocalDate.parse("2012-12-28")).withNRHMReportMonth(1).withNRHMReportYear(2013).withId(2))); when(httpAgent.put("bamboo.url", mapOf("update", reportJson))).thenReturn(new HttpResponse(true, "")); aggregateReportsService.sendReportsToAggregator(); verify(httpAgent).put("bamboo.url", mapOf("update", reportJson)); verify(tokenRepository).saveAggregateReportsToken(2); }
@Test(expected = RuntimeException.class) public void shouldThrowExceptionWhenReportsAreNotSent() throws Exception { when(tokenRepository.getAggregateReportsToken()).thenReturn(0); ServiceProvidedReport firstServiceProvidedReport = new ServiceProvidedReport().withId(1); ServiceProvidedReport secondServiceProvidedReport = new ServiceProvidedReport().withId(2); when(servicesProvidedRepository.getNewReports(0, batchSizeToUpdate)).thenReturn(asList(firstServiceProvidedReport, secondServiceProvidedReport)); String firstReportJson = new Gson().toJson(firstServiceProvidedReport); String secondReportAsJson = new Gson().toJson(secondServiceProvidedReport); when(httpAgent.put("bamboo.url", mapOf("update", firstReportJson))).thenReturn(new HttpResponse(false, "")); aggregateReportsService.sendReportsToAggregator(); verify(httpAgent).put("bamboo.url", mapOf("update", firstReportJson)); verify(httpAgent, never()).put("bamboo.url", mapOf("update", secondReportAsJson)); }
@Test public void shouldNotAggregateReportsWhenThereIsNoNewReport() throws Exception { when(tokenRepository.getAggregateReportsToken()).thenReturn(12345); when(servicesProvidedRepository.getNewReports(12345, batchSizeToUpdate)).thenReturn(Collections.<ServiceProvidedReport>emptyList()); aggregateReportsService.sendReportsToAggregator(); verify(httpAgent, never()).put(anyString(), anyMap()); verify(tokenRepository, never()).saveAggregateReportsToken(anyInt()); }