From 6b998a7a4489453089b4d504e7e889376f4222f2 Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Mon, 5 Aug 2024 18:56:29 +0200 Subject: [PATCH 1/3] #568 sort rule results in ReportHelper before printing to console --- .../configuration/ConfigurationSerializerTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java index 9d4847201e..3103a2344e 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java @@ -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 @@ -61,8 +61,7 @@ void serializeAndRestoreConfiguration() { 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("overwritten-user-value", "overwritten"); } private String toYaml(TestConfiguration configuration) { From fb106e7d355eacb34f6a79ce7b889c6809293970 Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Tue, 6 Aug 2024 13:44:48 +0200 Subject: [PATCH 2/3] #568 fixed Sonar violation --- .../core/report/api/ReportHelper.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java b/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java index 72e219aa4f..fc658b35fa 100644 --- a/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java +++ b/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java @@ -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; @@ -13,11 +9,7 @@ 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; @@ -67,10 +59,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; } /** @@ -84,7 +76,7 @@ public ReportHelper(Report configuration, Logger log) { public static String escapeRuleId(Rule rule) { return rule != null ? rule.getId() - .replaceAll("\\:", "_") : + .replace(":", "_") : null; } @@ -300,7 +292,8 @@ private List getResultRows(Result result, boolean logResult) { if (logResult) { for (Row row : result.getRows()) { StringBuilder value = new StringBuilder(); - for (Map.Entry> entry : row.getColumns().entrySet()) { + for (Map.Entry> entry : row.getColumns() + .entrySet()) { if (value.length() > 0) { value.append(", "); } From ce4a335889e23067be003a3339714cca899d81c9 Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Tue, 6 Aug 2024 16:57:02 +0200 Subject: [PATCH 3/3] #568 fixed Sonar violation --- .../core/report/api/ReportHelper.java | 20 ++++++++-- .../core/report/api/ReportHelperTest.java | 39 ++++++++++++++----- .../ConfigurationSerializerTest.java | 3 +- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java b/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java index fc658b35fa..5c99016bb7 100644 --- a/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java +++ b/core/report/src/main/java/com/buschmais/jqassistant/core/report/api/ReportHelper.java @@ -16,7 +16,6 @@ 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; @@ -26,8 +25,6 @@ */ public final class ReportHelper { - private static final Logger log = LoggerFactory.getLogger(ReportHelper.class); - public interface FailAction { void fail(String message) throws E; @@ -231,7 +228,22 @@ int verifyConstraintResults(InMemoryReportPlugin inMemoryReportWriter) { */ private int verifyRuleResults(Collection> results, String type, String header, boolean logResult) { int failures = 0; - for (Result result : results) { + List> sortedResult = results.stream() + .sorted((Comparator>) (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(); diff --git a/core/report/src/test/java/com/buschmais/jqassistant/core/report/api/ReportHelperTest.java b/core/report/src/test/java/com/buschmais/jqassistant/core/report/api/ReportHelperTest.java index c3723856c8..6476fd6327 100644 --- a/core/report/src/test/java/com/buschmais/jqassistant/core/report/api/ReportHelperTest.java +++ b/core/report/src/test/java/com/buschmais/jqassistant/core/report/api/ReportHelperTest.java @@ -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; @@ -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 @@ -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 @@ -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 @@ -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 @@ -228,7 +247,7 @@ void failedConstraintsWithOverriddenSeverity() { } @Test - void continueOnFailureEnabled() throws ReportException { + void continueOnFailureEnabled() { doReturn(Severity.MAJOR.name()).when(report) .failOnSeverity(); doReturn(true).when(report) @@ -381,7 +400,7 @@ private LogAnswer(List 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; diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java index 3103a2344e..2f8b54fc72 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationSerializerTest.java @@ -59,8 +59,7 @@ 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()).containsEntry("user-value", "default"); assertThat(scan.properties()).containsEntry("overwritten-user-value", "overwritten"); }