FormSubmissionReportService { public void reportFor(FormSubmission submission) throws Exception { if (reportDefinition == null) { logger.info("Loading report definition."); reportDefinition = reportDefinitionLoader.load(); } List<ReportIndicator> reportIndicators = reportDefinition.getIndicatorsByFormName(submission.formName()); for (ReportIndicator reportIndicator : reportIndicators) { SafeMap reportFields = createReportFields(submission, reportIndicator); boolean didAllRulesSucceed = processRules(reportIndicator.reportingRules(), reportFields, reportIndicator.indicator()); if (didAllRulesSucceed) { String entityId = reportIndicator.reportEntityIdField() == null ? submission.entityId() : submission.getField(reportIndicator.reportEntityIdField()); Location location = locationLoader.loadLocationFor(reportIndicator.reportEntityType(), entityId); report(submission, reportIndicator, location, reportFields); } } } @Autowired  FormSubmissionReportService(ILocationLoader locationLoader, IRulesFactory rulesFactory, IReporterFactory reporterFactory, IReferenceDataRepository referenceDataRepository, IReportDefinitionLoader reportDefinitionLoader); void reportFor(FormSubmission submission);  }
@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(); }
@Test public void shouldNotReportWhenRulesAreNotSatisfied() throws Exception { FormSubmission submission = create() .withFormName("child_close") .withANMId("anm id 1") .withEntityId("child id 1") .addFormField("submissionDate", "2013-03-01") .addFormField("closeReason", "permanent_relocation") .build(); when(reportDefinitionLoader.load()).thenReturn(reportDefinition()); when(rulesFactory.ruleByName(any(String.class))).thenReturn(rule); when(referenceDataRepository.getReferenceData(any(FormSubmission.class), any(ReferenceData.class))).thenReturn(new SafeMap()); when(rule.apply(any(SafeMap.class))).thenReturn(false); service.reportFor(submission); verifyZeroInteractions(reportingService); verifyZeroInteractions(locationLoader); }