ANCService { public void ancVisit(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn("Found ANC visit without registered mother for Entity ID: " + submission.entityId()); return; } updateMotherAfterANCVisit(submission, mother); ancSchedulesService.ancVisitHasHappened(submission.entityId(), submission.anmId(), parseInt(submission.getField(AllConstants.ANCFormFields.ANC_VISIT_NUMBER_FIELD)), submission.getField(AllConstants.ANCFormFields.ANC_VISIT_DATE_FIELD)); List<String> reportFields = reportFieldsDefinition.get(submission.formName()); reportingService.ancVisit(new SafeMap(submission.getFields(reportFields))); } @Autowired  ANCService(AllMothers allMothers,
                      AllEligibleCouples eligibleCouples,
                      ANCSchedulesService ancSchedulesService,
                      ActionService actionService,
                      MotherReportingService reportingService,
                      ReportFieldsDefinition reportFieldsDefinition); void registerANC(FormSubmission submission); void registerOutOfAreaANC(FormSubmission submission); void ancVisit(FormSubmission submission); void ttProvided(FormSubmission submission); void ifaTabletsGiven(FormSubmission submission); void hbTest(FormSubmission submission); void deliveryPlanned(FormSubmission submission); void deliveryOutcome(FormSubmission submission); void close(FormSubmission submission); void ancInvestigations(FormSubmission submission);  }
