FormSubmissionRouter { public void route(String instanceId) throws Exception { FormSubmission submission = formSubmissionsRepository.findByInstanceId(instanceId); FormSubmissionHandler handler = handlerMap.get(submission.formName()); if (handler == null) { logger.warn("Could not find a handler due to unknown form submission: " + submission); return; } logger.info(format("Handling {0} form submission with instance Id: {1} for entity: {2}", submission.formName(), submission.instanceId(), submission.entityId())); try { handler.handle(submission); formSubmissionReportService.reportFor(submission); mctsReportService.reportFor(submission); } catch (Exception e) { logger.error(format("Handling {0} form submission with instance Id: {1} for entity: {2} failed with exception : {3}", submission.formName(), submission.instanceId(), submission.entityId(), getFullStackTrace(e))); throw e; } } @Autowired  FormSubmissionRouter(AllFormSubmissions formSubmissionsRepository,
                                ECRegistrationHandler ecRegistrationHandler,
                                FPComplicationsHandler fpComplicationsHandler,
                                FPChangeHandler fpChangeHandler,
                                RenewFPProductHandler renewFPProductHandler,
                                FPFollowupHandler fpFollowupHandler,
                                FPReferralFollowupHandler fpReferralFollowupHandler,
                                ECCloseHandler ecCloseHandler,
                                ANCRegistrationHandler ancRegistrationHandler,
                                ANCRegistrationOAHandler ancRegistrationOAHandler,
                                ANCVisitHandler ancVisitHandler,
                                ANCCloseHandler ancCloseHandler,
                                TTHandler ttHandler,
                                IFAHandler ifaHandler,
                                HbTestHandler hbTestHandler,
                                DeliveryOutcomeHandler deliveryOutcomeHandler,
                                PNCRegistrationOAHandler pncRegistrationOAHandler,
                                PNCCloseHandler pncCloseHandler,
                                PNCVisitHandler pncVisitHandler,
                                ChildRegistrationECHandler childRegistrationECHandler,
                                ChildRegistrationOAHandler childRegistrationOAHandler,
                                VitaminAHandler vitaminAHandler,
                                ChildImmunizationsHandler childImmunizationsHandler,
                                ChildIllnessHandler childIllnessHandler,
                                ChildCloseHandler childCloseHandler,
                                DeliveryPlanHandler deliveryPlanHandler,
                                PostpartumFamilyPlanningHandler postpartumFamilyPlanningHandler,
                                RecordECPsHandler recordECPsHandler,
                                ECEditHandler ecEditHandler,
                                ANCInvestigationsHandler ancInvestigationsHandler,
                                FormSubmissionReportService formSubmissionReportService,
                                MCTSReportService mctsReportService); void route(String instanceId);  }
@Test public void shouldDelegateFormSubmissionHandlingToHandlerBasedOnFormName() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "ec_registration", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(ecRegistrationHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); formSubmission = new FormSubmission("anm id 1", "instance id 2", "fp_complications", "entity id 2", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 2")).thenReturn(formSubmission); router.route("instance id 2"); verify(fpComplicationsHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateRenewFPProductFormSubmissionHandlingToRenewFPProductHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "renew_fp_product", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(renewFPProductHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateFPReferralFollowupFormSubmissionHandlingToFPReferralFollowupHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "fp_referral_followup", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(fpReferralFollowupHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateECCloseFormSubmissionHandlingToECCloseHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "ec_close", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ecCloseHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateANCRegistrationFormSubmissionHandlingToANCRegistrationHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "anc_registration", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ancRegistrationHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateANCRegistrationOAFormSubmissionHandlingToANCRegistrationOAHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "anc_registration_oa", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ancRegistrationOAHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateANCVisitFormSubmissionHandlingToANCVisitHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "anc_visit", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ancVisitHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateANCCloseFormSubmissionHandlingToANCCloseHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "anc_close", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ancCloseHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateTTBoosterFormSubmissionHandlingToTTHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "tt_booster", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ttHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateTT1FormSubmissionHandlingToTTHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "tt_1", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ttHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateTT2FormSubmissionHandlingToTTHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "tt_2", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ttHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateIFAFormSubmissionHandlingToIFAHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "ifa", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ifaHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateHbTestFormSubmissionHandlingToHBTestHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "hb_test", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(hbTestHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateDeliveryOutcomeTestFormSubmissionHandlingToDeliveryOutcomeHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "delivery_outcome", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(deliveryOutcomeHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegatePNCRegistrationOAFormSubmissionHandlingToPNCRegistrationOAHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "pnc_registration_oa", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(pncRegistrationOAHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegatePNCCloseFormSubmissionHandlingToPNCCloseHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "pnc_close", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(pncCloseHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegatePNCVisitFormSubmissionHandlingToPNCVisitHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "pnc_visit", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(pncVisitHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateChildRegistrationECFormSubmissionHandlingToChildRegistrationECHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "child_registration_ec", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(childRegistrationECHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateChildImmunizationsFormSubmissionHandlingToChildImmunizationsHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "child_immunizations", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(childImmunizationsHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateChildCloseFormSubmissionHandlingToChildCloseHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "child_close", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(childCloseHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateChildRegistrationOAFormSubmissionHandlingToChildRegistrationOAHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "child_registration_oa", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(childRegistrationOAHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateVitaminAFormSubmissionHandlingToVitaminAHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "vitamin_a", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(vitaminAHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateDeliveryPlanFormSubmissionHandlingToDeliveryPlanHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "delivery_plan", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(deliveryPlanHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegatePostpartumFamilyPlanningFormSubmissionHandlingToPostpartumFamilyPlanningHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "postpartum_family_planning", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(postpartumFamilyPlanningHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateRecordECPsFormSubmissionHandlingToRecordECPsHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "record_ecps", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(recordECPsHandler).handle(formSubmission); }
@Test public void shouldDelegateECEditFormSubmissionHandlingToECEditHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "ec_edit", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(ecEditHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }
@Test public void shouldDelegateANCInvestigationsFormSubmissionHandlingToANCInvestigationswHandler() throws Exception { FormSubmission formSubmission = new FormSubmission("anm id 1", "instance id 1", "anc_investigations", "entity id 1", 0L, "1", null, 0L); when(formSubmissionsRepository.findByInstanceId("instance id 1")).thenReturn(formSubmission); router.route("instance id 1"); verify(formSubmissionsRepository).findByInstanceId("instance id 1"); verify(ancInvestigationsHandler).handle(formSubmission); verify(formSubmissionReportService).reportFor(formSubmission); }