diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-03-04 11:03:37 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-03-04 14:57:45 +0100 |
commit | 2593c337e721282228699637692c6a80fd2871e7 (patch) | |
tree | f7dd0e7ece2a1aab4677575532038fc9f73f4162 /sonar-db | |
parent | 1b239a703582f3f95fcb220dbb9f13b25a00811e (diff) | |
download | sonarqube-2593c337e721282228699637692c6a80fd2871e7.tar.gz sonarqube-2593c337e721282228699637692c6a80fd2871e7.zip |
SONAR-7330 When searching active rules by profile, ignore active rule on removed rules
Diffstat (limited to 'sonar-db')
5 files changed, 82 insertions, 32 deletions
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 843ff2faee3..287161c5667 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 @@ -73,6 +73,9 @@ public class ActiveRuleDao implements Dao { return mapper(dbSession).selectAll(); } + /** + * Active rule on removed rule are NOT returned + */ public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) { return mapper(session).selectByProfileKey(profileKey); } diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDtoFunctions.java new file mode 100644 index 00000000000..a7830fbe10f --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDtoFunctions.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.qualityprofile; + +import com.google.common.base.Function; +import javax.annotation.Nonnull; + +public class ActiveRuleDtoFunctions { + + private ActiveRuleDtoFunctions() { + // Only static methods + } + + public enum ActiveRuleDtoToId implements Function<ActiveRuleDto, Integer> { + INSTANCE; + + @Override + public Integer apply(@Nonnull ActiveRuleDto input) { + return input.getId(); + } + } + + public enum ActiveRuleParamDtoToActiveRuleId implements Function<ActiveRuleParamDto, Integer> { + INSTANCE; + + @Override + public Integer apply(@Nonnull ActiveRuleParamDto input) { + return input.getActiveRuleId(); + } + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java index e7a08a978b2..2d5ca3379c1 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java @@ -31,9 +31,6 @@ public interface ActiveRuleMapper { void delete(int activeRuleId); - @CheckForNull - ActiveRuleDto selectById(Integer id); - ActiveRuleDto selectByKey(@Param("profileKey") String profileKey, @Param("repository") String repository, @Param("rule") String rule); List<ActiveRuleDto> selectByKeys(@Param("keys") List<ActiveRuleKey> keys); diff --git a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 1c9eb67c767..139724abb63 100644 --- a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -21,23 +21,6 @@ INNER JOIN rules r ON r.id = a.rule_id </sql> - <!-- Should be removed when ActiveRuleDao v2 will be removed --> - <sql id="activeRuleColumns"> - a.id, - a.profile_id as profileId, - a.rule_id as ruleId, - a.failure_level as severity, - a.inheritance as inheritance, - a.created_at as "createdAt", - a.updated_at as "updatedAt" - </sql> - - <!-- Should be removed when ActiveRuleDao v2 will be removed --> - <sql id="activeRuleJoin"> - INNER JOIN rules_profiles qp ON qp.id=a.profile_id - LEFT JOIN rules_profiles profile_parent ON profile_parent.kee=qp.parent_kee - </sql> - <insert id="insert" parameterType="ActiveRule" keyColumn="id" useGeneratedKeys="true" keyProperty="id"> INSERT INTO active_rules (profile_id, rule_id, failure_level, inheritance, created_at, updated_at) VALUES (#{profileId}, #{ruleId}, #{severity}, #{inheritance}, #{createdAt}, #{updatedAt}) @@ -57,14 +40,6 @@ DELETE FROM active_rules WHERE id=#{id} </update> - <select id="selectById" parameterType="int" resultType="ActiveRule"> - SELECT - <include refid="activeRuleColumns"/> - FROM active_rules a - <include refid="activeRuleJoin"/> - WHERE a.id=#{id} - </select> - <select id="selectByKey" parameterType="map" resultType="ActiveRule"> SELECT <include refid="activeRuleKeyColumns"/> @@ -94,7 +69,8 @@ SELECT <include refid="activeRuleKeyColumns"/> FROM active_rules a - <include refid="activeRuleKeyJoin"/> + INNER JOIN rules_profiles qp ON qp.id=a.profile_id + INNER JOIN rules r ON r.id = a.rule_id AND r.status != 'REMOVED' where qp.kee=#{id} </select> @@ -120,9 +96,9 @@ <select id="selectAll" parameterType="map" resultType="ActiveRule"> select - <include refid="activeRuleColumns"/> + <include refid="activeRuleKeyColumns"/> from active_rules a - <include refid="activeRuleJoin"/> + <include refid="activeRuleKeyJoin"/> </select> <!-- Parameters --> @@ -173,7 +149,7 @@ <where> <foreach collection="ids" item="id" open="(" separator=" or " close=")"> p.active_rule_id=#{id} - </foreach> + </foreach> </where> </select> 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 bf20c99f5b0..8389519dc81 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 @@ -23,6 +23,8 @@ import java.util.Collections; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; import org.sonar.api.utils.System2; import org.sonar.db.DbClient; @@ -84,6 +86,29 @@ public class ActiveRuleDaoTest { } @Test + public void select_by_profile() throws Exception { + ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(QPROFILE_1, RULE_1).setSeverity(Severity.BLOCKER); + ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(QPROFILE_1, RULE_2).setSeverity(Severity.BLOCKER); + underTest.insert(dbTester.getSession(), activeRule1); + underTest.insert(dbTester.getSession(), activeRule2); + dbSession.commit(); + + assertThat(underTest.selectByProfileKey(dbSession, QPROFILE_1.getKey())).hasSize(2); + assertThat(underTest.selectByProfileKey(dbSession, QPROFILE_2.getKey())).isEmpty(); + } + + @Test + public void select_by_profile_ignore_removed_rules() throws Exception { + RuleDto removedRule = RuleTesting.newDto(RuleKey.of("removed", "rule")).setStatus(RuleStatus.REMOVED); + dbClient.ruleDao().insert(dbTester.getSession(), removedRule); + ActiveRuleDto activeRule = ActiveRuleDto.createFor(QPROFILE_1, removedRule).setSeverity(Severity.BLOCKER); + underTest.insert(dbTester.getSession(), activeRule); + dbSession.commit(); + + assertThat(underTest.selectByProfileKey(dbSession, QPROFILE_1.getKey())).isEmpty(); + } + + @Test public void select_by_rule_ids() { ActiveRuleDto activeRule1 = ActiveRuleDto.createFor(QPROFILE_1, RULE_1).setSeverity(Severity.BLOCKER); ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(QPROFILE_1, RULE_2).setSeverity(Severity.BLOCKER); |