ActionService { public void alertForBeneficiary(BeneficiaryType beneficiaryType, String caseID, String scheduleName, String visitCode, AlertStatus alertStatus, DateTime startDate, DateTime expiryDate) { String anmIdentifier; if (mother.equals(beneficiaryType)) { Mother mother = allMothers.findByCaseId(caseID); anmIdentifier = mother == null ? null : mother.anmIdentifier(); } else if (child.equals(beneficiaryType)) { Child child = allChildren.findByCaseId(caseID); anmIdentifier = child == null ? null : child.anmIdentifier(); } else if (ec.equals(beneficiaryType)) { EligibleCouple eligibleCouple = allEligibleCouples.findByCaseId(caseID); anmIdentifier = eligibleCouple == null ? null : eligibleCouple.anmIdentifier(); } else { throw new IllegalArgumentException("Beneficiary Type : " + beneficiaryType + " is of unknown type"); } if (anmIdentifier != null) { allActions.addOrUpdateAlert(new Action(caseID, anmIdentifier, ActionData.createAlert(beneficiaryType, scheduleName, visitCode, alertStatus, startDate, expiryDate))); } } @Autowired  ActionService(AllActions allActions, AllMothers allMothers, AllChildren allChildren, AllEligibleCouples allEligibleCouples); List<Action> getNewAlertsForANM(String anmIdentifier, long timeStamp); void alertForBeneficiary(BeneficiaryType beneficiaryType, String caseID, String scheduleName, String visitCode, AlertStatus alertStatus, DateTime startDate, DateTime expiryDate); void markAllAlertsAsInactive(String entityId); void markAlertAsInactive(String anmId, String entityId, String scheduleName); void markAlertAsClosed(String caseId, String anmIdentifier, String visitCode, String completionDate); void closeMother(String caseId, String anmIdentifier, String reasonForClose); void reportForIndicator(String anmIdentifier, ActionData actionData); void deleteReportActions();  }
@Test public void shouldSaveAlertActionForMother() throws Exception { when(allMothers.findByCaseId("Case X")).thenReturn(new Mother("Case X", "EC-CASE-1", "Thayi 1").withAnm("ANM ID M")); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(mother, "Case X", "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate); verify(allActions).addOrUpdateAlert(new Action("Case X", "ANM ID M", ActionData.createAlert(mother, "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate))); }
@Test public void shouldNotSaveAlertActionIfThereIsNoMotherForTheCaseId() throws Exception { when(allMothers.findByCaseId("Case X")).thenReturn(null); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(mother, "Case X", "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate); verify(allActions, never()).addOrUpdateAlert(new Action("Case X", "ANM ID M", ActionData.createAlert(mother, "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate))); }
@Test public void shouldDeleteExistingAlertBeforeCreatingNewOne() throws Exception { when(allMothers.findByCaseId("Case X")).thenReturn(new Mother("Case X", "EC-CASE-1", "Thayi 1").withAnm("ANM ID M")); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(mother, "Case X", "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate); verify(allActions).addOrUpdateAlert(new Action("Case X", "ANM ID M", ActionData.createAlert(mother, "Ante Natal Care - Normal", "ANC 1", normal, dueDate, expiryDate))); }
@Test public void shouldSaveAlertActionForChild() throws Exception { when(allChildren.findByCaseId("Case X")).thenReturn(new Child("Case X", "MOTHER-CASE-1", "bcg", "3", "female").withAnm("ANM ID C")); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(child, "Case X", "OPV", "OPV", normal, dueDate, expiryDate); verify(allActions).addOrUpdateAlert(new Action("Case X", "ANM ID C", ActionData.createAlert(child, "OPV", "OPV", normal, dueDate, expiryDate))); }
@Test public void shouldNotSaveAlertActionIfThereIsNoChildForTheCaseId() throws Exception { when(allChildren.findByCaseId("Case X")).thenReturn(null); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(child, "Case X", "OPV", "OPV", normal, dueDate, expiryDate); verify(allActions, never()).addOrUpdateAlert(new Action("Case X", "ANM ID C", ActionData.createAlert(child, "OPV", "OPV", normal, dueDate, expiryDate))); }
@Test public void shouldSaveAlertActionForEC() throws Exception { when(allEligibleCouples.findByCaseId("Case X")).thenReturn(new EligibleCouple("Case X", "EC-CASE-1").withANMIdentifier("ANM ID C")); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(ec, "Case X", "OCP Refill", "OCP Refill", urgent, dueDate, expiryDate); verify(allActions).addOrUpdateAlert(new Action("Case X", "ANM ID C", ActionData.createAlert(ec, "OCP Refill", "OCP Refill", urgent, dueDate, expiryDate))); }
@Test public void shouldNotSaveAlertActionIfThereIsNoECForTheCaseId() throws Exception { when(allEligibleCouples.findByCaseId("Case X")).thenReturn(null); DateTime dueDate = DateTime.now().minusDays(1); DateTime expiryDate = dueDate.plusWeeks(2); service.alertForBeneficiary(ec, "Case X", "OCP Refill", "OCP Refill", urgent, dueDate, expiryDate); verify(allActions, never()).addOrUpdateAlert(new Action("Case X", "ANM ID C", ActionData.createAlert(ec, "OCP Refill", "OCP Refill", urgent, dueDate, expiryDate))); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionIfBeneficiaryTypeIsUnknown() throws Exception { service.alertForBeneficiary(null, "Case X", "Schedule name", "FP Complications", urgent, null, null); }