ChildService { public void registerChildren(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn("Failed to handle children registration as there is no mother registered with id: " + submission.entityId()); return; } SubFormData subFormData = submission.getSubFormByName(AllConstants.DeliveryOutcomeFields.CHILD_REGISTRATION_SUB_FORM_NAME); if (handleStillBirth(submission, subFormData)) return; String referenceDate = submission.getField(REFERENCE_DATE); for (Map<String, String> childFields : subFormData.instances()) { Child child = allChildren.findByCaseId(childFields.get(ID)); List<String> immunizationsGiven = getNamesOfImmunizationsGiven(childFields.get(IMMUNIZATIONS_GIVEN_FIELD_NAME)); child = child.withAnm(submission.anmId()).withDateOfBirth(referenceDate) .withThayiCard(mother.thayiCardNumber()).setIsClosed(false) .withImmunizations(getChildImmunizationDetails(submission, child, immunizationsGiven)); allChildren.update(child); SafeMap reportingData = new SafeMap(); reportingData.put(ChildReportingService.CHILD_ID_FIELD, child.caseId()); reportingData.put(DELIVERY_PLACE, submission.getField(DELIVERY_PLACE)); reportingData.put(BF_POSTBIRTH, submission.getField(DID_BREAST_FEEDING_START)); reportingData.put(REGISTRATION_DATE, referenceDate); reportingData.put(SUBMISSION_DATE_FIELD_NAME, submission.getField(SUBMISSION_DATE_FIELD_NAME)); childReportingService.registerChild(reportingData); childSchedulesService.enrollChild(child); } } @Autowired  ChildService(ChildSchedulesService childSchedulesService,
                        AllMothers allMothers,
                        AllChildren allChildren,
                        ChildReportingService childReportingService, ActionService actionService, ReportFieldsDefinition reportFieldsDefinition); void registerChildren(FormSubmission submission); void registerChildrenForEC(FormSubmission submission); void registerChildrenForOA(FormSubmission submission); void updateChildImmunization(FormSubmission submission); void vitaminAProvided(FormSubmission submission); void closeChild(FormSubmission submission); void pncOAChildRegistration(FormSubmission submission); void pncVisitHappened(FormSubmission submission); void sickVisitHappened(FormSubmission submission); static final String IMMUNIZATIONS_SEPARATOR; }
@Test public void shouldEnrollEveryChildIntoSchedulesAndReportDuringRegistration() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "EC-CASE-1", "TC1")); Child firstChild = new Child("child id 1", "mother id 1", "opv", "2", "female"); Child secondChild = new Child("child id 2", "mother id 1", "opv", "2", "male"); when(allChildren.findByMotherId("mother id 1")).thenReturn(asList(firstChild, secondChild)); when(allChildren.findByCaseId("child id 1")).thenReturn(firstChild); when(allChildren.findByCaseId("child id 2")).thenReturn(secondChild); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didBreastfeedingStart", "no") .addFormField("deliveryPlace", "phc") .addFormField("deliveryRegistrationDate", "2012-01-02") .addFormField("submissionDate", "2012-01-02") .withSubForm(new SubFormData("child_registration", asList(mapOf("id", "child id 1"), mapOf("id", "child id 2")))) .build(); service.registerChildren(submission); InOrder inOrder = inOrder(childReportingService, childSchedulesService); inOrder.verify(childReportingService).registerChild(new SafeMap(create("didBreastfeedingStart", "no").put("childId", "child id 1").put("deliveryPlace", "phc").put("registrationDate", "2012-01-01").put("submissionDate", "2012-01-02").map())); inOrder.verify(childSchedulesService).enrollChild(firstChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); inOrder.verify(childReportingService).registerChild(new SafeMap(create("didBreastfeedingStart", "no").put("childId", "child id 2").put("deliveryPlace", "phc").put("registrationDate", "2012-01-01").put("submissionDate", "2012-01-02").map())); inOrder.verify(childSchedulesService).enrollChild(secondChild.withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); assertFalse(firstChild.isClosed()); assertFalse(secondChild.isClosed()); }
@Test public void shouldDeleteChildRegisteredThroughDeliveryOutcomeFormWhenDeliveryOutcomeIsStillBirth() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("deliveryOutcome", "still_birth") .withSubForm(new SubFormData("child_registration", asList(mapOf("id", "child id 1")))) .build(); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "EC-CASE-1", "TC1")); service.registerChildren(submission); verify(allChildren).remove("child id 1"); verifyZeroInteractions(childReportingService); verifyZeroInteractions(childSchedulesService); }
@Test public void shouldUpdateEveryChildWithMotherInfoAndImmunizationInfoDuringRegistration() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByCaseId("mother id 1")).thenReturn(new Mother("mother id 1", "EC-CASE-1", "TC1")); Child firstChild = new Child("child id 1", "mother id 1", "opv", "2", "female"); Child secondChild = new Child("child id 2", "mother id 1", "opv", "2", "male"); when(allChildren.findByMotherId("mother id 1")).thenReturn(asList(firstChild, secondChild)); when(allChildren.findByCaseId("child id 1")).thenReturn(firstChild); when(allChildren.findByCaseId("child id 2")).thenReturn(secondChild); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("submissionDate", "2012-01-01") .addFormField("didBreastfeedingStart", "no") .withSubForm(new SubFormData("child_registration", asList(create("id", "child id 1").put("immunizationsGiven", "bcg").map(), mapOf("id", "child id 2")))) .build(); service.registerChildren(submission); verify(allChildren).update(new Child("child id 1", "mother id 1", "opv", "2", "female").withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1") .withImmunizations(mapOf("bcg", "2012-01-01"))); verify(allChildren).update(new Child("child id 2", "mother id 1", "opv", "2", "male").withAnm("anm id 1").withDateOfBirth("2012-01-01").withThayiCard("TC1")); assertFalse(firstChild.isClosed()); assertFalse(secondChild.isClosed()); }
@Test public void shouldNotHandleChildRegistrationWhenMotherIsNotFound() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); when(allMothers.findByCaseId("MOTHER-CASE-1")).thenReturn(null); FormSubmission submission = create() .withFormName("delivery_outcome") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("referenceDate", "2012-01-01") .addFormField("didBreastfeedingStart", "no") .build(); service.registerChildren(submission); verifyZeroInteractions(allChildren); verifyZeroInteractions(childReportingService); verifyZeroInteractions(childSchedulesService); }