ANCSchedulesService { public void hbTestDone(String entityId, String anmId, String date, String anaemicStatus, LocalDate lmp) { if (fulfillMilestoneIfPossible(entityId, anmId, SCHEDULE_HB_TEST_1, SCHEDULE_HB_TEST_1, parse(date))) { if (isNotBlank(anaemicStatus)) { logger.info(format("ANC is anaemic so enrolling her to Hb Followup Test schedule: Entity id:{0}, Anaemic status: {1}", entityId, anaemicStatus)); scheduleService.enroll(entityId, SCHEDULE_HB_FOLLOWUP_TEST, date); } else { enrollANCToHbTest2Schedule(entityId, lmp); } } else if (fulfillMilestoneIfPossible(entityId, anmId, SCHEDULE_HB_FOLLOWUP_TEST, SCHEDULE_HB_FOLLOWUP_TEST, parse(date))) { if (parse(date).isAfter(lmp.plusWeeks(NUMBER_OF_WEEKS_BEFORE_HB_TEST_2_BECOMES_DUE))) { fulfillMilestoneIfPossible(entityId, anmId, SCHEDULE_HB_TEST_2, SCHEDULE_HB_TEST_2, parse(date)); } else { enrollANCToHbTest2Schedule(entityId, lmp); } } else { fulfillMilestoneIfPossible(entityId, anmId, SCHEDULE_HB_TEST_2, SCHEDULE_HB_TEST_2, parse(date)); } } @Autowired  ANCSchedulesService(ScheduleTrackingService trackingService, ActionService actionService, ScheduleService scheduleService); void enrollMother(String caseId, LocalDate referenceDateForSchedule); void ancVisitHasHappened(String entityId, String anmId, int visitNumberToFulfill, String visitDate); void deliveryHasBeenPlanned(String entityId, String anmId, String deliveryPlanDate); void ttVisitHasHappened(String entityId, String anmId, String ttDose, String ttDate); void ifaTabletsGiven(String entityId, String anmId, String numberOfIFATabletsGiven, String ifaGivenDate); void hbTestDone(String entityId, String anmId, String date, String anaemicStatus, LocalDate lmp); void forceFulfillMilestone(String externalId, String scheduleName); void unEnrollFromAllSchedules(String entityId); static final int NUMBER_OF_WEEKS_BEFORE_HB_TEST_2_BECOMES_DUE; }
@Test public void shouldFulfillHbTest1WhenHbTestIsDone() { when(trackingService.getEnrollment("Case X", "Hb Test 1")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 1", "HB Test 1", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-01-01", null, parse("2012-09-01")); verify(trackingService).fulfillCurrentMilestone(eq("Case X"), eq("Hb Test 1"), eq(parse("2013-01-01")), any(Time.class)); }
@Test public void shouldEnrollANCToHbFollowupTestWhenHbTestIsDoneAndSheIsAnaemic() { when(trackingService.getEnrollment("Case X", "Hb Test 1")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 1", "HB Test 1", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-01-01", "Anaemic", null); verify(scheduleService).enroll("Case X", "Hb Followup Test", "2013-01-01"); }
@Test public void shouldEnrollANCToHbTest2WhenHbTestIsDoneAndSheIsNotAnaemic() { when(trackingService.getEnrollment("Case X", "Hb Test 1")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 1", "HB Test 1", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-01-01", null, parse("2012-09-01")); verify(scheduleService).enroll("Case X", "Hb Test 2", "2012-09-01"); }
@Test public void shouldFulfillHbFollowupTestWhenHbTestIsDone() { when(trackingService.getEnrollment("Case X", "Hb Test 1")).thenReturn(null); when(trackingService.getEnrollment("Case X", "Hb Followup Test")) .thenReturn(new EnrollmentRecord("Case X", "Hb Followup Test", "Hb Followup Test", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-01-01", "Anaemic", parse("2012-09-01")); verify(trackingService).fulfillCurrentMilestone(eq("Case X"), eq("Hb Followup Test"), eq(parse("2013-01-01")), any(Time.class)); }
@Test public void shouldFulfillHbTest2WhenHbTestIsDoneAndFollowupIsFulfilledAndDateIs28WeeksAfterLMP() { when(trackingService.getEnrollment("Case X", "Hb Test 1")).thenReturn(null); when(trackingService.getEnrollment("Case X", "Hb Followup Test")) .thenReturn(new EnrollmentRecord("Case X", "Hb Followup Test", "Hb Followup Test", null, null, null, null, null, null, null)); when(trackingService.getEnrollment("Case X", "Hb Test 2")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 2", "Hb Test 2", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-04-16", "Anaemic", parse("2012-09-01")); verify(trackingService).fulfillCurrentMilestone(eq("Case X"), eq("Hb Test 2"), eq(parse("2013-04-16")), any(Time.class)); }
@Test public void shouldEnrollToHbTest2WhenHbTestIsDoneAndFollowupIsFulfilledAndDateIsNot28WeeksAfterLMP() { when(trackingService.getEnrollment("Case X", "Hb Test 1")).thenReturn(null); when(trackingService.getEnrollment("Case X", "Hb Followup Test")) .thenReturn(new EnrollmentRecord("Case X", "Hb Followup Test", "Hb Followup Test", null, null, null, null, null, null, null)); when(trackingService.getEnrollment("Case X", "Hb Test 2")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 2", "Hb Test 2", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-02-16", "Anaemic", parse("2012-09-01")); verify(trackingService, times(0)).fulfillCurrentMilestone(eq("Case X"), eq("Hb Test 2"), eq(parse("2013-04-16")), any(Time.class)); verify(scheduleService).enroll("Case X", "Hb Test 2", "2012-09-01"); }
@Test public void shouldFulfillHbTest2WhenHbTestIsDoneAndItsTheActiveMilestone() { when(trackingService.getEnrollment("Case X", "Hb Test 1")).thenReturn(null); when(trackingService.getEnrollment("Case X", "Hb Followup Test")).thenReturn(null); when(trackingService.getEnrollment("Case X", "Hb Test 2")) .thenReturn(new EnrollmentRecord("Case X", "Hb Test 2", "Hb Test 2", null, null, null, null, null, null, null)); ancSchedulesService.hbTestDone("Case X", "ANM 1", "2013-04-16", "Anaemic", parse("2012-09-01")); verify(trackingService).fulfillCurrentMilestone(eq("Case X"), eq("Hb Test 2"), eq(parse("2013-04-16")), any(Time.class)); }