MCTSReportService { public void reportFor(FormSubmission submission) throws Exception { if (reportDefinition == null) { logger.info("Loading MCTS report definition."); reportDefinition = reportDefinitionLoader.load(); } List<MCTSReportIndicator> reportIndicators = reportDefinition.getIndicatorsByFormName(submission.formName()); for (MCTSReportIndicator reportIndicator : reportIndicators) { SafeMap reportFields = createReportFields(submission, reportIndicator); boolean didAllRulesSucceed = processRules(reportIndicator.reportingRules(), reportFields, reportIndicator.indicator()); if (didAllRulesSucceed) { logger.info(MessageFormat.format("Sending MCTS Report for indicator: {0} for entity with id {1} ", reportIndicator, submission.entityId())); reportToMCTS(submission.entityId(), reportFields, reportIndicator, getServiceProvidedDate(submission, reportIndicator)); } } } @Autowired  MCTSReportService(IRulesFactory rulesFactory,
                             IReferenceDataRepository referenceDataRepository,
                             IMCTSReportDefinitionLoader reportDefinitionLoader, MCTSReporter reporter); void reportFor(FormSubmission submission); static final String SUBJECT; }
@Test public void shouldReportIndicatorWhenAllRulesSucceed() throws Exception { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("submissionDate", "2012-03-02") .addFormField("ancVisitDate", "2012-03-01") .addFormField("ancVisitNumber", "1") .build(); when(rulesFactory.ruleByName(any(String.class))).thenReturn(rule); when(reportDefinitionLoader.load()).thenReturn(reportDefinition()); when(referenceDataRepository .getReferenceData(any(FormSubmission.class), any(ReferenceData.class))) .thenReturn( new SafeMap(EasyMap .create("thayiCardNumber", "thayi card 1") .put("registrationDate", "2012-02-01").map()) ); when(rule.apply(any(SafeMap.class))).thenReturn(true); service.reportFor(submission); verify(reporter).report("mother id 1", "thayi card 1", "ANC1", "2012-02-01", "2012-03-01"); }
@Test public void shouldAddReferenceFieldsToReportData() throws Exception { FormSubmission submission = create() .withFormName("anc_visit") .withANMId("anm id 1") .withEntityId("mother id 1") .addFormField("submissionDate", "2012-03-02") .addFormField("ancVisitDate", "2012-03-01") .addFormField("ancVisitNumber", "1") .build(); when(rulesFactory.ruleByName(any(String.class))).thenReturn(rule); when(reportDefinitionLoader.load()).thenReturn(reportDefinition()); when(referenceDataRepository .getReferenceData(any(FormSubmission.class), any(ReferenceData.class))) .thenReturn( new SafeMap(EasyMap .create("thayiCardNumber", "thayi card 1") .put("registrationDate", "2012-02-01").map()) ); service.reportFor(submission); verify(rule).apply(new SafeMap(EasyMap .create("thayiCardNumber", "thayi card 1") .put("id", "mother id 1") .put("registrationDate", "2012-02-01") .put("ancVisitNumber", "1") .put("submissionDate", "2012-03-02") .map())); }
@Test public void shouldLoadReportDefinitionOnlyWhenItIsNull() throws Exception { FormSubmission submission = create() .withFormName("new_form") .withANMId("anm id 1") .withEntityId("child id 1") .addFormField("submissionDate", "2012-03-01") .build(); when(reportDefinitionLoader.load()).thenReturn(reportDefinition()); service.reportFor(submission); service.reportFor(submission); verify(reportDefinitionLoader, times(1)).load(); }