Search in sources :

Example 46 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class InheritanceActionMediumTest method inheritance_nominal.

@Test
public void inheritance_nominal() throws Exception {
    RuleDto rule1 = createRule("xoo", "rule1");
    RuleDto rule2 = createRule("xoo", "rule2");
    RuleDto rule3 = createRule("xoo", "rule3");
    /*
     * groupWide (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
     */
    QualityProfileDto groupWide = createProfile("xoo", "My Group Profile", "xoo-my-group-profile-01234");
    createActiveRule(rule1, groupWide);
    createActiveRule(rule2, groupWide);
    session.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    QualityProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345");
    setParent(groupWide, companyWide);
    QualityProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456");
    setParent(companyWide, buWide);
    overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
    QualityProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567");
    setParent(buWide, forProject1);
    createActiveRule(rule3, forProject1);
    session.commit();
    activeRuleIndexer.index();
    QualityProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678");
    setParent(buWide, forProject2);
    overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
    wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", buWide.getKee()).execute().assertJson(getClass(), "inheritance-buWide.json");
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 47 with RuleDto

use of org.sonar.db.rule.RuleDto 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 48 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method replace_existing_parent.

@Test
public void replace_existing_parent() throws Exception {
    QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
    QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
    QualityProfileDto child = createProfile("xoo", "Child");
    RuleDto rule1 = createRule("xoo", "rule1");
    RuleDto rule2 = createRule("xoo", "rule2");
    createActiveRule(rule1, parent1);
    createActiveRule(rule2, parent2);
    session.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    // Set parent 1
    tester.get(RuleActivator.class).setParent(session, child.getKey(), parent1.getKey());
    session.clearCache();
    // Set parent 2 through WS
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).setParam("parentKey", parent2.getKey()).execute();
    session.clearCache();
    // Check rule 2 enabled
    List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
    assertThat(activeRules2).hasSize(1);
    assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleActivator(org.sonar.server.qualityprofile.RuleActivator) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 49 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method createRule.

private RuleDto createRule(String lang, String id) {
    RuleDto rule = RuleTesting.newDto(RuleKey.of("blah", id)).setLanguage(lang).setSeverity(Severity.BLOCKER).setStatus(RuleStatus.READY);
    db.ruleDao().insert(session, rule);
    return rule;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto)

Example 50 with RuleDto

use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.

the class ChangelogLoaderTest method insertRule.

private void insertRule(RuleKey key, String name) {
    RuleDto dto = RuleTesting.newDto(key).setName(name);
    dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), dto);
    dbTester.getSession().commit();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto)

Aggregations

RuleDto (org.sonar.db.rule.RuleDto)197 Test (org.junit.Test)140 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)80 ComponentDto (org.sonar.db.component.ComponentDto)47 WsTester (org.sonar.server.ws.WsTester)38 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)29 RuleParamDto (org.sonar.db.rule.RuleParamDto)29 IssueDto (org.sonar.db.issue.IssueDto)28 RuleKey (org.sonar.api.rule.RuleKey)24 SearchOptions (org.sonar.server.es.SearchOptions)16 RuleQuery (org.sonar.server.rule.index.RuleQuery)16 BadRequestException (org.sonar.server.exceptions.BadRequestException)15 RuleTesting.newRuleDto (org.sonar.db.rule.RuleTesting.newRuleDto)14 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)12 IssueIndexer (org.sonar.server.issue.index.IssueIndexer)10 Date (java.util.Date)9 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)8 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)7 ArrayList (java.util.ArrayList)6