ECService { public void reportFPChange(FormSubmission submission) { EligibleCouple couple = allEligibleCouples.findByCaseId(submission.entityId()); if (couple == null) { logger.warn("Tried to report FP Change of a non-existing EC, with submission: " + submission); return; } String newFPMethod = submission.getField(NEW_FP_METHOD_FIELD_NAME); couple.details().put(CURRENT_FP_METHOD_FIELD_NAME, newFPMethod); updateECWithFPDetails(couple, submission, newFPMethod); allEligibleCouples.update(couple); List<String> reportFields = reportFieldsDefinition.get(submission.formName()); Map<String, String> reportFieldsMap = submission.getFields(reportFields); reportFieldsMap.put(CASTE, couple.details().get(CASTE)); reportFieldsMap.put(ECONOMIC_STATUS, couple.details().get(ECONOMIC_STATUS)); reportingService.fpChange(new SafeMap(reportFieldsMap)); String fpMethodChangeDate = submission.getField(FP_METHOD_CHANGE_DATE_FIELD_NAME); if (isBlank(fpMethodChangeDate)) { fpMethodChangeDate = submission.getField(SUBMISSION_DATE_FIELD_NAME); } FPProductInformation fpProductInformation = new FPProductInformation( submission.entityId(), submission.anmId(), newFPMethod, submission.getField(PREVIOUS_FP_METHOD_FIELD_NAME), null, submission.getField(NUMBER_OF_OCP_STRIPS_SUPPLIED_FIELD_NAME), null, submission.getField(NUMBER_OF_CONDOMS_SUPPLIED_FIELD_NAME), submission.getField(SUBMISSION_DATE_FIELD_NAME), fpMethodChangeDate, null, null, null); schedulingService.fpChange(fpProductInformation); } @Autowired  ECService(AllEligibleCouples allEligibleCouples, ECSchedulingService schedulingService, ECReportingService reportingService,
                     ReportFieldsDefinition reportFieldsDefinition); void registerEligibleCouple(FormSubmission submission); void reportFPComplications(FormSubmission submission); void reportFPChange(FormSubmission submission); void renewFPProduct(FormSubmission submission); void followupOnFPMethod(FormSubmission submission); void handleReferralFollowup(FormSubmission submission); void closeEligibleCouple(FormSubmission submission);  }
