MinimumThreeANCVisitsHappenedOnTimeRule implements IRule { @Override public boolean apply(SafeMap reportFields) { LocalDate lmp = LocalDate.parse(reportFields.get(REFERENCE_DATE)); LocalDate ancVisitDate = LocalDate.parse(reportFields.get(AllConstants.ANCFormFields.ANC_VISIT_DATE_FIELD)); Mother mother = mothers.findByCaseId(reportFields.get(ID)); int numberOfANCVisitsHappened = mother.ancVisits().size(); return (numberOfANCVisitsHappened == MINIMUM_NUMBER_OF_ANC_VISITS) && !(lmp.plusWeeks(START_RANGE_FOR_THIRD_ANC_VISIT_IN_WEEKS).isAfter(ancVisitDate) || lmp.plusWeeks(END_RANGE_FOR_THIRD_ANC_VISIT_IN_WEEKS).isBefore(ancVisitDate)); } @Autowired  MinimumThreeANCVisitsHappenedOnTimeRule(AllMothers mothers); @Override boolean apply(SafeMap reportFields);  }
@Test public void shouldReturnTrueWhenThirdANCVisitHappensBetween28WeeksAnd40WeeksAfterLMP() { Map<String, String> reportFields = create("id", "mother id 1") .put("referenceDate", "2013-01-01") .put("ancVisitDate", "2013-07-16") .map(); when(mothers.findByCaseId("mother id 1")).thenReturn(mother); List<Map<String, String>> ancVisits = asList(mapOf("ancVisit1", "1"), mapOf("ancVisit2", "2"), mapOf("ancVisit3", "3")); when(mother.ancVisits()).thenReturn(ancVisits); assertTrue(thirdANCVisitHappenedOnTimeRule.apply(new SafeMap(reportFields))); }
@Test public void shouldNotReportTheFourthANCVisit() { Map<String, String> reportFields = create("id", "mother id 1") .put("referenceDate", "2013-01-01") .put("ancVisitDate", "2013-10-08") .map(); when(mothers.findByCaseId("mother id 1")).thenReturn(mother); List<Map<String, String>> ancVisits = asList(mapOf("ancVisit1", "1"), mapOf("ancVisit2", "2"), mapOf("ancVisit3", "3"), mapOf("ancVisit4", "4")); when(mother.ancVisits()).thenReturn(ancVisits); assertFalse(thirdANCVisitHappenedOnTimeRule.apply(new SafeMap(reportFields))); }
@Test public void shouldReturnFalseWhenAnLessThanThreeANCVisitsHasHappened() { Map<String, String> reportFields = create("id", "mother id 1") .put("referenceDate", "2013-01-01") .put("ancVisitDate", "2013-07-16") .map(); when(mothers.findByCaseId("mother id 1")).thenReturn(mother); List<Map<String, String>> ancVisits = asList(mapOf("ancVisit1", "1"), mapOf("ancVisit2", "2")); when(mother.ancVisits()).thenReturn(ancVisits); assertFalse(thirdANCVisitHappenedOnTimeRule.apply(new SafeMap(reportFields))); }
@Test public void shouldReturnFalseWhenThirdANCVisitHappensBefore28WeeksAfterLMP() { Map<String, String> reportFields = create("id", "mother id 1") .put("referenceDate", "2013-01-01") .put("ancVisitDate", "2013-07-15") .map(); when(mothers.findByCaseId("mother id 1")).thenReturn(mother); List<Map<String, String>> ancVisits = asList(mapOf("ancVisit1", "1"), mapOf("ancVisit2", "2"), mapOf("ancVisit3", "3")); when(mother.ancVisits()).thenReturn(ancVisits); assertFalse(thirdANCVisitHappenedOnTimeRule.apply(new SafeMap(reportFields))); }
@Test public void shouldReturnFalseWhenThirdANCVisitHappensAfter34WeeksAfterLMP() { Map<String, String> reportFields = create("id", "mother id 1") .put("referenceDate", "2013-01-01") .put("ancVisitDate", "2013-10-09") .map(); when(mothers.findByCaseId("mother id 1")).thenReturn(mother); List<Map<String, String>> ancVisits = asList(mapOf("ancVisit1", "1"), mapOf("ancVisit2", "2"), mapOf("ancVisit3", "3")); when(mother.ancVisits()).thenReturn(ancVisits); assertFalse(thirdANCVisitHappenedOnTimeRule.apply(new SafeMap(reportFields))); }