Skip to content

Commit

Permalink
SONAR-11514 Backdate issue when rule parameter is updated (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-sns authored and SonarTech committed Dec 3, 2018
1 parent e96e630 commit 40c5846
Show file tree
Hide file tree
Showing 38 changed files with 416 additions and 143 deletions.
Expand Up @@ -88,7 +88,7 @@ public void onIssue(Component component, DefaultIssue issue) {
} else { } else {
// Rule can't be inactive (see contract of IssueVisitor) // Rule can't be inactive (see contract of IssueVisitor)
ActiveRule activeRule = activeRulesHolder.get(issue.getRuleKey()).get(); ActiveRule activeRule = activeRulesHolder.get(issue.getRuleKey()).get();
if (activeRuleIsNew(activeRule, lastAnalysisOptional.get()) if (activeRuleIsNewOrChanged(activeRule, lastAnalysisOptional.get())
|| ruleImplementationChanged(activeRule.getRuleKey(), activeRule.getPluginKey(), lastAnalysisOptional.get())) { || ruleImplementationChanged(activeRule.getRuleKey(), activeRule.getPluginKey(), lastAnalysisOptional.get())) {
backdateIssue(component, issue); backdateIssue(component, issue);
} }
Expand Down Expand Up @@ -127,9 +127,8 @@ private static boolean pluginIsNew(ScannerPlugin scannerPlugin, long lastAnalysi
return lastAnalysisDate < scannerPlugin.getUpdatedAt(); return lastAnalysisDate < scannerPlugin.getUpdatedAt();
} }


private static boolean activeRuleIsNew(ActiveRule activeRule, Long lastAnalysisDate) { private static boolean activeRuleIsNewOrChanged(ActiveRule activeRule, Long lastAnalysisDate) {
long ruleCreationDate = activeRule.getCreatedAt(); return lastAnalysisDate < activeRule.getUpdatedAt();
return lastAnalysisDate < ruleCreationDate;
} }


private Optional<Date> getDateOfLatestChange(Component component, DefaultIssue issue) { private Optional<Date> getDateOfLatestChange(Component component, DefaultIssue issue) {
Expand Down
Expand Up @@ -33,13 +33,15 @@ public class ActiveRule {
private final Map<String, String> params; private final Map<String, String> params;
private final long createdAt; private final long createdAt;
private final String pluginKey; private final String pluginKey;
private final long updatedAt;


public ActiveRule(RuleKey ruleKey, String severity, Map<String, String> params, long createdAt, @Nullable String pluginKey) { public ActiveRule(RuleKey ruleKey, String severity, Map<String, String> params, long createdAt, long updatedAt, @Nullable String pluginKey) {
this.ruleKey = ruleKey; this.ruleKey = ruleKey;
this.severity = severity; this.severity = severity;
this.pluginKey = pluginKey; this.pluginKey = pluginKey;
this.params = ImmutableMap.copyOf(params); this.params = ImmutableMap.copyOf(params);
this.createdAt = createdAt; this.createdAt = createdAt;
this.updatedAt = updatedAt;
} }


public RuleKey getRuleKey() { public RuleKey getRuleKey() {
Expand All @@ -58,6 +60,10 @@ public long getCreatedAt() {
return createdAt; return createdAt;
} }


public long getUpdatedAt() {
return updatedAt;
}

@CheckForNull @CheckForNull
public String getPluginKey() { public String getPluginKey() {
return pluginKey; return pluginKey;
Expand Down
Expand Up @@ -72,6 +72,6 @@ public String getDescription() {
private static ActiveRule convert(ScannerReport.ActiveRule input, Rule rule) { private static ActiveRule convert(ScannerReport.ActiveRule input, Rule rule) {
RuleKey key = RuleKey.of(input.getRuleRepository(), input.getRuleKey()); RuleKey key = RuleKey.of(input.getRuleRepository(), input.getRuleKey());
Map<String, String> params = new HashMap<>(input.getParamsByKeyMap()); Map<String, String> params = new HashMap<>(input.getParamsByKeyMap());
return new ActiveRule(key, input.getSeverity().name(), params, input.getCreatedAt(), rule.getPluginKey()); return new ActiveRule(key, input.getSeverity().name(), params, input.getCreatedAt(), input.getUpdatedAt(), rule.getPluginKey());
} }
} }
Expand Up @@ -208,6 +208,22 @@ public void should_backdate_date_if_scm_is_available_and_rule_is_new(BiConsumer<
assertChangeOfCreationDateTo(expectedDate); assertChangeOfCreationDateTo(expectedDate);
} }


@Test
@UseDataProvider("backdatingDateCases")
public void should_backdate_date_if_scm_is_available_and_rule_has_changed(BiConsumer<DefaultIssue, ScmInfo> configure, long expectedDate) {
previousAnalysisWas(2000L);
currentAnalysisIs(3000L);

makeIssueNew();
configure.accept(issue, createMockScmInfo());
setRuleCreatedAt(1200L);
setRuleUpdatedAt(2800L);

run();

assertChangeOfCreationDateTo(expectedDate);
}

@Test @Test
@UseDataProvider("backdatingDateCases") @UseDataProvider("backdatingDateCases")
public void should_backdate_date_if_scm_is_available_and_first_analysis(BiConsumer<DefaultIssue, ScmInfo> configure, long expectedDate) { public void should_backdate_date_if_scm_is_available_and_first_analysis(BiConsumer<DefaultIssue, ScmInfo> configure, long expectedDate) {
Expand Down Expand Up @@ -418,6 +434,11 @@ private ScmInfo createMockScmInfo() {


private void setRuleCreatedAt(long createdAt) { private void setRuleCreatedAt(long createdAt) {
when(activeRule.getCreatedAt()).thenReturn(createdAt); when(activeRule.getCreatedAt()).thenReturn(createdAt);
when(activeRule.getUpdatedAt()).thenReturn(createdAt);
}

private void setRuleUpdatedAt(long updateAt) {
when(activeRule.getUpdatedAt()).thenReturn(updateAt);
} }


private void rulePlugin(String pluginKey) { private void rulePlugin(String pluginKey) {
Expand Down
Expand Up @@ -367,6 +367,6 @@ private void assertInitializedExternalIssue(DefaultIssue issue) {
} }


private void markRuleAsActive(RuleKey ruleKey) { private void markRuleAsActive(RuleKey ruleKey) {
activeRulesHolder.put(new ActiveRule(ruleKey, Severity.CRITICAL, emptyMap(), 1_000L, null)); activeRulesHolder.put(new ActiveRule(ruleKey, Severity.CRITICAL, emptyMap(), 1_000L, 1_000L, null));
} }
} }
Expand Up @@ -77,7 +77,7 @@ public class CommentDensityRuleTest {


@Test @Test
public void no_issues_if_enough_comments() { public void no_issues_if_enough_comments() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(90.0, 1)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(90.0, 1));


DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY); DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY);
Expand Down Expand Up @@ -135,7 +135,7 @@ public void fail_if_min_density_is_100() {
} }


private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) { private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(commentLineDensity, 1)); measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(commentLineDensity, 1));
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_KEY, Measure.newMeasureBuilder().create(commentLines)); measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_KEY, Measure.newMeasureBuilder().create(commentLines));
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.NCLOC_KEY, Measure.newMeasureBuilder().create(ncloc)); measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.NCLOC_KEY, Measure.newMeasureBuilder().create(ncloc));
Expand Down
Expand Up @@ -38,7 +38,7 @@ public class CommonRuleTest {


@Test @Test
public void test_getMinDensityParam() { public void test_getMinDensityParam() {
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "30.5"), 1_000L, PLUGIN_KEY); ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "30.5"), 1_000L, 1_000L, PLUGIN_KEY);
double minDensity = CommonRule.getMinDensityParam(activeRule, "minDensity"); double minDensity = CommonRule.getMinDensityParam(activeRule, "minDensity");


