Search in sources :

Example 6 with Result

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

the class AbstractCypherRuleInterpreterPlugin method execute.

protected <T extends ExecutableRule<?>> Result<T> execute(String cypher, T executableRule, Map<String, Object> parameters, Severity severity, AnalyzerContext context) throws RuleException {
    List<Map<String, Object>> rows = new LinkedList<>();
    context.getLogger().debug("Executing query '" + cypher + "' with parameters [" + parameters + "]");
    String primaryColumn = null;
    List<String> columnNames = null;
    try (Query.Result<Query.Result.CompositeRowObject> compositeRowObjects = context.getStore().executeQuery(cypher, parameters)) {
        for (Query.Result.CompositeRowObject rowObject : compositeRowObjects) {
            if (columnNames == null) {
                columnNames = unmodifiableList(rowObject.getColumns());
                primaryColumn = executableRule.getReport().getPrimaryColumn();
                if (primaryColumn == null) {
                    primaryColumn = columnNames.get(0);
                }
            }
            Map<String, Object> row = new LinkedHashMap<>();
            for (String columnName : columnNames) {
                row.put(columnName, rowObject.get(columnName, Object.class));
            }
            if (!isSuppressedRow(executableRule.getId(), row, primaryColumn)) {
                rows.add(row);
            }
        }
    } catch (Exception e) {
        throw new RuleException("Cannot execute query for rule '" + executableRule + "'.", e);
    }
    Status status = getStatus(executableRule, columnNames, rows, context);
    return Result.<T>builder().rule(executableRule).status(status).severity(severity).columnNames(columnNames).rows(rows).build();
}
Also used : Status(com.buschmais.jqassistant.core.report.api.model.Result.Status) Query(com.buschmais.xo.api.Query) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) LinkedList(java.util.LinkedList) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Result(com.buschmais.jqassistant.core.report.api.model.Result) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with Result

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

the class AggregationVerificationStrategy method verify.

@Override
public <T extends ExecutableRule> Result.Status verify(T executable, AggregationVerification verification, List<String> columnNames, List<Map<String, Object>> rows) throws RuleException {
    LOGGER.debug("Verifying result of " + executable);
    if (rows.isEmpty()) {
        return getStatus(executable, 0, verification.getMin(), verification.getMax());
    }
    if (columnNames.isEmpty()) {
        throw new RuleException("Result contains no columns, at least one with a numeric value is expected.");
    }
    String column = verification.getColumn();
    if (column == null) {
        column = columnNames.get(0);
        LOGGER.debug("No aggregation column specified, using " + column);
    }
    for (Map<String, Object> row : rows) {
        Object value = row.get(column);
        if (value == null) {
            throw new RuleException("The result does not contain a column '" + column);
        } else if (!Number.class.isAssignableFrom(value.getClass())) {
            throw new RuleException("The value in column '" + column + "' must be a numeric value but was '" + value + "'");
        }
        int aggregationValue = ((Number) value).intValue();
        Result.Status status = getStatus(executable, aggregationValue, verification.getMin(), verification.getMax());
        if (Result.Status.FAILURE.equals(status)) {
            return Result.Status.FAILURE;
        }
    }
    return Result.Status.SUCCESS;
}
Also used : RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Result(com.buschmais.jqassistant.core.report.api.model.Result)

Example 8 with Result

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

the class AnalyzerRuleVisitorTest method executeConcept.

@Test
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);
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyMap(org.mockito.Matchers.anyMap) Result(com.buschmais.jqassistant.core.report.api.model.Result) Test(org.junit.jupiter.api.Test)

Example 9 with Result

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

the class AnalyzerRuleVisitorTest method skipConstraint.

@Test
void skipConstraint() throws RuleException {
    analyzerRuleVisitor.skipConstraint(constraint, Severity.BLOCKER);
    verify(store, never()).executeQuery(eq(statement), anyMap());
    verify(reportWriter).beginConstraint(constraint);
    ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class);
    verify(reportWriter).setResult(resultCaptor.capture());
    Result result = resultCaptor.getValue();
    assertThat(result.getStatus(), equalTo(Result.Status.SKIPPED));
    assertThat(result.getSeverity(), equalTo(Severity.BLOCKER));
    verify(reportWriter).endConstraint();
}
Also used : Result(com.buschmais.jqassistant.core.report.api.model.Result) Test(org.junit.jupiter.api.Test)

Example 10 with Result

use of com.buschmais.jqassistant.core.report.api.model.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
void packageCycles() throws Exception {
    scanClassPathDirectory(getClassesDirectory(A.class));
    assertThat(validateConstraint("dependency:PackageCycles").getStatus(), equalTo(FAILURE));
    store.beginTransaction();
    Map<String, Result<Constraint>> constraintViolations = reportPlugin.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.rule.api.model.Constraint) PackageDescriptor(com.buschmais.jqassistant.plugin.java.api.model.PackageDescriptor) Result(com.buschmais.jqassistant.core.report.api.model.Result) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

Result (com.buschmais.jqassistant.core.report.api.model.Result)16 Test (org.junit.jupiter.api.Test)12 Constraint (com.buschmais.jqassistant.core.rule.api.model.Constraint)7 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 Concept (com.buschmais.jqassistant.core.rule.api.model.Concept)4 RuleException (com.buschmais.jqassistant.core.rule.api.model.RuleException)3 Map (java.util.Map)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Matchers.anyMap (org.mockito.Matchers.anyMap)3 CompositeObject (com.buschmais.xo.api.CompositeObject)2 ConceptDescriptor (com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor)1 Status (com.buschmais.jqassistant.core.report.api.model.Result.Status)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ScriptEngine (javax.script.ScriptEngine)1