ANCService { public void registerANC(FormSubmission submission) { String motherId = submission.getField(AllConstants.ANCFormFields.MOTHER_ID); if (!eligibleCouples.exists(submission.entityId())) { logger.warn(format("Found mother without registered eligible couple. Ignoring: {0} for mother with id: {1} for ANM: {2}", submission.entityId(), motherId, submission.anmId())); return; } Mother mother = allMothers.findByCaseId(motherId); allMothers.update(mother.withAnm(submission.anmId())); ancSchedulesService.enrollMother(motherId, parse(submission.getField(REFERENCE_DATE))); List<String> reportFields = reportFieldsDefinition.get(submission.formName()); reportingService.registerANC(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 shouldRegisterANC() { LocalDate lmp = today(); FormSubmission submission = create() .withFormName("anc_registration") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("motherId", "Mother 1") .addFormField("thayiCardNumber", "thayi 1") .addFormField("referenceDate", lmp.toString()) .addFormField("someKey", "someValue") .build(); Mother mother = new Mother("Mother 1", "ec id 1", "thayi 1").withLMP(lmp); when(eligibleCouples.exists("ec id 1")).thenReturn(true); when(allMothers.findByCaseId("Mother 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_registration")).thenReturn(asList("someKey")); service.registerANC(submission); verify(allMothers).update(mother.withAnm("anm id 1")); verify(motherReportingService).registerANC(new SafeMap(mapOf("someKey", "someValue"))); }
@Test public void shouldNotRegisterANCIfECIsNotFound() { FormSubmission submission = create() .withFormName("anc_registration") .withANMId("anm id 1") .withEntityId("ec id 1") .build(); when(eligibleCouples.exists("ec id 1")).thenReturn(false); service.registerANC(submission); verifyZeroInteractions(allMothers); verifyZeroInteractions(ancSchedulesService); verifyZeroInteractions(motherReportingService); }
@Test public void shouldEnrollMotherIntoDefaultScheduleDuringEnrollmentBasedOnLMP() { LocalDate lmp = today().minusDays(2); FormSubmission submission = create() .withFormName("anc_registration") .withANMId("anm id 1") .withEntityId("ec id 1") .addFormField("motherId", "Mother 1") .addFormField("referenceDate", lmp.toString()) .addFormField("someKey", "someValue") .build(); Mother mother = new Mother("Mother 1", "ec id 1", "thayi 1").withLMP(lmp); when(eligibleCouples.exists("ec id 1")).thenReturn(true); when(allMothers.findByCaseId("Mother 1")).thenReturn(mother); when(reportFieldsDefinition.get("anc_registration")).thenReturn(asList("someKey")); service.registerANC(submission); verify(ancSchedulesService).enrollMother(eq("Mother 1"), eq(lmp)); }