use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method fail_to_update_custom_rule_when_empty_description.
@Test
public void fail_to_update_custom_rule_when_empty_description() {
// 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("New name").setMarkdownDescription("");
try {
underTest.update(update, userSessionRule);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The description is missing");
}
}
use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method remove_tags.
@Test
public void remove_tags() {
ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setTags(Sets.newHashSet("security")).setSystemTags(Sets.newHashSet("java8", "javadoc")));
dbSession.commit();
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setTags(null);
underTest.update(update, userSessionRule);
dbSession.clearCache();
RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RULE_KEY);
assertThat(rule.getTags()).isEmpty();
assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");
// verify that tags are indexed in index
Set<String> tags = tester.get(RuleService.class).listTags();
assertThat(tags).containsOnly("java8", "javadoc");
}
use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method reset_remediation_function.
@Test
public void reset_remediation_function() {
ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()).setDefaultRemediationGapMultiplier("1d").setDefaultRemediationBaseEffort("5min").setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()).setRemediationGapMultiplier(null).setRemediationBaseEffort("1min"));
dbSession.commit();
RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY).setDebtRemediationFunction(null);
underTest.update(update, userSessionRule);
dbSession.clearCache();
// verify debt is coming from default values
RuleDto rule = ruleDao.selectOrFailByKey(dbSession, RULE_KEY);
assertThat(rule.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(rule.getDefaultRemediationGapMultiplier()).isEqualTo("1d");
assertThat(rule.getDefaultRemediationBaseEffort()).isEqualTo("5min");
assertThat(rule.getRemediationFunction()).isNull();
assertThat(rule.getRemediationGapMultiplier()).isNull();
assertThat(rule.getRemediationBaseEffort()).isNull();
}
use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method update_active_rule_parameters_when_updating_custom_rule.
@Test
public void update_active_rule_parameters_when_updating_custom_rule() {
// Create template rule with 3 parameters
RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
ruleDao.insert(dbSession, templateRule);
RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("format").setDefaultValue("csv");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
RuleParamDto templateRuleParam3 = RuleParamDto.createFor(templateRule).setName("message").setType("STRING").setDescription("message");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);
// Create custom rule
RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
ruleDao.insert(dbSession, customRule);
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);
// Create a quality profile
QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
// Activate the custom rule
RuleActivation activation = new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
// Update custom rule parameter 'regex', add 'message' and remove 'format'
RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
underTest.update(update, userSessionRule);
dbSession.clearCache();
// Verify custom rule parameters has been updated
List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRule.getKey());
assertThat(params).hasSize(3);
Map<String, RuleParamDto> paramsByKey = paramsByKey(params);
assertThat(paramsByKey.get("regex")).isNotNull();
assertThat(paramsByKey.get("regex").getDefaultValue()).isEqualTo("b.*");
assertThat(paramsByKey.get("message")).isNotNull();
assertThat(paramsByKey.get("message").getDefaultValue()).isEqualTo("a message");
assertThat(paramsByKey.get("format")).isNotNull();
assertThat(paramsByKey.get("format").getDefaultValue()).isNull();
// Verify that severity has not changed
ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
assertThat(activeRuleDto.getSeverityString()).isEqualTo(Severity.BLOCKER);
// Verify active rule parameters has been updated
List<ActiveRuleParamDto> activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParams).hasSize(2);
Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams);
assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*");
assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message");
assertThat(activeRuleParamsByKey.get("format")).isNull();
}
use of org.sonar.db.rule.RuleDto in project sonarqube by SonarSource.
the class QProfilesWsMediumTest method reset.
@Test
public void reset() throws Exception {
QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
QualityProfileDto subProfile = QProfileTesting.newXooP2("org-123").setParentKee(QProfileTesting.XOO_P1_KEY);
db.qualityProfileDao().insert(session, profile, subProfile);
RuleDto rule = createRule(profile.getLanguage(), "rule");
ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule).setSeverity("MINOR");
db.activeRuleDao().insert(session, active1);
db.activeRuleDao().insert(session, active2);
session.commit();
ruIndexer.index();
activeRuIndexer.index();
// 0. assert rule child rule is minor
assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()).isEqualTo("MINOR");
// 1. reset child rule
WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
request.setParam("profile_key", subProfile.getKey());
request.setParam("rule_key", rule.getKey().toString());
request.setParam("reset", "true");
request.execute();
session.clearCache();
// 2. assert rule child rule is NOT minor
assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()).isNotEqualTo("MINOR");
}
Aggregations