Search in sources :

Example 31 with RuleParamDto

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

the class RegisterRulesTest method insert_new_rules.

@Test
public void insert_new_rules() {
    execute(new FakeRepositoryV1());
    // verify db
    assertThat(dbClient.ruleDao().selectAll(dbTester.getSession())).hasSize(2);
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule1.getName()).isEqualTo("One");
    assertThat(rule1.getDescription()).isEqualTo("Description of One");
    assertThat(rule1.getSeverityString()).isEqualTo(BLOCKER);
    assertThat(rule1.getTags()).isEmpty();
    assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3");
    assertThat(rule1.getConfigKey()).isEqualTo("config1");
    assertThat(rule1.getStatus()).isEqualTo(RuleStatus.BETA);
    assertThat(rule1.getCreatedAt()).isEqualTo(DATE1.getTime());
    assertThat(rule1.getUpdatedAt()).isEqualTo(DATE1.getTime());
    assertThat(rule1.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
    assertThat(rule1.getDefaultRemediationGapMultiplier()).isEqualTo("5d");
    assertThat(rule1.getDefaultRemediationBaseEffort()).isEqualTo("10h");
    assertThat(rule1.getType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
    List<RuleParamDto> params = dbClient.ruleDao().selectRuleParamsByRuleKey(dbTester.getSession(), RULE_KEY1);
    assertThat(params).hasSize(2);
    RuleParamDto param = getParam(params, "param1");
    assertThat(param.getDescription()).isEqualTo("parameter one");
    assertThat(param.getDefaultValue()).isEqualTo("default1");
    // verify index
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY2);
    // verify repositories
    assertThat(dbClient.ruleRepositoryDao().selectAll(dbTester.getSession())).extracting(RuleRepositoryDto::getKey).containsOnly("fake");
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleParamDto(org.sonar.db.rule.RuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 32 with RuleParamDto

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

the class RuleCreatorMediumTest method create_custom_rule.

@Test
public void create_custom_rule() {
    // insert template rule
    RuleDto templateRule = createTemplateRule();
    // Create custom rule
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setMarkdownDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "a.*"));
    RuleKey customRuleKey = creator.create(newRule);
    dbSession.clearCache();
    RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, customRuleKey);
    assertThat(rule).isNotNull();
    assertThat(rule.getKey()).isEqualTo(RuleKey.of("java", "CUSTOM_RULE"));
    assertThat(rule.getTemplateId()).isEqualTo(templateRule.getId());
    assertThat(rule.getName()).isEqualTo("My custom");
    assertThat(rule.getDescription()).isEqualTo("Some description");
    assertThat(rule.getSeverityString()).isEqualTo("MAJOR");
    assertThat(rule.getStatus()).isEqualTo(RuleStatus.READY);
    assertThat(rule.getLanguage()).isEqualTo("java");
    assertThat(rule.getConfigKey()).isEqualTo("S001");
    assertThat(rule.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET");
    assertThat(rule.getDefaultRemediationGapMultiplier()).isEqualTo("1h");
    assertThat(rule.getDefaultRemediationBaseEffort()).isEqualTo("5min");
    assertThat(rule.getGapDescription()).isEqualTo("desc");
    assertThat(rule.getTags()).containsOnly("usertag1", "usertag2");
    assertThat(rule.getSystemTags()).containsOnly("tag1", "tag4");
    List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(params).hasSize(1);
    RuleParamDto param = params.get(0);
    // From template rule
    assertThat(param.getName()).isEqualTo("regex");
    assertThat(param.getDescription()).isEqualTo("Reg ex");
    assertThat(param.getType()).isEqualTo("STRING");
    // From user
    assertThat(param.getDefaultValue()).isEqualTo("a.*");
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(customRuleKey, templateRule.getKey());
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleParamDto(org.sonar.db.rule.RuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 33 with RuleParamDto

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

the class RuleCreatorMediumTest method create_custom_rule_with_empty_parameter_value.

@Test
public void create_custom_rule_with_empty_parameter_value() {
    // insert template rule
    RuleDto templateRule = createTemplateRule();
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", ""));
    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("regex");
    assertThat(param.getDescription()).isEqualTo("Reg ex");
    assertThat(param.getType()).isEqualTo("STRING");
    assertThat(param.getDefaultValue()).isNull();
}
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 34 with RuleParamDto

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

the class RuleCreatorMediumTest method createTemplateRuleWithTwoIntParams.

private RuleDto createTemplateRuleWithTwoIntParams() {
    RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S003")).setIsTemplate(true).setLanguage("java").setConfigKey("S003").setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setDefaultRemediationGapMultiplier("1h").setDefaultRemediationBaseEffort("5min").setGapDescription("desc").setCreatedAt(new Date().getTime()).setUpdatedAt(new Date().getTime());
    dao.insert(dbSession, templateRule);
    RuleParamDto ruleParam1Dto = RuleParamDto.createFor(templateRule).setName("first").setType("INTEGER").setDescription("First integer").setDefaultValue("0");
    dao.insertRuleParam(dbSession, templateRule, ruleParam1Dto);
    RuleParamDto ruleParam2Dto = RuleParamDto.createFor(templateRule).setName("second").setType("INTEGER").setDescription("Second integer").setDefaultValue("0");
    dao.insertRuleParam(dbSession, templateRule, ruleParam2Dto);
    dbSession.commit();
    return templateRule;
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Date(java.util.Date)

Example 35 with RuleParamDto

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

the class RuleCreatorMediumTest method createTemplateRule.

private RuleDto createTemplateRule() {
    RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true).setLanguage("java").setConfigKey("S001").setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setDefaultRemediationGapMultiplier("1h").setDefaultRemediationBaseEffort("5min").setGapDescription("desc").setTags(Sets.newHashSet("usertag1", "usertag2")).setSystemTags(Sets.newHashSet("tag1", "tag4")).setCreatedAt(new Date().getTime()).setUpdatedAt(new Date().getTime());
    dao.insert(dbSession, templateRule);
    RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    dao.insertRuleParam(dbSession, templateRule, ruleParamDto);
    dbSession.commit();
    ruleIndexer.index();
    return templateRule;
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Date(java.util.Date)

Aggregations

RuleParamDto (org.sonar.db.rule.RuleParamDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 Test (org.junit.Test)17 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)14 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)12 RuleKey (org.sonar.api.rule.RuleKey)10 ArrayList (java.util.ArrayList)6 WsTester (org.sonar.server.ws.WsTester)6 Date (java.util.Date)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 DbSession (org.sonar.db.DbSession)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Before (org.junit.Before)1