ECService { public void closeEligibleCouple(FormSubmission submission) { if (!BOOLEAN_TRUE_VALUE.equalsIgnoreCase(submission.getField(IS_EC_CLOSE_CONFIRMED_FIELD_NAME))) { logger.warn("ANM has not confirmed the close so not closing EC! Form Submission: " + submission); return; } if (!allEligibleCouples.exists(submission.entityId())) { logger.warn("Cannot close EC as it does not exist! Form Submission: " + submission); return; } if (WRONG_ENTRY_VALUE.equalsIgnoreCase(submission.getField(CLOSE_REASON_FIELD_NAME))) { logger.info("Deleting Reports for EC with Entity Id: " + submission.entityId()); reportingService.deleteReportsForEC(submission.entityId()); } logger.info("Closing EC : " + submission); allEligibleCouples.close(submission.entityId()); } @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 shouldCloseECWithoutDeletingReportsWhenCloseReasonIsNotWrongEntry() throws Exception { when(allEligibleCouples.exists("entity id 1")).thenReturn(true); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("ec_close") .addFormField("isECCloseConfirmed", "yes") .addFormField("closeReason", "permanent_relocation") .build(); ecService.closeEligibleCouple(submission); verify(allEligibleCouples).close("entity id 1"); verifyZeroInteractions(reportingService); }
@Test public void shouldCloseECAsWrongEntryAndDeleteAllReports() throws Exception { when(allEligibleCouples.exists("entity id 1")).thenReturn(true); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("ec_close") .addFormField("isECCloseConfirmed", "yes") .addFormField("closeReason", "wrong_entry") .build(); ecService.closeEligibleCouple(submission); verify(allEligibleCouples).close("entity id 1"); verify(reportingService).deleteReportsForEC("entity id 1"); }
@Test public void shouldNotCloseECWhenANMDoesNotConfirmClose() throws Exception { when(allEligibleCouples.exists("entity id 1")).thenReturn(true); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("ec_close") .addFormField("isECCloseConfirmed", "no") .build(); ecService.closeEligibleCouple(submission); verify(allEligibleCouples, times(0)).close("entity id 1"); }
@Test public void shouldNotCloseECWhenECDoesNotExist() throws Exception { when(allEligibleCouples.exists("entity id 1")).thenReturn(false); FormSubmission submission = FormSubmissionBuilder.create() .withFormName("ec_close") .build(); ecService.closeEligibleCouple(submission); verify(allEligibleCouples, times(0)).close("entity id 1"); }