AllBeneficiaries { public void closeAllMothersForEC(String ecId) { List<Mother> mothers = motherRepository.findAllCasesForEC(ecId); if (mothers == null || mothers.isEmpty()) return; for (Mother mother : mothers) { closeMother(mother.caseId()); } } AllBeneficiaries(MotherRepository motherRepository, ChildRepository childRepository,
                            AlertRepository alertRepository, TimelineEventRepository timelineEventRepository); Mother findMotherWithOpenStatus(String caseId); Mother findMother(String caseId); Child findChild(String caseId); long ancCount(); long pncCount(); long childCount(); List<Pair<Mother, EligibleCouple>> allANCsWithEC(); List<Pair<Mother, EligibleCouple>> allPNCsWithEC(); Mother findMotherByECCaseId(String ecCaseId); List<Child> findAllChildrenByMotherId(String entityId); List<Child> findAllChildrenByCaseIDs(List<String> caseIds); List<Mother> findAllMothersByCaseIDs(List<String> caseIds); void switchMotherToPNC(String entityId); void closeMother(String entityId); void closeChild(String entityId); void closeAllMothersForEC(String ecId); List<Child> allChildrenWithMotherAndEC(); List<Child> findAllChildrenByECId(String ecId); Mother findMotherWithOpenStatusByECId(String ecId); boolean isPregnant(String ecId); void updateChild(Child child); void updateMother(Mother mother);  }
@Test public void shouldDeleteTimelineEventsAndAlertsForAllMothersWhenECIsClosed() throws Exception { when(motherRepository.findAllCasesForEC("ec id 1")) .thenReturn(asList(new Mother("mother id 1", "ec id 1", "12345", "2012-12-12"), new Mother("mother id 2", "ec id 2", "123456", "2012-12-10"))); allBeneficiaries.closeAllMothersForEC("ec id 1"); verify(alertRepository).deleteAllAlertsForEntity("mother id 1"); verify(alertRepository).deleteAllAlertsForEntity("mother id 2"); verify(timelineEventRepository).deleteAllTimelineEventsForEntity("mother id 1"); verify(timelineEventRepository).deleteAllTimelineEventsForEntity("mother id 2"); verify(motherRepository).close("mother id 1"); verify(motherRepository).close("mother id 2"); }
@Test public void shouldNotFailClosingMotherWhenECIsClosedAndDoesNotHaveAnyMothers() throws Exception { when(motherRepository.findAllCasesForEC("ec id 1")).thenReturn(null); when(motherRepository.findAllCasesForEC("ec id 1")).thenReturn(Collections.<Mother>emptyList()); allBeneficiaries.closeAllMothersForEC("ec id 1"); verifyZeroInteractions(alertRepository); verifyZeroInteractions(timelineEventRepository); verify(motherRepository, times(0)).close(any(String.class)); }