From 5907f2dcf56d1763a222c01e408912f99f75be13 Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Fri, 12 Jul 2024 17:26:36 +0200 Subject: [PATCH] #408 fixed failing unit test --- .../impl/AnalyzerContextImplTest.java | 84 +++++++++++++++++++ .../impl/CypherRuleInterpreterPluginTest.java | 65 ++------------ 2 files changed, 91 insertions(+), 58 deletions(-) diff --git a/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/AnalyzerContextImplTest.java b/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/AnalyzerContextImplTest.java index 8753c254a9..b47388cddc 100644 --- a/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/AnalyzerContextImplTest.java +++ b/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/AnalyzerContextImplTest.java @@ -1,5 +1,7 @@ package com.buschmais.jqassistant.core.analysis.impl; +import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Stream; @@ -9,7 +11,9 @@ import com.buschmais.jqassistant.core.report.api.configuration.Report; import com.buschmais.jqassistant.core.report.api.model.Column; import com.buschmais.jqassistant.core.report.api.model.Row; +import com.buschmais.jqassistant.core.report.api.model.Suppress; 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.RuleException; import com.buschmais.jqassistant.core.rule.api.model.Severity; import com.buschmais.jqassistant.core.shared.map.MapBuilder; @@ -21,6 +25,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static java.util.Optional.empty; +import static java.util.Optional.of; import static java.util.stream.Collectors.toSet; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doReturn; @@ -28,6 +34,10 @@ @ExtendWith(MockitoExtension.class) class AnalyzerContextImplTest { + private static final String CONSTRAINT_ID = "constraint"; + public static final String PRIMARY_COLUMN = "primary"; + public static final String SECONDARY_COLUMN = "secondary"; + private AnalyzerContext analyzerContext; @Mock @@ -82,4 +92,78 @@ void createUniqueRowKeys() { assertThat(rowKeys).hasSize(3); } + @Test + void withoutSuppression() { + Constraint constraint = getConstraint(); + Row row = analyzerContext.toRow(constraint, + Map.of(PRIMARY_COLUMN, analyzerContext.toColumn("value1_1"), SECONDARY_COLUMN, analyzerContext.toColumn("value1_2"))); + + assertThat(analyzerContext.isSuppressed(constraint, PRIMARY_COLUMN, row)).isFalse(); + } + + @Test + void suppressByPrimaryColumn() { + Suppress suppressedValue = createSuppressedValue(empty(), CONSTRAINT_ID); + Constraint constraint = getConstraint(); + + Row row = analyzerContext.toRow(constraint, + Map.of(PRIMARY_COLUMN, analyzerContext.toColumn(suppressedValue), SECONDARY_COLUMN, analyzerContext.toColumn("value"))); + + assertThat(analyzerContext.isSuppressed(constraint, PRIMARY_COLUMN, row)).isTrue(); + } + + @Test + void suppressByNonPrimaryColumn() throws RuleException { + Suppress suppressedValue = createSuppressedValue(of(SECONDARY_COLUMN), CONSTRAINT_ID); + Constraint constraint = getConstraint(); + + Row row = analyzerContext.toRow(constraint, + Map.of(PRIMARY_COLUMN, analyzerContext.toColumn("value"), SECONDARY_COLUMN, analyzerContext.toColumn(suppressedValue))); + + assertThat(analyzerContext.isSuppressed(constraint, PRIMARY_COLUMN, row)).isTrue(); + } + + @Test + void nonMatchingSuppressId() { + Suppress suppressedValue = createSuppressedValue(empty(), "otherConstraint"); + Constraint constraint = getConstraint(); + Row row = analyzerContext.toRow(constraint, + Map.of(PRIMARY_COLUMN, analyzerContext.toColumn(suppressedValue), SECONDARY_COLUMN, analyzerContext.toColumn("value"))); + + assertThat(analyzerContext.isSuppressed(constraint, PRIMARY_COLUMN, row)).isFalse(); + } + + private Constraint getConstraint() { + com.buschmais.jqassistant.core.rule.api.model.Report report = com.buschmais.jqassistant.core.rule.api.model.Report.builder() + .primaryColumn(PRIMARY_COLUMN) + .build(); + Constraint constraint = Constraint.builder() + .id(CONSTRAINT_ID) + .report(report) + .build(); + return constraint; + } + + private static Suppress createSuppressedValue(Optional suppressColumn, String... suppressIds) { + Suppress suppress = new Suppress() { + @Override + public String[] getSuppressIds() { + return suppressIds; + } + + @Override + public void setSuppressIds(String[] suppressIds) { + } + + @Override + public String getSuppressColumn() { + return suppressColumn.orElse(null); + } + + @Override + public void setSuppressColumn(String suppressColumn) { + } + }; + return suppress; + } } diff --git a/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/CypherRuleInterpreterPluginTest.java b/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/CypherRuleInterpreterPluginTest.java index 6b5ffb67b9..9913abf757 100644 --- a/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/CypherRuleInterpreterPluginTest.java +++ b/core/analysis/src/test/java/com/buschmais/jqassistant/core/analysis/impl/CypherRuleInterpreterPluginTest.java @@ -1,11 +1,14 @@ package com.buschmais.jqassistant.core.analysis.impl; -import java.util.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import com.buschmais.jqassistant.core.analysis.api.AnalyzerContext; import com.buschmais.jqassistant.core.report.api.ReportHelper; import com.buschmais.jqassistant.core.report.api.model.Result; -import com.buschmais.jqassistant.core.report.api.model.Suppress; +import com.buschmais.jqassistant.core.report.api.model.Row; import com.buschmais.jqassistant.core.rule.api.model.Constraint; import com.buschmais.jqassistant.core.rule.api.model.Report; import com.buschmais.jqassistant.core.rule.api.model.RuleException; @@ -24,8 +27,6 @@ import static com.buschmais.jqassistant.core.rule.api.model.Severity.MAJOR; import static java.util.Arrays.stream; import static java.util.Collections.emptyMap; -import static java.util.Optional.empty; -import static java.util.Optional.of; import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.*; @@ -59,43 +60,14 @@ void beforeEach() { } @Test - void withoutSuppression() throws RuleException { + void rows() throws RuleException { Constraint constraint = prepareConstraint(Map.of(PRIMARY_COLUMN, "value1_1", SECONDARY_COLUMN, "value1_2"), Map.of(PRIMARY_COLUMN, "value2_1", SECONDARY_COLUMN, "value2_2")); Result result = interpreterPlugin.execute("MATCH n RETURN n", constraint, emptyMap(), MAJOR, analyzerContext); assertThat(result.getRows()).hasSize(2); - } - - @Test - void suppressByPrimaryColumn() throws RuleException { - Suppress suppressedValue = createSuppressedValue(empty(), CONSTRAINT_ID); - Constraint constraint = prepareConstraint(Map.of(PRIMARY_COLUMN, suppressedValue, SECONDARY_COLUMN, "value")); - - Result result = interpreterPlugin.execute("MATCH n RETURN n", constraint, emptyMap(), MAJOR, analyzerContext); - - assertThat(result.getRows()).isEmpty(); - } - - @Test - void suppressByNonPrimaryColumn() throws RuleException { - Suppress suppressedValue = createSuppressedValue(of(SECONDARY_COLUMN), CONSTRAINT_ID); - Constraint constraint = prepareConstraint(Map.of(PRIMARY_COLUMN, "value", SECONDARY_COLUMN, suppressedValue)); - - Result result = interpreterPlugin.execute("MATCH n RETURN n", constraint, emptyMap(), MAJOR, analyzerContext); - - assertThat(result.getRows()).isEmpty(); - } - - @Test - void nonMatchingSuppressId() throws RuleException { - Suppress suppressedValue = createSuppressedValue(empty(), "otherConstraint"); - Constraint constraint = prepareConstraint(Map.of(PRIMARY_COLUMN, suppressedValue, SECONDARY_COLUMN, "value")); - - Result result = interpreterPlugin.execute("MATCH n RETURN n", constraint, emptyMap(), MAJOR, analyzerContext); - - assertThat(result.getRows()).hasSize(1); + verify(analyzerContext, times(2)).isSuppressed(eq(constraint), eq(PRIMARY_COLUMN), any(Row.class)); } private Constraint prepareConstraint(Map... resultRows) { @@ -117,29 +89,6 @@ private Constraint prepareConstraint(Map... resultRows) { return constraint; } - private static Suppress createSuppressedValue(Optional suppressColumn, String... suppressIds) { - Suppress suppress = new Suppress() { - @Override - public String[] getSuppressIds() { - return suppressIds; - } - - @Override - public void setSuppressIds(String[] suppressIds) { - } - - @Override - public String getSuppressColumn() { - return suppressColumn.orElse(null); - } - - @Override - public void setSuppressColumn(String suppressColumn) { - } - }; - return suppress; - } - private static ResultIterator asResultIterator(List queryRows) { Iterator iterator = queryRows.iterator(); ResultIterator resultIterator = new ResultIterator<>() {