ChildService { public void registerChildrenForEC(FormSubmission submission) { if (shouldCloseMother(submission.getField(SHOULD_CLOSE_MOTHER))) { closeMother(submission.getField(MOTHER_ID)); } Child child = allChildren.findByCaseId(submission.getField(ChildReportingService.CHILD_ID_FIELD)); child.withAnm(submission.anmId()).withThayiCard(submission.getField(THAYI_CARD_NUMBER)).setIsClosed(false); List<String> immunizationsGiven = getNamesOfImmunizationsGiven(submission.getField(IMMUNIZATIONS_GIVEN_FIELD_NAME)); List<String> vitaminHistory = getVitaminDoses(submission.getField("childVitaminAHistory")); child.withImmunizations(getChildImmunizationDetails(submission, child, immunizationsGiven)); child.withVitaminADoses(getVitaminDoseDetails(submission, child, vitaminHistory)); allChildren.update(child); 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 shouldUpdateChildWithANMDetailsAndImmunizationDetailsWhenItIsRegisteredForAnEC() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); Child child = new Child("child id 1", "mother id 1", "opv", "2", "female"); Mother mother = new Mother("mother id 1", "ec id 1", "thayi card number"); when(allChildren.findByCaseId("child id 1")).thenReturn(child); when(allMothers.findByCaseId("mother id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("child_registration_ec") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("motherId", "mother id 1") .addFormField("childId", "child id 1") .addFormField("immunizationsGiven", "bcg opv_0") .addFormField("bcgDate", "2013-01-01") .addFormField("opv0Date", "2013-01-02") .addFormField("childVitaminAHistory", "1 2") .addFormField("vitamin1Date", "2013-01-01") .addFormField("vitamin2Date", "2013-01-02") .addFormField("shouldCloseMother", "") .build(); service.registerChildrenForEC(submission); verify(allMothers).update(mother.setIsClosed(true)); verify(allChildren).update(new Child("child id 1", "mother id 1", "opv", "2", "female").withAnm("anm id 1") .withImmunizations(create("bcg", "2013-01-01") .put("opv_0", "2013-01-02").map()) .withVitaminADoses(create("dose1", "2013-01-01") .put("dose2", "2013-01-02") .map())); assertFalse(child.isClosed()); }
@Test public void shouldNotCloseMotherWhenAnOpenANCIsPresentAlready() { DateTime currentTime = DateUtil.now(); mockCurrentDate(currentTime); Child child = new Child("child id 1", "mother id 1", "opv", "2", "female"); Mother mother = new Mother("mother id 1", "ec id 1", "thayi card number"); when(allChildren.findByCaseId("child id 1")).thenReturn(child); when(allMothers.findByCaseId("mother id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("child_registration_ec") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("motherId", "mother id 1") .addFormField("childId", "child id 1") .addFormField("immunizationsGiven", "bcg opv_0") .addFormField("bcgDate", "2013-01-01") .addFormField("opv0Date", "2013-01-02") .addFormField("childVitaminAHistory", "1 2") .addFormField("vitamin1Date", "2013-01-01") .addFormField("vitamin2Date", "2013-01-02") .addFormField("shouldCloseMother", "false") .build(); service.registerChildrenForEC(submission); verify(allChildren).update(new Child("child id 1", "mother id 1", "opv", "2", "female").withAnm("anm id 1") .withImmunizations(create("bcg", "2013-01-01") .put("opv_0", "2013-01-02").map()) .withVitaminADoses(create("dose1", "2013-01-01") .put("dose2", "2013-01-02") .map())); assertFalse(child.isClosed()); }