PNCService { public void deliveryOutcome(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn(format("Failed to handle delivery outcome as there is no mother registered with ID: {0}", submission.entityId())); return; } if (BOOLEAN_FALSE_VALUE.equals(submission.getField(DID_WOMAN_SURVIVE)) || BOOLEAN_FALSE_VALUE.equals(submission.getField(DID_MOTHER_SURVIVE))) { logger.info("Closing Mother as the mother died during delivery. Mother Id: " + mother.caseId()); closeMother(mother); } else if (BOOLEAN_TRUE_VALUE.equals(submission.getField(DID_WOMAN_SURVIVE)) || BOOLEAN_TRUE_VALUE.equals(submission.getField(DID_MOTHER_SURVIVE))) { pncSchedulesService.deliveryOutcome(submission.entityId(), submission.getField(REFERENCE_DATE)); } SubFormData subFormData = submission.getSubFormByName(CHILD_REGISTRATION_SUB_FORM_NAME); addChildrenDetailsToMother(submission, subFormData, mother); allMothers.update(mother); } @Autowired  PNCService(ActionService actionService,
                      PNCSchedulesService pncSchedulesService,
                      AllEligibleCouples allEligibleCouples,
                      AllMothers allMothers,
                      AllChildren allChildren,
                      MotherReportingService motherReportingService,
                      ReportFieldsDefinition reportFieldsDefinition); void deliveryOutcome(FormSubmission submission); void pncRegistrationOA(FormSubmission submission); void close(FormSubmission submission); void autoClosePNCCase(String entityId); void pncVisitHappened(FormSubmission submission); void reportPPFamilyPlanning(FormSubmission submission);  }
@Test public void shouldEnrollPNCIntoSchedulesDuringDeliveryOutcomeIfWomanOrMotherSurvives() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "1234567")); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "yes") .addFormField("didMotherSurvive", "") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); verify(pncSchedulesService).deliveryOutcome("mother id 1", "2012-01-01"); }
@Test public void shouldNotEnrollPNCIntoSchedulesDuringDeliveryOutcomeIfMotherDoesNotExists() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.exists("mother id 1")).thenReturn(false); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "no") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldNotEnrollPNCIntoSchedulesDuringDeliveryOutcomeIfWomanDied() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.exists("mother id 1")).thenReturn(true); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "no") .addFormField("didMotherSurvive", "") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldNotEnrollPNCIntoSchedulesDuringDeliveryOutcomeIfMotherDied() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.exists("mother id 1")).thenReturn(true); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "") .addFormField("didMotherSurvive", "no") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldCloseMotherAndECDuringDeliveryOutcomeIfMotherDied() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.exists("mother id 1")).thenReturn(true); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "1234567")); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "") .addFormField("didMotherSurvive", "no") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); Mother expectedMother = new Mother("mother id 1", "ec id 1", "1234567").setIsClosed(true); EligibleCouple expectedEC = new EligibleCouple("ec id 1", "123").setIsClosed(true); verify(allMothers).update(expectedMother); verify(allEligibleCouples).update(expectedEC); verify(actionService).markAllAlertsAsInactive("mother id 1"); verify(pncSchedulesService).unEnrollFromSchedules("mother id 1"); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldCloseMotherAndECDuringDeliveryOutcomeIfWomanDied() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.exists("mother id 1")).thenReturn(true); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "1234567")); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "") .addFormField("didMotherSurvive", "no") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.deliveryOutcome(submission); Mother expectedMother = new Mother("mother id 1", "ec id 1", "1234567").setIsClosed(true); EligibleCouple expectedEC = new EligibleCouple("ec id 1", "123").setIsClosed(true); verify(allMothers).update(expectedMother); verify(allEligibleCouples).update(expectedEC); verify(actionService).markAllAlertsAsInactive("mother id 1"); verify(pncSchedulesService).unEnrollFromSchedules("mother id 1"); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldUpdateMotherWithChildrenDetailsWhenDeliveryOutcome() { when(allMothers.findByCaseId("mother id 1")) .thenReturn(new Mother("mother id 1", "ec id 1", "tc 1") .withChildrenDetails(asList(EasyMap.create("id", "child id") .put("gender", "female") .put("weight", "2.5") .put("immunizationsGiven", "bcg") .map()))); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration", asList(EasyMap.create("id", "child id 1") .put("gender", "male") .put("weight", "2") .put("immunizationsGiven", "bcg") .map(), EasyMap.create("id", "child id 2") .put("gender", "female") .put("weight", "3") .map() ) )) .build(); service.deliveryOutcome(submission); verify(allMothers).update(new Mother("mother id 1", "ec id 1", "tc 1") .withChildrenDetails(asList(EasyMap.create("id", "child id") .put("gender", "female") .put("weight", "2.5") .put("immunizationsGiven", "bcg") .map(), EasyMap.create("id", "child id 1") .put("gender", "male") .put("weight", "2") .put("immunizationsAtBirth", "bcg") .map(), EasyMap.create("id", "child id 2") .put("gender", "female") .put("weight", "3") .put("immunizationsAtBirth", null) .map() ))); }
@Test public void shouldNotAddChildrenDetailsInTheCaseOfStillBirthDuringDeliveryOutcome() throws Exception { when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "ec id 1", "tc 1")); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("deliveryOutcome", "still_birth") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration", asList(EasyMap.create("id", "child id 1").map()))) .build(); service.deliveryOutcome(submission); verify(allMothers).update(new Mother("mother id 1", "ec id 1", "tc 1")); }