ANMReportsRepository { @Transactional("anm_report") public List<ANMIndicatorSummary> fetchANMSummary(String anmIdentifier) { List<ANMIndicatorSummary> anmIndicatorSummaries = new ArrayList<>(); List<ANMReportData> allReportData = anmReportDataRepository.fetchByANMIdAndDate(anmIdentifier, reportMonth.startDateOfReportingYear()); Collection<Indicator> indicators = getDistinctIndicators(allReportData); for (Indicator indicator : indicators) { List<ANMReportData> allReportDataForIndicator = filterReportsByIndicator(allReportData, indicator); int aggregatedProgress = 0; List<MonthSummary> monthSummaries = new ArrayList<>(); for (LocalDate indexDate = new LocalDate(reportMonth.startDateOfReportingYear()); indexDate.isBefore(reportMonth.startDateOfNextReportingMonth(today())); indexDate = indexDate.plusMonths(1)) { LocalDate reportingMonthEndDate = new LocalDate(indexDate).plusMonths(1); List<ANMReportData> allReportDataForAMonth = filterReportsByMonth(allReportDataForIndicator, indexDate, reportMonth.endDateOfReportingMonthGivenStartDate(indexDate)); if (allReportDataForAMonth.size() == 0) { continue; } int currentProgress = allReportDataForAMonth.size(); aggregatedProgress += currentProgress; List<String> externalIds = getAllExternalIds(allReportDataForAMonth); monthSummaries.add(new MonthSummary(valueOf(reportingMonthEndDate.getMonthOfYear()), valueOf(reportingMonthEndDate.getYear()), valueOf(currentProgress), valueOf(aggregatedProgress), externalIds)); } AnnualTarget annualTarget = annualTargetsRepository.fetchFor(anmIdentifier, indicator, today().toDate()); String target = annualTarget == null ? null : annualTarget.target(); anmIndicatorSummaries.add(new ANMIndicatorSummary(indicator.indicator(), target, monthSummaries)); } return anmIndicatorSummaries; } protected  ANMReportsRepository(); @Autowired  ANMReportsRepository(ANMCacheableRepository anmRepository,
                                @Qualifier("anmReportsIndicatorRepository") IndicatorCacheableRepository indicatorRepository,
                                AllANMReportDataRepository anmReportDataRepository,
                                AllAnnualTargetsRepository annualTargetsRepository, Monitor monitor, ReportMonth reportMonth); @Transactional("anm_report") void save(String anmIdentifier, String externalId, String indicator, String date, String quantity); @Transactional("anm_report") List<ANMIndicatorSummary> fetchANMSummary(String anmIdentifier); @Transactional("anm_report") List<ANMReport> fetchAllANMsReport(); @Transactional("anm_report") void update(ReportDataUpdateRequest request); @Transactional("anm_report") List getReportsFor(String anmId, String startDate, String endDate); @Transactional("anm_report") void delete(ReportDataDeleteRequest request);  }
