diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-12-24 11:30:40 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-12-24 11:30:40 +0100 |
commit | f43ee223be35c6162b9f9c4b976c8fd4c2d488b0 (patch) | |
tree | bf8aa20c986edb41069854a35a7ad3fe55da0a98 | |
parent | e1af2f331051a08ea1f5dee369847340be85ad5e (diff) | |
download | sonarqube-f43ee223be35c6162b9f9c4b976c8fd4c2d488b0.tar.gz sonarqube-f43ee223be35c6162b9f9c4b976c8fd4c2d488b0.zip |
SONAR-4535 Delete rule now use Java facade
14 files changed, 254 insertions, 90 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java index 1ab38d9a113..069b07d7cf6 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDao.java @@ -83,6 +83,40 @@ public class ActiveRuleDao implements ServerComponent { return session.getMapper(ActiveRuleMapper.class).selectParamByActiveRuleAndKey(activeRuleId, key); } + public List<ActiveRuleDto> selectByRuleId(Integer ruleId) { + SqlSession session = mybatis.openSession(); + try { + return selectByRuleId(ruleId, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<ActiveRuleDto> selectByRuleId(Integer ruleId, SqlSession session) { + return session.getMapper(ActiveRuleMapper.class).selectByRuleId(ruleId); + } + + public List<ActiveRuleDto> selectByIds(List<Integer> ids) { + SqlSession session = mybatis.openSession(); + try { + return selectByIds(ids, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + public List<ActiveRuleDto> selectByIds(Collection<Integer> ids, SqlSession session) { + if (ids.isEmpty()) { + return Collections.emptyList(); + } + List<ActiveRuleDto> dtosList = newArrayList(); + List<List<Integer>> idsPartitionList = Lists.partition(newArrayList(ids), 1000); + for (List<Integer> idsPartition : idsPartitionList) { + List<ActiveRuleDto> dtos = session.selectList("org.sonar.core.qualityprofile.db.ActiveRuleMapper.selectByIds", newArrayList(idsPartition)); + dtosList.addAll(dtos); + } + return dtosList; + } public void insert(ActiveRuleDto dto, SqlSession session) { session.getMapper(ActiveRuleMapper.class).insert(dto); @@ -112,131 +146,124 @@ public class ActiveRuleDao implements ServerComponent { } } - public void insert(ActiveRuleParamDto dto, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).insertParameter(dto); - } - - public void insert(ActiveRuleParamDto dto) { + public List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId) { SqlSession session = mybatis.openSession(); try { - insert(dto, session); - session.commit(); + return selectParamsByActiveRuleId(activeRuleId, session); } finally { MyBatis.closeQuietly(session); } } - public void update(ActiveRuleParamDto dto, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).updateParameter(dto); + public List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId, SqlSession session) { + return session.getMapper(ActiveRuleMapper.class).selectParamsByActiveRuleId(activeRuleId); } - public void update(ActiveRuleParamDto dto) { + public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(List<Integer> activeRuleIds) { SqlSession session = mybatis.openSession(); try { - update(dto, session); - session.commit(); + return selectParamsByActiveRuleIds(activeRuleIds, session); } finally { MyBatis.closeQuietly(session); } } - public void delete(Integer activeRuleId, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).delete(activeRuleId); + public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(Collection<Integer> activeRuleIds, SqlSession session) { + if (activeRuleIds.isEmpty()) { + return Collections.emptyList(); + } + List<ActiveRuleParamDto> dtosList = newArrayList(); + List<List<Integer>> idsPartitionList = Lists.partition(newArrayList(activeRuleIds), 1000); + for (List<Integer> idsPartition : idsPartitionList) { + List<ActiveRuleParamDto> dtos = session.selectList("org.sonar.core.qualityprofile.db.ActiveRuleMapper.selectParamsByActiveRuleIds", newArrayList(idsPartition)); + dtosList.addAll(dtos); + } + return dtosList; } - public void delete(Integer activeRuleId) { + public void insert(ActiveRuleParamDto dto, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).insertParameter(dto); + } + + public void insert(ActiveRuleParamDto dto) { SqlSession session = mybatis.openSession(); try { - delete(activeRuleId, session); + insert(dto, session); session.commit(); } finally { MyBatis.closeQuietly(session); } } - public void deleteParameter(Integer activeRuleParamId, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).deleteParameter(activeRuleParamId); + public void update(ActiveRuleParamDto dto, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).updateParameter(dto); } - public void deleteParameter(Integer activeRuleParamId) { + public void update(ActiveRuleParamDto dto) { SqlSession session = mybatis.openSession(); try { - deleteParameter(activeRuleParamId, session); + update(dto, session); session.commit(); } finally { MyBatis.closeQuietly(session); } } - public void deleteParameters(Integer activeRuleId, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).deleteParameters(activeRuleId); + public void delete(Integer activeRuleId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).delete(activeRuleId); } - public void deleteParameters(Integer activeRuleId) { + public void delete(Integer activeRuleId) { SqlSession session = mybatis.openSession(); try { - deleteParameters(activeRuleId, session); + delete(activeRuleId, session); session.commit(); } finally { MyBatis.closeQuietly(session); } } - public List<ActiveRuleDto> selectByIds(List<Integer> ids) { + public void deleteFromRule(Integer ruleId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).deleteFromRule(ruleId); + } + + public void deleteFromRule(Integer ruleId) { SqlSession session = mybatis.openSession(); try { - return selectByIds(ids, session); + deleteFromRule(ruleId, session); + session.commit(); } finally { MyBatis.closeQuietly(session); } } - public List<ActiveRuleDto> selectByIds(Collection<Integer> ids, SqlSession session) { - if (ids.isEmpty()) { - return Collections.emptyList(); - } - List<ActiveRuleDto> dtosList = newArrayList(); - List<List<Integer>> idsPartitionList = Lists.partition(newArrayList(ids), 1000); - for (List<Integer> idsPartition : idsPartitionList) { - List<ActiveRuleDto> dtos = session.selectList("org.sonar.core.qualityprofile.db.ActiveRuleMapper.selectByIds", newArrayList(idsPartition)); - dtosList.addAll(dtos); - } - return dtosList; + public void deleteParameter(Integer activeRuleParamId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).deleteParameter(activeRuleParamId); } - public List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId) { + public void deleteParameter(Integer activeRuleParamId) { SqlSession session = mybatis.openSession(); try { - return selectParamsByActiveRuleId(activeRuleId, session); + deleteParameter(activeRuleParamId, session); + session.commit(); } finally { MyBatis.closeQuietly(session); } } - public List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId, SqlSession session) { - return session.getMapper(ActiveRuleMapper.class).selectParamsByActiveRuleId(activeRuleId); + public void deleteParameters(Integer activeRuleId, SqlSession session) { + session.getMapper(ActiveRuleMapper.class).deleteParameters(activeRuleId); } - public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(List<Integer> activeRuleIds) { + public void deleteParameters(Integer activeRuleId) { SqlSession session = mybatis.openSession(); try { - return selectParamsByActiveRuleIds(activeRuleIds, session); + deleteParameters(activeRuleId, session); + session.commit(); } finally { MyBatis.closeQuietly(session); } } - public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(Collection<Integer> activeRuleIds, SqlSession session) { - if (activeRuleIds.isEmpty()) { - return Collections.emptyList(); - } - List<ActiveRuleParamDto> dtosList = newArrayList(); - List<List<Integer>> idsPartitionList = Lists.partition(newArrayList(activeRuleIds), 1000); - for (List<Integer> idsPartition : idsPartitionList) { - List<ActiveRuleParamDto> dtos = session.selectList("org.sonar.core.qualityprofile.db.ActiveRuleMapper.selectParamsByActiveRuleIds", newArrayList(idsPartition)); - dtosList.addAll(dtos); - } - return dtosList; - } } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java index dd142cd99bc..02d9a9b3604 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleMapper.java @@ -23,7 +23,6 @@ package org.sonar.core.qualityprofile.db; import org.apache.ibatis.annotations.Param; import javax.annotation.CheckForNull; - import java.util.List; public interface ActiveRuleMapper { @@ -34,7 +33,11 @@ public interface ActiveRuleMapper { @CheckForNull ActiveRuleDto selectByProfileAndRule(@Param("profileId") Integer profileId, @Param("ruleId") Integer ruleId); - List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId); + List<ActiveRuleDto> selectByRuleId(Integer ruleId); + + void insert(ActiveRuleDto dto); + + void update(ActiveRuleDto dto); @CheckForNull ActiveRuleParamDto selectParamById(Integer activeRuleParamId); @@ -42,9 +45,7 @@ public interface ActiveRuleMapper { @CheckForNull ActiveRuleParamDto selectParamByActiveRuleAndKey(@Param("activeRuleId") Integer activeRuleId, @Param("key") String key); - void insert(ActiveRuleDto dto); - - void update(ActiveRuleDto dto); + List<ActiveRuleParamDto> selectParamsByActiveRuleId(Integer activeRuleId); void insertParameter(ActiveRuleParamDto dto); @@ -52,6 +53,8 @@ public interface ActiveRuleMapper { void delete(Integer activeRuleId); + void deleteFromRule(Integer ruleId); + void deleteParameters(Integer activeRuleId); void deleteParameter(Integer activeRuleParamId); diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml index 66346a0f28e..d7e0e5476b5 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/ActiveRuleMapper.xml @@ -27,7 +27,7 @@ SELECT <include refid="activeRuleColumns"/> FROM active_rules a <where> - AND id=#{id} + AND a.id=#{id} </where> </select> @@ -35,7 +35,15 @@ SELECT <include refid="activeRuleColumns"/> FROM active_rules a <where> - AND profile_id=#{profileId} + AND a.profile_id=#{profileId} + AND a.rule_id=#{ruleId} + </where> + </select> + + <select id="selectByRuleId" parameterType="Integer" resultType="ActiveRule"> + SELECT <include refid="activeRuleColumns"/> + FROM active_rules a + <where> AND rule_id=#{ruleId} </where> </select> @@ -44,7 +52,7 @@ SELECT <include refid="activeRuleParamColumns"/> FROM active_rule_parameters p <where> - AND id=#{id} + AND p.id=#{id} </where> </select> @@ -52,8 +60,8 @@ SELECT <include refid="activeRuleParamColumns"/> FROM active_rule_parameters p <where> - AND active_rule_id=#{activeRuleId} - AND rules_parameter_key=#{key} + AND p.active_rule_id=#{activeRuleId} + AND p.rules_parameter_key=#{key} </where> </select> @@ -93,6 +101,10 @@ DELETE FROM active_rules WHERE id=#{id} </update> + <update id="deleteFromRule" parameterType="Integer"> + DELETE FROM active_rules WHERE rule_id=#{ruleId} + </update> + <update id="deleteParameters" parameterType="Integer"> DELETE FROM active_rule_parameters WHERE active_rule_id=#{id} </update> diff --git a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest.java index b1b9ca7b7d8..a2af9d31c67 100644 --- a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest.java @@ -26,6 +26,8 @@ import org.junit.Test; import org.sonar.api.utils.DateUtils; import org.sonar.core.persistence.AbstractDaoTestCase; +import java.util.List; + import static org.fest.assertions.Assertions.assertThat; public class ActiveRuleDaoTest extends AbstractDaoTestCase { @@ -79,6 +81,14 @@ public class ActiveRuleDaoTest extends AbstractDaoTestCase { } @Test + public void select_by_rule() { + setupData("shared"); + + List<ActiveRuleDto> result = dao.selectByRuleId(11); + assertThat(result).hasSize(2); + } + + @Test public void select_param_by_id() { setupData("shared"); @@ -192,6 +202,15 @@ public class ActiveRuleDaoTest extends AbstractDaoTestCase { } @Test + public void delete_from_rule() { + setupData("shared"); + + dao.deleteFromRule(11); + + checkTables("delete_from_rule", "active_rules"); + } + + @Test public void delete_parameters() { setupData("shared"); diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete-result.xml index 2a65aa4de98..0fea497a741 100644 --- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete-result.xml @@ -6,4 +6,7 @@ <active_rules id="2" profile_id="1" rule_id="11" failure_level="0" inheritance="[null]" note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/> + <active_rules id="3" profile_id="2" rule_id="11" failure_level="1" inheritance="[null]" + note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="henry" note_data="other note"/> + </dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_from_rule-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_from_rule-result.xml new file mode 100644 index 00000000000..448dafc06a9 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/delete_from_rule-result.xml @@ -0,0 +1,12 @@ +<dataset> + + <active_rules id="1" profile_id="1" rule_id="10" failure_level="2" inheritance="INHERITED" + note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="henry" note_data="some note"/> + + <!--<active_rules id="2" profile_id="1" rule_id="11" failure_level="0" inheritance="[null]"--> + <!--note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/>--> + + <!--<active_rules id="3" profile_id="2" rule_id="11" failure_level="1" inheritance="[null]"--> + <!--note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="henry" note_data="other note"/>--> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/shared.xml index 3d92151f292..46d4663ad4e 100644 --- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/shared.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/shared.xml @@ -6,6 +6,9 @@ <active_rules id="2" profile_id="1" rule_id="11" failure_level="0" inheritance="[null]" note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/> + <active_rules id="3" profile_id="2" rule_id="11" failure_level="1" inheritance="[null]" + note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="henry" note_data="other note"/> + <active_rule_parameters id="1" active_rule_id="1" rules_parameter_id="1" rules_parameter_key="max" value="20"/> <active_rule_parameters id="2" active_rule_id="1" rules_parameter_id="2" rules_parameter_key="format" value="html"/> diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/update-result.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/update-result.xml index 87b48ac77a3..edca28c6e60 100644 --- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/update-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/ActiveRuleDaoTest/update-result.xml @@ -6,4 +6,7 @@ <active_rules id="2" profile_id="1" rule_id="11" failure_level="0" inheritance="[null]" note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="john" note_data="other note"/> + <active_rules id="3" profile_id="2" rule_id="11" failure_level="1" inheritance="[null]" + note_created_at="2013-12-18" note_updated_at="2013-12-18" note_user_login="henry" note_data="other note"/> + </dataset> diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java index 0a64684dbdd..b8a9eed3c38 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java @@ -21,8 +21,10 @@ package org.sonar.server.qualityprofile; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; import com.google.common.base.Strings; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import org.apache.commons.lang.StringUtils; @@ -200,8 +202,8 @@ public class QProfileOperations implements ServerComponent { try { RuleInheritanceActions actions = profilesManager.deactivated(activeRule.getProfileId(), activeRule.getId(), userSession.name()); - activeRuleDao.delete(activeRule.getId(), session); activeRuleDao.deleteParameters(activeRule.getId(), session); + activeRuleDao.delete(activeRule.getId(), session); actions.addToDelete(activeRule.getId()); session.commit(); @@ -393,7 +395,7 @@ public class QProfileOperations implements ServerComponent { } public void updateRule(RuleDto rule, String name, String severity, String description, Map<String, String> paramsByKey, - UserSession userSession) { + UserSession userSession) { checkPermission(userSession); SqlSession session = myBatis.openSession(); try { @@ -416,6 +418,35 @@ public class QProfileOperations implements ServerComponent { } } + public void deleteRule(RuleDto rule, UserSession userSession) { + checkPermission(userSession); + SqlSession session = myBatis.openSession(); + try { + // Set status REMOVED on rule + rule.setStatus(Rule.STATUS_REMOVED) + .setUpdatedAt(new Date(system.now())); + ruleDao.update(rule, session); + session.commit(); + ruleRegistry.save(rule, ruleDao.selectParameters(rule.getId(), session)); + + // Delete all active rules and active rule params linked to the rule + List<ActiveRuleDto> activeRules = activeRuleDao.selectByRuleId(rule.getId()); + for (ActiveRuleDto activeRule : activeRules) { + activeRuleDao.deleteParameters(activeRule.getId(), session); + } + activeRuleDao.deleteFromRule(rule.getId(), session); + session.commit(); + ruleRegistry.deleteActiveRules(newArrayList(Iterables.transform(activeRules, new Function<ActiveRuleDto, Integer>() { + @Override + public Integer apply(ActiveRuleDto input) { + return input.getId(); + } + }))); + } finally { + MyBatis.closeQuietly(session); + } + } + private void reindexInheritanceResult(RuleInheritanceActions actions, SqlSession session) { ruleRegistry.deleteActiveRules(actions.idsToDelete()); List<ActiveRuleDto> activeRules = activeRuleDao.selectByIds(actions.idsToIndex(), session); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java index 42096d410db..49aee31fdb1 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java @@ -92,8 +92,6 @@ public class QProfiles implements ServerComponent { // active rule parameter validation (only Integer types are checked) // // TEMPLATE RULES - // create template rule - // edit template rule // delete template rule public QProfile profile(int id) { @@ -320,6 +318,18 @@ public class QProfiles implements ServerComponent { return rules.getFromRuleId(ruleId); } + public void deleteRule(int ruleId) { + RuleDto rule = findRuleNotNull(ruleId); + if (rule.getParentId() == null) { + throw new NotFoundException("Unknown rule"); + } + operations.deleteRule(rule, UserSession.get()); + } + + public int countActiveRules(QProfileRule rule){ + return activeRuleDao.selectByRuleId(rule.id()).size(); + } + // // Quality profile validation diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/new_rules_configuration_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/new_rules_configuration_controller.rb index 6b1b0abb6fa..ff31ca69110 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/new_rules_configuration_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/new_rules_configuration_controller.rb @@ -189,12 +189,15 @@ class NewRulesConfigurationController < ApplicationController # form to edit a rule require_parameters :id, :rule_id - @profile = Internal.quality_profiles.profile(params[:id].to_i) - @rule = Internal.quality_profiles.rule(params[:rule_id].to_i) - if @rule.parentId().nil? - redirect_to :action => 'index', :id => params[:id] - else - @parent_rule = Internal.quality_profiles.rule(@rule.parentId()) + call_backend do + @profile = Internal.quality_profiles.profile(params[:id].to_i) + @rule = Internal.quality_profiles.rule(params[:rule_id].to_i) + if @rule.parentId().nil? + redirect_to :action => 'index', :id => params[:id] + else + @parent_rule = Internal.quality_profiles.rule(@rule.parentId()) + @active_rules = Internal.quality_profiles.countActiveRules(@rule) + end end end @@ -229,20 +232,11 @@ class NewRulesConfigurationController < ApplicationController # def delete verify_post_request - access_denied unless has_role?(:profileadmin) require_parameters :id, :rule_id - rule=Rule.find(params[:rule_id]) - if rule.editable? - rule.status=Rule::STATUS_REMOVED - rule.save - Internal.rules.saveOrUpdate(rule.id) - - # it's mandatory to execute 'destroy_all' but not 'delete_all' because active_rule_parameters must - # also be destroyed in cascade. - ActiveRule.destroy_all("rule_id=#{rule.id}") + + call_backend do + Internal.quality_profiles.deleteRule(params[:rule_id].to_i) flash[:notice]=message('rules_configuration.rule_deleted') - else - flash[:error]=message('rules_configuration.unknown_rule') end redirect_to :action => 'index', :id => params[:id] end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/new_rules_configuration/edit.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/new_rules_configuration/edit.html.erb index 94812eb5d13..5e980933a47 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/new_rules_configuration/edit.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/new_rules_configuration/edit.html.erb @@ -1,13 +1,13 @@ -<h1 class="marginbottom10"><%= link_to message('quality_profiles.quality_profiles'), :controller => 'profiles', :action => 'index' -%> / <%= h @profile.language -%> / <%= h @profile.name %></h1> +<h1 class="marginbottom10"><%= link_to message('quality_profiles.quality_profiles'), :controller => 'profiles', :action => 'index' -%> / <%= h @profile.language() -%> / <%= h @profile.name() %></h1> <%= render :partial => 'profiles/tabs', :locals => {:new_tab => 'Edit rule'} %> <div class="tabs-panel marginbottom10 background-gray"> -<form method="POST" id="delete-form" action="<%= url_for :action => 'delete', :id => @profile.id, :rule_id => @rule.id -%>" style="display: none"></form> +<form method="POST" id="delete-form" action="<%= url_for :action => 'delete', :id => @profile.id(), :rule_id => @rule.id() -%>" style="display: none"></form> <script> function deleteRule() { - var count = <%= ActiveRule.count(:conditions => {:rule_id => @rule.id}) -%>; + var count = <%= @active_rules -%>; var message = 'Are you sure?'; if (count>0) { message += ' This rule is activated in ' + count + ' profiles.'; diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java index b1f976c282a..6a410b1c874 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java @@ -591,4 +591,30 @@ public class QProfileOperationsTest { verify(ruleRegistry).save(eq(ruleArgument.getValue()), eq(newArrayList(ruleParamArgument.getValue()))); } + @Test + public void delete_rule() throws Exception { + RuleDto rule = new RuleDto().setId(11).setRepositoryKey("squid").setRuleKey("XPath_1387869254").setConfigKey("Xpath").setUpdatedAt(DateUtils.parseDate("2013-12-23")); + RuleParamDto param = new RuleParamDto().setId(21).setName("max").setDefaultValue("20"); + when(ruleDao.selectParameters(eq(11), eq(session))).thenReturn(newArrayList(param)); + + ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(11).setSeverity(1); + when(activeRuleDao.selectByRuleId(11)).thenReturn(newArrayList(activeRule)); + + long now = System.currentTimeMillis(); + doReturn(now).when(system).now(); + + operations.deleteRule(rule, authorizedUserSession); + + ArgumentCaptor<RuleDto> ruleArgument = ArgumentCaptor.forClass(RuleDto.class); + verify(ruleDao).update(ruleArgument.capture(), eq(session)); + assertThat(ruleArgument.getValue().getStatus()).isEqualTo(Rule.STATUS_REMOVED); + assertThat(ruleArgument.getValue().getUpdatedAt()).isEqualTo(new Date(now)); + + verify(ruleRegistry).save(eq(ruleArgument.getValue()), eq(newArrayList(param))); + verify(activeRuleDao).deleteParameters(eq(5), eq(session)); + verify(activeRuleDao).deleteFromRule(eq(11), eq(session)); + verify(session, times(2)).commit(); + verify(ruleRegistry).deleteActiveRules(newArrayList(5)); + } + } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java index c2d2d960162..e31dfabf775 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java @@ -41,6 +41,7 @@ import org.sonar.server.user.UserSession; import java.util.Map; +import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -639,4 +640,24 @@ public class QProfilesTest { verifyZeroInteractions(rules); } + @Test + public void delete_rule() throws Exception { + RuleDto rule = new RuleDto().setId(11).setRepositoryKey("squid").setRuleKey("XPath_1387869254").setParentId(10); + when(ruleDao.selectById(11)).thenReturn(rule); + + qProfiles.deleteRule(11); + + verify(service).deleteRule(eq(rule), any(UserSession.class)); + } + + @Test + public void count_active_rules() throws Exception { + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + + when(activeRuleDao.selectByRuleId(10)).thenReturn(newArrayList(new ActiveRuleDto().setId(50), new ActiveRuleDto().setId(51))); + + assertThat(qProfiles.countActiveRules(rule)).isEqualTo(2); + } + } |