@Test public void shouldUpdateCurrentFPMethodOfECWhenFPMethodIsChanged() throws Exception { List<CondomFPDetails> condomFPDetails = new ArrayList<>(); condomFPDetails.add(new CondomFPDetails( "2010-01-01", asList(create("date", "2010-01-01").put("quantity", "20").map()))); EligibleCouple ec = new EligibleCouple("entity id 1", "EC Number 1") .withDetails(create("currentMethod", "ocp").put("caste", "c_others").map()) .withIUDFPDetails(Collections.<IUDFPDetails>emptyList()) .withCondomFPDetails(condomFPDetails) .withOCPFPDetails(asList(new OCPFPDetails( "2010-06-01", asList(create("date", "2011-06-01").put("quantity", "20").map()), "2011-06-01", "negative"))) .withFemaleSterilizationFPDetails(Collections.<FemaleSterilizationFPDetails>emptyList()) .withMaleSterilizationFPDetails(Collections.<MaleSterilizationFPDetails>emptyList()); when(allEligibleCouples.findByCaseId("entity id 1")).thenReturn(ec); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("fp_change") .addFormField("currentMethod", "ocp") .addFormField("newMethod", "condom") .addFormField("familyPlanningMethodChangeDate", "2011-01-01") .addFormField("numberOfCondomsSupplied", "20") .build(); EligibleCouple expectedEc = new EligibleCouple("entity id 1", "EC Number 1") .withDetails(create("currentMethod", "condom") .put("caste", "c_others") .map()) .withIUDFPDetails(Collections.<IUDFPDetails>emptyList()) .withOCPFPDetails(asList(new OCPFPDetails( "2010-06-01", asList(create("date", "2011-06-01").put("quantity", "20").map()), "2011-06-01", "negative"))) .withFemaleSterilizationFPDetails(Collections.<FemaleSterilizationFPDetails>emptyList()) .withMaleSterilizationFPDetails(Collections.<MaleSterilizationFPDetails>emptyList()) .withCondomFPDetails(asList(new CondomFPDetails( "2010-01-01", asList(create("date", "2010-01-01").put("quantity", "20").map())), new CondomFPDetails("2011-01-01", asList(create("date", "2011-01-01").put("quantity", "20").map())))); ecService.reportFPChange(submission); verify(allEligibleCouples).update(expectedEc); }
@Test public void shouldSendDataToReportingServiceDuringReportFPChange() throws Exception { EligibleCouple ec = new EligibleCouple("entity id 1", "EC Number 1").withDetails(create("caste", "sc").put("economicStatus", "bpl").map()) .withIUDFPDetails(new ArrayList<IUDFPDetails>()) .withCondomFPDetails(new ArrayList<CondomFPDetails>()) .withOCPFPDetails(new ArrayList<OCPFPDetails>()) .withFemaleSterilizationFPDetails(new ArrayList<FemaleSterilizationFPDetails>()) .withMaleSterilizationFPDetails(new ArrayList<MaleSterilizationFPDetails>()); when(allEligibleCouples.findByCaseId("entity id 1")).thenReturn(ec); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("fp_change") .addFormField("someKey", "someValue") .addFormField("newMethod", "condom") .build(); when(reportFieldsDefinition.get("fp_change")).thenReturn(asList("someKey")); ecService.reportFPChange(submission); verify(allEligibleCouples).findByCaseId("entity id 1"); verify(reportingService).fpChange(new SafeMap(create("someKey", "someValue").put("caste", "sc").put("economicStatus", "bpl").map())); }
@Test public void shouldNotDoAnythingDuringReportFPChangeIfNoECIsFound() throws Exception { when(allEligibleCouples.findByCaseId("entity id 1")).thenReturn(null); FormSubmission submission = FormSubmissionBuilder.create().build(); ecService.reportFPChange(submission); verify(allEligibleCouples).findByCaseId("entity id 1"); verifyZeroInteractions(reportingService); verifyZeroInteractions(schedulingService); }
@Test public void shouldUpdateECSchedulesWhenFPMethodIsChanged() throws Exception { EligibleCouple ec = new EligibleCouple("entity id 1", "EC Number 1").withDetails(Collections.<String, String>emptyMap()); when(allEligibleCouples.findByCaseId("entity id 1")).thenReturn(ec); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("fp_change") .withANMId("anm id 1") .addFormField("currentMethod", "previous method") .addFormField("newMethod", "new method") .addFormField("submissionDate", "2011-01-01") .addFormField("familyPlanningMethodChangeDate", "2011-01-02") .addFormField("numberOfOCPDelivered", "1") .addFormField("numberOfCondomsSupplied", "20") .build(); when(reportFieldsDefinition.get("fp_change")).thenReturn(asList("someKey")); ecService.reportFPChange(submission); verify(schedulingService).fpChange( new FPProductInformation("entity id 1", "anm id 1", "new method", "previous method", null, "1", null, "20", "2011-01-01", "2011-01-02", null, null, null)); }
@Test public void shouldUseSubmissionDateAsChangeDateWhenFPMethodIsChangedAndChangeDateIsBlank() throws Exception { EligibleCouple ec = new EligibleCouple("entity id 1", "EC Number 1").withDetails(Collections.<String, String>emptyMap()); when(allEligibleCouples.findByCaseId("entity id 1")).thenReturn(ec); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("fp_change") .withANMId("anm id 1") .addFormField("currentMethod", "previous method") .addFormField("newMethod", "none") .addFormField("submissionDate", "2011-02-01") .build(); when(reportFieldsDefinition.get("fp_change")).thenReturn(asList("someKey")); ecService.reportFPChange(submission); verify(schedulingService).fpChange( new FPProductInformation("entity id 1", "anm id 1", "none", "previous method", null, null, null, null, "2011-02-01", "2011-02-01", null, null, null)); }