@Test public void shouldFetchANMIndicatorSummaries() throws Exception { DateUtil.fakeIt(parse("2012-06-30")); ANM anm = new ANM("ANM X"); Date date1 = parse("2012-03-29").toDate(); Date date2 = parse("2012-05-15").toDate(); Date date3 = parse("2012-05-30").toDate(); Indicator indicator1 = new Indicator("IUD"); Indicator indicator2 = new Indicator("ANC"); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate())).thenReturn(asList( new ANMReportData(anm, "CASE 1", indicator1, date1), new ANMReportData(anm, "CASE 2", indicator1, date1), new ANMReportData(anm, "CASE 3", indicator1, date2), new ANMReportData(anm, "CASE 4", indicator1, date2), new ANMReportData(anm, "CASE 5", indicator2, date3), new ANMReportData(anm, "CASE 6", indicator2, date3))); when(annualTargetsRepository.fetchFor("ANM X", indicator1, parse("2012-06-30").toDate())).thenReturn(new AnnualTarget(1, 1, "40", parse("2012-03-26").toDate(), parse("2013-03-25").toDate())); when(annualTargetsRepository.fetchFor("ANM X", indicator2, parse("2012-06-30").toDate())).thenReturn(new AnnualTarget(1, 1, "30", parse("2012-03-26").toDate(), parse("2013-03-25").toDate())); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); assertTrue(anmIndicatorSummaries.containsAll(asList(createSummaryForANC(), createSummaryForIUD()))); assertEquals(2, anmIndicatorSummaries.size()); }
@Test public void shouldSetMonthOfSummaryToBeNextYearsInCaseOfLastMonthOfTheYear() throws Exception { DateUtil.fakeIt(parse("2013-01-30")); ANM anm = new ANM("ANM X"); Date date = parse("2012-12-29").toDate(); Indicator indicator = new Indicator("IUD"); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate())).thenReturn(asList(new ANMReportData(anm, "CASE 1", indicator, date))); when(annualTargetsRepository.fetchFor("ANM X", indicator, parse("2013-01-30").toDate())).thenReturn(new AnnualTarget(1, 1, "40", parse("2012-03-26").toDate(), parse("2013-03-25").toDate())); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); assertEquals(asList(new ANMIndicatorSummary("IUD", "40", asList(new MonthSummary("1", "2013", "1", "1", asList("CASE 1"))))), anmIndicatorSummaries); }
@Test public void shouldFetchANMIndicatorSummariesIncludingAllServicesProvidedTillTheCurrentMonth() throws Exception { DateUtil.fakeIt(parse("2013-01-15")); ANM anm = new ANM("ANM X"); Date january = parse("2013-01-11").toDate(); Indicator indicator1 = new Indicator("IUD"); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate())).thenReturn(asList( new ANMReportData(anm, "CASE 1", indicator1, january))); when(annualTargetsRepository.fetchFor("ANM X", indicator1, parse("2013-01-15").toDate())).thenReturn(new AnnualTarget(1, 1, "40", parse("2012-03-26").toDate(), parse("2013-03-25").toDate())); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); assertEquals(1, anmIndicatorSummaries.size()); assertEquals(1, anmIndicatorSummaries.get(0).monthlySummaries().size()); }
@Test public void shouldFetchANMIndicatorSummariesWithDistinctExternalIds() throws Exception { DateUtil.fakeIt(parse("2013-03-22")); ANM anm = new ANM("ANM X"); Date dates = parse("2013-03-21").toDate(); Indicator indicator = new Indicator("CONDOM"); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate())).thenReturn(asList( new ANMReportData(anm, "CASE 7", indicator, dates), new ANMReportData(anm, "CASE 7", indicator, dates))); when(annualTargetsRepository.fetchFor("ANM X", indicator, parse("2013-03-22").toDate())).thenReturn(new AnnualTarget(1, 1, "20", parse("2012-03-26").toDate(), parse("2013-03-25").toDate())); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); System.out.println(anmIndicatorSummaries); assertTrue(anmIndicatorSummaries.containsAll(asList(createSummaryForCondom()))); assertEquals(1, anmIndicatorSummaries.size()); }
@Test public void shouldFetchANMIndicatorSummariesForCurrentReportingYear() throws Exception { DateUtil.fakeIt(parse("2013-01-02")); repository.fetchANMSummary("ANM X"); verify(anmReportDataRepository).fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate()); }
@Test public void shouldFetchReportsForCurrentYearIfCurrentDayIsFirstDayOfTheReportingYear() throws Exception { DateUtil.fakeIt(parse("2012-03-26")); repository.fetchANMSummary("ANM X"); verify(anmReportDataRepository).fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate()); }
@Test public void shouldFetchReportsForPreviousYearIfCurrentDayIsLastDayOfTheReportingYear() throws Exception { DateUtil.fakeIt(parse("2013-03-25")); repository.fetchANMSummary("ANM X"); verify(anmReportDataRepository).fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate()); }
@Test public void shouldReturnEmptyANMIndicatorSummariesWhenThereIsNoDataForANM() throws Exception { DateUtil.fakeIt(parse("2012-03-31")); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-01-01").toDate())).thenReturn(null); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); assertEquals(0, anmIndicatorSummaries.size()); }
@Test public void shouldReturnANMIndicatorSummariesWithNullAnnualTargetWhenThereIsNoAnnualTargetForANM() throws Exception { DateUtil.fakeIt(parse("2012-06-30")); ANM anm = new ANM("ANM X"); Date date1 = parse("2012-03-29").toDate(); Date date2 = parse("2012-05-15").toDate(); Indicator indicator = new Indicator("IUD"); when(anmReportDataRepository.fetchByANMIdAndDate("ANM X", parse("2012-03-26").toDate())).thenReturn(asList( new ANMReportData(anm, "CASE 1", indicator, date1), new ANMReportData(anm, "CASE 2", indicator, date1), new ANMReportData(anm, "CASE 3", indicator, date2), new ANMReportData(anm, "CASE 4", indicator, date2))); when(annualTargetsRepository.fetchFor("ANM X", new Indicator("IUD"), parse("2012-06-30").toDate())).thenReturn(null); List<ANMIndicatorSummary> anmIndicatorSummaries = repository.fetchANMSummary("ANM X"); ANMIndicatorSummary expectedANMIUDSummary = createSummaryForIUD(null); assertEquals(asList(expectedANMIUDSummary), anmIndicatorSummaries); }