MotherReportingService { public void registerANC(SafeMap reportData) { Mother mother = allMothers.findByCaseId(reportData.get(MOTHER_ID)); Location location = loadLocationFromEC(mother); reportToBoth(mother, ANC, reportData.get(REGISTRATION_DATE), reportData.get(SUBMISSION_DATE_FIELD_NAME), location); boolean isRegisteredWithinTwelveWeeks = !(parse(reportData.get(REGISTRATION_DATE)).minusDays(NUMBER_OF_DAYS_IN_12_WEEKS) .isAfter(parse(reportData.get(REFERENCE_DATE)))); if (isRegisteredWithinTwelveWeeks) { reportToBoth(mother, ANC_BEFORE_12_WEEKS, reportData.get(REGISTRATION_DATE), reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } else { reportToBoth(mother, ANC_AFTER_12_WEEKS, reportData.get(REGISTRATION_DATE), reportData.get(SUBMISSION_DATE_FIELD_NAME), location); } } @Autowired  MotherReportingService(ReportingService reportingService, AllMothers allMothers, AllEligibleCouples allEligibleCouples, ReportMonth reportMonth); void registerANC(SafeMap reportData); void ancVisit(SafeMap reportData); void ttProvided(SafeMap reportData); void deliveryOutcome(SafeMap reportData); void pncVisitHappened(SafeMap reportData); void closeANC(SafeMap reportData); void closePNC(SafeMap reportData); void reportToBoth(Mother mother, Indicator indicator, String date, String submissionDate, Location location); void pncRegistrationOA(SafeMap reportData); void deleteReports(String motherCaseId); static final int NUMBER_OF_DAYS_IN_12_WEEKS; static final int NUMBER_OF_DAYS_IN_PNC_PERIOD; }
@Test public void shouldReportANCRegistrationWhichHasBeenDoneAfterTwelveWeeksLMPAsLateRegistration() throws Exception { SafeMap reportData = new SafeMap(); reportData.put("referenceDate", "2011-10-08"); reportData.put("motherId", "CASE-1"); reportData.put("registrationDate", "2012-01-01"); reportData.put("submissionDate", "2012-01-01"); when(allMothers.findByCaseId("CASE-1")).thenReturn(MOTHER); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(parse("2012-01-01"), parse("2012-01-01"))).thenReturn(true); service.registerANC(reportData); verifyNoReportingCalls(ANC_BEFORE_12_WEEKS, "2012-01-01", "CASE-1"); verifyBothReportingCalls(ANC_AFTER_12_WEEKS, "2012-01-01", "CASE-1"); }
@Test public void shouldReportANCRegistrationWhichHasBeenDoneBeforeTwelveWeeksLMPAsEarlyRegistration() throws Exception { SafeMap reportData = new SafeMap(); reportData.put("referenceDate", "2011-10-08"); reportData.put("motherId", "CASE-1"); reportData.put("registrationDate", "2011-12-15"); reportData.put("submissionDate", "2011-12-15"); when(allMothers.findByCaseId("CASE-1")).thenReturn(MOTHER); when(allEligibleCouples.findByCaseId("EC-CASE-1")).thenReturn(new EligibleCouple().withLocation("bherya", "Sub Center", "PHC X")); when(reportMonth.areDatesBelongToSameReportingMonth(parse("2011-12-15"), parse("2011-12-15"))).thenReturn(true); service.registerANC(reportData); verifyBothReportingCalls(ANC_BEFORE_12_WEEKS, "2011-12-15", "CASE-1"); verifyNoReportingCalls(ANC_AFTER_12_WEEKS, "2011-12-15", "CASE-1"); }