ChildService { public void updateChildImmunization(FormSubmission submission) { Child child = allChildren.findByCaseId(submission.entityId()); if (child == null) { logger.warn("Found immunization update without registered child for entity ID: " + submission.entityId()); return; } String previousImmunizationsField = isBlank(submission.getField(PREVIOUS_IMMUNIZATIONS_FIELD_NAME)) ? "" : submission.getField(PREVIOUS_IMMUNIZATIONS_FIELD_NAME); List<String> previousImmunizations = asList(previousImmunizationsField.split(IMMUNIZATIONS_SEPARATOR)); List<String> immunizationsGiven = getNamesOfImmunizationsGiven(submission.getField(IMMUNIZATIONS_GIVEN_FIELD_NAME)); immunizationsGiven.removeAll(previousImmunizations); child.withImmunizations(getChildImmunizationDetails(submission, child, immunizationsGiven)); allChildren.update(child); SafeMap reportFieldsMap = new SafeMap(submission.getFields(reportFieldsDefinition.get(submission.formName()))); childReportingService.immunizationProvided(reportFieldsMap, previousImmunizations); childSchedulesService.updateEnrollments(submission.entityId(), previousImmunizations); } @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 shouldUpdateEnrollmentsForUpdatedImmunizations() { Child child = mock(Child.class); when(allChildren.childExists("Case X")).thenReturn(true); when(allChildren.findByCaseId("Case X")).thenReturn(child); FormSubmission submission = create() .withFormName("child_immunizations") .withANMId("anm id 1") .withEntityId("Case X") .addFormField("previousImmunizations", "bcg") .addFormField("immunizationsGiven", "bcg") .addFormField("bcgDate", "2013-01-01") .build(); service.updateChildImmunization(submission); verify(childSchedulesService).updateEnrollments("Case X", asList("bcg")); }
@Test public void shouldNotDoAnythingIfChildIsNotFoundForUpdatedImmunizations() { when(allChildren.childExists("Case X")).thenReturn(false); FormSubmission submission = create() .withFormName("child_immunizations") .withANMId("anm id 1") .withEntityId("Case X") .addFormField("previousImmunizations", "bcg") .addFormField("immunizationsGiven", "bcg opv_0") .build(); service.updateChildImmunization(submission); verifyZeroInteractions(actionService); verifyZeroInteractions(childReportingService); verifyZeroInteractions(childSchedulesService); }
@Test public void shouldCallReportServiceWithPreviousImmunizationsInsteadOfCurrentImmunizations() throws Exception { when(allChildren.findByCaseId("Case X")).thenReturn(new Child("Case X", "MOTHER-CASE-1", "bcg", "3", "female") .withDetails(EXTRA_DATA.get("details"))); when(reportFieldsDefinition.get("child_immunizations")).thenReturn(asList("id", "immunizationsGiven", "immunizationDate")); FormSubmission submission = create() .withFormName("child_immunizations") .withANMId("anm id 1") .withEntityId("Case X") .addFormField("id", "Case X") .addFormField("previousImmunizations", "bcg") .addFormField("immunizationsGiven", "bcg opv_0") .addFormField("immunizationDate", "2013-01-01") .build(); service.updateChildImmunization(submission); verify(childReportingService).immunizationProvided(new SafeMap(create("id", "Case X").put("immunizationsGiven", "bcg opv_0").put("immunizationDate", "2013-01-01").map()), asList("bcg")); }
@Test public void shouldUpdateChildWithImmunizationsInfoFromChildImmunizationsForm() throws Exception { when(allChildren.findByCaseId("Case X")).thenReturn(new Child("Case X", "MOTHER-CASE-1", "bcg", "3", "female") .withDetails(EXTRA_DATA.get("details"))); FormSubmission submission = create() .withFormName("child_immunizations") .withANMId("anm id 1") .withEntityId("Case X") .addFormField("id", "Case X") .addFormField("previousImmunizations", "bcg") .addFormField("immunizationsGiven", "bcg opv_0") .addFormField("immunizationDate", "2013-01-01") .build(); service.updateChildImmunization(submission); verify(allChildren).update(new Child("Case X", "MOTHER-CASE-1", "bcg", "3", "female") .withDetails(EXTRA_DATA.get("details")).withImmunizations(mapOf("opv_0", "2013-01-01"))); }
@Test public void shouldCallReportServiceWithEmptyImmunizationsWhenThereIsNoPreviousImmunization() throws Exception { when(allChildren.childExists("Case X")).thenReturn(true); when(allChildren.findByCaseId("Case X")).thenReturn(new Child("Case X", "MOTHER-CASE-1", "bcg", "3", "female") .withDetails(EXTRA_DATA.get("details"))); when(reportFieldsDefinition.get("child_immunizations")).thenReturn(asList("id", "immunizationsGiven", "immunizationDate")); FormSubmission submission = create() .withFormName("child_immunizations") .withANMId("anm id 1") .withEntityId("Case X") .addFormField("id", "Case X") .addFormField("immunizationsGiven", "bcg opv_0") .addFormField("immunizationDate", "2013-01-01") .build(); service.updateChildImmunization(submission); verify(childReportingService).immunizationProvided( new SafeMap(create("id", "Case X").put("immunizationsGiven", "bcg opv_0").put("immunizationDate", "2013-01-01").map()), asList("")); }