From: Simon Brandhof Date: Sun, 8 Jun 2014 21:53:11 +0000 (+0200) Subject: SONAR-5007 use QProfileService in Rails webapp for changing parent X-Git-Tag: 4.4-RC1~564 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ef02c1113a6cd1541ea3134ef883f21d9c1ecedc;p=sonarqube.git SONAR-5007 use QProfileService in Rails webapp for changing parent and setting default --- diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index a9755d354ca..5963360a0a1 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -176,7 +176,6 @@ import org.sonar.server.qualitygate.ws.QGatesUnsetDefaultAction; import org.sonar.server.qualitygate.ws.QGatesUpdateConditionAction; import org.sonar.server.qualitygate.ws.QGatesWs; import org.sonar.server.qualityprofile.BuiltInProfiles; -import org.sonar.server.qualityprofile.ProfilesManager; import org.sonar.server.qualityprofile.QProfileBackuper; import org.sonar.server.qualityprofile.QProfileCopier; import org.sonar.server.qualityprofile.QProfileLookup; @@ -421,7 +420,6 @@ class ServerComponents { pico.addSingleton(XMLProfileParser.class); pico.addSingleton(XMLProfileSerializer.class); pico.addComponent(ProfilesDao.class, false); - pico.addComponent(ProfilesManager.class, false); pico.addSingleton(AnnotationProfileParser.class); pico.addSingleton(QProfiles.class); pico.addSingleton(QProfileLookup.class); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ProfilesManager.java deleted file mode 100644 index b10000f8e31..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ProfilesManager.java +++ /dev/null @@ -1,374 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.server.qualityprofile; - -import com.google.common.collect.Lists; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.ActiveRuleChange; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleParam; -import org.sonar.api.rules.RulePriority; -import org.sonar.core.preview.PreviewCache; -import org.sonar.jpa.dao.BaseDao; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import java.util.List; - -/** - * @deprecated to be dropped in 4.4 - */ -@Deprecated -public class ProfilesManager extends BaseDao { - - private PreviewCache dryRunCache; - - public ProfilesManager(DatabaseSession session, PreviewCache dryRunCache) { - super(session); - this.dryRunCache = dryRunCache; - } - - // Managing inheritance of profiles - - public RuleInheritanceActions profileParentChanged(Integer profileId, @Nullable String parentName, String userName) { - RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); - RulesProfile oldParent = getParentProfile(profile); - RulesProfile newParent = getProfile(profile.getLanguage(), parentName); - - RuleInheritanceActions actions = new RuleInheritanceActions(); - // Deactivate all inherited rules from old parent - if (oldParent != null) { - for (ActiveRule activeRule : oldParent.getActiveRules()) { - actions.add(deactivate(profile, activeRule.getRule(), userName)); - } - } - // Activate all inherited rules of new parent - if (newParent != null) { - for (ActiveRule activeRule : newParent.getActiveRules()) { - actions.add(activateOrChange(profile, activeRule, userName)); - } - } - getSession().commit(); - dryRunCache.reportGlobalModification(); - return actions; - } - - /** - * Rule was activated - */ - public RuleInheritanceActions activated(int profileId, int activeRuleId, String userName) { - ActiveRule activeRule = getSession().getEntity(ActiveRule.class, activeRuleId); - RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); - ruleEnabled(profile, activeRule, userName); - // Notify child profiles - return activatedOrChanged(profileId, activeRuleId, userName); - } - - /** - * Rule param was changed - */ - public RuleInheritanceActions ruleParamChanged(int profileId, int activeRuleId, String paramKey, @Nullable String oldValue, @Nullable String newValue, String userName) { - ActiveRule activeRule = getSession().getEntity(ActiveRule.class, activeRuleId); - RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); - - ruleParamChanged(profile, activeRule.getRule(), paramKey, oldValue, newValue, userName); - - // Notify child profiles - return activatedOrChanged(profileId, activeRuleId, userName); - } - - /** - * Rule severity was changed - */ - public RuleInheritanceActions ruleSeverityChanged(int profileId, int activeRuleId, RulePriority oldSeverity, RulePriority newSeverity, String userName) { - ActiveRule activeRule = getSession().getEntity(ActiveRule.class, activeRuleId); - RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId); - - ruleSeverityChanged(profile, activeRule.getRule(), oldSeverity, newSeverity, userName); - - // Notify child profiles - return activatedOrChanged(profileId, activeRuleId, userName); - } - - /** - * Rule was activated/changed in parent profile. - */ - private RuleInheritanceActions activatedOrChanged(int parentProfileId, int activeRuleId, String userName) { - RuleInheritanceActions actions = new RuleInheritanceActions(); - ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId); - if (parentActiveRule.isInherited()) { - parentActiveRule.setInheritance(ActiveRule.OVERRIDES); - getSession().saveWithoutFlush(parentActiveRule); - } - actions.addToIndex(activeRuleId); - for (RulesProfile child : getChildren(parentProfileId)) { - actions.add(activateOrChange(child, parentActiveRule, userName)); - } - getSession().commit(); - dryRunCache.reportGlobalModification(); - return actions; - } - - /** - * Rule was deactivated in parent profile. - */ - public RuleInheritanceActions deactivated(int parentProfileId, int deactivatedRuleId, String userName) { - RuleInheritanceActions actions = new RuleInheritanceActions(); - ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, deactivatedRuleId); - RulesProfile profile = getSession().getEntity(RulesProfile.class, parentProfileId); - ruleDisabled(profile, parentActiveRule, userName); - actions.addToIndex(parentActiveRule.getId()); - for (RulesProfile child : getChildren(parentProfileId)) { - actions.add(deactivate(child, parentActiveRule.getRule(), userName)); - } - getSession().commit(); - dryRunCache.reportGlobalModification(); - return actions; - } - - private synchronized void incrementProfileVersionIfNeeded(RulesProfile profile) { - if (profile.getUsed()) { - profile.setVersion(profile.getVersion() + 1); - profile.setUsed(false); - getSession().saveWithoutFlush(profile); - } - } - - /** - * Deal with creation of ActiveRuleChange item when a rule param is changed on a profile - */ - private void ruleParamChanged(RulesProfile profile, Rule rule, String paramKey, String oldValue, String newValue, String userName) { - incrementProfileVersionIfNeeded(profile); - ActiveRuleChange rc = new ActiveRuleChange(userName, profile, rule); - if (!StringUtils.equals(oldValue, newValue)) { - rc.setParameterChange(paramKey, oldValue, newValue); - getSession().saveWithoutFlush(rc); - } - } - - /** - * Deal with creation of ActiveRuleChange item when a rule severity is changed on a profile - */ - private void ruleSeverityChanged(RulesProfile profile, Rule rule, RulePriority oldSeverity, RulePriority newSeverity, String userName) { - incrementProfileVersionIfNeeded(profile); - ActiveRuleChange rc = new ActiveRuleChange(userName, profile, rule); - if (!ObjectUtils.equals(oldSeverity, newSeverity)) { - rc.setOldSeverity(oldSeverity); - rc.setNewSeverity(newSeverity); - getSession().saveWithoutFlush(rc); - } - } - - /** - * Deal with creation of ActiveRuleChange item when a rule is changed (severity and/or param(s)) on a profile - */ - private void ruleChanged(RulesProfile profile, ActiveRule oldActiveRule, ActiveRule newActiveRule, String userName) { - incrementProfileVersionIfNeeded(profile); - ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule()); - - if (oldActiveRule.getSeverity() != newActiveRule.getSeverity()) { - rc.setOldSeverity(oldActiveRule.getSeverity()); - rc.setNewSeverity(newActiveRule.getSeverity()); - } - if (oldActiveRule.getRule().getParams() != null) { - for (RuleParam p : oldActiveRule.getRule().getParams()) { - String oldParam = oldActiveRule.getParameter(p.getKey()); - String newParam = newActiveRule.getParameter(p.getKey()); - if (!StringUtils.equals(oldParam, newParam)) { - rc.setParameterChange(p.getKey(), oldParam, newParam); - } - } - } - - getSession().saveWithoutFlush(rc); - } - - /** - * Deal with creation of ActiveRuleChange item when a rule is enabled on a profile - */ - private void ruleEnabled(RulesProfile profile, ActiveRule newActiveRule, String userName) { - incrementProfileVersionIfNeeded(profile); - ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule()); - rc.setEnabled(true); - rc.setNewSeverity(newActiveRule.getSeverity()); - if (newActiveRule.getRule().getParams() != null) { - for (RuleParam p : newActiveRule.getRule().getParams()) { - String newParam = newActiveRule.getParameter(p.getKey()); - if (newParam != null) { - rc.setParameterChange(p.getKey(), null, newParam); - } - } - } - getSession().saveWithoutFlush(rc); - } - - /** - * Deal with creation of ActiveRuleChange item when a rule is disabled on a profile - */ - private void ruleDisabled(RulesProfile profile, ActiveRule disabledRule, String userName) { - incrementProfileVersionIfNeeded(profile); - ActiveRuleChange rc = new ActiveRuleChange(userName, profile, disabledRule.getRule()); - rc.setEnabled(false); - rc.setOldSeverity(disabledRule.getSeverity()); - if (disabledRule.getRule().getParams() != null) { - for (RuleParam p : disabledRule.getRule().getParams()) { - String oldParam = disabledRule.getParameter(p.getKey()); - if (oldParam != null) { - rc.setParameterChange(p.getKey(), oldParam, null); - } - } - } - getSession().saveWithoutFlush(rc); - } - - private RuleInheritanceActions activateOrChange(RulesProfile profile, ActiveRule parentActiveRule, String userName) { - ActiveRule oldActiveRule = profile.getActiveRule(parentActiveRule.getRule()); - RuleInheritanceActions actions = new RuleInheritanceActions(); - if (oldActiveRule != null) { - if (oldActiveRule.isInherited()) { - removeActiveRule(oldActiveRule); - actions.addToDelete(oldActiveRule.getId()); - } else { - oldActiveRule.setInheritance(ActiveRule.OVERRIDES); - getSession().saveWithoutFlush(oldActiveRule); - // no need to change in children - actions.addToIndex(oldActiveRule.getId()); - return actions; - } - } - ActiveRule newActiveRule = (ActiveRule) parentActiveRule.clone(); - newActiveRule.setRulesProfile(profile); - newActiveRule.setInheritance(ActiveRule.INHERITED); - profile.addActiveRule(newActiveRule); - getSession().saveWithoutFlush(newActiveRule); - - actions.addToIndex(newActiveRule.getId()); - - if (oldActiveRule != null) { - ruleChanged(profile, oldActiveRule, newActiveRule, userName); - } else { - ruleEnabled(profile, newActiveRule, userName); - } - - for (RulesProfile child : getChildren(profile)) { - actions.add(activateOrChange(child, newActiveRule, userName)); - } - - return actions; - } - - private RuleInheritanceActions deactivate(RulesProfile profile, Rule rule, String userName) { - RuleInheritanceActions actions = new RuleInheritanceActions(); - ActiveRule activeRule = profile.getActiveRule(rule); - if (activeRule != null) { - if (activeRule.isInherited()) { - ruleDisabled(profile, activeRule, userName); - actions.addToDelete(activeRule.getId()); - removeActiveRule(activeRule); - } else { - activeRule.setInheritance(null); - getSession().saveWithoutFlush(activeRule); - actions.addToIndex(activeRule.getId()); - // no need to change in children - return actions; - } - - for (RulesProfile child : getChildren(profile)) { - actions.add(deactivate(child, rule, userName)); - } - } - return actions; - } - - private List getChildren(int parentId) { - RulesProfile parent = getSession().getEntity(RulesProfile.class, parentId); - return getChildren(parent); - } - - private List getChildren(RulesProfile parent) { - return getSession().getResults(RulesProfile.class, - "language", parent.getLanguage(), - "parentName", parent.getName()); - } - - private void removeActiveRule(ActiveRule activeRule) { - org.sonar.api.profiles.RulesProfile profile = activeRule.getRulesProfile(); - profile.removeActiveRule(activeRule); - getSession().removeWithoutFlush(activeRule); - } - - @CheckForNull - RulesProfile getProfile(String language, @Nullable String name) { - return getSession().getSingleResult(RulesProfile.class, - "language", language, - "name", name); - } - - @CheckForNull - RulesProfile getParentProfile(RulesProfile profile) { - if (profile.getParentName() == null) { - return null; - } - return getProfile(profile.getLanguage(), profile.getParentName()); - } - - /** - * Track changes made to active rules through profile inheritance that impact ES index - */ - public static class RuleInheritanceActions { - - private List idsToIndex; - private List idsToDelete; - - public RuleInheritanceActions() { - idsToIndex = Lists.newArrayList(); - idsToDelete = Lists.newArrayList(); - } - - public RuleInheritanceActions add(RuleInheritanceActions actions) { - idsToIndex.addAll(actions.idsToIndex); - idsToDelete.addAll(actions.idsToDelete); - return this; - } - - public RuleInheritanceActions addToIndex(Integer activeRuleId) { - idsToIndex.add(activeRuleId); - return this; - } - - public RuleInheritanceActions addToDelete(Integer activeRuleId) { - idsToDelete.add(activeRuleId); - return this; - } - - public List idsToIndex() { - return idsToIndex; - } - - public List idsToDelete() { - return idsToDelete; - } - } - -} 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 67622d72769..4cacf688da4 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 @@ -20,21 +20,18 @@ package org.sonar.server.qualityprofile; -import com.google.common.annotations.VisibleForTesting; import org.sonar.api.ServerComponent; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.core.preview.PreviewCache; import org.sonar.core.properties.PropertiesDao; -import org.sonar.core.properties.PropertyDto; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.qualityprofile.db.ActiveRuleDao; import org.sonar.server.user.UserSession; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.util.List; import java.util.Map; @@ -50,11 +47,9 @@ public class QProfileOperations implements ServerComponent { private final QProfileRepositoryExporter exporter; private final PreviewCache dryRunCache; private final QProfileLookup profileLookup; - private final ProfilesManager profilesManager; public QProfileOperations(MyBatis myBatis, QualityProfileDao dao, ActiveRuleDao activeRuleDao, PropertiesDao propertiesDao, - QProfileRepositoryExporter exporter, PreviewCache dryRunCache, QProfileLookup profileLookup, - ProfilesManager profilesManager) { + QProfileRepositoryExporter exporter, PreviewCache dryRunCache, QProfileLookup profileLookup) { this.myBatis = myBatis; this.dao = dao; this.activeRuleDao = activeRuleDao; @@ -62,7 +57,6 @@ public class QProfileOperations implements ServerComponent { this.exporter = exporter; this.dryRunCache = dryRunCache; this.profileLookup = profileLookup; - this.profilesManager = profilesManager; } public QProfileResult newProfile(String name, String language, Map xmlProfilesByPlugin, UserSession userSession) { @@ -153,64 +147,6 @@ public class QProfileOperations implements ServerComponent { dryRunCache.reportGlobalModification(session); } - public void setDefaultProfile(int profileId, UserSession userSession) { - checkPermission(userSession); - DbSession session = myBatis.openSession(false); - try { - QualityProfileDto qualityProfile = findNotNull(profileId, session); - propertiesDao.setProperty(new PropertyDto().setKey(PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName())); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } - - public void updateParentProfile(int profileId, @Nullable Integer parentId, UserSession userSession) { - checkPermission(userSession); - DbSession session = myBatis.openSession(false); - try { - QualityProfileDto profile = findNotNull(profileId, session); - QualityProfileDto parentProfile = null; - if (parentId != null) { - parentProfile = findNotNull(parentId, session); - } - if (isCycle(profile, parentProfile, session)) { - throw new BadRequestException("Please do not select a child profile as parent."); - } - String newParentName = parentProfile != null ? parentProfile.getName() : null; - // Modification of inheritance has to be done before setting new parent name in order to be able to disable rules from old parent - ProfilesManager.RuleInheritanceActions actions = profilesManager.profileParentChanged(profile.getId(), newParentName, userSession.name()); - profile.setParent(newParentName); - dao.update(session, profile); - session.commit(); - - //esActiveRule.deleteActiveRules(actions.idsToDelete()); - //esActiveRule.bulkIndexActiveRuleIds(actions.idsToIndex(), session); - } finally { - MyBatis.closeQuietly(session); - } - } - - @VisibleForTesting - boolean isCycle(QualityProfileDto childProfile, @Nullable QualityProfileDto parentProfile, DbSession session) { - QualityProfileDto currentParent = parentProfile; - while (currentParent != null) { - if (childProfile.getName().equals(currentParent.getName())) { - return true; - } - currentParent = getParent(currentParent, session); - } - return false; - } - - @CheckForNull - private QualityProfileDto getParent(QualityProfileDto profile, DbSession session) { - if (profile.getParent() != null) { - return dao.selectParent(profile.getId(), session); - } - return null; - } - private void checkPermission(UserSession userSession) { userSession.checkLoggedIn(); userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN); 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 17b8cc5c3d1..22fbfe298be 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 @@ -28,7 +28,6 @@ import org.sonar.server.user.UserSession; import org.sonar.server.util.Validation; import javax.annotation.CheckForNull; -import javax.annotation.Nullable; import java.util.List; import java.util.Map; @@ -89,10 +88,6 @@ public class QProfiles implements ServerComponent { operations.renameProfile(profileId, newName, UserSession.get()); } - public void setDefaultProfile(int profileId) { - operations.setDefaultProfile(profileId, UserSession.get()); - } - @CheckForNull public QProfile parent(QProfile profile) { return profileLookup.parent(profile); @@ -106,10 +101,6 @@ public class QProfiles implements ServerComponent { return profileLookup.ancestors(profile); } - public void updateParentProfile(int profileId, @Nullable Integer parentId) { - operations.updateParentProfile(profileId, parentId, UserSession.get()); - } - public void deleteProfile(int profileId) { operations.deleteProfile(profileId, UserSession.get()); } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb index 7a44ddd906a..329de8a6eb3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb @@ -98,7 +98,8 @@ class Api::ProfilesController < Api::ApiController verify_post_request profile = Internal.quality_profiles.profile(params[:name], params[:language]) not_found('Profile not found') unless profile - Internal.quality_profiles.setDefaultProfile(profile.id) + profile_key=Java::OrgSonarCoreQualityprofileDb::QualityProfileKey.of(profile.name, profile.language) + Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).setDefault(profile_key) render_success end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb index 9ca213b79eb..af688a1cbda 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb @@ -103,8 +103,9 @@ class ProfilesController < ApplicationController verify_post_request require_parameters 'id' + profile_key = profile_id_to_key(params[:id].to_i) call_backend do - Internal.quality_profiles.setDefaultProfile(params[:id].to_i) + Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).setDefault(profile_key) end redirect_to :action => 'index' end @@ -223,12 +224,12 @@ class ProfilesController < ApplicationController access_denied unless has_role?(:profileadmin) require_parameters 'id' - id = params[:id].to_i - parent_id = params[:parent_id].to_i unless params[:parent_id].empty? + profile_key = profile_id_to_key(params[:id].to_i) + parent_key = profile_id_to_key(params[:parent_id].to_i) unless params[:parent_id].empty? call_backend do - Internal.quality_profiles.updateParentProfile(id, parent_id) + Internal.component(Java::OrgSonarServerQualityprofile::QProfileService.java_class).setParent(profile_key, parent_key) end - redirect_to :action => 'inheritance', :id => id + redirect_to :action => 'inheritance', :id => params[:id] end # GET /profiles/changelog?id= diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/InheritedProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/InheritedProfilesTest.java deleted file mode 100644 index 0417d57c4e4..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/InheritedProfilesTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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.server.qualityprofile; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.core.preview.PreviewCache; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class InheritedProfilesTest extends AbstractDbUnitTestCase { - ProfilesManager profilesManager; - - @Before - public void setUp() { - profilesManager = new ProfilesManager(getSession(), mock(PreviewCache.class)); - } - - @Test - public void shouldSetParent() { - setupData("shouldSetParent"); - profilesManager.profileParentChanged(2, "parent", "admin"); - checkTables("shouldSetParent", "active_rules"); - } - - @Test - public void shouldChangeParent() { - setupData("shouldChangeParent"); - profilesManager.profileParentChanged(3, "new_parent", "admin"); - checkTables("shouldChangeParent", "active_rules"); - } - - @Test - public void shouldRemoveParent() { - setupData("shouldRemoveParent"); - profilesManager.profileParentChanged(2, null, "admin"); - checkTables("shouldRemoveParent", "active_rules"); - } - - @Test - public void shouldDeactivateInChildren() { - setupData("shouldDeactivateInChildren"); - ProfilesManager.RuleInheritanceActions actions = profilesManager.deactivated(1, 1, "admin"); - checkTables("shouldDeactivateInChildren", "active_rules", "rules_profiles"); - assertThat(actions.idsToIndex()).containsOnly(1); - assertThat(actions.idsToDelete()).containsOnly(2); - } - - @Test - public void shouldNotDeactivateOverridingChildren() { - setupData("shouldNotDeactivateOverridingChildren"); - ProfilesManager.RuleInheritanceActions actions = profilesManager.deactivated(1, 1, "admin"); - checkTables("shouldNotDeactivateOverridingChildren", "active_rules", "rules_profiles"); - assertThat(actions.idsToIndex()).containsOnly(1, 2); - assertThat(actions.idsToDelete()).isEmpty(); - } - - @Test - public void shouldActivateInChildren() { - setupData("shouldActivateInChildren"); - ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(1, 1, "admin"); - checkTables("shouldActivateInChildren", "active_rules", "rules_profiles", "active_rule_parameters"); - assertThat(actions.idsToIndex()).containsOnly(1, 2); - assertThat(actions.idsToDelete()).isEmpty(); - } - -} 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 78f212a48d2..93f49317afb 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 @@ -54,13 +54,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class QProfileOperationsTest { @@ -86,9 +80,6 @@ public class QProfileOperationsTest { @Mock QProfileLookup profileLookup; - @Mock - ProfilesManager profilesManager; - @Mock QProfileRepositoryExporter exporter; @@ -121,7 +112,7 @@ public class QProfileOperationsTest { } }).when(qualityProfileDao).insert(any(DbSession.class), any(QualityProfileDto.class)); - operations = new QProfileOperations(myBatis, qualityProfileDao, activeRuleDao, propertiesDao, exporter, dryRunCache, profileLookup, profilesManager); + operations = new QProfileOperations(myBatis, qualityProfileDao, activeRuleDao, propertiesDao, exporter, dryRunCache, profileLookup); } @Test @@ -228,121 +219,6 @@ public class QProfileOperationsTest { verify(session).commit(); } - @Test - public void update_default_profile() throws Exception { - when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java")); - - operations.setDefaultProfile(1, authorizedUserSession); - - ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(PropertyDto.class); - verify(propertiesDao).setProperty(argumentCaptor.capture()); - assertThat(argumentCaptor.getValue().getKey()).isEqualTo("sonar.profile.java"); - assertThat(argumentCaptor.getValue().getValue()).isEqualTo("Default"); - } - - @Test - public void update_parent_profile() { - QualityProfileDto oldParent = new QualityProfileDto().setId(2).setName("Old Parent").setLanguage("java"); - when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Child").setLanguage("java").setParent("Old Parent")); - when(qualityProfileDao.selectById(3, session)).thenReturn(new QualityProfileDto().setId(3).setName("Parent").setLanguage("java")); - - when(qualityProfileDao.selectParent(2, session)).thenReturn(oldParent); - when(profilesManager.profileParentChanged(anyInt(), anyString(), anyString())).thenReturn(new ProfilesManager.RuleInheritanceActions()); - - operations.updateParentProfile(1, 3, authorizedUserSession); - ArgumentCaptor profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class); - verify(qualityProfileDao).update(eq(session), profileArgument.capture()); - assertThat(profileArgument.getValue().getParent()).isEqualTo("Parent"); - assertThat(profileArgument.getValue().getLanguage()).isEqualTo("java"); - - verify(session).commit(); - verify(profilesManager).profileParentChanged(1, "Parent", "Nicolas"); - } - - @Test - public void set_parent_profile() { - when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Child").setLanguage("java").setParent(null)); - when(qualityProfileDao.selectById(2, session)).thenReturn(new QualityProfileDto().setId(2).setName("Parent").setLanguage("java")); - - when(profilesManager.profileParentChanged(anyInt(), anyString(), anyString())).thenReturn(new ProfilesManager.RuleInheritanceActions()); - - operations.updateParentProfile(1, 2, authorizedUserSession); - - ArgumentCaptor profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class); - verify(qualityProfileDao).update(eq(session), profileArgument.capture()); - assertThat(profileArgument.getValue().getParent()).isEqualTo("Parent"); - assertThat(profileArgument.getValue().getLanguage()).isEqualTo("java"); - - verify(session).commit(); - verify(profilesManager).profileParentChanged(1, "Parent", "Nicolas"); - } - - @Test - public void remove_parent_profile() { - QualityProfileDto parent = new QualityProfileDto().setId(2).setName("Old Parent").setLanguage("java"); - when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Child").setLanguage("java").setParent("Old Parent")); - - when(qualityProfileDao.selectParent(2, session)).thenReturn(parent); - when(profilesManager.profileParentChanged(anyInt(), anyString(), anyString())).thenReturn(new ProfilesManager.RuleInheritanceActions()); - - operations.updateParentProfile(1, null, authorizedUserSession); - - ArgumentCaptor profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class); - verify(qualityProfileDao).update(eq(session), profileArgument.capture()); - assertThat(profileArgument.getValue().getParent()).isNull(); - assertThat(profileArgument.getValue().getLanguage()).isEqualTo("java"); - - verify(session).commit(); - verify(profilesManager).profileParentChanged(1, null, "Nicolas"); - } - - @Test - public void fail_to_update_parent_if_not_exists() throws Exception { - when(qualityProfileDao.selectById(1, session)).thenReturn(null); - when(qualityProfileDao.selectById(3, session)).thenReturn(new QualityProfileDto().setId(3).setName("Parent").setLanguage("java")); - - try { - operations.updateParentProfile(1, 3, authorizedUserSession); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class); - } - } - - @Test - public void fail_to_update_parent_on_cycle() { - when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Child").setLanguage("java").setParent("parent")); - when(qualityProfileDao.selectById(2, session)).thenReturn(new QualityProfileDto().setId(2).setName("Parent").setLanguage("java")); - - QualityProfileDto parent = new QualityProfileDto().setId(2).setName("Parent").setLanguage("java"); - when(qualityProfileDao.selectParent(1, session)).thenReturn(parent); - when(qualityProfileDao.selectParent(2, session)).thenReturn(null); - try { - operations.updateParentProfile(2, 1, authorizedUserSession); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("Please do not select a child profile as parent."); - } - } - - @Test - public void detect_cycle() { - QualityProfileDto level1 = new QualityProfileDto().setId(1).setName("level1").setLanguage("java"); - QualityProfileDto level2 = new QualityProfileDto().setId(2).setName("level2").setLanguage("java").setParent("level1"); - QualityProfileDto level3 = new QualityProfileDto().setId(3).setName("level3").setLanguage("java").setParent("level2"); - - when(qualityProfileDao.selectParent(1, session)).thenReturn(null); - when(qualityProfileDao.selectParent(2, session)).thenReturn(level1); - when(qualityProfileDao.selectParent(3, session)).thenReturn(level2); - - assertThat(operations.isCycle(level3, level1, session)).isFalse(); - assertThat(operations.isCycle(level1, level1, session)).isTrue(); - assertThat(operations.isCycle(level1, level1, session)).isTrue(); - assertThat(operations.isCycle(level1, level3, session)).isTrue(); - assertThat(operations.isCycle(level1, level2, session)).isTrue(); - assertThat(operations.isCycle(level2, level3, session)).isTrue(); - } - @Test public void delete_profile() { when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java")); 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 3cbc77f3c4b..805f47a0677 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 @@ -147,18 +147,6 @@ public class QProfilesTest { verify(profileOperations).deleteProfile(eq(1), any(UserSession.class)); } - @Test - public void update_default_profile() throws Exception { - qProfiles.setDefaultProfile(1); - verify(profileOperations).setDefaultProfile(eq(1), any(UserSession.class)); - } - - @Test - public void update_parent_profile() throws Exception { - qProfiles.updateParentProfile(1, 2); - verify(profileOperations).updateParentProfile(eq(1), eq(2), any(UserSession.class)); - } - @Test public void projects() throws Exception { qProfiles.projects(1);