ANCService { public void close(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn("Tried to close case without registered mother for case ID: " + submission.entityId()); return; } allMothers.close(submission.entityId()); List<String> reportFields = reportFieldsDefinition.get(submission.formName()); reportingService.closeANC(new SafeMap(submission.getFields(reportFields))); ancSchedulesService.unEnrollFromAllSchedules(submission.entityId()); actionService.markAllAlertsAsInactive(submission.entityId()); if (DEATH_OF_WOMAN_VALUE.equalsIgnoreCase(submission.getField(CLOSE_REASON_FIELD_NAME)) || PERMANENT_RELOCATION_VALUE.equalsIgnoreCase(submission.getField(CLOSE_REASON_FIELD_NAME))) { logger.info("Closing EC case along with ANC case. Submission: " + submission); eligibleCouples.close(mother.ecCaseId()); } } @Autowired  ANCService(AllMothers allMothers,
                      AllEligibleCouples eligibleCouples,
                      ANCSchedulesService ancSchedulesService,
                      ActionService actionService,
                      MotherReportingService reportingService,
                      ReportFieldsDefinition reportFieldsDefinition); void registerANC(FormSubmission submission); void registerOutOfAreaANC(FormSubmission submission); void ancVisit(FormSubmission submission); void ttProvided(FormSubmission submission); void ifaTabletsGiven(FormSubmission submission); void hbTest(FormSubmission submission); void deliveryPlanned(FormSubmission submission); void deliveryOutcome(FormSubmission submission); void close(FormSubmission submission); void ancInvestigations(FormSubmission submission);  }
@Test public void shouldUnEnrollAMotherFromScheduleWhenANCCaseIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(ancSchedulesService).unEnrollFromAllSchedules("entity id 1"); verify(motherReportingService, never()).deleteReports(anyString()); }
@Test public void shouldCloseAMotherWhenANCIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(allMothers).close("entity id 1"); }
@Test public void shouldNotUnEnrollAMotherFromScheduleWhenSheIsNotRegistered() { when(allMothers.findByCaseId("entity id 1")).thenReturn(null); service.close(create().build()); verifyZeroInteractions(ancSchedulesService); }
@Test public void shouldCloseECCaseAlsoWhenANCIsClosedAndReasonIsDeath() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "death_of_woman").build()); verify(eligibleCouples).close("ec entity id 1"); }
@Test public void shouldCloseECCaseAlsoWhenANCIsClosedAndReasonIsPermanentRelocation() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "relocation_permanent").build()); verify(eligibleCouples).close("ec entity id 1"); }
@Test public void shouldNotCloseECCaseWhenANCIsClosedAndReasonIsNeitherDeathOrPermanentRelocation() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "other_reason").build()); verifyZeroInteractions(ecService); }
@Test public void shouldMarkAllActionsAsInactiveWhenANCIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(actionService).markAllAlertsAsInactive("entity id 1"); }