assertThat(minDensity).isEqualTo(30.5); assertThat(minDensity).isEqualTo(30.5);
Expand All @@ -49,7 +49,7 @@ public void getMinDensityParam_fails_if_param_value_is_absent() {
thrown.expect(IllegalStateException.class); thrown.expect(IllegalStateException.class);
thrown.expectMessage("Required parameter [minDensity] is missing on rule [xoo:x1]"); thrown.expectMessage("Required parameter [minDensity] is missing on rule [xoo:x1]");


ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, PLUGIN_KEY); ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, 1_000L, PLUGIN_KEY);
CommonRule.getMinDensityParam(activeRule, "minDensity"); CommonRule.getMinDensityParam(activeRule, "minDensity");
} }


Expand All @@ -58,7 +58,7 @@ public void getMinDensityParam_fails_if_param_value_is_negative() {
thrown.expect(IllegalStateException.class); thrown.expect(IllegalStateException.class);
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100."); thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100.");


ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, PLUGIN_KEY); ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, 1_000L, PLUGIN_KEY);
CommonRule.getMinDensityParam(activeRule, "minDensity"); CommonRule.getMinDensityParam(activeRule, "minDensity");
} }


Expand All @@ -67,7 +67,7 @@ public void getMinDensityParam_fails_if_param_value_is_greater_than_100() {
thrown.expect(IllegalStateException.class); thrown.expect(IllegalStateException.class);
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100."); thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100.");


ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, PLUGIN_KEY); ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, 1_000L, PLUGIN_KEY);
CommonRule.getMinDensityParam(activeRule, "minDensity"); CommonRule.getMinDensityParam(activeRule, "minDensity");
} }
} }
Expand Up @@ -86,7 +86,7 @@ public void setUp() {


@Test @Test
public void no_issue_if_enough_coverage() { public void no_issue_if_enough_coverage() {
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");
Expand All @@ -96,7 +96,7 @@ public void no_issue_if_enough_coverage() {


@Test @Test
public void issue_if_coverage_is_too_low() { public void issue_if_coverage_is_too_low() {
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50));
Expand All @@ -114,7 +114,7 @@ public void issue_if_coverage_is_too_low() {


@Test @Test
public void no_issue_if_coverage_is_not_set() { public void no_issue_if_coverage_is_not_set() {
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");


Expand Down
Expand Up @@ -66,7 +66,7 @@ public class DuplicatedBlockRuleTest {


@Test @Test
public void no_issue_if_no_duplicated_blocks() { public void no_issue_if_no_duplicated_blocks() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(0));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");
Expand All @@ -76,7 +76,7 @@ public void no_issue_if_no_duplicated_blocks() {


@Test @Test
public void issue_if_duplicated_blocks() { public void issue_if_duplicated_blocks() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(3)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(3));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");
Expand Down
Expand Up @@ -67,7 +67,7 @@ public class SkippedTestRuleTest {


@Test @Test
public void issue_if_skipped_tests() { public void issue_if_skipped_tests() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(2)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(2));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");
Expand All @@ -80,7 +80,7 @@ public void issue_if_skipped_tests() {


@Test @Test
public void no_issues_if_zero_skipped_tests() { public void no_issues_if_zero_skipped_tests() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(0));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");
Expand All @@ -90,7 +90,7 @@ public void no_issues_if_zero_skipped_tests() {


@Test @Test
public void no_issues_if_measure_is_absent() { public void no_issues_if_measure_is_absent() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");


Expand Down
Expand Up @@ -69,7 +69,7 @@ public class TestErrorRuleTest {


@Test @Test
public void issue_if_errors_or_failures() { public void issue_if_errors_or_failures() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(2)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(2));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(1)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(1));


Expand All @@ -83,7 +83,7 @@ public void issue_if_errors_or_failures() {


@Test @Test
public void no_issues_if_zero_errors_and_failures() { public void no_issues_if_zero_errors_and_failures() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(0));
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(0)); measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(0));


Expand All @@ -94,7 +94,7 @@ public void no_issues_if_zero_errors_and_failures() {


@Test @Test
public void no_issues_if_test_measures_are_absent() { public void no_issues_if_test_measures_are_absent() {
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY));


DefaultIssue issue = underTest.processFile(FILE, "java"); DefaultIssue issue = underTest.processFile(FILE, "java");


Expand Down
Expand Up @@ -52,7 +52,7 @@ public void get_inactive_rule() {


@Test @Test
public void get_active_rule() { public void get_active_rule() {
underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, PLUGIN_KEY))); underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY)));


Optional<ActiveRule> activeRule = underTest.get(RULE_KEY); Optional<ActiveRule> activeRule = underTest.get(RULE_KEY);
assertThat(activeRule.isPresent()).isTrue(); assertThat(activeRule.isPresent()).isTrue();
Expand All @@ -65,7 +65,7 @@ public void can_not_set_twice() {
thrown.expect(IllegalStateException.class); thrown.expect(IllegalStateException.class);
thrown.expectMessage("Active rules have already been initialized"); thrown.expectMessage("Active rules have already been initialized");


underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), 1_000L, PLUGIN_KEY))); underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY)));
underTest.set(Collections.emptyList()); underTest.set(Collections.emptyList());


} }
Expand All @@ -84,7 +84,7 @@ public void can_not_set_duplicated_rules() {
thrown.expectMessage("Active rule must not be declared multiple times: squid:S001"); thrown.expectMessage("Active rule must not be declared multiple times: squid:S001");


underTest.set(asList( underTest.set(asList(
new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), 1_000L, PLUGIN_KEY), new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY),
new ActiveRule(RULE_KEY, Severity.MAJOR, Collections.emptyMap(), 1_000L, PLUGIN_KEY))); new ActiveRule(RULE_KEY, Severity.MAJOR, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY)));
} }
} }
Expand Up @@ -28,7 +28,7 @@
public class AlwaysActiveRulesHolderImpl implements ActiveRulesHolder { public class AlwaysActiveRulesHolderImpl implements ActiveRulesHolder {
@Override @Override
public Optional<ActiveRule> get(RuleKey ruleKey) { public Optional<ActiveRule> get(RuleKey ruleKey) {
return Optional.of(new ActiveRule(ruleKey, Severity.MAJOR, emptyMap(), 1_000L, null)); return Optional.of(new ActiveRule(ruleKey, Severity.MAJOR, emptyMap(), 1_000L, 1_000L, null));
} }


} }
Expand Up @@ -19,7 +19,6 @@
*/ */
package org.sonar.ce.task.projectanalysis.step; package org.sonar.ce.task.projectanalysis.step;


