@Test public void add_issue_to_cache() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(SQUID_RULE_KEY) .overrideSeverity(org.sonar.api.batch.rule.Severity.CRITICAL); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isTrue(); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); }
@Test public void should_accept_issues_on_no_sonar_rules() { // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !! ruleBuilder.add(NOSONAR_RULE_KEY).setName("No Sonar"); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(NOSONAR_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); file.noSonarAt(new HashSet<>(Collections.singletonList(3))); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(NOSONAR_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isTrue(); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), any()); }
@Test public void use_severity_from_active_rule_if_no_severity_on_issue() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(SQUID_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); moduleIssues.initAndAddIssue(issue); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.INFO); }
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey) { NewActiveRule.Builder builder = new NewActiveRule.Builder(); builder .setRuleKey(activeRule.getRuleKey()) .setName(activeRule.getName()) .setSeverity(activeRule.getSeverity()) .setCreatedAt(activeRule.getCreatedAt()) .setUpdatedAt(activeRule.getUpdatedAt()) .setLanguage(activeRule.getLanguage()) .setInternalKey(activeRule.getInternalKey()) .setTemplateRuleKey(activeRule.getTemplateRuleKey()) .setQProfileKey(qProfileKey); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { builder.setParam(params.getKey(), params.getValue()); } } return builder.build(); }
@Test public void filter_issue() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(SQUID_RULE_KEY); when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(false); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }
@Test public void should_ignore_lines_commented_with_nosonar() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder() .setRuleKey(SQUID_RULE_KEY) .setSeverity(Severity.INFO) .setQProfileKey("qp-1") .build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(SQUID_RULE_KEY); file.noSonarAt(new HashSet<>(Collections.singletonList(3))); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }
@Test public void ignore_null_rule_of_active_rule() { ruleBuilder.add(SQUID_RULE_KEY).setName(SQUID_RULE_NAME); activeRulesBuilder.addRule(new NewActiveRule.Builder().setRuleKey(SQUID_RULE_KEY).setQProfileKey("qp-1").build()); initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(SQUID_RULE_KEY); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isFalse(); verifyZeroInteractions(reportPublisher); }