use of org.sonar.api.batch.rule.ActiveRules 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();
}
}
use of org.sonar.api.batch.rule.ActiveRules 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();
}
use of org.sonar.api.batch.rule.ActiveRules in project sonarqube by SonarSource.
the class ActiveRulesBuilderTest method no_rules.
@Test
public void no_rules() {
ActiveRulesBuilder builder = new ActiveRulesBuilder();
ActiveRules rules = builder.build();
assertThat(rules.findAll()).isEmpty();
}
use of org.sonar.api.batch.rule.ActiveRules in project sonarqube by SonarSource.
the class ActiveRulesProviderTest method testParamsAreTransformed.
@Test
public void testParamsAreTransformed() {
LoadedActiveRule r1 = mockRule("rule1");
LoadedActiveRule r2 = mockRule("rule2");
r2.setParams(ImmutableMap.of("foo1", "bar1", "foo2", "bar2"));
List<LoadedActiveRule> qpRules = ImmutableList.of(r1, r2);
when(loader.load("qp")).thenReturn(qpRules);
QualityProfiles profiles = mockProfiles("qp");
ActiveRules activeRules = provider.provide(loader, profiles);
assertThat(activeRules.findAll()).hasSize(2);
assertThat(activeRules.findAll()).extracting("ruleKey", "params").containsOnly(Tuple.tuple(RuleKey.of("rule1", "rule1"), ImmutableMap.of()), Tuple.tuple(RuleKey.of("rule2", "rule2"), ImmutableMap.of("foo1", "bar1", "foo2", "bar2")));
verify(loader).load("qp");
verifyNoMoreInteractions(loader);
}
use of org.sonar.api.batch.rule.ActiveRules in project sonar-go by SonarSource.
the class GoSensorTest method getSensor.
private GoSensor getSensor(String... activeRuleArray) {
Set<String> activeRuleSet = new HashSet<>(Arrays.asList(activeRuleArray));
List<Class> ruleClasses = GoChecks.getChecks();
List<String> allKeys = ruleClasses.stream().map(ruleClass -> ((org.sonar.check.Rule) ruleClass.getAnnotations()[0]).key()).collect(Collectors.toList());
ActiveRulesBuilder rulesBuilder = new ActiveRulesBuilder();
allKeys.forEach(key -> {
NewActiveRule newActiveRule = rulesBuilder.create(RuleKey.of(GoRulesDefinition.REPOSITORY_KEY, key));
if (activeRuleSet.contains(key)) {
newActiveRule.activate();
}
});
ActiveRules activeRules = rulesBuilder.build();
CheckFactory checkFactory = new CheckFactory(activeRules);
Checks<Check> checks = checkFactory.create(GoRulesDefinition.REPOSITORY_KEY);
checks.addAnnotatedChecks((Iterable) ruleClasses);
return new GoSensor(checkFactory, fileLinesContextFactory);
}
Aggregations