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();
}
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;
}
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);
}
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();
}
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();
}
Aggregations