use of org.sonar.db.qualityprofile.ActiveRuleDao in project sonarqube by SonarSource.
the class RuleActivator method doInsert.
private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDto activeRule;
ActiveRuleDao dao = db.activeRuleDao();
activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
String severity = change.getSeverity();
if (severity != null) {
activeRule.setSeverity(severity);
}
ActiveRule.Inheritance inheritance = change.getInheritance();
if (inheritance != null) {
activeRule.setInheritance(inheritance.name());
}
activeRule.setUpdatedAt(system2.now());
activeRule.setCreatedAt(system2.now());
dao.insert(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
if (param.getValue() != null) {
ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
paramDto.setValue(param.getValue());
dao.insertParam(dbSession, activeRule, paramDto);
}
}
return activeRule;
}
use of org.sonar.db.qualityprofile.ActiveRuleDao in project sonarqube by SonarSource.
the class RegisterQualityProfilesMediumTest method register_profile_definitions.
@Test
public void register_profile_definitions() {
tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
// Check Profile in DB
QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
assertThat(profile).isNotNull();
// Check Default Profile
verifyDefaultProfile("xoo", "Basic");
// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
// Check ActiveRuleParameters in DB
Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
// default value
assertThat(params.get("max").getValue()).isEqualTo("10");
}
use of org.sonar.db.qualityprofile.ActiveRuleDao in project sonarqube by SonarSource.
the class RegisterQualityProfilesMediumTest method register_existing_profile_definitions.
@Test
public void register_existing_profile_definitions() {
tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
// Check Profile in DB
QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
assertThat(profile).isNotNull();
// Check ActiveRules in DB
ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
// Check in ES
assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
tester.get(Platform.class).restart();
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
// Check ActiveRules
ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
// Check in ES
assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
// TODO
// Check ActiveRuleParameters in DB
Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
// default value
assertThat(params.get("max").getValue()).isEqualTo("10");
}
use of org.sonar.db.qualityprofile.ActiveRuleDao in project sonarqube by SonarSource.
the class RuleActivator method doUpdate.
private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDao dao = db.activeRuleDao();
ActiveRuleDto activeRule = context.activeRule();
if (activeRule != null) {
String severity = change.getSeverity();
if (severity != null) {
activeRule.setSeverity(severity);
}
ActiveRule.Inheritance inheritance = change.getInheritance();
if (inheritance != null) {
activeRule.setInheritance(inheritance.name());
}
activeRule.setUpdatedAt(system2.now());
dao.update(dbSession, activeRule);
for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
ActiveRuleParamDto activeRuleParamDto = context.activeRuleParamsAsMap().get(param.getKey());
if (activeRuleParamDto == null) {
// did not exist
if (param.getValue() != null) {
activeRuleParamDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
activeRuleParamDto.setValue(param.getValue());
dao.insertParam(dbSession, activeRule, activeRuleParamDto);
}
} else {
if (param.getValue() != null) {
activeRuleParamDto.setValue(param.getValue());
dao.updateParam(dbSession, activeRule, activeRuleParamDto);
} else {
dao.deleteParam(dbSession, activeRule, activeRuleParamDto);
}
}
}
}
return activeRule;
}
use of org.sonar.db.qualityprofile.ActiveRuleDao in project sonarqube by SonarSource.
the class RuleActivator method persist.
private ActiveRuleDto persist(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDto activeRule = null;
if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
activeRule = doInsert(change, context, dbSession);
} else if (change.getType() == ActiveRuleChange.Type.DEACTIVATED) {
ActiveRuleDao dao = db.activeRuleDao();
dao.delete(dbSession, change.getKey());
} else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
activeRule = doUpdate(change, context, dbSession);
}
db.qProfileChangeDao().insert(dbSession, change.toDto(userSession.getLogin()));
return activeRule;
}
Aggregations