Skip to content

Commit

Permalink
#408 added rule filters to BaselineManager
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed Jul 13, 2024
1 parent b6f1e3d commit e1804ac
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.buschmais.jqassistant.core.analysis.api.baseline;

import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
import java.util.function.Function;

import com.buschmais.jqassistant.core.report.api.model.Column;
import com.buschmais.jqassistant.core.report.api.model.Row;
import com.buschmais.jqassistant.core.rule.api.filter.RuleFilter;
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 lombok.RequiredArgsConstructor;

import static java.util.Collections.emptyList;

/**
* Verifies result rows gathered for rule during an analyze task against a baseline
* <p>
Expand All @@ -32,9 +33,9 @@ public class BaselineManager {

private final BaselineRepository baselineRepository;

private Optional<Baseline> optionalOldBaseline = null;
private Optional<Baseline> optionalOldBaseline;

private Baseline newBaseline = null;
private Baseline newBaseline;

public void start() {
if (configuration.enabled()) {
Expand All @@ -53,14 +54,29 @@ public boolean isExisting(ExecutableRule<?> executableRule, Row row) {
if (!configuration.enabled()) {
return false;
}
if (optionalOldBaseline == null) {
if (newBaseline == null) {
throw new IllegalStateException("Baseline manager has not been started yet");
}
if (executableRule instanceof Concept) {
return isExistingResult(executableRule, row, configuration.includeConcepts()
.orElse(emptyList()), Baseline::getConcepts);
} else if (executableRule instanceof Constraint) {
return isExistingResult(executableRule, row, configuration.includeConstraints(), Baseline::getConstraints);
}
throw new IllegalArgumentException("Unsupported executable rule: " + executableRule);
}

private Boolean isExistingResult(ExecutableRule<?> executableRule, Row row, List<String> ruleFilters,
Function<Baseline, SortedMap<String, Baseline.RuleBaseline>> rows) {
String ruleId = executableRule.getId();
if (ruleFilters.stream()
.noneMatch(filter -> RuleFilter.matches(ruleId, filter))) {
return false;
}
String rowKey = row.getKey();
Map<String, Column<?>> columns = row.getColumns();
return optionalOldBaseline.map(oldBaseline -> {
SortedMap<String, Baseline.RuleBaseline> ruleBaseline = getRows(oldBaseline, executableRule);
SortedMap<String, Baseline.RuleBaseline> ruleBaseline = rows.apply(oldBaseline);
Baseline.RuleBaseline oldRuleBaseline = ruleBaseline.get(ruleId);
if (oldRuleBaseline != null && oldRuleBaseline.getRows()
.containsKey(rowKey)) {
Expand All @@ -75,15 +91,6 @@ public boolean isExisting(ExecutableRule<?> executableRule, Row row) {
});
}

private static SortedMap<String, Baseline.RuleBaseline> getRows(Baseline baseline, ExecutableRule<?> executableRule) {
if (executableRule instanceof Concept) {
return baseline.getConcepts();
} else if (executableRule instanceof Constraint) {
return baseline.getConstraints();
}
throw new IllegalArgumentException("Unsupported executable rule: " + executableRule);
}

private void add(String constraintId, String rowKey, Map<String, Column<?>> columns) {
Baseline.RuleBaseline newRuleBaseline = newBaseline.getConstraints()
.computeIfAbsent(constraintId, key -> new Baseline.RuleBaseline());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.buschmais.jqassistant.core.analysis.api.baseline;

import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Expand All @@ -13,6 +14,7 @@
import com.buschmais.jqassistant.core.rule.api.model.ExecutableRule;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -58,8 +60,7 @@ void setUp() {
@ParameterizedTest
@MethodSource("rules")
void baselineDisabled(ExecutableRule<?> executableRule) {
doReturn(false).when(configuration)
.enabled();
stubConfigForAllRules(false);
Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
Expand All @@ -78,8 +79,7 @@ void baselineDisabled(ExecutableRule<?> executableRule) {
@ParameterizedTest
@MethodSource("rules")
void noBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
stubConfigForAllRules(true);
doReturn(empty()).when(baselineRepository)
.read();
Row row = Row.builder()
Expand All @@ -99,8 +99,7 @@ void noBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule) {
@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithExistingConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
stubConfigForAllRules(true);
Baseline oldBaseline = createOldBaseline(executableRule, "1");
doReturn(of(oldBaseline)).when(baselineRepository)
.read();
Expand All @@ -121,8 +120,7 @@ void existingBaselineWithExistingConstraintViolation(ExecutableRule<?> executabl
@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
stubConfigForAllRules(true);
Baseline oldBaseline = createOldBaseline(executableRule, "1");
Row oldRow = Row.builder()
.key("1")
Expand Down Expand Up @@ -150,8 +148,7 @@ void existingBaselineWithNewConstraintViolation(ExecutableRule<?> executableRule
@ParameterizedTest
@MethodSource("rules")
void existingBaselineWithRemovedConstraintViolation(ExecutableRule<?> executableRule) {
doReturn(true).when(configuration)
.enabled();
stubConfigForAllRules(true);
Baseline oldBaseline = createOldBaseline(executableRule, "1", "2");
Row row = Row.builder()
.key("1")
Expand All @@ -169,6 +166,71 @@ void existingBaselineWithRemovedConstraintViolation(ExecutableRule<?> executable
verifyNewBaseline(executableRule, baseline -> baseline.getConstraints(), "1");
}

@Test
void filterConcept() {
doReturn(true).when(configuration)
.enabled();
doReturn(of(List.of("included-concept"))).when(configuration)
.includeConcepts();
doReturn(empty()).when(baselineRepository)
.read();

Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();

baselineManager.start();
assertThat(baselineManager.isExisting(Concept.builder()
.id("non-matching")
.build(), row)).isFalse();
baselineManager.stop();

verify(baselineRepository).write(baselineArgumentCaptor.capture());
Baseline newBaseline = baselineArgumentCaptor.getValue();
assertThat(newBaseline.getConcepts()).isEmpty();
}

@Test
void filterConstraint() {
doReturn(true).when(configuration)
.enabled();
doReturn(List.of("included-constraint")).when(configuration)
.includeConstraints();
doReturn(empty()).when(baselineRepository)
.read();

Row row = Row.builder()
.key("1")
.columns(Map.of("c1", Column.builder()
.label("1")
.build()))
.build();

baselineManager.start();
assertThat(baselineManager.isExisting(Constraint.builder()
.id("non-matching")
.build(), row)).isFalse();
baselineManager.stop();

verify(baselineRepository).write(baselineArgumentCaptor.capture());
Baseline newBaseline = baselineArgumentCaptor.getValue();
assertThat(newBaseline.getConstraints()).isEmpty();
}

private void stubConfigForAllRules(boolean enabled) {
doReturn(enabled).when(configuration)
.enabled();
lenient().doReturn(of(List.of("*")))
.when(configuration)
.includeConcepts();
lenient().doReturn(List.of("*"))
.when(configuration)
.includeConstraints();
}

private static Baseline createOldBaseline(ExecutableRule<?> rule, String... rowKeys) {
Baseline.RuleBaseline oldRuleBaseline = new Baseline.RuleBaseline();
for (String rowKey : rowKeys) {
Expand Down

0 comments on commit e1804ac

Please sign in to comment.