ChildService { public void pncVisitHappened(FormSubmission submission) { String pncVisitDate = submission.getFieldValue(AllConstants.PNCVisitFields.PNC_VISIT_DATE); String pncVisitDay = submission.getFieldValue(AllConstants.PNCVisitFields.PNC_VISIT_DAY); SubForm subForm = submission.getSubFormByName(AllConstants.PNCVisitFields.CHILD_PNC_VISIT_SUB_FORM_NAME); if (handleStillBirth(submission, subForm)) return; for (Map<String, String> childInstances : subForm.instances()) { allTimelines.add(forChildPNCVisit(childInstances.get(ENTITY_ID_FIELD_NAME), pncVisitDay, pncVisitDate, childInstances.get(AllConstants.PNCVisitFields.WEIGHT), childInstances.get(AllConstants.PNCVisitFields.TEMPERATURE))); serviceProvidedService.add(ServiceProvided.forChildPNCVisit(childInstances.get(ENTITY_ID_FIELD_NAME), pncVisitDay, pncVisitDate)); } } 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 shouldAddPNCVisitTimelineEventWhenPNCVisitHappens() throws Exception { FormSubmission submission = mock(FormSubmission.class); SubForm subForm = mock(SubForm.class); when(submission.entityId()).thenReturn("mother id 1"); when(submission.getFieldValue("pncVisitDay")).thenReturn("2"); when(submission.getFieldValue("pncVisitDate")).thenReturn("2012-01-01"); when(submission.getSubFormByName("child_pnc_visit")).thenReturn(subForm); when(subForm.instances()).thenReturn( asList( create("id", "child id 1") .put("weight", "3") .put("temperature", "98") .map(), create("id", "child id 2") .put("weight", "4") .put("temperature", "98.1") .map())); service.pncVisitHappened(submission); verify(allTimelineEvents).add(TimelineEvent.forChildPNCVisit("child id 1", "2", "2012-01-01", "3", "98")); verify(allTimelineEvents).add(TimelineEvent.forChildPNCVisit("child id 2", "2", "2012-01-01", "4", "98.1")); }
@Test public void shouldHandleStillBirthWhenPNCVisitHappens() throws Exception { FormSubmission submission = mock(FormSubmission.class); SubForm subForm = mock(SubForm.class); when(submission.entityId()).thenReturn("mother id 1"); when(submission.getFieldValue("pncVisitDay")).thenReturn("2"); when(submission.getFieldValue("pncVisitDate")).thenReturn("2012-01-01"); when(submission.getFieldValue("deliveryOutcome")).thenReturn("still_birth"); when(submission.getSubFormByName("child_pnc_visit")).thenReturn(subForm); when(subForm.instances()).thenReturn(asList(create("id", "child id 1").map())); service.pncVisitHappened(submission); verify(childRepository).delete("child id 1"); verifyNoMoreInteractions(allTimelineEvents); }
@Test public void shouldAddPNCVisitServiceProvidedWhenPNCVisitHappens() throws Exception { FormSubmission submission = mock(FormSubmission.class); SubForm subForm = mock(SubForm.class); when(submission.entityId()).thenReturn("mother id 1"); when(submission.getFieldValue("pncVisitDay")).thenReturn("2"); when(submission.getFieldValue("pncVisitDate")).thenReturn("2012-01-01"); when(submission.getSubFormByName("child_pnc_visit")).thenReturn(subForm); when(subForm.instances()).thenReturn( asList(mapOf("id", "child id 1"), mapOf("id", "child id 2"))); service.pncVisitHappened(submission); verify(serviceProvidedService).add(new ServiceProvided("child id 1", "PNC", "2012-01-01", mapOf("day", "2"))); verify(serviceProvidedService).add(new ServiceProvided("child id 2", "PNC", "2012-01-01", mapOf("day", "2"))); }