Search in sources :

Example 11 with Result

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

the class ReportHelperTest method failedConstraintsWithOverriddenSeverity.

@Test
void failedConstraintsWithOverriddenSeverity() {
    Map<String, Object> infoRow = new HashMap<>();
    infoRow.put("InfoElement", "InfoValue");
    Result<Constraint> infoConstraintResult = mockResult("test:infoConstraint", Constraint.class, Result.Status.FAILURE, Severity.INFO, Severity.MINOR, singletonList(infoRow));
    Map<String, Object> minorRow = new HashMap<>();
    minorRow.put("MinorElement", "MinorValue");
    Result<Constraint> minorConstraintResult = mockResult("test:minorConstraint", Constraint.class, Result.Status.FAILURE, Severity.MINOR, Severity.MAJOR, singletonList(minorRow));
    Map<String, Object> majorRow = new HashMap<>();
    majorRow.put("MajorElement", "MajorValue");
    Result<Constraint> majorConstraintResult = mockResult("test:majorConstraint", Constraint.class, Result.Status.FAILURE, Severity.MAJOR, Severity.CRITICAL, singletonList(majorRow));
    Map<String, Result<Constraint>> constraintResults = new HashMap<>();
    constraintResults.put("test:infoConstraint", infoConstraintResult);
    constraintResults.put("test:minorConstraint", minorConstraintResult);
    constraintResults.put("test:majorConstraint", majorConstraintResult);
    when(inMemoryReportWriter.getConstraintResults()).thenReturn(constraintResults);
    int violations = reportHelper.verifyConstraintResults(Severity.MAJOR, Severity.CRITICAL, inMemoryReportWriter);
    assertThat(violations, equalTo(1));
    verifyMessages(debugMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:infoConstraint", "Severity: MINOR (from INFO)", "InfoElement=InfoValue");
    verifyMessages(warnMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:minorConstraint", "Severity: MAJOR (from MINOR)", "MinorElement=MinorValue");
    verifyMessages(errorMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:majorConstraint", "Severity: CRITICAL (from MAJOR)", "MajorElement=MajorValue");
}
Also used : Constraint(com.buschmais.jqassistant.core.rule.api.model.Constraint) CompositeObject(com.buschmais.xo.api.CompositeObject) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Constraint(com.buschmais.jqassistant.core.rule.api.model.Constraint) Result(com.buschmais.jqassistant.core.report.api.model.Result) Test(org.junit.jupiter.api.Test)

Example 12 with Result

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

the class ReportHelperTest method failedConcepts.

@Test
void failedConcepts() {
    Result<Concept> infoConceptResult = mockResult("test:infoConcept", Concept.class, Result.Status.FAILURE, Severity.INFO);
    Result<Concept> minorConceptResult = mockResult("test:minorConcept", Concept.class, Result.Status.FAILURE, Severity.MINOR);
    Result<Concept> majorConceptResult = mockResult("test:majorConcept", Concept.class, Result.Status.FAILURE, Severity.MAJOR);
    Map<String, Result<Concept>> conceptResults = new HashMap<>();
    conceptResults.put("test:infoConcept", infoConceptResult);
    conceptResults.put("test:minorConcept", minorConceptResult);
    conceptResults.put("test:majorConcept", majorConceptResult);
    when(inMemoryReportWriter.getConceptResults()).thenReturn(conceptResults);
    int violations = reportHelper.verifyConceptResults(Severity.MINOR, Severity.MAJOR, inMemoryReportWriter);
    assertThat(violations, equalTo(1));
    verifyMessages(debugMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:infoConcept", "Severity: INFO");
    verifyMessages(warnMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:minorConcept", "Severity: MINOR");
    verifyMessages(errorMessages, ReportHelper.CONCEPT_FAILED_HEADER, "Concept: test:majorConcept", "Severity: MAJOR");
}
Also used : Concept(com.buschmais.jqassistant.core.rule.api.model.Concept) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Constraint(com.buschmais.jqassistant.core.rule.api.model.Constraint) Result(com.buschmais.jqassistant.core.report.api.model.Result) Test(org.junit.jupiter.api.Test)

Example 13 with Result

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

the class ScriptRuleInterpreterPlugin method execute.

@Override
public <T extends ExecutableRule<?>> Result<T> execute(T executableRule, Map<String, Object> ruleParameters, Severity severity, AnalyzerContext context) throws RuleException {
    Executable<String> executable = executableRule.getExecutable();
    String language = executable.getLanguage();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
    if (scriptEngine == null) {
        List<String> availableLanguages = new ArrayList<>();
        for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
            availableLanguages.addAll(factory.getNames());
        }
        throw new RuleException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
    }
    // Set default variables
    scriptEngine.put(ScriptVariable.STORE.getVariableName(), context.getStore());
    scriptEngine.put(ScriptVariable.CONTEXT.getVariableName(), context);
    scriptEngine.put(ScriptVariable.RULE.getVariableName(), executableRule);
    scriptEngine.put(ScriptVariable.SEVERITY.getVariableName(), severity);
    // Set rule parameters
    for (Map.Entry<String, Object> entry : ruleParameters.entrySet()) {
        scriptEngine.put(entry.getKey(), entry.getValue());
    }
    Object scriptResult;
    try {
        scriptResult = scriptEngine.eval(executable.getSource());
    } catch (ScriptException e) {
        throw new RuleException("Cannot execute script.", e);
    }
    if (!(scriptResult instanceof Result)) {
        throw new RuleException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
    }
    return Result.class.cast(scriptResult);
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ArrayList(java.util.ArrayList) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.report.api.model.Result)

Example 14 with Result

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

the class AnalyzerRuleVisitorTest method executeConstraint.

@Test
void executeConstraint() throws RuleException {
    doReturn(Result.Status.FAILURE).when(analyzerContext).verify(eq(constraint), anyList(), anyList());
    analyzerRuleVisitor.visitConstraint(constraint, Severity.BLOCKER);
    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).beginConstraint(constraint);
    ArgumentCaptor<Result> resultCaptor = ArgumentCaptor.forClass(Result.class);
    verify(reportWriter).setResult(resultCaptor.capture());
    Result result = resultCaptor.getValue();
    assertThat(result.getStatus(), equalTo(Result.Status.FAILURE));
    assertThat(result.getSeverity(), equalTo(Severity.BLOCKER));
    verify(reportWriter).endConstraint();
}
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 15 with Result

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

the class AnalyzerRuleVisitorTest method columnOrder.

/**
 * Verifies that columns of a query a reported in the order given by the query.
 *
 * @throws RuleException
 *             If the test fails.
 */
@Test
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));
}
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)

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