Search in sources :

Example 16 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class NewActiveRuleTest method params_should_be_empty_map_if_no_params.

@Test
public void params_should_be_empty_map_if_no_params() {
    NewActiveRule rule = builder.build();
    assertThat(rule.params).isEqualTo(ImmutableMap.of());
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Test(org.junit.Test)

Example 17 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesPublisherTest method write.

@Test
public void write() throws Exception {
    File outputDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(outputDir);
    NewActiveRule ar = new NewActiveRule.Builder().setRuleKey(RuleKey.of("java", "S001")).setSeverity("BLOCKER").setParam("p1", "v1").setCreatedAt(1_000L).setUpdatedAt(2_000L).setQProfileKey("qp1").build();
    ActiveRules activeRules = new DefaultActiveRules(singletonList(ar));
    ActiveRulesPublisher underTest = new ActiveRulesPublisher(activeRules);
    underTest.publish(writer);
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    try (CloseableIterator<ScannerReport.ActiveRule> readIt = reader.readActiveRules()) {
        ScannerReport.ActiveRule reportAr = readIt.next();
        assertThat(reportAr.getRuleRepository()).isEqualTo("java");
        assertThat(reportAr.getRuleKey()).isEqualTo("S001");
        assertThat(reportAr.getSeverity()).isEqualTo(Constants.Severity.BLOCKER);
        assertThat(reportAr.getCreatedAt()).isEqualTo(1_000L);
        assertThat(reportAr.getUpdatedAt()).isEqualTo(2_000L);
        assertThat(reportAr.getQProfileKey()).isEqualTo("qp1");
        assertThat(reportAr.getParamsByKeyMap()).hasSize(1);
        assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getKey()).isEqualTo("p1");
        assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getValue()).isEqualTo("v1");
        assertThat(readIt.hasNext()).isFalse();
    }
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) File(java.io.File) Test(org.junit.Test)

Example 18 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesBuilderTest method fail_to_add_twice_the_same_rule.

@Test
public void fail_to_add_twice_the_same_rule() {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0001")).build();
    builder.addRule(rule);
    assertThatThrownBy(() -> builder.addRule(rule)).isInstanceOf(IllegalStateException.class).hasMessage("Rule 'squid:S0001' is already activated");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Test(org.junit.Test)

Example 19 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class ActiveRulesBuilderTest method build_rules.

@Test
public void build_rules() {
    NewActiveRule activeRule = new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").build();
    ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0002")).build()).addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).build()).build();
    assertThat(activeRules.findAll()).hasSize(3);
    assertThat(activeRules.findByRepository("squid")).hasSize(2);
    assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
    assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
    assertThat(activeRules.findByRepository("unknown")).isEmpty();
    ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
    assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
    assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
    assertThat(squid1.internalKey()).isEqualTo("__S0001__");
    assertThat(squid1.params()).hasSize(1);
    assertThat(squid1.param("min")).isEqualTo("20");
    ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
    assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
    assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(squid2.params()).isEmpty();
    ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
    assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(findbugsRule.internalKey()).isNull();
    assertThat(findbugsRule.params()).isEmpty();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) ActiveRule(org.sonar.api.batch.rule.ActiveRule) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 20 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method fail_if_missing_field.

@Test
public void fail_if_missing_field() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("unknown", "foo").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    assertThatThrownBy(() -> checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class)).isInstanceOf(IllegalStateException.class).hasMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty");
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Aggregations

NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)24 Test (org.junit.Test)17 RuleKey (org.sonar.api.rule.RuleKey)11 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)9 ActiveRules (org.sonar.api.batch.rule.ActiveRules)7 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 CheckFactory (org.sonar.api.batch.rule.CheckFactory)3 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)3 FileLinesContext (org.sonar.api.measures.FileLinesContext)3 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 File (java.io.File)1