ReportIndicatorListViewController { public String get() { reportsCategory = ReportsCategory.valueOf(category); reports = allReports.allFor(reportsCategory.indicators()); List<IndicatorReport> indicatorReports = new ArrayList<IndicatorReport>(); for (Report report : reports) { ReportIndicator indicator = report.reportIndicator(); List<MonthSummaryDatum> monthSummaryData = report.monthlySummaries(); if (monthSummaryData.size() == 0) continue; sortMonthlySummaries(monthSummaryData); MonthSummaryDatum currentMonthSummary = monthSummaryData.get(0); String currentMonth = today().monthOfYear().getAsString(); String currentProgress = currentMonthSummary.month().equals(currentMonth) ? currentMonthSummary.currentProgress() : "0"; String annualTarget = (isBlank(report.annualTarget())) ? "NA" : report.annualTarget(); indicatorReports.add(new IndicatorReport(indicator.name(), indicator.description(), annualTarget, currentProgress, currentMonth, currentMonthSummary.year(), currentMonthSummary.aggregatedProgress() )); } return new Gson().toJson(new CategoryReports(reportsCategory.description(), indicatorReports)); } ReportIndicatorListViewController(Context context, AllReports allReports, String category); String get(); void sortMonthlySummaries(List<MonthSummaryDatum> monthSummaryData); void startReportIndicatorDetail(String indicator);  }
@Test public void shouldGetIndicatorReportsForGivenCategory() throws Exception { List<MonthSummaryDatum> monthlySummaries = asList(new MonthSummaryDatum("10", "2012", "2", "2", asList("123", "456"))); Report iudReport = new Report("IUD", "40", new Gson().toJson(monthlySummaries)); Report condomReport = new Report("CONDOM", "30", new Gson().toJson(monthlySummaries)); when(allReports.allFor(anyList())).thenReturn(asList(iudReport, condomReport)); String indicatorReports = controller.get(); IndicatorReport iud = new IndicatorReport("IUD", "IUD Adoption", "40", "2", "10", "2012", "2"); IndicatorReport condom = new IndicatorReport("CONDOM", "Condom Usage", "30", "2", "10", "2012", "2"); String expectedIndicatorReports = new Gson().toJson(new CategoryReports("Family Planning Services", asList(iud, condom))); assertEquals(expectedIndicatorReports, indicatorReports); }
@Test public void shouldIgnoreThoseReportsWhichHaveEmptyMonthlySummary() throws Exception { List<MonthSummaryDatum> monthlySummaries = asList(new MonthSummaryDatum("10", "2012", "2", "2", asList("123", "456"))); Report iudReport = new Report("IUD", "40", new Gson().toJson(monthlySummaries)); Report condomReport = new Report("CONDOM", "30", "[]"); when(allReports.allFor(anyList())).thenReturn(asList(iudReport, condomReport)); String indicatorReports = controller.get(); IndicatorReport iud = new IndicatorReport("IUD", "IUD Adoption", "40", "2", "10", "2012", "2"); String expectedIndicatorReports = new Gson().toJson(new CategoryReports("Family Planning Services", asList(iud))); assertEquals(expectedIndicatorReports, indicatorReports); }
@Test public void shouldUseCurrentMonthDataForIndicatorReport() throws Exception { List<MonthSummaryDatum> monthlySummaries = asList(new MonthSummaryDatum("1", "2012", "2", "2", asList("123", "456")), new MonthSummaryDatum("10", "2012", "2", "4", asList("321", "654"))); Report earlyANCRegistrationReport = new Report("ANC_LT_12", "40", new Gson().toJson(monthlySummaries)); when(allReports.allFor(ANC_SERVICES.indicators())).thenReturn(asList(earlyANCRegistrationReport)); controller = new ReportIndicatorListViewController(context, allReports, ANC_SERVICES.value()); String reports = controller.get(); IndicatorReport earlyANCRegistration = new IndicatorReport("EARLY_ANC_REGISTRATIONS", "Early ANC Registration", "40", "2", "10", "2012", "4"); String expectedIndicatorReports = new Gson().toJson(new CategoryReports("ANC Services", asList(earlyANCRegistration))); assertEquals(expectedIndicatorReports, reports); }
@Test public void shouldUseLatestMonthDataIfCurrentMonthDataNotAvailable() throws Exception { List<MonthSummaryDatum> monthlySummaries = asList(new MonthSummaryDatum("6", "2012", "2", "2", asList("123", "456")), new MonthSummaryDatum("8", "2012", "2", "4", asList("321", "654"))); Report iudReport = new Report("IUD", "40", new Gson().toJson(monthlySummaries)); when(allReports.allFor(FPS.indicators())).thenReturn(asList(iudReport)); String indicatorReports = controller.get(); IndicatorReport iud = new IndicatorReport("IUD", "IUD Adoption", "40", "0", "10", "2012", "4"); String expectedIndicatorReports = new Gson().toJson(new CategoryReports("Family Planning Services", asList(iud))); assertEquals(expectedIndicatorReports, indicatorReports); }
@Test public void shouldShowNAIfAnnualTargetNotAvailable() throws Exception { List<MonthSummaryDatum> monthlySummaries = asList(new MonthSummaryDatum("6", "2012", "2", "2", asList("123", "456")), new MonthSummaryDatum("8", "2012", "2", "4", asList("321", "654"))); Report iudReport = new Report("IUD", null, new Gson().toJson(monthlySummaries)); when(allReports.allFor(FPS.indicators())).thenReturn(asList(iudReport)); String indicatorReports = controller.get(); IndicatorReport iud = new IndicatorReport("IUD", "IUD Adoption", "NA", "0", "10", "2012", "4"); String expectedIndicatorReports = new Gson().toJson(new CategoryReports("Family Planning Services", asList(iud))); assertEquals(expectedIndicatorReports, indicatorReports); }