Search in sources :

Example 76 with RuleDto

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

the class RuleUpdaterMediumTest method fail_to_update_custom_rule_when_empty_name.

@Test
public void fail_to_update_custom_rule_when_empty_name() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule);
    ruleDao.insert(dbSession, customRule);
    dbSession.commit();
    // Update custom rule
    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setName("").setMarkdownDescription("New desc");
    try {
        underTest.update(update, userSessionRule);
        fail();
    } catch (Exception e) {
        assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The name is missing");
    }
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 77 with RuleDto

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

the class RuleUpdaterMediumTest method set_markdown_note.

@Test
public void set_markdown_note() {
    userSessionRule.logIn("me");
    ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setNoteData(null).setNoteUserLogin(null).setTags(ImmutableSet.of("tag1")).setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()).setRemediationGapMultiplier("1d").setRemediationBaseEffort("5min"));
    dbSession.commit();
    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
    update.setMarkdownNote("my *note*");
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isEqualTo("my *note*");
    assertThat(rule.getNoteUserLogin()).isEqualTo("me");
    assertThat(rule.getNoteCreatedAt()).isNotNull();
    assertThat(rule.getNoteUpdatedAt()).isNotNull();
    // no other changes
    assertThat(rule.getTags()).containsOnly("tag1");
    assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
    assertThat(rule.getRemediationGapMultiplier()).isEqualTo("1d");
    assertThat(rule.getRemediationBaseEffort()).isEqualTo("5min");
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 78 with RuleDto

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

the class RuleUpdaterMediumTest method no_changes.

@Test
public void no_changes() {
    ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setNoteData("my *note*").setNoteUserLogin("me").setTags(ImmutableSet.of("tag1")).setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()).setRemediationGapMultiplier("1d").setRemediationBaseEffort("5min"));
    dbSession.commit();
    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY);
    assertThat(update.isEmpty()).isTrue();
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RULE_KEY);
    assertThat(rule.getNoteData()).isEqualTo("my *note*");
    assertThat(rule.getNoteUserLogin()).isEqualTo("me");
    assertThat(rule.getTags()).containsOnly("tag1");
    assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
    assertThat(rule.getRemediationGapMultiplier()).isEqualTo("1d");
    assertThat(rule.getRemediationBaseEffort()).isEqualTo("5min");
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 79 with RuleDto

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

the class RuleUpdaterMediumTest method set_tags.

@Test
public void set_tags() {
    // insert db
    ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setTags(Sets.newHashSet("security")).setSystemTags(Sets.newHashSet("java8", "javadoc")));
    dbSession.commit();
    // java8 is a system tag -> ignore
    RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(Sets.newHashSet("bug", "java8"));
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RULE_KEY);
    assertThat(rule.getTags()).containsOnly("bug");
    assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");
    // verify that tags are indexed in index
    Set<String> tags = tester.get(RuleService.class).listTags();
    assertThat(tags).containsOnly("bug", "java8", "javadoc");
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 80 with RuleDto

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

the class RuleUpdaterMediumTest method update_custom_rule_with_empty_parameter.

@Test
public void update_custom_rule_with_empty_parameter() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setName("Old name").setDescription("Old description").setSeverity(Severity.MINOR).setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam);
    dbSession.commit();
    // Update custom rule without setting a value for the parameter
    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setName("New name").setMarkdownDescription("New description").setSeverity("MAJOR").setStatus(RuleStatus.READY);
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    // Verify custom rule is updated
    List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRule.getKey());
    assertThat(params.get(0).getDefaultValue()).isNull();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) 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