@Test public void shouldHandleANCVisit() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("entity id 1") .addFormField("ancVisitDate", "2013-01-01") .addFormField("ancVisitNumber", "2") .addFormField("someKey", "someValue") .addFormField("weight", "55") .addFormField("bpSystolic", "120") .addFormField("bpDiastolic", "80") .build(); Mother mother = new Mother("entity id 1", "ec id 1", "TC1") .withDetails(mapOf("someKey", "someValue")); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_visit")).thenReturn(asList("someKey")); service.ancVisit(submission); verify(allMothers).update(mother .withANCVisits(asList( create("ancVisitDate", "2013-01-01") .put("weight", "55") .put("bpSystolic", "120") .put("bpDiastolic", "80") .map() ))); verify(ancSchedulesService).ancVisitHasHappened("entity id 1", "anm id 1", 2, "2013-01-01"); verify(motherReportingService).ancVisit(new SafeMap(mapOf("someKey", "someValue"))); }
@Test public void shouldUpdateANCVisitInformationWhenItAlreadyExists() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("entity id 1") .addFormField("ancVisitDate", "2013-01-01") .addFormField("ancVisitNumber", "2") .addFormField("someKey", "someValue") .addFormField("weight", "55") .addFormField("bpSystolic", "120") .addFormField("bpDiastolic", "80") .build(); List<Map<String, String>> ancVisits = new ArrayList<>(); ancVisits.add(create("ancVisitDate", "2012-09-01") .put("weight", "55") .put("bpSystolic", "121") .put("bpDiastolic", "81") .put("ancVisitNumber", "1") .map()); Mother mother = new Mother("entity id 1", "ec id 1", "TC1") .withANCVisits(ancVisits) .withDetails(mapOf("someKey", "someValue")); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_visit")).thenReturn(asList("someKey")); service.ancVisit(submission); Mother updatedMother = new Mother("entity id 1", "ec id 1", "TC1") .withDetails(mapOf("someKey", "someValue")) .withANCVisits(asList( create("ancVisitDate", "2012-09-01") .put("weight", "55") .put("bpSystolic", "121") .put("bpDiastolic", "81") .put("ancVisitNumber", "1") .map(), create("ancVisitDate", "2013-01-01") .put("weight", "55") .put("bpSystolic", "120") .put("bpDiastolic", "80") .put("ancVisitNumber", "2") .map() )); verify(allMothers).update(updatedMother); verify(ancSchedulesService).ancVisitHasHappened("entity id 1", "anm id 1", 2, "2013-01-01"); verify(motherReportingService).ancVisit(new SafeMap(mapOf("someKey", "someValue"))); }
@Test public void shouldHandleANCVisitAndUpdateHyperTensionDetectedForFirstTimeAsTrueWhenHyperTensionOccursForFirstTime() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("thayiCardNumber", "TC1") .addFormField("ecCaseId", "ec id 1") .addFormField("ancVisitDate", "2013-01-01") .addFormField("ancVisitNumber", "2") .addFormField("someKey", "someValue") .addFormField("bpSystolic", "140") .addFormField("bpDiastolic", "90") .build(); Mother mother = new Mother("mother id 1", "ec id 1", "TC1") .withDetails(create("someKey", "someValue").put("bpDiastolic", "90").put("bpSystolic", "140").map()); when(allMothers.findByCaseId("mother id 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_visit")).thenReturn(asList("someKey", "bpSystolic", "bpDiastolic")); service.ancVisit(submission); verify(ancSchedulesService).ancVisitHasHappened("mother id 1", "anm id 1", 2, "2013-01-01"); verify(motherReportingService).ancVisit(new SafeMap(create("someKey", "someValue").put("bpDiastolic", "90").put("bpSystolic", "140").map())); Mother updatedMother = new Mother("mother id 1", "ec id 1", "TC1") .withANCVisits(asList(create("ancVisitDate", "2013-01-01") .put("weight", null) .put("bpSystolic", "140") .put("bpDiastolic", "90") .put("ancVisitNumber", "2") .map())) .withDetails(create("someKey", "someValue") .put("isHypertensionDetectedForFirstTime", "true") .put("bpDiastolic", "90") .put("bpSystolic", "140") .map()); verify(allMothers).update(updatedMother); }
@Test public void shouldHandleANCVisitAndSetHyperTensionDetectedForFirstTimeAsFalseWhenHyperTensionOccursForSecondTime() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("thayiCardNumber", "TC1") .addFormField("ecCaseId", "ec id 1") .addFormField("ancVisitDate", "2013-01-01") .addFormField("ancVisitNumber", "2") .addFormField("someKey", "someValue") .addFormField("bpSystolic", "140") .addFormField("bpDiastolic", "90") .build(); Mother mother = new Mother("mother id 1", "ec id 1", "TC1") .withDetails(create("someKey", "someValue") .put("bpDiastolic", "90") .put("bpSystolic", "140") .put("isHypertensionDetectedForFirstTime", "true") .map()); when(allMothers.findByCaseId("mother id 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_visit")).thenReturn(asList("someKey", "bpSystolic", "bpDiastolic")); service.ancVisit(submission); verify(ancSchedulesService).ancVisitHasHappened("mother id 1", "anm id 1", 2, "2013-01-01"); verify(motherReportingService).ancVisit(new SafeMap(create("someKey", "someValue").put("bpDiastolic", "90").put("bpSystolic", "140").map())); Mother updatedMother = new Mother("mother id 1", "ec id 1", "TC1") .withANCVisits(asList(create("ancVisitDate", "2013-01-01") .put("weight", null) .put("bpSystolic", "140") .put("bpDiastolic", "90") .put("ancVisitNumber", "2") .map())) .withDetails(create("someKey", "someValue") .put("isHypertensionDetectedForFirstTime", "false") .put("bpDiastolic", "90") .put("bpSystolic", "140") .map()); verify(allMothers).update(updatedMother); }
@Test public void shouldHandleANCVisitAndShouldNotUpdateMotherWhenThereIsNoHyperTensionDetected() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("thayiCardNumber", "TC1") .addFormField("ecCaseId", "ec id 1") .addFormField("ancVisitDate", "2013-01-01") .addFormField("ancVisitNumber", "2") .addFormField("someKey", "someValue") .addFormField("bpSystolic", "120") .addFormField("bpDiastolic", "80") .build(); Mother mother = new Mother("mother id 1", "ec id 1", "TC1") .withDetails(create("someKey", "someValue").put("bpDiastolic", "80").put("bpSystolic", "120").map()); when(allMothers.findByCaseId("mother id 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_visit")).thenReturn(asList("someKey", "bpSystolic", "bpDiastolic")); service.ancVisit(submission); verify(ancSchedulesService).ancVisitHasHappened("mother id 1", "anm id 1", 2, "2013-01-01"); verify(motherReportingService).ancVisit(new SafeMap(create("someKey", "someValue").put("bpDiastolic", "80").put("bpSystolic", "120").map())); Mother updatedMother = new Mother("mother id 1", "ec id 1", "TC1").withDetails( create("someKey", "someValue") .put("bpDiastolic", "80") .put("bpSystolic", "120") .map() ); verify(allMothers, never()).update(updatedMother); }
@Test public void shouldNotHandleANCVisitIfMotherIsNotFound() { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("entity id 1") .build(); when(allMothers.findByCaseId("entity id 1")).thenReturn(null); service.ancVisit(submission); verifyZeroInteractions(reportFieldsDefinition); verifyZeroInteractions(ancSchedulesService); verifyZeroInteractions(motherReportingService); }