Skip to content

Commit

Permalink
#408 fixed failing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jul 12, 2024
1 parent 85e8729 commit 5907f2d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -21,13 +25,19 @@
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;

@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
Expand Down Expand Up @@ -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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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<Constraint> 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<Constraint> 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<Constraint> 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<Constraint> 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<String, Object>... resultRows) {
Expand All @@ -117,29 +89,6 @@ private Constraint prepareConstraint(Map<String, Object>... resultRows) {
return constraint;
}

private static Suppress createSuppressedValue(Optional<String> 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<CompositeRowObject> asResultIterator(List<CompositeRowObject> queryRows) {
Iterator<CompositeRowObject> iterator = queryRows.iterator();
ResultIterator<CompositeRowObject> resultIterator = new ResultIterator<>() {
Expand Down

0 comments on commit 5907f2d

Please sign in to comment.