PNCService { public void close(FormSubmission submission) { Mother mother = allMothers.findByCaseId(submission.entityId()); if (mother == null) { logger.warn(format("Failed to close PNC as there is no mother registered with ID: {0}", submission.entityId())); return; } logger.info("Closing PNC case. Entity Id: " + submission.entityId()); allMothers.close(submission.entityId()); actionService.markAllAlertsAsInactive(submission.entityId()); pncSchedulesService.unEnrollFromSchedules(submission.entityId()); List<String> reportFields = reportFieldsDefinition.get(submission.formName()); motherReportingService.closePNC(new SafeMap(submission.getFields(reportFields))); if (DEATH_OF_MOTHER_VALUE.equalsIgnoreCase(submission.getField(CLOSE_REASON_FIELD_NAME)) || PERMANENT_RELOCATION_VALUE.equalsIgnoreCase(submission.getField(CLOSE_REASON_FIELD_NAME))) { logger.info("Closing EC case along with PNC case. Submission: " + submission); allEligibleCouples.close(mother.ecCaseId()); } } @Autowired  PNCService(ActionService actionService,
                      PNCSchedulesService pncSchedulesService,
                      AllEligibleCouples allEligibleCouples,
                      AllMothers allMothers,
                      AllChildren allChildren,
                      MotherReportingService motherReportingService,
                      ReportFieldsDefinition reportFieldsDefinition); void deliveryOutcome(FormSubmission submission); void pncRegistrationOA(FormSubmission submission); void close(FormSubmission submission); void autoClosePNCCase(String entityId); void pncVisitHappened(FormSubmission submission); void reportPPFamilyPlanning(FormSubmission submission);  }
@Test public void shouldUnEnrollAMotherFromScheduleWhenPNCCaseIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(pncSchedulesService).unEnrollFromSchedules("entity id 1"); }
@Test public void shouldCloseAMotherWhenPNCIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(allMothers).close("entity id 1"); }
@Test public void shouldNotDoAnythingIfMotherIsNotRegistered() { when(allMothers.findByCaseId("entity id 1")).thenReturn(null); service.close(create().build()); verifyZeroInteractions(pncSchedulesService); verifyZeroInteractions(allEligibleCouples); verifyZeroInteractions(motherReportingService); verifyZeroInteractions(actionService); }
@Test public void shouldCloseECCaseAlsoWhenPNCIsClosedAndReasonIsDeath() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "death_of_mother").build()); verify(allEligibleCouples).close("ec entity id 1"); }
@Test public void shouldCloseECCaseAlsoWhenPNCIsClosedAndReasonIsPermanentRelocation() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "permanent_relocation").build()); verify(allEligibleCouples).close("ec entity id 1"); }
@Test public void shouldNotCloseECCaseWhenPNCIsClosedAndReasonIsNeitherDeathOrPermanentRelocation() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().addFormField("closeReason", "other_reason").build()); verifyZeroInteractions(allEligibleCouples); }
@Test public void shouldMarkAllActionsAsInactiveWhenPNCIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); service.close(create().build()); verify(actionService).markAllAlertsAsInactive("entity id 1"); }
@Test public void shouldDoReportingWhenPNCIsClosed() { when(allMothers.findByCaseId("entity id 1")).thenReturn(new Mother("entity id 1", "ec entity id 1", "thayi 1")); when(reportFieldsDefinition.get("pnc_close")).thenReturn(asList("some-key")); FormSubmission submission = create() .withFormName("pnc_close") .addFormField("some-key", "some-value") .build(); service.close(submission); verify(motherReportingService).closePNC(new SafeMap(mapOf("some-key", "some-value"))); }