Search in sources :

Example 26 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class IssueIndexTest method filter_by_rules.

@Test
public void filter_by_rules() {
    ComponentDto project = newProjectDto(newOrganizationDto());
    ComponentDto file = newFileDto(project, null);
    RuleKey ruleKey = RuleKey.of("repo", "X1");
    indexIssues(IssueDocTesting.newDoc("ISSUE1", file).setRuleKey(ruleKey.toString()));
    assertThat(underTest.search(IssueQuery.builder().rules(newArrayList(ruleKey)).build(), new SearchOptions()).getDocs()).hasSize(1);
    assertThat(underTest.search(IssueQuery.builder().rules(newArrayList(RuleKey.of("rule", "without issue"))).build(), new SearchOptions()).getDocs()).isEmpty();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ComponentDto(org.sonar.db.component.ComponentDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 27 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class IssueIndexTest method facets_on_languages.

@Test
public void facets_on_languages() {
    ComponentDto project = newProjectDto(newOrganizationDto());
    ComponentDto file = newFileDto(project, null);
    RuleKey ruleKey = RuleKey.of("repo", "X1");
    indexIssues(IssueDocTesting.newDoc("ISSUE1", file).setRuleKey(ruleKey.toString()).setLanguage("xoo"));
    SearchResult<IssueDoc> result = underTest.search(IssueQuery.builder().build(), new SearchOptions().addFacets(newArrayList("languages")));
    assertThat(result.getFacets().getNames()).containsOnly("languages");
    assertThat(result.getFacets().get("languages")).containsOnly(entry("xoo", 1L));
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ComponentDto(org.sonar.db.component.ComponentDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 28 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class QProfileBackuperMediumTest method backup.

@Test
public void backup() throws Exception {
    RuleKey blahRuleKey = RuleKey.of("blah", "my-rule");
    RuleDto blahRule = newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
    db.ruleDao().insert(dbSession, blahRule);
    dbSession.commit();
    dbSession.clearCache();
    ruleIndexer.index();
    // create profile P1 with rules x2 and x1 activated
    db.qualityProfileDao().insert(dbSession, newXooP1("org-123"));
    RuleActivation activation1 = new RuleActivation(XOO_X2).setSeverity("MINOR");
    RuleActivation activation2 = new RuleActivation(XOO_X1);
    RuleActivation activation3 = new RuleActivation(blahRuleKey);
    activation2.setSeverity(Severity.BLOCKER);
    activation2.setParameter("max", "7");
    tester.get(RuleActivator.class).activate(dbSession, activation1, XOO_P1_NAME);
    tester.get(RuleActivator.class).activate(dbSession, activation2, XOO_P1_NAME);
    tester.get(RuleActivator.class).activate(dbSession, activation3, XOO_P1_NAME);
    dbSession.commit();
    dbSession.clearCache();
    activeRuleIndexer.index();
    StringWriter output = new StringWriter();
    tester.get(QProfileBackuper.class).backup(XOO_P1_KEY, output);
    String expectedXml = Resources.toString(getClass().getResource("QProfileBackuperMediumTest/expected-backup.xml"), StandardCharsets.UTF_8);
    assertThat(output.toString()).isXmlEqualTo(expectedXml);
}
Also used : StringWriter(java.io.StringWriter) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 29 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class QProfileResetMediumTest method reset_language_profile.

@Test
public void reset_language_profile() {
    RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
    defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MINOR);
            x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
    // Change the severity and the value of the parameter in the active rule
    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(ruleKey).setSeverity(BLOCKER).setParameter("acceptWhitespace", "false"), profile.getKey());
    dbSession.commit();
    // Verify severity and param has changed
    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
    List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
    reset.resetLanguage(ServerTester.Xoo.KEY);
    dbSession.commit();
    // Severity and parameter value come back to origin after reset
    activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
    activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleParam(org.sonar.api.rules.RuleParam) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 30 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RegisterQualityProfilesMediumTest method register_profile_definitions.

@Test
public void register_profile_definitions() {
    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
    tester.start();
    dbSession = dbClient().openSession(false);
    // Check Profile in DB
    QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
    assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
    QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
    assertThat(profile).isNotNull();
    // Check Default Profile
    verifyDefaultProfile("xoo", "Basic");
    // Check ActiveRules in DB
    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
    assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
    assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
    assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
    // Check ActiveRuleParameters in DB
    Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
    assertThat(params).hasSize(2);
    // set by profile
    assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
    // default value
    assertThat(params.get("max").getValue()).isEqualTo("10");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ServerTester(org.sonar.server.tester.ServerTester) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3