AnalyzerRuleVisitor extends AbstractRuleVisitor { @Override public boolean visitConcept(Concept concept, Severity effectiveSeverity) throws RuleException { ConceptDescriptor conceptDescriptor = analyzerContext.getStore().find(ConceptDescriptor.class, concept.getId()); Result.Status status; if (conceptDescriptor == null || configuration.isExecuteAppliedConcepts()) { analyzerContext.getLogger() .info("Applying concept '" + concept.getId() + "' with severity: '" + concept.getSeverity().getInfo(effectiveSeverity) + "'" + "."); reportPlugin.beginConcept(concept); Result<Concept> result = execute(concept, effectiveSeverity); reportPlugin.setResult(result); status = result.getStatus(); if (conceptDescriptor == null) { conceptDescriptor = analyzerContext.getStore().create(ConceptDescriptor.class); conceptDescriptor.setId(concept.getId()); conceptDescriptor.setStatus(status); } reportPlugin.endConcept(); } else { status = conceptDescriptor.getStatus(); } return Result.Status.SUCCESS.equals(status); } AnalyzerRuleVisitor(AnalyzerConfiguration configuration, AnalyzerContext analyzerContext, Map<String, String> ruleParameters,
            Map<String, Collection<RuleInterpreterPlugin>> ruleInterpreterPlugins, ReportPlugin reportPlugin); @Override void beforeRules(); @Override void afterRules(); @Override boolean visitConcept(Concept concept, Severity effectiveSeverity); @Override void skipConcept(Concept concept, Severity effectiveSeverity); @Override void visitConstraint(Constraint constraint, Severity effectiveSeverity); @Override void skipConstraint(Constraint constraint, Severity effectiveSeverity); @Override void beforeGroup(Group group, Severity effectiveSeverity); @Override void afterGroup(Group group);  }
@Test public void columnOrder() throws RuleException { analyzerRuleVisitor.visitConcept(concept, Severity.MINOR); ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class); verify(reportWriter).setResult(resultCaptor.capture()); Result capturedResult = resultCaptor.getValue(); assertThat("The reported column names must match the given column names.", capturedResult.getColumnNames(), equalTo(columnNames)); List<Map<String, Object>> capturedRows = capturedResult.getRows(); assertThat("Expecting one row.", capturedRows.size(), equalTo(1)); Map<String, Object> capturedRow = capturedRows.get(0); assertThat("The reported column names must match the given column names.", new ArrayList<>(capturedRow.keySet()), equalTo(columnNames)); }
@Test public void executeConcept() throws RuleException { doReturn(Result.Status.SUCCESS).when(analyzerContext).verify(eq(concept), anyList(), anyList()); boolean visitConcept = analyzerRuleVisitor.visitConcept(concept, Severity.MAJOR); assertThat(visitConcept, equalTo(true)); ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class); verify(store).executeQuery(eq(statement), argumentCaptor.capture()); Map<String, Object> parameters = argumentCaptor.getValue(); assertThat(parameters.get(PARAMETER_WITHOUT_DEFAULT), equalTo("value")); assertThat(parameters.get(PARAMETER_WITH_DEFAULT), equalTo("defaultValue")); verify(reportWriter).beginConcept(concept); ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class); verify(reportWriter).setResult(resultCaptor.capture()); Result result = resultCaptor.getValue(); assertThat(result.getStatus(), equalTo(Result.Status.SUCCESS)); assertThat(result.getSeverity(), equalTo(Severity.MAJOR)); verify(reportWriter).endConcept(); verify(store).create(ConceptDescriptor.class); }
@Test public void skipAppliedConcept() throws RuleException { when(store.find(ConceptDescriptor.class, concept.getId())).thenReturn(mock(ConceptDescriptor.class)); analyzerRuleVisitor.visitConcept(concept, Severity.MINOR); verify(reportWriter, never()).beginConcept(concept); verify(reportWriter, never()).endConcept(); verify(store, never()).create(ConceptDescriptor.class); verify(store, never()).executeQuery(eq(statement), anyMap()); }
@Test public void executeAppliedConcept() throws RuleException { when(store.find(ConceptDescriptor.class, concept.getId())).thenReturn(mock(ConceptDescriptor.class)); when(configuration.isExecuteAppliedConcepts()).thenReturn(true); analyzerRuleVisitor.visitConcept(concept, Severity.MINOR); verify(reportWriter).beginConcept(concept); verify(store, never()).create(ConceptDescriptor.class); }
@Test public void missingParameter() { String statement = "match (n) return n"; Concept concept = createConcept(statement); ReportPlugin reportWriter = mock(ReportPlugin.class); try { AnalyzerRuleVisitor analyzerRuleVisitor = new AnalyzerRuleVisitor(configuration, analyzerContext, Collections.emptyMap(), ruleInterpreterPlugins, reportWriter); analyzerRuleVisitor.visitConcept(concept, Severity.MINOR); fail("Expecting an " + RuleException.class.getName()); } catch (RuleException e) { String message = e.getMessage(); assertThat(message, containsString(concept.getId())); assertThat(message, containsString(PARAMETER_WITHOUT_DEFAULT)); } }
@Test public void ruleSourceInErrorMessage() { String statement = "match (n) return n"; Concept concept = createConcept(statement); when(store.executeQuery(eq(statement), anyMap())).thenThrow(new IllegalStateException("An error")); ReportPlugin reportWriter = mock(ReportPlugin.class); try { AnalyzerRuleVisitor analyzerRuleVisitor = new AnalyzerRuleVisitor(configuration, analyzerContext, ruleParameters, ruleInterpreterPlugins, reportWriter); analyzerRuleVisitor.visitConcept(concept, Severity.MINOR); fail("Expecting a " + RuleException.class.getName()); } catch (RuleException e) { String message = e.getMessage(); assertThat(message, containsString(RULESOURCE)); } }