Search in sources :

Example 66 with RuleDto

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

the class RegisterRulesTest method update_only_rule_description.

@Test
public void update_only_rule_description() throws Exception {
    when(system.now()).thenReturn(DATE1.getTime());
    execute(new RulesDefinition() {

        @Override
        public void define(Context context) {
            NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule").setName("Name").setHtmlDescription("Desc1");
            repo.done();
        }
    });
    when(system.now()).thenReturn(DATE2.getTime());
    execute(new RulesDefinition() {

        @Override
        public void define(Context context) {
            NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule").setName("Name").setHtmlDescription("Desc2");
            repo.done();
        }
    });
    // rule1 has been updated
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RuleKey.of("fake", "rule"));
    assertThat(rule1.getName()).isEqualTo("Name");
    assertThat(rule1.getDescription()).isEqualTo("Desc2");
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc2"), new SearchOptions()).getTotal()).isEqualTo(1);
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc1"), new SearchOptions()).getTotal()).isEqualTo(0);
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 67 with RuleDto

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

the class RuleCreatorMediumTest method create_custom_rule_with_multiple_parameter_values.

@Test
public void create_custom_rule_with_multiple_parameter_values() {
    // insert template rule
    RuleDto templateRule = createTemplateRuleWithIntArrayParam();
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("myIntegers", "1,3"));
    RuleKey customRuleKey = creator.create(newRule);
    dbSession.clearCache();
    List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(params).hasSize(1);
    RuleParamDto param = params.get(0);
    assertThat(param.getName()).isEqualTo("myIntegers");
    assertThat(param.getDescription()).isEqualTo("My Integers");
    assertThat(param.getType()).isEqualTo("INTEGER,multiple=true,values=1;2;3");
    assertThat(param.getDefaultValue()).isEqualTo("1,3");
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 68 with RuleDto

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

the class RuleDeleterMediumTest method delete_custom_rule.

@Test
public void delete_custom_rule() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
    dao.insert(dbSession, templateRule);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo").setCreatedAt(PAST).setUpdatedAt(PAST);
    dao.insert(dbSession, customRule);
    // Create a quality profile
    QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
    db.qualityProfileDao().insert(dbSession, profileDto);
    dbSession.commit();
    dbSession.clearCache();
    ruleIndexer.index();
    activeRuleIndexer.index();
    // Activate the custom rule
    activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);
    // Delete custom rule
    deleter.delete(customRule.getKey());
    // Verify custom rule have status REMOVED
    RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED);
    assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
    // Verify there's no more active rule from custom rule
    assertThat(index.searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).isEmpty();
    // Verify in index
    assertThat(index.searchAll(new RuleQuery())).containsOnly(templateRule.getKey());
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleQuery(org.sonar.server.rule.index.RuleQuery) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 69 with RuleDto

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

the class RuleTagHelperTest method applyTags_remove_all_existing_tags.

@Test
public void applyTags_remove_all_existing_tags() {
    RuleDto rule = new RuleDto().setTags(Sets.newHashSet("performance"));
    boolean changed = RuleTagHelper.applyTags(rule, Collections.<String>emptySet());
    assertThat(rule.getTags()).isEmpty();
    assertThat(changed).isTrue();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 70 with RuleDto

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

the class RuleTagHelperTest method applyTags_no_changes.

@Test
public void applyTags_no_changes() {
    RuleDto rule = new RuleDto().setTags(Sets.newHashSet("performance"));
    boolean changed = RuleTagHelper.applyTags(rule, Sets.newHashSet("performance"));
    assertThat(rule.getTags()).containsOnly("performance");
    assertThat(changed).isFalse();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

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