ChildService { public void register(FormSubmission submission) { SubForm subForm = submission.getSubFormByName(AllConstants.DeliveryOutcomeFields.CHILD_REGISTRATION_SUB_FORM_NAME); if (handleStillBirth(submission, subForm)) return; String referenceDate = submission.getFieldValue(AllConstants.DeliveryOutcomeFields.REFERENCE_DATE); String deliveryPlace = submission.getFieldValue(AllConstants.DeliveryOutcomeFields.DELIVERY_PLACE); Mother mother = motherRepository.findById(submission.entityId()); for (Map<String, String> childInstance : subForm.instances()) { Child child = childRepository.find(childInstance.get(ENTITY_ID_FIELD_NAME)); childRepository.update(child.setIsClosed(false).setThayiCardNumber(mother.thayiCardNumber()).setDateOfBirth(referenceDate)); allTimelines.add(forChildBirthInChildProfile(child.caseId(), referenceDate, child.getDetail(AllConstants.ChildRegistrationFields.WEIGHT), child.getDetail(AllConstants.ChildRegistrationFields.IMMUNIZATIONS_GIVEN))); allTimelines.add(forChildBirthInMotherProfile(mother.caseId(), referenceDate, child.gender(), referenceDate, deliveryPlace)); allTimelines.add(forChildBirthInECProfile(mother.ecCaseId(), referenceDate, child.gender(), referenceDate)); String immunizationsGiven = child.getDetail(AllConstants.ChildRegistrationFields.IMMUNIZATIONS_GIVEN); for (String immunization : immunizationsGiven.split(SPACE)) { serviceProvidedService.add(ServiceProvided.forChildImmunization(child.caseId(), immunization, referenceDate)); } } } ChildService(AllBeneficiaries allBeneficiaries, MotherRepository motherRepository, ChildRepository childRepository,
                        AllTimelineEvents allTimelineEvents, ServiceProvidedService serviceProvidedService, AllAlerts allAlerts); void register(FormSubmission submission); void registerForEC(FormSubmission submission); void pncRegistrationOA(FormSubmission submission); void updateImmunizations(FormSubmission submission); void pncVisitHappened(FormSubmission submission); void close(FormSubmission submission); void updatePhotoPath(String entityId, String imagePath); void updateIllnessStatus(FormSubmission submission); void updateVitaminAProvided(FormSubmission submission); void registerForOA(FormSubmission submission);  }
@Test public void shouldUpdateEveryOnlyNewlyBornChildrenWhileRegistering() throws Exception { Child firstChild = new Child("Child X", "Mother X", "female", create("weight", "3").put("immunizationsGiven", "bcg opv_0").map()); Child secondChild = new Child("Child Y", "Mother X", "female", create("weight", "4").put("immunizationsGiven", "bcg").map()); FormSubmission submission = mock(FormSubmission.class); SubForm subForm = mock(SubForm.class); when(motherRepository.findById("Mother X")).thenReturn(new Mother("Mother X", "EC 1", "TC 1", "2012-01-01")); when(childRepository.find("Child X")).thenReturn(firstChild); when(childRepository.find("Child Y")).thenReturn(secondChild); when(submission.entityId()).thenReturn("Mother X"); when(submission.getFieldValue("referenceDate")).thenReturn("2012-01-01"); when(submission.getFieldValue("deliveryPlace")).thenReturn("phc"); when(submission.getSubFormByName("child_registration")).thenReturn(subForm); when(subForm.instances()).thenReturn(asList(mapOf("id", "Child X"), mapOf("id", "Child Y"))); service.register(submission); verify(childRepository).find("Child X"); verify(childRepository).find("Child Y"); verify(childRepository).update(firstChild.setIsClosed(false).setDateOfBirth("2012-01-01").setThayiCardNumber("TC 1")); verify(childRepository).update(secondChild.setIsClosed(false).setDateOfBirth("2012-01-01").setThayiCardNumber("TC 1")); verify(allTimelineEvents).add(forChildBirthInChildProfile("Child X", "2012-01-01", "3", "bcg opv_0")); verify(allTimelineEvents).add(forChildBirthInChildProfile("Child Y", "2012-01-01", "4", "bcg")); verify(allTimelineEvents, times(2)).add(forChildBirthInMotherProfile("Mother X", "2012-01-01", "female", "2012-01-01", "phc")); verify(allTimelineEvents, times(2)).add(forChildBirthInECProfile("EC 1", "2012-01-01", "female", "2012-01-01")); verify(serviceProvidedService).add(ServiceProvided.forChildImmunization("Child X", "bcg", "2012-01-01")); verify(serviceProvidedService).add(ServiceProvided.forChildImmunization("Child X", "opv_0", "2012-01-01")); verify(serviceProvidedService).add(ServiceProvided.forChildImmunization("Child Y", "bcg", "2012-01-01")); verifyNoMoreInteractions(childRepository); verifyNoMoreInteractions(allTimelineEvents); }
@Test public void shouldDeleteRegisteredChildWhenDeliveryOutcomeIsStillBirth() throws Exception { FormSubmission submission = mock(FormSubmission.class); SubForm subForm = mock(SubForm.class); when(submission.entityId()).thenReturn("Mother X"); when(submission.getFieldValue("referenceDate")).thenReturn("2012-01-01"); when(submission.getFieldValue("deliveryPlace")).thenReturn("phc"); when(submission.getFieldValue("deliveryOutcome")).thenReturn("still_birth"); when(submission.getSubFormByName("child_registration")).thenReturn(subForm); when(subForm.instances()).thenReturn(asList(mapOf("id", "Child X"))); service.register(submission); verify(childRepository).delete("Child X"); verifyNoMoreInteractions(childRepository); verifyNoMoreInteractions(allTimelineEvents); verifyNoMoreInteractions(serviceProvidedService); }