From a144c0d4882cafcca24b5d58c6ca878d2b0d3f21 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 14 Jan 2014 09:42:55 +0100 Subject: [PATCH] SONAR-4923 Move some actions from QProfiles to QProfileOperations --- .../QProfileActiveRuleOperations.java | 133 ++++++++++---- .../server/qualityprofile/QProfileLookup.java | 8 - .../qualityprofile/QProfileOperations.java | 5 +- .../qualityprofile/QProfileValidations.java | 52 ++++++ .../server/qualityprofile/QProfiles.java | 129 ++------------ .../org/sonar/server/rule/ProfileRules.java | 2 +- .../QProfileActiveRuleOperationsTest.java | 133 ++++++++++---- .../server/qualityprofile/QProfilesTest.java | 167 +----------------- 8 files changed, 281 insertions(+), 348 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileValidations.java diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java index 1aef41d8f35..d38c8cc40ba 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperations.java @@ -36,16 +36,16 @@ import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; -import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.rule.RuleDao; -import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.configuration.ProfilesManager; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.rule.ProfileRules; import org.sonar.server.rule.RuleRegistry; import org.sonar.server.user.UserSession; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.util.Date; @@ -83,30 +83,31 @@ public class QProfileActiveRuleOperations implements ServerComponent { this.system = system; } -// public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) { -// QProfile profile = profileLookup.profile(profileId); -// RuleDto rule = findRuleNotNull(ruleId); -// ActiveRuleDto activeRule = findActiveRule(qualityProfile, rule); -// if (activeRule == null) { -// activeRule = activeRuleOperations.createActiveRule(qualityProfile, rule, severity, UserSession.get()); -// } else { -// activeRuleOperations.updateSeverity(activeRule, severity, UserSession.get()); -// } -// return activeRuleChanged(qualityProfile, activeRule); -// } - - public ActiveRuleDto createActiveRule(QualityProfileDto qualityProfile, RuleDto rule, String severity, UserSession userSession) { + public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity, UserSession userSession) { validatePermission(userSession); validateSeverity(severity); + + QProfile profile = findProfileNotNull(profileId); + QProfileRule rule = findRuleNotNull(ruleId); + ActiveRuleDto activeRule = findActiveRule(profileId, ruleId); + if (activeRule == null) { + activeRule = createActiveRule(profile, rule, severity, userSession); + } else { + updateSeverity(activeRule, severity, userSession); + } + return activeRuleChanged(profile, activeRule.getId()); + } + + private ActiveRuleDto createActiveRule(QProfile profile, QProfileRule rule, String severity, UserSession userSession) { SqlSession session = myBatis.openSession(); try { ActiveRuleDto activeRule = new ActiveRuleDto() - .setProfileId(qualityProfile.getId()) - .setRuleId(rule.getId()) + .setProfileId(profile.id()) + .setRuleId(rule.id()) .setSeverity(getSeverityOrdinal(severity)); activeRuleDao.insert(activeRule, session); - List ruleParams = ruleDao.selectParameters(rule.getId(), session); + List ruleParams = ruleDao.selectParameters(rule.id(), session); List activeRuleParams = newArrayList(); for (RuleParamDto ruleParam : ruleParams) { ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() @@ -119,7 +120,7 @@ public class QProfileActiveRuleOperations implements ServerComponent { } session.commit(); - ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(qualityProfile.getId(), activeRule.getId(), userSession.name()); + ProfilesManager.RuleInheritanceActions actions = profilesManager.activated(profile.id(), activeRule.getId(), userSession.name()); reindexInheritanceResult(actions, session); return activeRule; @@ -128,9 +129,7 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void updateSeverity(ActiveRuleDto activeRule, String newSeverity, UserSession userSession) { - validatePermission(userSession); - validateSeverity(newSeverity); + private void updateSeverity(ActiveRuleDto activeRule, String newSeverity, UserSession userSession) { SqlSession session = myBatis.openSession(); try { Integer oldSeverity = activeRule.getSeverity(); @@ -144,9 +143,16 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void deactivateRule(ActiveRuleDto activeRule, UserSession userSession) { + public ProfileRuleChanged deactivateRule(int profileId, int ruleId, UserSession userSession) { validatePermission(userSession); + QProfile profile = findProfileNotNull(profileId); + ActiveRuleDto activeRule = findActiveRule(profileId, ruleId); + deactivateRule(activeRule, userSession); + return ruleChanged(profile, ruleId); + } + + private void deactivateRule(ActiveRuleDto activeRule, UserSession userSession) { SqlSession session = myBatis.openSession(); try { ProfilesManager.RuleInheritanceActions actions = profilesManager.deactivated(activeRule.getProfileId(), activeRule.getId(), userSession.name()); @@ -162,9 +168,25 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void createActiveRuleParam(ActiveRuleDto activeRule, String key, String value, UserSession userSession) { + public ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value, UserSession userSession) { validatePermission(userSession); + String sanitizedValue = Strings.emptyToNull(value); + QProfile profile = findProfileNotNull(profileId); + ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key); + ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId); + if (activeRuleParam == null && sanitizedValue != null) { + createActiveRuleParam(activeRule, key, value, userSession); + } else if (activeRuleParam != null && sanitizedValue == null) { + deleteActiveRuleParam(activeRule, activeRuleParam, userSession); + } else if (activeRuleParam != null) { + updateActiveRuleParam(activeRule, activeRuleParam, value, userSession); + } + // If no active rule param and no value -> do nothing + return activeRuleChanged(profile, activeRuleId); + } + + private void createActiveRuleParam(ActiveRuleDto activeRule, String key, String value, UserSession userSession) { SqlSession session = myBatis.openSession(); try { RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), key, session); @@ -180,9 +202,7 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void deleteActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, UserSession userSession) { - validatePermission(userSession); - + private void deleteActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, UserSession userSession) { SqlSession session = myBatis.openSession(); try { activeRuleDao.deleteParameter(activeRuleParam.getId(), session); @@ -194,9 +214,7 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String value, UserSession userSession) { - validatePermission(userSession); - + private void updateActiveRuleParam(ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam, String value, UserSession userSession) { SqlSession session = myBatis.openSession(); try { RuleParamDto ruleParam = findRuleParamNotNull(activeRule.getRulId(), activeRuleParam.getKey(), session); @@ -216,9 +234,16 @@ public class QProfileActiveRuleOperations implements ServerComponent { } } - public void revertActiveRule(ActiveRuleDto activeRule, UserSession userSession) { + public ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId, UserSession userSession) { validatePermission(userSession); + QProfile profile = findProfileNotNull(profileId); + ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId); + revertActiveRule(activeRule, userSession); + return activeRuleChanged(profile, activeRuleId); + } + + private void revertActiveRule(ActiveRuleDto activeRule, UserSession userSession) { if (activeRule.doesOverride()) { SqlSession session = myBatis.openSession(); try { @@ -409,6 +434,44 @@ public class QProfileActiveRuleOperations implements ServerComponent { return ruleParam; } + private QProfile findProfileNotNull(int profileId) { + QProfile profile = profileLookup.profile(profileId); + QProfileValidations.checkProfileIsNotNull(profile); + return profile; + } + + private QProfileRule findRuleNotNull(int ruleId) { + QProfileRule rule = rulesLookup.findByRuleId(ruleId); + QProfileValidations.checkRuleIsNotNull(rule); + return rule; + } + + @CheckForNull + private ActiveRuleDto findActiveRule(int profileId, int ruleId) { + return activeRuleDao.selectByProfileAndRule(profileId, ruleId); + } + + private ActiveRuleDto findActiveRuleNotNull(int profileId, int ruleId) { + ActiveRuleDto activeRuleDto = findActiveRule(profileId, ruleId); + if (activeRuleDto == null) { + throw new BadRequestException("No rule has been activated on this profile."); + } + return activeRuleDto; + } + + private ActiveRuleDto findActiveRuleNotNull(int activeRuleId) { + ActiveRuleDto activeRule = activeRuleDao.selectById(activeRuleId); + if (activeRule == null) { + throw new NotFoundException("This active rule does not exists."); + } + return activeRule; + } + + @CheckForNull + private ActiveRuleParamDto findActiveRuleParam(int activeRuleId, String key) { + return activeRuleDao.selectParamByActiveRuleAndKey(activeRuleId, key); + } + private static String getSeverityFromOrdinal(int ordinal) { return Severity.ALL.get(ordinal); } @@ -417,6 +480,14 @@ public class QProfileActiveRuleOperations implements ServerComponent { return Severity.ALL.indexOf(severity); } + private ProfileRuleChanged activeRuleChanged(QProfile profile, int activeRuleId) { + return new ProfileRuleChanged(profile, profileLookup.parent(profile), rulesLookup.findByActiveRuleId(activeRuleId)); + } + + private ProfileRuleChanged ruleChanged(QProfile profile, int ruleId) { + return new ProfileRuleChanged(profile, profileLookup.parent(profile), rulesLookup.findByRuleId(ruleId)); + } + public static class ProfileRuleChanged { private QProfile profile; diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java index b6dc599d6b0..b86e4370194 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java @@ -27,7 +27,6 @@ import org.sonar.api.ServerComponent; import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; -import org.sonar.server.exceptions.NotFoundException; import javax.annotation.CheckForNull; @@ -141,11 +140,4 @@ public class QProfileLookup implements ServerComponent { return dao.selectByNameAndLanguage(name, language); } - private QualityProfileDto checkNotNull(QualityProfileDto qualityProfile) { - if (qualityProfile == null) { - throw new NotFoundException("This quality profile does not exists."); - } - return qualityProfile; - } - } 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 a8440691db3..ab44f2e7ee5 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 @@ -41,7 +41,6 @@ import org.sonar.core.properties.PropertyDto; import org.sonar.core.qualityprofile.db.*; import org.sonar.server.configuration.ProfilesManager; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.rule.RuleRegistry; import org.sonar.server.user.UserSession; @@ -255,9 +254,7 @@ public class QProfileOperations implements ServerComponent { private QProfile findNotNull(int profileId) { QProfile profile = profileLookup.profile(profileId); - if (profile == null) { - throw new NotFoundException("This quality profile does not exists."); - } + QProfileValidations.checkProfileIsNotNull(profile); return profile; } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileValidations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileValidations.java new file mode 100644 index 00000000000..c4c21831d78 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileValidations.java @@ -0,0 +1,52 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.core.rule.RuleDto; +import org.sonar.server.exceptions.NotFoundException; + +public class QProfileValidations { + + public static void checkProfileIsNotNull(QProfile profile) { + if (profile == null) { + throw new NotFoundException("This quality profile does not exists."); + } + } + + public static void checkProfileIsNotNull(QualityProfileDto profile) { + if (profile == null) { + throw new NotFoundException("This quality profile does not exists."); + } + } + + public static void checkRuleIsNotNull(QProfileRule rule) { + if (rule == null) { + throw new NotFoundException("This rule does not exists."); + } + } + + public static void checkRuleIsNotNull(RuleDto rule) { + if (rule == null) { + throw new NotFoundException("This rule does not exists."); + } + } +} 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 7023de4c422..86ebf632768 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 @@ -24,7 +24,10 @@ import com.google.common.base.Strings; import org.sonar.api.ServerComponent; import org.sonar.api.component.Component; import org.sonar.core.component.ComponentDto; -import org.sonar.core.qualityprofile.db.*; +import org.sonar.core.qualityprofile.db.ActiveRuleDao; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.resource.ResourceDao; import org.sonar.core.rule.RuleDao; import org.sonar.core.rule.RuleDto; @@ -215,49 +218,20 @@ public class QProfiles implements ServerComponent { return rules.countProfileRules(ProfileRuleQuery.create(profile.id()).setInheritance(QProfileRule.OVERRIDES)); } - public ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) { - QualityProfileDto qualityProfile = findNotNull(profileId); - RuleDto rule = findRuleNotNull(ruleId); - ActiveRuleDto activeRule = findActiveRule(qualityProfile, rule); - if (activeRule == null) { - activeRule = activeRuleOperations.createActiveRule(qualityProfile, rule, severity, UserSession.get()); - } else { - activeRuleOperations.updateSeverity(activeRule, severity, UserSession.get()); - } - return activeRuleChanged(qualityProfile, activeRule); + public QProfileActiveRuleOperations.ProfileRuleChanged activateRule(int profileId, int ruleId, String severity) { + return activeRuleOperations.activateRule(profileId, ruleId, severity, UserSession.get()); } - public ProfileRuleChanged deactivateRule(int profileId, int ruleId) { - QualityProfileDto qualityProfile = findNotNull(profileId); - RuleDto rule = findRuleNotNull(ruleId); - ActiveRuleDto activeRule = findActiveRuleNotNull(qualityProfile, rule); - activeRuleOperations.deactivateRule(activeRule, UserSession.get()); - return activeRuleChanged(qualityProfile, rule); + public QProfileActiveRuleOperations.ProfileRuleChanged deactivateRule(int profileId, int ruleId) { + return activeRuleOperations.deactivateRule(profileId, ruleId, UserSession.get()); } - public ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value) { - String sanitizedValue = Strings.emptyToNull(value); - QualityProfileDto qualityProfile = findNotNull(profileId); - ActiveRuleParamDto activeRuleParam = findActiveRuleParam(activeRuleId, key); - ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId); - UserSession userSession = UserSession.get(); - if (activeRuleParam == null && sanitizedValue != null) { - activeRuleOperations.createActiveRuleParam(activeRule, key, value, userSession); - } else if (activeRuleParam != null && sanitizedValue == null) { - activeRuleOperations.deleteActiveRuleParam(activeRule, activeRuleParam, userSession); - } else if (activeRuleParam != null) { - activeRuleOperations.updateActiveRuleParam(activeRule, activeRuleParam, value, userSession); - } - // If no active rule param and no value -> do nothing - - return activeRuleChanged(qualityProfile, activeRule); + public QProfileActiveRuleOperations.ProfileRuleChanged updateActiveRuleParam(int profileId, int activeRuleId, String key, @Nullable String value) { + return activeRuleOperations.updateActiveRuleParam(profileId, activeRuleId, key, value, UserSession.get()); } - public ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId) { - QualityProfileDto qualityProfile = findNotNull(profileId); - ActiveRuleDto activeRule = findActiveRuleNotNull(activeRuleId); - activeRuleOperations.revertActiveRule(activeRule, UserSession.get()); - return activeRuleChanged(qualityProfile, activeRule); + public QProfileActiveRuleOperations.ProfileRuleChanged revertActiveRule(int profileId, int activeRuleId) { + return activeRuleOperations.revertActiveRule(profileId, activeRuleId, UserSession.get()); } public QProfileRule updateActiveRuleNote(int activeRuleId, String note) { @@ -339,18 +313,7 @@ public class QProfiles implements ServerComponent { private QualityProfileDto findNotNull(int id) { QualityProfileDto qualityProfile = findQualityProfile(id); - return checkNotNull(qualityProfile); - } - - private QualityProfileDto findNotNull(String name, String language) { - QualityProfileDto qualityProfile = findQualityProfile(name, language); - return checkNotNull(qualityProfile); - } - - private QualityProfileDto checkNotNull(QualityProfileDto qualityProfile) { - if (qualityProfile == null) { - throw new NotFoundException("This quality profile does not exists."); - } + QProfileValidations.checkProfileIsNotNull(qualityProfile); return qualityProfile; } @@ -415,9 +378,7 @@ public class QProfiles implements ServerComponent { private RuleDto findRuleNotNull(int ruleId) { RuleDto rule = ruleDao.selectById(ruleId); - if (rule == null) { - throw new NotFoundException("This rule does not exists."); - } + QProfileValidations.checkRuleIsNotNull(rule); return rule; } @@ -439,66 +400,4 @@ public class QProfiles implements ServerComponent { return activeRule; } - private ActiveRuleDto findActiveRuleNotNull(QualityProfileDto qualityProfile, RuleDto rule) { - ActiveRuleDto activeRuleDto = findActiveRule(qualityProfile, rule); - if (activeRuleDto == null) { - throw new BadRequestException("No rule has been activated on this profile."); - } - return activeRuleDto; - } - - @CheckForNull - private ActiveRuleDto findActiveRule(QualityProfileDto qualityProfile, RuleDto rule) { - return activeRuleDao.selectByProfileAndRule(qualityProfile.getId(), rule.getId()); - } - - @CheckForNull - private ActiveRuleParamDto findActiveRuleParam(int activeRuleId, String key) { - return activeRuleDao.selectParamByActiveRuleAndKey(activeRuleId, key); - } - - @CheckForNull - private QProfile findParent(QProfile profile) { - QualityProfileDto parent = findQualityProfile(profile.parent(), profile.language()); - if (parent != null) { - return QProfile.from(parent); - } - return null; - } - - private ProfileRuleChanged activeRuleChanged(QualityProfileDto qualityProfile, ActiveRuleDto activeRule) { - QProfile profile = QProfile.from(qualityProfile); - return new ProfileRuleChanged(profile, findParent(profile), rules.findByActiveRuleId(activeRule.getId())); - } - - private ProfileRuleChanged activeRuleChanged(QualityProfileDto qualityProfile, RuleDto rule) { - QProfile profile = QProfile.from(qualityProfile); - return new ProfileRuleChanged(profile, findParent(profile), rules.findByRuleId(rule.getId())); - } - - public static class ProfileRuleChanged { - - private QProfile profile; - private QProfile parentProfile; - private QProfileRule rule; - - public ProfileRuleChanged(QProfile profile, @Nullable QProfile parentProfile, QProfileRule rule) { - this.profile = profile; - this.parentProfile = parentProfile; - this.rule = rule; - } - - public QProfile profile() { - return profile; - } - - public QProfile parentProfile() { - return parentProfile; - } - - public QProfileRule rule() { - return rule; - } - } - } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java index 37b6ac4c7e7..2026bf23ae1 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java @@ -80,7 +80,7 @@ public class ProfileRules implements ServerExtension { } @CheckForNull - public QProfileRule findByRuleId(Integer ruleId) { + public QProfileRule findByRuleId(int ruleId) { Map ruleSource = index.client().prepareGet(INDEX_RULES, TYPE_RULE, Integer.toString(ruleId)) .execute().actionGet().getSourceAsMap(); if (ruleSource != null) { diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java index 7cca299905a..589b74822cd 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileActiveRuleOperationsTest.java @@ -31,7 +31,6 @@ import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.sonar.api.PropertyType; import org.sonar.api.rule.Severity; -import org.sonar.api.rules.Rule; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; @@ -40,9 +39,7 @@ import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; -import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.rule.RuleDao; -import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.configuration.ProfilesManager; import org.sonar.server.exceptions.BadRequestException; @@ -120,11 +117,14 @@ public class QProfileActiveRuleOperationsTest { @Test public void fail_to_activate_rule_without_profile_admin_permission() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); try { - operations.createActiveRule(qualityProfile, rule, Severity.CRITICAL, unauthorizedUserSession); + operations.activateRule(1, 10, Severity.CRITICAL, unauthorizedUserSession); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(ForbiddenException.class); @@ -135,8 +135,12 @@ public class QProfileActiveRuleOperationsTest { @Test public void activate_rule() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); + when(ruleDao.selectParameters(eq(10), eq(session))).thenReturn(newArrayList(new RuleParamDto().setId(20).setName("max").setDefaultValue("10"))); final int idActiveRuleToUpdate = 42; final int idActiveRuleToDelete = 24; @@ -145,8 +149,7 @@ public class QProfileActiveRuleOperationsTest { .addToDelete(idActiveRuleToDelete); when(profilesManager.activated(eq(1), anyInt(), eq("Nicolas"))).thenReturn(inheritanceActions); - ActiveRuleDto result = operations.createActiveRule(qualityProfile, rule, Severity.CRITICAL, authorizedUserSession); - assertThat(result).isNotNull(); + operations.activateRule(1, 10, Severity.CRITICAL, authorizedUserSession); ArgumentCaptor activeRuleArgument = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao).insert(activeRuleArgument.capture(), eq(session)); @@ -166,12 +169,17 @@ public class QProfileActiveRuleOperationsTest { @Test public void update_severity() throws Exception { - Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle"); - rule.setId(10); + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule); + when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.updateSeverity(activeRule, Severity.MAJOR, authorizedUserSession); + operations.activateRule(1, 10, Severity.MAJOR, authorizedUserSession); verify(activeRuleDao).update(eq(activeRule), eq(session)); verify(session).commit(); @@ -182,12 +190,16 @@ public class QProfileActiveRuleOperationsTest { @Test public void fail_to_update_severity_on_invalid_severity() throws Exception { - Rule rule = Rule.create().setRepositoryKey("squid").setKey("AvoidCycle"); - rule.setId(10); + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule); try { - operations.updateSeverity(activeRule, "Unknown", authorizedUserSession); + operations.activateRule(1, 10, "Unknown", authorizedUserSession); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); @@ -198,11 +210,16 @@ public class QProfileActiveRuleOperationsTest { @Test public void deactivate_rule() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule); when(profilesManager.deactivated(eq(1), anyInt(), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.deactivateRule(activeRule, authorizedUserSession); + operations.deactivateRule(1, 10, authorizedUserSession); verify(activeRuleDao).delete(eq(5), eq(session)); verify(activeRuleDao).deleteParameters(eq(5), eq(session)); @@ -214,12 +231,18 @@ public class QProfileActiveRuleOperationsTest { @Test public void create_active_rule_param() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name()); when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam); when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq((String) null), eq("30"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.createActiveRuleParam(activeRule, "max", "30", authorizedUserSession); + operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleParamDto.class); verify(activeRuleDao).insert(argumentCaptor.capture(), eq(session)); @@ -235,10 +258,17 @@ public class QProfileActiveRuleOperationsTest { @Test public void fail_to_create_active_rule_if_no_rule_param() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(null); + try { - operations.createActiveRuleParam(activeRule, "max", "30", authorizedUserSession); + operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalArgumentException.class); @@ -249,12 +279,18 @@ public class QProfileActiveRuleOperationsTest { @Test public void fail_to_create_active_rule_if_type_is_invalid() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); + ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name()); when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); try { - operations.createActiveRuleParam(activeRule, "max", "invalid integer", authorizedUserSession); + operations.updateActiveRuleParam(1, 5, "max", "invalid integer", authorizedUserSession); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); @@ -265,15 +301,21 @@ public class QProfileActiveRuleOperationsTest { @Test public void update_active_rule_param() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); - ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); - + when(activeRuleDao.selectById(5)).thenReturn(activeRule); RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name()); when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam); + ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); + when(activeRuleDao.selectParamByActiveRuleAndKey(5, "max")).thenReturn(activeRuleParam); when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq("30"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.updateActiveRuleParam(activeRule, activeRuleParam, "30", authorizedUserSession); + operations.updateActiveRuleParam(1, 5, "max", "30", authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleParamDto.class); verify(activeRuleDao).update(argumentCaptor.capture(), eq(session)); @@ -288,11 +330,21 @@ public class QProfileActiveRuleOperationsTest { @Test public void remove_active_rule_param() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + QProfileRule rule = mock(QProfileRule.class); + when(rule.id()).thenReturn(10); + when(rulesLookup.findByRuleId(10)).thenReturn(rule); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); + RuleParamDto ruleParam = new RuleParamDto().setRuleId(10).setName("max").setDefaultValue("20").setType(PropertyType.INTEGER.name()); + when(ruleDao.selectParamByRuleAndKey(10, "max", session)).thenReturn(ruleParam); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); + when(activeRuleDao.selectParamByActiveRuleAndKey(5, "max")).thenReturn(activeRuleParam); + when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq((String) null), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.deleteActiveRuleParam(activeRule, activeRuleParam, authorizedUserSession); + operations.updateActiveRuleParam(1, 5, "max", null, authorizedUserSession); verify(session).commit(); verify(activeRuleDao).deleteParameter(100, session); @@ -303,13 +355,16 @@ public class QProfileActiveRuleOperationsTest { @Test public void revert_active_rule_with_severity_to_update() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); ActiveRuleDto parent = new ActiveRuleDto().setId(4).setProfileId(1).setRuleId(10).setSeverity(2); when(activeRuleDao.selectById(4, session)).thenReturn(parent); when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.revertActiveRule(activeRule, authorizedUserSession); + operations.revertActiveRule(1, 5, authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao, times(2)).update(argumentCaptor.capture(), eq(session)); @@ -326,12 +381,15 @@ public class QProfileActiveRuleOperationsTest { @Test public void fail_to_revert_active_rule_if_no_parent() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); when(activeRuleDao.selectById(4, session)).thenReturn(null); when(profilesManager.ruleSeverityChanged(eq(1), eq(5), eq(RulePriority.MINOR), eq(RulePriority.MAJOR), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); try { - operations.revertActiveRule(activeRule, authorizedUserSession); + operations.revertActiveRule(1, 5, authorizedUserSession); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); } @@ -339,7 +397,11 @@ public class QProfileActiveRuleOperationsTest { @Test public void revert_active_rule_with_param_to_update() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); when(activeRuleDao.selectParamsByActiveRuleId(eq(5), eq(session))).thenReturn(newArrayList( new ActiveRuleParamDto().setId(102).setActiveRuleId(5).setKey("max").setValue("20") )); @@ -352,7 +414,7 @@ public class QProfileActiveRuleOperationsTest { when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("max"), eq("20"), eq("15"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.revertActiveRule(activeRule, authorizedUserSession); + operations.revertActiveRule(1, 5, authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao).update(argumentCaptor.capture(), eq(session)); @@ -373,7 +435,11 @@ public class QProfileActiveRuleOperationsTest { @Test public void revert_active_rule_with_param_to_delete() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); when(activeRuleDao.selectParamsByActiveRuleId(eq(5), eq(session))).thenReturn(newArrayList( new ActiveRuleParamDto().setId(103).setActiveRuleId(5).setKey("format").setValue("abc")) ); @@ -383,7 +449,7 @@ public class QProfileActiveRuleOperationsTest { when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("format"), eq("abc"), eq((String) null), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.revertActiveRule(activeRule, authorizedUserSession); + operations.revertActiveRule(1, 5, authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao).update(argumentCaptor.capture(), eq(session)); @@ -400,7 +466,11 @@ public class QProfileActiveRuleOperationsTest { @Test public void revert_active_rule_with_param_to_create() throws Exception { + QProfile profile = new QProfile().setId(1).setName("Default").setLanguage("java"); + when(profileLookup.profile(1)).thenReturn(profile); + ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(ActiveRuleDto.OVERRIDES).setParentId(4); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); ActiveRuleDto parent = new ActiveRuleDto().setId(4).setProfileId(1).setRuleId(10).setSeverity(1); when(activeRuleDao.selectById(4, session)).thenReturn(parent); @@ -410,7 +480,7 @@ public class QProfileActiveRuleOperationsTest { when(profilesManager.ruleParamChanged(eq(1), eq(5), eq("minimum"), eq((String) null), eq("2"), eq("Nicolas"))).thenReturn(new ProfilesManager.RuleInheritanceActions()); - operations.revertActiveRule(activeRule, authorizedUserSession); + operations.revertActiveRule(1, 5, authorizedUserSession); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(ActiveRuleDto.class); verify(activeRuleDao).update(argumentCaptor.capture(), eq(session)); @@ -431,8 +501,9 @@ public class QProfileActiveRuleOperationsTest { @Test public void no_revert_when_active_rule_do_not_override() throws Exception { ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1).setInheritance(null); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); - operations.revertActiveRule(activeRule, authorizedUserSession); + when(activeRuleDao.selectById(5)).thenReturn(activeRule); verifyZeroInteractions(activeRuleDao); verifyZeroInteractions(session); 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 9693c6d548e..c996c79f609 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 @@ -29,7 +29,10 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.rule.Severity; import org.sonar.core.component.ComponentDto; -import org.sonar.core.qualityprofile.db.*; +import org.sonar.core.qualityprofile.db.ActiveRuleDao; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.resource.ResourceDao; import org.sonar.core.rule.RuleDao; import org.sonar.core.rule.RuleDto; @@ -319,178 +322,26 @@ public class QProfilesTest { @Test public void activate_rule() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); - when(ruleDao.selectById(10)).thenReturn(rule); - - when(activeRuleOperations.createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class))) - .thenReturn(new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1)); - qProfiles.activateRule(1, 10, Severity.BLOCKER); - - verify(activeRuleOperations).createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class)); - } - - @Test - public void activate_rule_with_parent_profile() throws Exception { - QualityProfileDto parentProfile = new QualityProfileDto().setId(2).setName("Parent").setLanguage("java"); - when(qualityProfileDao.selectByNameAndLanguage("Parent", "java")).thenReturn(parentProfile); - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java").setParent("Parent"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); - when(ruleDao.selectById(10)).thenReturn(rule); - - when(activeRuleOperations.createActiveRule(eq(qualityProfile), eq(rule), eq(Severity.BLOCKER), any(UserSession.class))) - .thenReturn(new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1)); - - QProfiles.ProfileRuleChanged result = qProfiles.activateRule(1, 10, Severity.BLOCKER); - - assertThat(result.parentProfile()).isNotNull(); - assertThat(result.parentProfile().id()).isEqualTo(2); - } - - @Test - public void fail_to_activate_rule_if_rule_not_found() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - QProfileRule rule = mock(QProfileRule.class); - when(rule.id()).thenReturn(10); - - try { - qProfiles.activateRule(1, 10, Severity.BLOCKER); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class); - } - verifyZeroInteractions(service); - } - - @Test - public void update_severity() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - - ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); - when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule); - - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); - when(ruleDao.selectById(10)).thenReturn(rule); - - qProfiles.activateRule(1, 10, Severity.BLOCKER); - - verify(activeRuleOperations).updateSeverity(eq(activeRule), eq(Severity.BLOCKER), any(UserSession.class)); + verify(activeRuleOperations).activateRule(eq(1), eq(10), eq(Severity.BLOCKER), any(UserSession.class)); } @Test public void deactivate_rule() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); - when(ruleDao.selectById(10)).thenReturn(rule); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(5).setProfileId(1).setRuleId(10).setSeverity(1); - when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(activeRule); - qProfiles.deactivateRule(1, 10); - - verify(activeRuleOperations).deactivateRule(eq(activeRule), any(UserSession.class)); - } - - @Test - public void fail_to_deactivate_rule_if_no_active_rule_on_profile() throws Exception { - QualityProfileDto qualityProfile = new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"); - when(qualityProfileDao.selectById(1)).thenReturn(qualityProfile); - RuleDto rule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle"); - when(ruleDao.selectById(10)).thenReturn(rule); - when(activeRuleDao.selectByProfileAndRule(1, 10)).thenReturn(null); - - try { - qProfiles.deactivateRule(1, 10); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(BadRequestException.class); - } + verify(activeRuleOperations).deactivateRule(eq(1), eq(10), any(UserSession.class)); } @Test public void update_active_rule_param() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - - ActiveRuleDto activeRule = new ActiveRuleDto().setId(50); - when(activeRuleDao.selectById(50)).thenReturn(activeRule); - - ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); - when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam); - qProfiles.updateActiveRuleParam(1, 50, "max", "20"); - - verify(activeRuleOperations).updateActiveRuleParam(eq(activeRule), eq(activeRuleParam), eq("20"), any(UserSession.class)); - } - - @Test - public void fail_to_update_active_rule_param_if_active_rule_not_found() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); - when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam); - - when(activeRuleDao.selectById(50)).thenReturn(null); - - try { - qProfiles.updateActiveRuleParam(1, 50, "max", "20"); - fail(); - } catch (Exception e) { - assertThat(e).isInstanceOf(NotFoundException.class); - } - verifyZeroInteractions(service); - } - - @Test - public void create_active_rule_param() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(50); - when(activeRuleDao.selectById(50)).thenReturn(activeRule); - when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(null); - - qProfiles.updateActiveRuleParam(1, 50, "max", "20"); - - verify(activeRuleOperations).createActiveRuleParam(eq(activeRule), eq("max"), eq("20"), any(UserSession.class)); - } - - @Test - public void delete_active_rule_param() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(50); - when(activeRuleDao.selectById(50)).thenReturn(activeRule); - - ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setId(100).setActiveRuleId(5).setKey("max").setValue("20"); - when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(activeRuleParam); - - qProfiles.updateActiveRuleParam(1, 50, "max", ""); - - verify(activeRuleOperations).deleteActiveRuleParam(eq(activeRule), eq(activeRuleParam), any(UserSession.class)); - } - - @Test - public void do_nothing_when_updating_active_rule_param_with_no_param_and_empty_value() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(50); - when(activeRuleDao.selectById(50)).thenReturn(activeRule); - when(activeRuleDao.selectParamByActiveRuleAndKey(50, "max")).thenReturn(null); - - qProfiles.updateActiveRuleParam(1, 50, "max", ""); - - verifyZeroInteractions(service); + verify(activeRuleOperations).updateActiveRuleParam(eq(1), eq(50), eq("max"), eq("20"), any(UserSession.class)); } @Test public void revert_active_rule() throws Exception { - when(qualityProfileDao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java")); - ActiveRuleDto activeRule = new ActiveRuleDto().setId(50); - when(activeRuleDao.selectById(50)).thenReturn(activeRule); - qProfiles.revertActiveRule(1, 50); - - verify(activeRuleOperations).revertActiveRule(eq(activeRule), any(UserSession.class)); + verify(activeRuleOperations).revertActiveRule(eq(1), eq(50), any(UserSession.class)); } @Test @@ -581,7 +432,7 @@ public class QProfilesTest { when(ruleOperations.createRule(eq(rule), eq("Rule name"), eq(Severity.MAJOR), eq("My note"), eq(paramsByKey), any(UserSession.class))).thenReturn(newRule); try { - qProfiles.createRule( 10, "", "", "", paramsByKey); + qProfiles.createRule(10, "", "", "", paramsByKey); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); -- 2.39.5