PNCService { public void pncRegistrationOA(FormSubmission submission) { List<Mother> mothers = allMothers.findByEcCaseId(submission.entityId()); if (mothers.size() <= 0) { logger.warn("Failed to handle PNC registration as there is no mother registered with ec id: " + submission.entityId()); return; } Mother mother = mothers.get(0); mother.withAnm(submission.anmId()); addChildrenDetailsToMother(submission, submission.getSubFormByName(CHILD_REGISTRATION_OA), mother); allMothers.update(mother); updateEligibleCouple(submission, mother); if (BOOLEAN_TRUE_VALUE.equals(submission.getField(DID_WOMAN_SURVIVE))) { pncSchedulesService.deliveryOutcome(mother.caseId(), submission.getField(REFERENCE_DATE)); } List<String> reportFields = reportFieldsDefinition.get(submission.formName()); motherReportingService.pncRegistrationOA(new SafeMap(submission.getFields(reportFields))); } @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 shouldUpdateANMInformationOfMotherWhenOAPNCIsRegistered() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); service.pncRegistrationOA(submission); verify(allMothers).update(new Mother("mother id 1", "ec id 1", "tc 1").withAnm("anm id 1")); }
@Test public void shouldUpdateANMInformationOfEligibleCoupleWhenOAPNCIsRegistered() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); service.pncRegistrationOA(submission); verify(allEligibleCouples).update(new EligibleCouple("ec id 1", "123").withANMIdentifier("anm id 1")); }
@Test public void shouldUpdateMotherWithChildrenDetailsWhenPNCRegistrationOA() { when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration_oa", asList(EasyMap.create("id", "child id 1") .put("gender", "male") .put("weight", "2") .map(), EasyMap.create("id", "child id 2") .put("gender", "female") .put("weight", "3") .map() ) )) .build(); service.pncRegistrationOA(submission); verify(allMothers).update(new Mother("mother id 1", "ec id 1", "tc 1") .withAnm("anm id 1") .withChildrenDetails(asList(EasyMap.create("id", "child id 1") .put("gender", "male") .put("weight", "2") .put("immunizationsAtBirth", null) .map(), EasyMap.create("id", "child id 2") .put("gender", "female") .put("weight", "3") .put("immunizationsAtBirth", null) .map() ) )); }
@Test public void shouldNotAddChildrenDetailsInTheCaseOfStillBirthDuringPNCRegistrationOA() throws Exception { when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("deliveryOutcome", "still_birth") .addFormField("didWomanSurvive", "yes") .withSubForm(new SubFormData("child_registration_oa", asList(EasyMap.create("id", "child id 1").map()))) .build(); service.pncRegistrationOA(submission); verify(allMothers).update(new Mother("mother id 1", "ec id 1", "tc 1") .withAnm("anm id 1")); }
@Test public void shouldNotEnrollPNCIntoSchedulesDuringPNCRegistrationIfMotherDoesNotExists() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(Collections.EMPTY_LIST); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "") .withSubForm(new SubFormData("child_registration", Collections.<Map<String, String>>emptyList())) .build(); service.pncRegistrationOA(submission); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldNotEnrollPNCIntoSchedulesPNCRegistrationIfWomanDied() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByEcCaseId("ec id 1")).thenReturn(asList(new Mother("mother id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didWomanSurvive", "no") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); service.pncRegistrationOA(submission); verifyZeroInteractions(pncSchedulesService); }
@Test public void shouldReportPNCRegistrationOA() throws Exception { when(reportFieldsDefinition.get("pnc_registration_oa")).thenReturn(asList("some-key")); when(allMothers.findByEcCaseId("entity id 1")).thenReturn(asList(new Mother("entity id 1", "ec id 1", "tc 1"))); when(allEligibleCouples.findByCaseId("ec id 1")).thenReturn(new EligibleCouple("ec id 1", "123")); FormSubmission submission = create() .withFormName("pnc_registration_oa") .addFormField("some-key", "value") .withSubForm(new SubFormData("child_registration_oa", Collections.<Map<String, String>>emptyList())) .build(); service.pncRegistrationOA(submission); SafeMap reportFields = new SafeMap(mapOf("some-key", "value")); verify(motherReportingService).pncRegistrationOA(reportFields); }