Search in sources :

Example 16 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method executeConcept.

@Test
public void executeConcept() throws RuleException {
    boolean visitConcept = analyzerVisitor.visitConcept(concept, Severity.MAJOR);
    assertThat(visitConcept, equalTo(true));
    ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);
    verify(store).executeQuery(Mockito.eq(statement), argumentCaptor.capture());
    Map<String, Object> parameters = argumentCaptor.getValue();
    assertThat(parameters.get(PARAMETER_WITHOUT_DEFAULT), CoreMatchers.<Object>equalTo("value"));
    assertThat(parameters.get(PARAMETER_WITH_DEFAULT), CoreMatchers.<Object>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);
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyMap(org.mockito.Matchers.anyMap) Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 17 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method executeCypher.

/**
     * Execute the cypher query of a rule.
     * 
     * @param executableRule
     *            The executable.
     * @param executable
     *            The executable.
     * @param parameters
     *            The parameters.
     * @param severity
     *            The severity.
     * @param <T>
     *            The rule type.
     * @return The result.
     * @throws RuleExecutorException
     *             If execution fails.
     */
private <T extends ExecutableRule> Result<T> executeCypher(T executableRule, CypherExecutable executable, Map<String, Object> parameters, Severity severity) throws RuleExecutorException {
    String cypher = executable.getStatement();
    List<Map<String, Object>> rows = new ArrayList<>();
    try (Query.Result<Query.Result.CompositeRowObject> compositeRowObjects = executeQuery(cypher, parameters)) {
        List<String> columnNames = null;
        for (Query.Result.CompositeRowObject rowObject : compositeRowObjects) {
            if (columnNames == null) {
                columnNames = new ArrayList<>(rowObject.getColumns());
            }
            Map<String, Object> row = new LinkedHashMap<>();
            for (String columnName : columnNames) {
                row.put(columnName, rowObject.get(columnName, Object.class));
            }
            rows.add(row);
        }
        Result.Status status = verify(executableRule, columnNames, rows);
        return new Result<>(executableRule, status, severity, columnNames, rows);
    } catch (Exception e) {
        throw new RuleExecutorException("Cannot execute query for rule '" + executableRule + "'.", e);
    }
}
Also used : Query(com.buschmais.xo.api.Query) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) XOException(com.buschmais.xo.api.XOException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptException(javax.script.ScriptException) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 18 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method columnOrder.

/**
     * Verifies that columns of a query a reported in the order given by the
     * query.
     *
     * @throws RuleException
     *             If the test fails.
     */
@Test
public void columnOrder() throws RuleException {
    analyzerVisitor.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(), CoreMatchers.<List>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));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyMap(org.mockito.Matchers.anyMap) Result(com.buschmais.jqassistant.core.analysis.api.Result) Test(org.junit.Test)

Example 19 with Result

use of com.buschmais.jqassistant.core.analysis.api.Result in project jqa-java-plugin by buschmais.

the class DependencyIT method packageCycles.

/**
     * Verifies the constraint "dependency:PackageCycles".
     *
     * @throws IOException
     *             If the test fails.
     */
@Test
public void packageCycles() throws Exception {
    scanClassPathDirectory(getClassesDirectory(A.class));
    assertThat(validateConstraint("dependency:PackageCycles").getStatus(), equalTo(FAILURE));
    store.beginTransaction();
    Map<String, Result<Constraint>> constraintViolations = reportWriter.getConstraintResults();
    Matcher<Iterable<? super Result<Constraint>>> matcher = hasItem(result(constraint("dependency:PackageCycles")));
    assertThat(constraintViolations.values(), matcher);
    Result<Constraint> result = constraintViolations.get("dependency:PackageCycles");
    List<Map<String, Object>> rows = result.getRows();
    assertThat(rows.size(), equalTo(2));
    for (Map<String, Object> row : rows) {
        PackageDescriptor p = (PackageDescriptor) row.get("Package");
        assertThat(p.getFullQualifiedName(), anyOf(equalTo(A.class.getPackage().getName()), equalTo(B.class.getPackage().getName())));
    }
    store.commitTransaction();
}
Also used : A(com.buschmais.jqassistant.plugin.java.test.set.rules.dependency.packages.a.A) Constraint(com.buschmais.jqassistant.core.analysis.api.rule.Constraint) PackageDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PackageDescriptor) Result(com.buschmais.jqassistant.core.analysis.api.Result) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Result (com.buschmais.jqassistant.core.analysis.api.Result)19 Test (org.junit.Test)12 Constraint (com.buschmais.jqassistant.core.analysis.api.rule.Constraint)7 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)4 Concept (com.buschmais.jqassistant.core.analysis.api.rule.Concept)3 XmlReportWriter (com.buschmais.jqassistant.core.report.impl.XmlReportWriter)3 StringWriter (java.io.StringWriter)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Matchers.anyMap (org.mockito.Matchers.anyMap)3 XOException (com.buschmais.xo.api.XOException)2 ScriptException (javax.script.ScriptException)2 ConceptDescriptor (com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor)1 PackageDescriptor (com.buschmais.jqassistant.plugin.java.api.model.PackageDescriptor)1 A (com.buschmais.jqassistant.plugin.java.test.set.rules.dependency.packages.a.A)1 Query (com.buschmais.xo.api.Query)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1