From d1e213c6f713cd38c9c7b06fb222c725c821f6c5 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 16 May 2016 11:21:08 +0200 Subject: [PATCH] SONAR-7400 Replace ActiveRuleDao#selectByRule by selectByRuleId --- .../org/sonar/server/qualityprofile/RuleActivator.java | 2 +- .../main/java/org/sonar/server/rule/RegisterRules.java | 4 ++-- .../main/java/org/sonar/server/rule/RuleUpdater.java | 2 +- .../org/sonar/server/rule/ws/ActiveRuleCompleter.java | 2 +- .../qualityprofile/ws/QProfilesWsMediumTest.java | 2 +- .../org/sonar/db/qualityprofile/ActiveRuleDao.java | 10 ++++------ .../org/sonar/db/qualityprofile/ActiveRuleDaoTest.java | 8 ++++---- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index 6c2b8422e01..4e1bc6ae9bd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -335,7 +335,7 @@ public class RuleActivator { */ public List deactivate(DbSession dbSession, RuleDto ruleDto) { List changes = Lists.newArrayList(); - List activeRules = db.activeRuleDao().selectByRule(dbSession, ruleDto); + List activeRules = db.activeRuleDao().selectByRuleId(dbSession, ruleDto.getId()); for (ActiveRuleDto activeRule : activeRules) { changes.addAll(deactivate(dbSession, activeRule.getKey(), true)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index 36ad359472e..dd8cd06cc9d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -290,7 +290,7 @@ public class RegisterRules implements Startable { for (RuleParamDto paramDto : paramDtos) { RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName()); if (paramDef == null) { - dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName()); + dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule.getId(), paramDto.getName()); dbClient.ruleDao().deleteRuleParam(session, paramDto.getId()); } else { if (mergeParam(paramDto, paramDef)) { @@ -312,7 +312,7 @@ public class RegisterRules implements Startable { dbClient.ruleDao().insertRuleParam(session, rule, paramDto); if (!StringUtils.isEmpty(param.defaultValue())) { // Propagate the default value to existing active rule parameters - for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRule(session, rule)) { + for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) { ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue()); dbClient.activeRuleDao().insertParam(session, activeRule, activeParam); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java index a031895eadf..fd0ef2727a9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java @@ -252,7 +252,7 @@ public class RuleUpdater { } private Multimap getActiveRuleParamsByActiveRule(DbSession dbSession, RuleDto customRule) { - List activeRuleDtos = dbClient.activeRuleDao().selectByRule(dbSession, customRule); + List activeRuleDtos = dbClient.activeRuleDao().selectByRuleId(dbSession, customRule.getId()); Map activeRuleById = from(activeRuleDtos).uniqueIndex(ActiveRuleDtoToId.INSTANCE); List activeRuleIds = from(activeRuleDtos) .transform(ActiveRuleDtoToId.INSTANCE) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java index e8c26af0b45..1a2a3122825 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java @@ -143,7 +143,7 @@ public class ActiveRuleCompleter { } void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) { - List activeRuleDtos = dbClient.activeRuleDao().selectByRule(dbSession, rule); + List activeRuleDtos = dbClient.activeRuleDao().selectByRuleId(dbSession, rule.getId()); Map activeRuleIdsByKey = new HashMap<>(); for (ActiveRuleDto activeRuleDto : activeRuleDtos) { activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java index 5a591744369..36bd0c012f2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java @@ -375,7 +375,7 @@ public class QProfilesWsMediumTest { session.commit(); // 2. Assert ActiveRule with MINOR severity - assertThat(tester.get(ActiveRuleDao.class).selectByRule(session, rule0).get(0).getSeverityString()).isEqualTo("MINOR"); + assertThat(tester.get(ActiveRuleDao.class).selectByRuleId(session, rule0.getId()).get(0).getSeverityString()).isEqualTo("MINOR"); assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery() .setQProfileKey(profile.getKey()) .setKey(rule0.getKey().toString()) diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java index 07bf2d770d6..a80c4423151 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java @@ -29,7 +29,6 @@ import org.sonar.db.Dao; import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; -import org.sonar.db.rule.RuleDto; public class ActiveRuleDao implements Dao { @@ -59,9 +58,8 @@ public class ActiveRuleDao implements Dao { return DatabaseUtils.executeLargeInputs(keys, new KeyToDto(mapper(dbSession))); } - public List selectByRule(DbSession dbSession, RuleDto rule) { - Preconditions.checkNotNull(rule.getId(), RULE_IS_NOT_PERSISTED); - return mapper(dbSession).selectByRuleId(rule.getId()); + public List selectByRuleId(DbSession dbSession, int ruleId) { + return mapper(dbSession).selectByRuleId(ruleId); } public List selectByRuleIds(DbSession dbSession, List ids) { @@ -168,8 +166,8 @@ public class ActiveRuleDao implements Dao { } } - public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) { - List activeRules = selectByRule(dbSession, rule); + public void deleteParamsByRuleParam(DbSession dbSession, int ruleId, String paramKey) { + List activeRules = selectByRuleId(dbSession, ruleId); for (ActiveRuleDto activeRule : activeRules) { for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleId(dbSession, activeRule.getId())) { if (activeParam.getKey().equals(paramKey)) { diff --git a/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java index 9567f8ecaa9..a7b09360e8c 100644 --- a/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java @@ -153,8 +153,8 @@ public class ActiveRuleDaoTest { underTest.insert(dbTester.getSession(), activeRule2); dbSession.commit(); - assertThat(underTest.selectByRule(dbSession, rule1)).extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey()); - assertThat(underTest.selectByRule(dbSession, rule3)).isEmpty(); + assertThat(underTest.selectByRuleId(dbSession, rule1.getId())).extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey()); + assertThat(underTest.selectByRuleId(dbSession, rule3.getId())).isEmpty(); } @Test @@ -512,7 +512,7 @@ public class ActiveRuleDaoTest { dbSession.commit(); - underTest.deleteParamsByRuleParam(dbSession, rule1, rule1Param1.getName()); + underTest.deleteParamsByRuleParam(dbSession, rule1.getId(), rule1Param1.getName()); dbSession.commit(); assertThat(underTest.selectParamByKeyAndName(activeRule1.getKey(), activeRuleParam1.getKey(), dbSession)).isNull(); @@ -533,6 +533,6 @@ public class ActiveRuleDaoTest { dbSession.commit(); - underTest.deleteParamsByRuleParam(dbSession, rule1, "unknown"); + underTest.deleteParamsByRuleParam(dbSession, rule1.getId(), "unknown"); } } -- 2.39.5