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");
}
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());
}
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();
}
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;
}
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;
}
Aggregations