ChildReportingService { public void closeChild(SafeMap reportData) { String closeReason = reportData.get(CLOSE_REASON_FIELD_NAME); if (WRONG_ENTRY_VALUE.equalsIgnoreCase(closeReason)) { deleteReports(reportData.get(ID)); return; } if (!DEATH_OF_CHILD_VALUE.equals(closeReason)) { return; } Child child = allChildren.findByCaseId(reportData.get(ID)); Location location = loadLocationOfChild(child); LocalDate childDateOfBirth = parse(child.dateOfBirth()); String diedOn = reportData.get(DATE_OF_DEATH_FIELD_NAME); LocalDate diedOnDate = parse(diedOn); if (childDateOfBirth.plusDays(CHILD_EARLY_NEONATAL_MORTALITY_THRESHOLD_IN_DAYS).isAfter(diedOnDate)) { reportToBoth(child, ENM, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } if (childDateOfBirth.plusDays(CHILD_NEONATAL_MORTALITY_THRESHOLD_IN_DAYS).isAfter(diedOnDate)) { reportToBoth(child, NM, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); reportToBoth(child, INFANT_MORTALITY, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } else if (childDateOfBirth.plusYears(INFANT_MORTALITY_THRESHOLD_IN_YEARS).isAfter(diedOnDate)) { reportToBoth(child, LNM, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); reportToBoth(child, INFANT_MORTALITY, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } if (childDateOfBirth.plusYears(CHILD_MORTALITY_THRESHOLD_IN_YEARS).isAfter(diedOnDate)) { reportToBoth(child, CHILD_MORTALITY, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } else { logger.warn("Not reporting for child with CaseID" + child.caseId() + "because child's age is more than " + CHILD_MORTALITY_THRESHOLD_IN_YEARS + " years."); } if (AllConstants.CommonChildFormFields.DIARRHEA_VALUE.equalsIgnoreCase(reportData.get(DEATH_CAUSE))) { reportToBoth(child, CHILD_MORTALITY_DUE_TO_DIARRHEA, diedOn, reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } } @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 shouldReportCloseChildCaseWhenReasonIsDeath() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-03-05"), LocalDate.parse("2012-03-10"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-03-05", "2012-03-10")); verifyBothReportingCalls(INFANT_MORTALITY, "2012-03-05"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldReportEarlyNeonatalMortalityDeath() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-07"), LocalDate.parse("2012-01-17"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-07", "2012-01-17")); verifyBothReportingCalls(ENM, "2012-01-07"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldNotReportEarlyNeonatalMortalityDeathIfDateOfDeathIsAfterOneWeekOfBirth() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-08"), LocalDate.parse("2012-01-08"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-08", "2012-01-08")); verifyNoReportingCalls(ENM, "2012-01-08"); }
@Test public void shouldReportNeonatalMortalityDeath() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-28"), LocalDate.parse("2012-01-28"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-28", "2012-01-28")); verifyBothReportingCalls(NM, "2012-01-28"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldNotReportNeonatalMortalityDeathIfDateOfDeathIsAfterOneWeekOfBirth() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-29"), LocalDate.parse("2012-01-29"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-29", "2012-01-29")); verifyNoReportingCalls(NM, "2012-01-29"); }
@Test public void shouldReportLateMortalityWithin29DaysAnd1YearOfBirth() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-29"), LocalDate.parse("2012-01-29"))).thenReturn(true); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-12-31"), LocalDate.parse("2012-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-29", "2012-01-29")); service.closeChild(reportDataForCloseChild("death_of_child", "2012-12-31", "2012-12-31")); verifyBothReportingCalls(LNM, "2012-01-29"); verifyBothReportingCalls(LNM, "2012-12-31"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldNotReportLateMortalityIfNotWithin29DaysAnd1YearOfBirth() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-28"), LocalDate.parse("2012-01-28"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-28", "2012-01-28")); service.closeChild(reportDataForCloseChild("death_of_child", "2013-01-01", "2013-01-01")); verifyNoReportingCalls(LNM, "2012-01-28"); verifyNoReportingCalls(LNM, "2013-12-31"); }
@Test public void shouldReportChildMortalityWithin7DaysAnd1YearOfBirth() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-08"), LocalDate.parse("2012-01-08"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2012-01-08", "2012-01-08")); service.closeChild(reportDataForCloseChild("death_of_child", "2012-12-31", "2012-12-31")); verifyNoReportingCalls(INFANT_MORTALITY, "2012-01-07"); verifyNoReportingCalls(INFANT_MORTALITY, "2013-01-01"); }
@Test public void shouldReportChildMortalityWithin5YearOfBirth() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2016-12-31"), LocalDate.parse("2016-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2016-12-31", "2016-12-31")); verifyBothReportingCalls(CHILD_MORTALITY, "2016-12-31"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldNotReportChildMortalityAfter5YearOfBirth() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-01-01"), LocalDate.parse("2012-01-01"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2017-01-01", "2017-01-01")); verifyNoReportingCalls(CHILD_MORTALITY, "2017-01-01"); }
@Test public void shouldNotReportCloseChildCaseWhenReasonIsNotDeath() { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2012-03-05"), LocalDate.parse("2012-03-05"))).thenReturn(true); service.closeChild(reportDataForCloseChild("child_over5", "2012-03-05", "2012-03-05")); verifyZeroInteractions(reportingService); }
@Test public void shouldReportChildDeathDueToDiarrheaWhenChildIsClosedWithCauseOfDeathAsDiarrhea() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2016-12-31"), LocalDate.parse("2016-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("death_of_child", "2016-12-31", "diarrhea", "2016-12-31")); verifyBothReportingCalls(CHILD_MORTALITY_DUE_TO_DIARRHEA, "2016-12-31"); verify(reportingService, never()).deleteReportData(any(ReportDataDeleteRequest.class)); }
@Test public void shouldDeleteReportsWhenCloseReasonIsWrongEntry() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2016-12-31"), LocalDate.parse("2016-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("wrong_entry", "2016-12-31", "2016-12-31")); verify(reportingService).deleteReportData(serviceProvidedDataDeleteRequest("CASE X")); verify(reportingService).deleteReportData(anmReportDataDeleteRequest("CASE X")); verifyNoMoreInteractions(reportingService); }
@Test public void shouldNotReportChildDeathDueToDiarrheaWhenChildIsClosedForOtherReasons() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2016-12-31"), LocalDate.parse("2016-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("other_reason", "2016-12-31", "diarrhea", "2016-12-31")); verifyNoReportingCalls(CHILD_MORTALITY_DUE_TO_DIARRHEA, "2016-12-31"); }
@Test public void shouldNotReportChildDeathDueToDiarrheaWhenChildIsClosedWithDeathCauseNotAsDiarrhea() throws Exception { when(allChildren.findByCaseId("CASE X")).thenReturn(CHILD); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(new Mother("MOTHER-CASE-1", "EC-CASE-1", "TC 1")); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(LocalDate.parse("2016-12-31"), LocalDate.parse("2016-12-31"))).thenReturn(true); service.closeChild(reportDataForCloseChild("other_reason", "2016-12-31", "sepsis", "2016-12-31")); verifyNoReportingCalls(CHILD_MORTALITY_DUE_TO_DIARRHEA, "2016-12-31"); }