ChildService { public void registerForEC(FormSubmission submission) { if (shouldCloseMother(submission.getFieldValue(SHOULD_CLOSE_MOTHER))) { closeMother(submission.getFieldValue(AllConstants.ChildRegistrationFields.MOTHER_ID)); } Map<String, String> immunizationDateFieldMap = createImmunizationDateFieldMap(); allTimelines.add(forChildBirthInChildProfile( submission.getFieldValue(AllConstants.ChildRegistrationFields.CHILD_ID), submission.getFieldValue(AllConstants.ChildRegistrationFields.DATE_OF_BIRTH), submission.getFieldValue(AllConstants.ChildRegistrationFields.WEIGHT), submission.getFieldValue(AllConstants.ChildRegistrationFields.IMMUNIZATIONS_GIVEN))); allTimelines.add(forChildBirthInMotherProfile( submission.getFieldValue(AllConstants.ChildRegistrationFields.MOTHER_ID), submission.getFieldValue(AllConstants.ChildRegistrationFields.DATE_OF_BIRTH), submission.getFieldValue(AllConstants.ChildRegistrationFields.GENDER), null, null)); allTimelines.add(forChildBirthInECProfile( submission.entityId(), submission.getFieldValue(AllConstants.ChildRegistrationFields.DATE_OF_BIRTH), submission.getFieldValue(AllConstants.ChildRegistrationFields.GENDER), null)); String immunizationsGiven = submission.getFieldValue(AllConstants.ChildRegistrationFields.IMMUNIZATIONS_GIVEN); for (String immunization : immunizationsGiven.split(SPACE)) { serviceProvidedService.add(ServiceProvided.forChildImmunization( submission.getFieldValue(AllConstants.ChildRegistrationFields.CHILD_ID), immunization, submission.getFieldValue(immunizationDateFieldMap.get(immunization)))); } } 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 shouldAddTimelineEventWhenChildIsRegisteredForEC() throws Exception { FormSubmission submission = mock(FormSubmission.class); Mother mother = new Mother("mother id 1", "ec id 1", "thayi card number", "2013-01-01"); when(submission.entityId()).thenReturn("ec id 1"); when(submission.getFieldValue("motherId")).thenReturn("mother id 1"); when(submission.getFieldValue("childId")).thenReturn("child id 1"); when(submission.getFieldValue("dateOfBirth")).thenReturn("2013-01-02"); when(submission.getFieldValue("gender")).thenReturn("female"); when(submission.getFieldValue("weight")).thenReturn("3"); when(submission.getFieldValue("immunizationsGiven")).thenReturn("bcg opv_0"); when(submission.getFieldValue("bcgDate")).thenReturn("2012-01-06"); when(submission.getFieldValue("opv0Date")).thenReturn("2012-01-07"); when(submission.getFieldValue("shouldCloseMother")).thenReturn(""); when(allBeneficiaries.findMother("mother id 1")).thenReturn(mother); service.registerForEC(submission); verify(allBeneficiaries).updateMother(mother.setIsClosed(true)); verify(allTimelineEvents).add(forChildBirthInChildProfile("child id 1", "2013-01-02", "3", "bcg opv_0")); verify(allTimelineEvents).add(forChildBirthInMotherProfile("mother id 1", "2013-01-02", "female", null, null)); verify(allTimelineEvents).add(forChildBirthInECProfile("ec id 1", "2013-01-02", "female", null)); verify(serviceProvidedService).add(forChildImmunization("child id 1", "bcg", "2012-01-06")); verify(serviceProvidedService).add(forChildImmunization("child id 1", "opv_0", "2012-01-07")); }
@Test public void shouldNotCloseMotherWhenAnOpenANCAlreadyExistWhileRegisteringAChildForEC() throws Exception { FormSubmission submission = mock(FormSubmission.class); when(submission.entityId()).thenReturn("ec id 1"); when(submission.getFieldValue("motherId")).thenReturn("mother id 1"); when(submission.getFieldValue("childId")).thenReturn("child id 1"); when(submission.getFieldValue("dateOfBirth")).thenReturn("2013-01-02"); when(submission.getFieldValue("gender")).thenReturn("female"); when(submission.getFieldValue("weight")).thenReturn("3"); when(submission.getFieldValue("immunizationsGiven")).thenReturn("bcg opv_0"); when(submission.getFieldValue("bcgDate")).thenReturn("2012-01-06"); when(submission.getFieldValue("opv0Date")).thenReturn("2012-01-07"); when(submission.getFieldValue("shouldCloseMother")).thenReturn("false"); service.registerForEC(submission); verify(allTimelineEvents).add(forChildBirthInChildProfile("child id 1", "2013-01-02", "3", "bcg opv_0")); verify(allTimelineEvents).add(forChildBirthInMotherProfile("mother id 1", "2013-01-02", "female", null, null)); verify(allTimelineEvents).add(forChildBirthInECProfile("ec id 1", "2013-01-02", "female", null)); verify(serviceProvidedService).add(forChildImmunization("child id 1", "bcg", "2012-01-06")); verify(serviceProvidedService).add(forChildImmunization("child id 1", "opv_0", "2012-01-07")); }