ANCService { public void ifaTabletsGiven(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn("Tried to handle ifa tablets given without registered mother. Submission: " + submission); return; } int numberOfIFATabletsGivenThisTime = IntegerUtil.tryParse(submission.getField(NUMBER_OF_IFA_TABLETS_GIVEN), 0); mother.updateTotalNumberOfIFATabletsGiven(numberOfIFATabletsGivenThisTime); Map<String, String> ifaTablets = create(IFA_TABLETS_DATE, submission.getField(IFA_TABLETS_DATE)) .put(NUMBER_OF_IFA_TABLETS_GIVEN, submission.getField(NUMBER_OF_IFA_TABLETS_GIVEN)) .map(); mother.updateIFATabletsInformation(ifaTablets); allMothers.update(mother); ancSchedulesService.ifaTabletsGiven( submission.entityId(), submission.anmId(), submission.getField(NUMBER_OF_IFA_TABLETS_GIVEN), submission.getField(IFA_TABLETS_DATE)); } @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 shouldUpdateIFASchedulesWhenNumberOfIFATabletsGivenIsMoreThanZero() { Mother mother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(create("some-key", "some-value").put("totalNumberOfIFATabletsGiven", "30").map()); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("ifa") .addFormField("numberOfIFATabletsGiven", "100") .addFormField("ifaTabletsDate", "2013-05-24") .build(); service.ifaTabletsGiven(submission); verify(ancSchedulesService).ifaTabletsGiven("entity id 1", "anmId", "100", "2013-05-24"); }
@Test public void shouldNotDoAnythingWhenIFATabletsAreGivenForNonExistentEC() { when(allMothers.findByCaseId("entity id 1")).thenReturn(null); FormSubmission submission = create() .withFormName("ifa") .withEntityId("entity id 1") .addFormField("numberOfIFATabletsGiven", "0") .build(); service.ifaTabletsGiven(submission); verifyZeroInteractions(ancSchedulesService); }
@Test public void shouldCreateTotalNumberOfIFATabletsGivenWhenIFATabletsIsGivenForFirstTime() { Mother mother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(mapOf("some-key", "some-value")); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("ifa") .withEntityId("entity id 1") .addFormField("numberOfIFATabletsGiven", "30") .addFormField("ifaTabletsDate", "2013-05-24") .build(); service.ifaTabletsGiven(submission); Mother updatedMother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(create("some-key", "some-value").put("totalNumberOfIFATabletsGiven", "30").map()) .withIFATablets(asList( create("ifaTabletsDate", "2013-05-24") .put("numberOfIFATabletsGiven", "30") .map() )); verify(allMothers).update(updatedMother); }
@Test public void shouldUpdateIFATabletsInformationWhenTheyAreGiven() { List<Map<String, String>> ifaTablets = new ArrayList<>(); ifaTablets.add(create("ifaTabletsDate", "2013-04-24") .put("numberOfIFATabletsGiven", "30") .map()); Mother mother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(mapOf("some-key", "some-value")) .withIFATablets(ifaTablets); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("ifa") .withEntityId("entity id 1") .addFormField("numberOfIFATabletsGiven", "30") .addFormField("ifaTabletsDate", "2013-05-24") .build(); service.ifaTabletsGiven(submission); Mother updatedMother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(create("some-key", "some-value").put("totalNumberOfIFATabletsGiven", "30").map()) .withIFATablets(asList( create("ifaTabletsDate", "2013-04-24") .put("numberOfIFATabletsGiven", "30") .map(), create("ifaTabletsDate", "2013-05-24") .put("numberOfIFATabletsGiven", "30") .map() )); verify(allMothers).update(updatedMother); }
@Test public void shouldMaintainTotalNumberOfIFATabletsGiven() { Mother mother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(create("some-key", "some-value").put("totalNumberOfIFATabletsGiven", "30").map()); when(allMothers.findByCaseId("entity id 1")).thenReturn(mother); FormSubmission submission = create() .withFormName("ifa") .withEntityId("entity id 1") .addFormField("numberOfIFATabletsGiven", "60") .addFormField("ifaTabletsDate", "2013-05-24") .build(); service.ifaTabletsGiven(submission); Mother updatedMother = new Mother("entity id 1", "ec entity id 1", "thayi 1") .withDetails(create("some-key", "some-value").put("totalNumberOfIFATabletsGiven", "90").map()) .withIFATablets(asList( create("ifaTabletsDate", "2013-05-24") .put("numberOfIFATabletsGiven", "60") .map() )); verify(allMothers).update(updatedMother); }