Skip to content

Commit

Permalink
Merge ce4a335 into a770169
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler authored Aug 6, 2024
2 parents a770169 + ce4a335 commit 2d7c41b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.buschmais.jqassistant.core.report.api;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.*;
import java.util.stream.StreamSupport;

import com.buschmais.jqassistant.core.report.api.configuration.Report;
Expand All @@ -13,18 +9,13 @@
import com.buschmais.jqassistant.core.report.api.model.Result;
import com.buschmais.jqassistant.core.report.api.model.Row;
import com.buschmais.jqassistant.core.report.impl.InMemoryReportPlugin;
import com.buschmais.jqassistant.core.rule.api.model.Concept;
import com.buschmais.jqassistant.core.rule.api.model.Constraint;
import com.buschmais.jqassistant.core.rule.api.model.ExecutableRule;
import com.buschmais.jqassistant.core.rule.api.model.Rule;
import com.buschmais.jqassistant.core.rule.api.model.Severity;
import com.buschmais.jqassistant.core.rule.api.model.*;
import com.buschmais.xo.api.CompositeObject;
import com.buschmais.xo.neo4j.api.model.Neo4jPropertyContainer;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
Expand All @@ -34,8 +25,6 @@
*/
public final class ReportHelper {

private static final Logger log = LoggerFactory.getLogger(ReportHelper.class);

public interface FailAction<E extends Exception> {

void fail(String message) throws E;
Expand Down Expand Up @@ -67,10 +56,10 @@ private interface LoggingStrategy {
*/
public ReportHelper(Report configuration, Logger log) {
this.configuration = configuration;
this.infoLogger = message -> log.info(message);
this.errorLogger = message -> log.error(message);
this.warnLogger = message -> log.warn(message);
this.debugLogger = message -> log.debug(message);
this.infoLogger = log::info;
this.errorLogger = log::error;
this.warnLogger = log::warn;
this.debugLogger = log::debug;
}

/**
Expand All @@ -84,7 +73,7 @@ public ReportHelper(Report configuration, Logger log) {
public static String escapeRuleId(Rule rule) {
return rule != null ?
rule.getId()
.replaceAll("\\:", "_") :
.replace(":", "_") :
null;
}

Expand Down Expand Up @@ -239,7 +228,22 @@ int verifyConstraintResults(InMemoryReportPlugin inMemoryReportWriter) {
*/
private int verifyRuleResults(Collection<? extends Result<? extends ExecutableRule>> results, String type, String header, boolean logResult) {
int failures = 0;
for (Result<?> result : results) {
List<? extends Result<? extends ExecutableRule>> sortedResult = results.stream()
.sorted((Comparator<Result<? extends ExecutableRule>>) (r1, r2) -> {
Integer l1 = r1.getSeverity()
.getLevel();
Integer l2 = r2.getSeverity()
.getLevel();
if (!l1.equals(l2)) {
return l2.compareTo(l1);
}
return r1.getRule()
.getId()
.compareTo(r2.getRule()
.getId());
})
.collect(toList());
for (Result<?> result : sortedResult) {
Result.Status status = result.getStatus();
ExecutableRule<?> rule = result.getRule();
Severity resultSeverity = result.getSeverity();
Expand Down Expand Up @@ -300,7 +304,8 @@ private List<String> getResultRows(Result<?> result, boolean logResult) {
if (logResult) {
for (Row row : result.getRows()) {
StringBuilder value = new StringBuilder();
for (Map.Entry<String, Column<?>> entry : row.getColumns().entrySet()) {
for (Map.Entry<String, Column<?>> entry : row.getColumns()
.entrySet()) {
if (value.length() > 0) {
value.append(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
Expand Down Expand Up @@ -89,10 +90,10 @@ void successfulConcept() {

int violations = reportHelper.verifyConceptResults(inMemoryReportWriter);

assertThat(violations).isEqualTo(0);
assertThat(debugMessages.size()).isGreaterThan(0);
assertThat(warnMessages.size()).isEqualTo(0);
assertThat(errorMessages.size()).isEqualTo(0);
assertThat(violations).isZero();
assertThat(debugMessages).isNotEmpty();
assertThat(warnMessages).isEmpty();
assertThat(errorMessages).isEmpty();
}

@Test
Expand All @@ -112,6 +113,15 @@ void failedConcepts() {
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");

int expectedLoggedRowsPerRule = 10;
InOrder inOrder = inOrder(logger);
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.debug(anyString());
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.warn(anyString());
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.error(anyString());
}

@Test
Expand Down Expand Up @@ -148,10 +158,10 @@ void validatedConstraint() {

int violations = reportHelper.verifyConstraintResults(inMemoryReportWriter);

assertThat(violations).isEqualTo(0);
assertThat(debugMessages.size()).isGreaterThan(0);
assertThat(warnMessages.size()).isEqualTo(0);
assertThat(errorMessages.size()).isEqualTo(0);
assertThat(violations).isZero();
assertThat(debugMessages).isNotEmpty();
assertThat(warnMessages).isEmpty();
assertThat(errorMessages).isEmpty();
}

@Test
Expand Down Expand Up @@ -188,6 +198,15 @@ void failedConstraints() {
"MinorElement=MinorValue");
verifyMessages(errorMessages, ReportHelper.CONSTRAINT_VIOLATION_HEADER, "Constraint: test:majorConstraint", "Severity: MAJOR",
"MajorElement=MajorValue");

int expectedLoggedRowsPerRule = 12;
InOrder inOrder = inOrder(logger);
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.debug(anyString());
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.warn(anyString());
inOrder.verify(logger, times(expectedLoggedRowsPerRule))
.error(anyString());
}

@Test
Expand Down Expand Up @@ -228,7 +247,7 @@ void failedConstraintsWithOverriddenSeverity() {
}

@Test
void continueOnFailureEnabled() throws ReportException {
void continueOnFailureEnabled() {
doReturn(Severity.MAJOR.name()).when(report)
.failOnSeverity();
doReturn(true).when(report)
Expand Down Expand Up @@ -381,7 +400,7 @@ private LogAnswer(List<String> messages) {
}

@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
public Object answer(InvocationOnMock invocation) {
String message = (String) invocation.getArguments()[0];
messages.add(message);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ void types() {

String yaml = toYaml(configuration);

assertThat(yaml).contains("group-id: org.jqassistant.plugin");
assertThat(yaml).contains("version: 1.0.0");
assertThat(yaml).contains("uri: bolt://localhost:7687");
assertThat(yaml).contains("directory: target/rules");
assertThat(yaml).contains("fail-on-severity: BLOCKER");
assertThat(yaml).contains("group-id: org.jqassistant.plugin")
.contains("version: 1.0.0")
.contains("uri: bolt://localhost:7687")
.contains("directory: target/rules")
.contains("fail-on-severity: BLOCKER");
}

@Test
Expand All @@ -59,10 +59,8 @@ void serializeAndRestoreConfiguration() {
TestConfiguration restoredConfiguration = ConfigurationMappingLoader.builder(TestConfiguration.class)
.load(yamlConfigSource);
Scan scan = restoredConfiguration.scan();
assertThat(scan.properties()
.get("user-value")).isEqualTo("default");
assertThat(scan.properties()
.get("overwritten-user-value")).isEqualTo("overwritten");
assertThat(scan.properties()).containsEntry("user-value", "default");
assertThat(scan.properties()).containsEntry("overwritten-user-value", "overwritten");
}

private String toYaml(TestConfiguration configuration) {
Expand Down

0 comments on commit 2d7c41b

Please sign in to comment.