ChildReportingService { public void reportInfantBalanceOnHand() { LocalDate today = DateUtil.today(); InfantBalanceOnHandReportToken infantBalanceOnHandReportToken = getInfantBalanceOnHandReportToken(today); if (reportMonth.isDateWithinCurrentReportMonth(infantBalanceOnHandReportToken.getLastReportedDate())) { logger.info(MessageFormat.format("Infant Balance (On Hand) was last reported on: {0}, so not reporting now: {1}.", infantBalanceOnHandReportToken.getLastReportedDate(), today)); return; } logger.info(MessageFormat.format("Infant Balance (On Hand) was last reported on: {0}, so reporting now: {1}.", infantBalanceOnHandReportToken.getLastReportedDate(), today)); LocalDate startOfCurrentReportMonth = this.reportMonth.startOfCurrentReportMonth(today); List<Child> childrenLessThanOneYearOld = allChildren.findAllChildrenLessThanOneYearOldAsOfDate( startOfCurrentReportMonth); logger.info(MessageFormat.format("Found {0} children for reporting Infant Balance (On Hand) and Infant Balance (Total) ", childrenLessThanOneYearOld.size())); for (Child child : childrenLessThanOneYearOld) { logger.info(MessageFormat.format("Reporting Infant Balance (On Hand) and Infant Balance (Total) on date: {0} for child: {1}.", startOfCurrentReportMonth.toString(), child)); Location location = loadLocationOfChild(child); reportToBoth(child, Indicator.INFANT_BALANCE_ON_HAND, startOfCurrentReportMonth.toString(), location); reportToBoth(child, Indicator.INFANT_BALANCE_TOTAL, startOfCurrentReportMonth.toString(), location); } logger.info(MessageFormat.format("Updating Infant Balance (On Hand) last reported date to {0}", today)); allInfantBalanceOnHandReportTokens.update(infantBalanceOnHandReportToken.withLastReportedDate(today)); } @Autowired  ChildReportingService(ReportingService reportingService, AllChildren allChildren, AllMothers allMothers,
                                 AllEligibleCouples allEligibleCouples,
                                 AllInfantBalanceOnHandReportTokens allInfantBalanceOnHandReportTokens, ReportMonth reportMonth, MCTSReporter mctsReporter); void registerChild(SafeMap reportData); void immunizationProvided(SafeMap reportData, List<String> previousImmunizations); void vitaminAProvided(SafeMap reportData); void pncVisitHappened(SafeMap reportData); void sickVisitHappened(SafeMap reportData); void closeChild(SafeMap reportData); void reportToBoth(Child child, Indicator indicator, String serviceProvidedDate, String submissionDate, Location location); void reportToBoth(Child child, Indicator indicator, String serviceProvidedDate, Location location); void reportInfantAndChildBalance(); void reportInfantBalanceOnHand(); void reportInfantBalanceTurningOneYearOld(); void reportInfantBalanceTotalNumberOfOAChildren(); void reportInfantBalanceLessThanOneYearOld(); void reportChildBalanceLessThanFiveYearOld(); static final String CHILD_ID_FIELD; }