import java.util.Optional;
import org.assertj.core.data.MapEntry; import org.assertj.core.data.MapEntry;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand All @@ -33,9 +32,11 @@
import org.sonar.ce.task.step.TestComputationStepContext; import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.Constants;
import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonarqube.ws.Rules;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.sonar.db.rule.RuleTesting.XOO_X1; import static org.sonar.db.rule.RuleTesting.XOO_X1;
import static org.sonar.db.rule.RuleTesting.XOO_X2; import static org.sonar.db.rule.RuleTesting.XOO_X2;


Expand All @@ -58,8 +59,11 @@ public void feed_active_rules() {
.setPluginKey("xoo"); .setPluginKey("xoo");


ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder() ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
.setRuleRepository(XOO_X1.repository()).setRuleKey(XOO_X1.rule()) .setRuleRepository(XOO_X1.repository())
.setSeverity(Constants.Severity.BLOCKER); .setRuleKey(XOO_X1.rule())
.setSeverity(Constants.Severity.BLOCKER)
.setCreatedAt(1000L)
.setUpdatedAt(1200L);
batch1.getMutableParamsByKey().put("p1", "v1"); batch1.getMutableParamsByKey().put("p1", "v1");


ScannerReport.ActiveRule.Builder batch2 = ScannerReport.ActiveRule.newBuilder() ScannerReport.ActiveRule.Builder batch2 = ScannerReport.ActiveRule.newBuilder()
Expand All @@ -70,15 +74,19 @@ public void feed_active_rules() {


assertThat(activeRulesHolder.getAll()).hasSize(2); assertThat(activeRulesHolder.getAll()).hasSize(2);


Optional<ActiveRule> ar1 = activeRulesHolder.get(XOO_X1); ActiveRule ar1 = activeRulesHolder.get(XOO_X1).get();
assertThat(ar1.get().getSeverity()).isEqualTo(Severity.BLOCKER); assertThat(ar1.getSeverity()).isEqualTo(Severity.BLOCKER);
assertThat(ar1.get().getParams()).containsExactly(MapEntry.entry("p1", "v1")); assertThat(ar1.getParams()).containsExactly(MapEntry.entry("p1", "v1"));
assertThat(ar1.get().getPluginKey()).isEqualTo("xoo"); assertThat(ar1.getPluginKey()).isEqualTo("xoo");

assertThat(ar1.getCreatedAt()).isEqualTo(1000L);
Optional<ActiveRule> ar2 = activeRulesHolder.get(XOO_X2); assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
assertThat(ar2.get().getSeverity()).isEqualTo(Severity.MAJOR);
assertThat(ar2.get().getParams()).isEmpty(); ActiveRule ar2 = activeRulesHolder.get(XOO_X2).get();
assertThat(ar2.get().getPluginKey()).isEqualTo("xoo"); assertThat(ar2.getSeverity()).isEqualTo(Severity.MAJOR);
assertThat(ar2.getParams()).isEmpty();
assertThat(ar2.getPluginKey()).isEqualTo("xoo");
assertThat(ar1.getCreatedAt()).isEqualTo(1000L);
assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
} }


@Test @Test
Expand Down
Expand Up @@ -167,6 +167,7 @@ private static Rules.Active buildActiveRuleResponse(OrgActiveRuleDto activeRule,
builder.setInherit(inheritance != null ? inheritance : ActiveRuleInheritance.NONE.name()); builder.setInherit(inheritance != null ? inheritance : ActiveRuleInheritance.NONE.name());
builder.setSeverity(activeRule.getSeverityString()); builder.setSeverity(activeRule.getSeverityString());
builder.setCreatedAt(DateUtils.formatDateTime(activeRule.getCreatedAt())); builder.setCreatedAt(DateUtils.formatDateTime(activeRule.getCreatedAt()));
builder.setUpdatedAt(DateUtils.formatDateTime(activeRule.getUpdatedAt()));
Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder(); Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder();
for (ActiveRuleParamDto parameter : parameters) { for (ActiveRuleParamDto parameter : parameters) {
builder.addParams(paramBuilder.clear() builder.addParams(paramBuilder.clear()
Expand Down

0 comments on commit 40c5846

Please sign in to comment.