@Test public void shouldReportAllChildrenLessThanOneYearOldAtTheBeginningOfReportMonthAsInfantBalanceOnHandWhenReportingInfantBalanceAndItIsNotReportedBeforeInThatMonth() throws Exception { DateUtil.fakeIt(parse("2013-02-01")); LocalDate previousReportMonthStartDate = LocalDate.parse("2012-12-26"); InfantBalanceOnHandReportToken infantBalanceOnHandReportToken = new InfantBalanceOnHandReportToken(previousReportMonthStartDate); when(allInfantBalanceOnHandTokens.getAll()).thenReturn(asList(infantBalanceOnHandReportToken)); String currentReportMonthStartDate = "2013-01-26"; when(allChildren.findAllChildrenLessThanOneYearOldAsOfDate(parse(currentReportMonthStartDate))) .thenReturn(asList( new Child("child id 1", "mother id 1", "", "5", "male").withThayiCard("thayi card 1").withAnm("ANM X"), new Child("child id 2", "mother id 2", "", "6", "female").withThayiCard("thayi card 2").withAnm("ANM X"))); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "thayi card 1")); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(allMothers.findByCaseId("mother id 2")).thenReturn(new Mother("mother id 2", "ec id 2", "thayi card 2")); when(allEligibleCouples.findByCaseId("ec id 2")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.startOfCurrentReportMonth(parse("2013-02-01"))).thenReturn(parse("2013-01-26")); when(reportMonth.isDateWithinCurrentReportMonth(LocalDate.parse("2013-01-26"))).thenReturn(true); service.reportInfantBalanceOnHand(); verifyBothReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 2", "thayi card 2"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 2", "thayi card 2"); verify(allInfantBalanceOnHandTokens).update(new InfantBalanceOnHandReportToken(parse("2013-02-01"))); }
@Test public void shouldReportInfantBalanceOnHandWhenNothingIsReportedSoFar() throws Exception { DateUtil.fakeIt(parse("2013-02-01")); when(allInfantBalanceOnHandTokens.getAll()).thenReturn(Collections.<InfantBalanceOnHandReportToken>emptyList()); String currentReportMonthStartDate = "2013-01-26"; when(allChildren.findAllChildrenLessThanOneYearOldAsOfDate(parse(currentReportMonthStartDate))) .thenReturn(asList( new Child("child id 1", "mother id 1", "", "5", "male").withThayiCard("thayi card 1").withAnm("ANM X"), new Child("child id 2", "mother id 2", "", "6", "female").withThayiCard("thayi card 2").withAnm("ANM X"))); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "thayi card 1")); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(allMothers.findByCaseId("mother id 2")).thenReturn(new Mother("mother id 2", "ec id 2", "thayi card 2")); when(allEligibleCouples.findByCaseId("ec id 2")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.startOfCurrentReportMonth(parse("2013-02-01"))).thenReturn(parse("2013-01-26")); when(reportMonth.isDateWithinCurrentReportMonth(parse("2013-01-26"))).thenReturn(true); service.reportInfantBalanceOnHand(); verifyBothReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 2", "thayi card 2"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyBothReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 2", "thayi card 2"); verify(allInfantBalanceOnHandTokens).update(new InfantBalanceOnHandReportToken(parse("2013-02-01"))); }
@Test public void shouldNotReportInfantBalanceOnHandWhenItHasBeenReportedAlreadyForCurrentReportMonth() throws Exception { DateUtil.fakeIt(parse("2013-02-01")); String currentReportMonthStartDate = "2013-01-26"; when(allInfantBalanceOnHandTokens.getAll()).thenReturn(asList(new InfantBalanceOnHandReportToken(parse(currentReportMonthStartDate)))); when(allChildren.findAllChildrenLessThanOneYearOldAsOfDate(parse(currentReportMonthStartDate))) .thenReturn(asList( new Child("child id 1", "mother id 1", "", "5", "male").withThayiCard("thayi card 1").withAnm("ANM X"), new Child("child id 2", "mother id 2", "", "6", "female").withThayiCard("thayi card 2").withAnm("ANM X"))); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "thayi card 1")); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(allMothers.findByCaseId("mother id 2")).thenReturn(new Mother("mother id 2", "ec id 2", "thayi card 2")); when(allEligibleCouples.findByCaseId("ec id 2")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.startOfCurrentReportMonth(parse("2013-02-01"))).thenReturn(parse("2013-01-26")); when(reportMonth.isDateWithinCurrentReportMonth(parse(currentReportMonthStartDate))).thenReturn(true); service.reportInfantBalanceOnHand(); verifyNoReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyNoReportingCalls(Indicator.INFANT_BALANCE_ON_HAND, currentReportMonthStartDate, "child id 2", "thayi card 2"); verifyNoReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 1", "thayi card 1"); verifyNoReportingCalls(Indicator.INFANT_BALANCE_TOTAL, currentReportMonthStartDate, "child id 2", "thayi card 2"); }