From 29690548db914fe7aef490fb5a2f3b8433854dd6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 28 May 2014 18:03:09 +0200 Subject: [PATCH] SONAR-5007 refactor QProfileService --- .../server/platform/ServerComponents.java | 6 +- .../qualityprofile/BulkRuleActivation.java | 24 --- .../qualityprofile/QProfileService.java | 81 +++++++++ .../RegisterQualityProfiles.java | 12 +- .../RuleActivationContextFactory.java | 4 + ...iveRuleService.java => RuleActivator.java} | 43 ++--- .../ws/BulkRuleActivationActions.java | 6 +- .../ws/RuleActivationActions.java | 6 +- .../server/rule/ws/ActiveRuleCompleter.java | 13 +- .../qualityprofile/QProfilesMediumTest.java | 146 ---------------- ...Test.java => RuleActivatorMediumTest.java} | 164 ++++++++++-------- .../ws/QProfileRecreateBuiltInActionTest.java | 8 +- .../qualityprofile/ws/QProfilesWsTest.java | 8 +- 13 files changed, 224 insertions(+), 297 deletions(-) delete mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkRuleActivation.java create mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java rename sonar-server/src/main/java/org/sonar/server/qualityprofile/{ActiveRuleService.java => RuleActivator.java} (88%) delete mode 100644 sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java rename sonar-server/src/test/java/org/sonar/server/qualityprofile/{ActiveRuleServiceMediumTest.java => RuleActivatorMediumTest.java} (72%) 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 5da0f2a2a64..3bbe22f5ac0 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 @@ -171,7 +171,8 @@ import org.sonar.server.qualitygate.ws.QGatesShowAction; 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.ActiveRuleService; +import org.sonar.server.qualityprofile.QProfileService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.DefaultProfilesCache; import org.sonar.server.qualityprofile.ProfilesManager; import org.sonar.server.qualityprofile.QProfileActiveRuleOperations; @@ -422,7 +423,8 @@ class ServerComponents { pico.addSingleton(ProfilesWs.class); pico.addSingleton(RuleActivationActions.class); pico.addSingleton(BulkRuleActivationActions.class); - pico.addSingleton(ActiveRuleService.class); + pico.addSingleton(RuleActivator.class); + pico.addSingleton(QProfileService.class); pico.addSingleton(RuleActivationContextFactory.class); // rule diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkRuleActivation.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkRuleActivation.java deleted file mode 100644 index 14b20bf40b1..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkRuleActivation.java +++ /dev/null @@ -1,24 +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; - -public class BulkRuleActivation { - -} diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java new file mode 100644 index 00000000000..c61b5dccda4 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -0,0 +1,81 @@ +/* + * 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.Multimap; +import org.sonar.api.ServerComponent; +import org.sonar.api.rule.RuleKey; +import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.core.qualityprofile.db.QualityProfileKey; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.rule.index.RuleQuery; +import org.sonar.server.search.IndexClient; + +import javax.annotation.CheckForNull; +import java.util.List; + +public class QProfileService implements ServerComponent { + + private final IndexClient index; + private final RuleActivator ruleActivator; + + public QProfileService(IndexClient index, RuleActivator ruleActivator) { + this.index = index; + this.ruleActivator = ruleActivator; + } + + @CheckForNull + public ActiveRule getActiveRule(ActiveRuleKey key) { + return index.get(ActiveRuleIndex.class).getByKey(key); + } + + public List findActiveRulesByRule(RuleKey key) { + return index.get(ActiveRuleIndex.class).findByRule(key); + } + + public List findActiveRulesByProfile(QualityProfileKey key) { + return index.get(ActiveRuleIndex.class).findByQProfile(key); + } + + /** + * Activate a rule on a Quality profile. Update configuration (severity/parameters) if the rule is already + * activated. + */ + public List activate(RuleActivation activation) { + return ruleActivator.activate(activation); + } + + /** + * Deactivate a rule on a Quality profile. Does nothing if the rule is not activated, but + * fails (fast) if the rule or the profile does not exist. + */ + public List deactivate(ActiveRuleKey key) { + return ruleActivator.deactivate(key); + } + + + public Multimap bulkActivate(RuleQuery ruleQuery, QualityProfileKey profile) { + return ruleActivator.activateByRuleQuery(ruleQuery, profile); + } + + public Multimap bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) { + return ruleActivator.deActivateByRuleQuery(ruleQuery, profile); + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index ee8eadc2947..99ac78de14d 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -62,7 +62,7 @@ public class RegisterQualityProfiles implements ServerComponent { private final List definitions; private final DefaultProfilesCache defaultProfilesCache; private final DbClient dbClient; - private final ActiveRuleService activeRuleService; + private final RuleActivator ruleActivator; /** * To be kept when no ProfileDefinition are injected @@ -70,19 +70,19 @@ public class RegisterQualityProfiles implements ServerComponent { public RegisterQualityProfiles(PersistentSettings settings, DefaultProfilesCache defaultProfilesCache, DbClient dbClient, - ActiveRuleService activeRuleService) { - this(settings, defaultProfilesCache, dbClient, activeRuleService, Collections.emptyList()); + RuleActivator ruleActivator) { + this(settings, defaultProfilesCache, dbClient, ruleActivator, Collections.emptyList()); } public RegisterQualityProfiles(PersistentSettings settings, DefaultProfilesCache defaultProfilesCache, DbClient dbClient, - ActiveRuleService activeRuleService, + RuleActivator ruleActivator, List definitions) { this.settings = settings; this.defaultProfilesCache = defaultProfilesCache; this.dbClient = dbClient; - this.activeRuleService = activeRuleService; + this.ruleActivator = ruleActivator; this.definitions = definitions; } @@ -141,7 +141,7 @@ public class RegisterQualityProfiles implements ServerComponent { for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { activation.setParameter(param.getKey(), param.getValue()); } - activeRuleService.activate(activation, session); + ruleActivator.activate(activation, session); } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java index 7b723b0e907..3469c196954 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivationContextFactory.java @@ -22,6 +22,7 @@ package org.sonar.server.qualityprofile; import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; +import org.sonar.check.Cardinality; import org.sonar.core.persistence.DbSession; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleKey; @@ -69,6 +70,9 @@ public class RuleActivationContextFactory implements ServerComponent { if (RuleStatus.REMOVED == RuleStatus.valueOf(rule.getStatus())) { throw new IllegalArgumentException("Rule was removed: " + ruleKey); } + if (Cardinality.MULTIPLE.equals(rule.getCardinality())) { + throw new IllegalArgumentException("A rule template can't be activated on a Quality profile: " + ruleKey); + } context.setRule(rule); context.setRuleParams(db.ruleDao().findRuleParamsByRuleKey(rule.getKey(), dbSession)); return rule; diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java similarity index 88% rename from sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java rename to sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index 5ef3c403eeb..e3935e857ce 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -26,7 +26,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; -import org.sonar.api.rule.RuleKey; import org.sonar.api.server.rule.RuleParamType; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; @@ -37,7 +36,6 @@ import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.db.DbClient; -import org.sonar.server.qualityprofile.index.ActiveRuleIndex; import org.sonar.server.qualityprofile.persistence.ActiveRuleDao; import org.sonar.server.rule.Rule; import org.sonar.server.rule.index.RuleIndex; @@ -48,7 +46,6 @@ import org.sonar.server.search.QueryOptions; import org.sonar.server.user.UserSession; import org.sonar.server.util.TypeValidations; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.util.Collection; import java.util.List; @@ -56,7 +53,10 @@ import java.util.Map; import static com.google.common.collect.Lists.newArrayList; -public class ActiveRuleService implements ServerComponent { +/** + * Activation and deactivation of rules in Quality profiles + */ +public class RuleActivator implements ServerComponent { private final DbClient db; private final TypeValidations typeValidations; @@ -64,9 +64,9 @@ public class ActiveRuleService implements ServerComponent { private final PreviewCache previewCache; private final IndexClient index; - public ActiveRuleService(DbClient db, IndexClient index, - RuleActivationContextFactory contextFactory, TypeValidations typeValidations, - PreviewCache previewCache) { + public RuleActivator(DbClient db, IndexClient index, + RuleActivationContextFactory contextFactory, TypeValidations typeValidations, + PreviewCache previewCache) { this.db = db; this.index = index; this.contextFactory = contextFactory; @@ -74,19 +74,6 @@ public class ActiveRuleService implements ServerComponent { this.previewCache = previewCache; } - @CheckForNull - public ActiveRule getByKey(ActiveRuleKey key) { - return index.get(ActiveRuleIndex.class).getByKey(key); - } - - public List findByRuleKey(RuleKey key) { - return index.get(ActiveRuleIndex.class).findByRule(key); - } - - public List findByQProfileKey(QualityProfileKey key) { - return index.get(ActiveRuleIndex.class).findByQProfile(key); - } - /** * Activate a rule on a Quality profile. Update configuration (severity/parameters) if the rule is already * activated. @@ -243,19 +230,19 @@ public class ActiveRuleService implements ServerComponent { RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of("template","severity")) + .setFieldsToReturn(ImmutableSet.of("template", "severity")) ); for (Rule rule : result.getHits()) { - if(!rule.template()) { + if (!rule.template()) { ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key()); RuleActivation activation = new RuleActivation(key); activation.setSeverity(rule.severity()); - for(ActiveRuleChange active : this.activate(activation, dbSession)){ - results.put("activated",active.getKey().ruleKey().toString()); + for (ActiveRuleChange active : this.activate(activation, dbSession)) { + results.put("activated", active.getKey().ruleKey().toString()); } } else { - results.put("ignored",rule.key().toString()); + results.put("ignored", rule.key().toString()); } } dbSession.commit(); @@ -275,13 +262,13 @@ public class ActiveRuleService implements ServerComponent { RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of("template","severity")) + .setFieldsToReturn(ImmutableSet.of("template", "severity")) ); for (Rule rule : result.getHits()) { ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key()); - for(ActiveRuleChange deActive :this.deactivate(key, dbSession)){ - results.put("deactivated",deActive.getKey().ruleKey().toString()); + for (ActiveRuleChange deActive : this.deactivate(key, dbSession)) { + results.put("deactivated", deActive.getKey().ruleKey().toString()); } } dbSession.commit(); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java index 5583074931a..ebf3da2978a 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java @@ -29,7 +29,7 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.qualityprofile.db.QualityProfileKey; -import org.sonar.server.qualityprofile.ActiveRuleService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.rule.RuleService; import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.rule.ws.SearchAction; @@ -40,10 +40,10 @@ public class BulkRuleActivationActions implements ServerComponent { public static final String PROFILE_KEY = "profile_key"; public static final String SEVERITY = "severity"; - private final ActiveRuleService service; + private final RuleActivator service; private final RuleService ruleService; - public BulkRuleActivationActions(ActiveRuleService service, RuleService ruleService) { + public BulkRuleActivationActions(RuleActivator service, RuleService ruleService) { this.service = service; this.ruleService = ruleService; } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java index 0a88e4e3e61..e22395b8411 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java @@ -29,7 +29,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.QualityProfileKey; -import org.sonar.server.qualityprofile.ActiveRuleService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.RuleActivation; public class RuleActivationActions implements ServerComponent { @@ -39,9 +39,9 @@ public class RuleActivationActions implements ServerComponent { public static final String SEVERITY = "severity"; public static final String PARAMS = "params"; - private final ActiveRuleService service; + private final RuleActivator service; - public RuleActivationActions(ActiveRuleService service) { + public RuleActivationActions(RuleActivator service) { this.service = service; } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java b/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java index 9028e6942e4..698339f00af 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ws/ActiveRuleCompleter.java @@ -25,7 +25,8 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.QualityProfileKey; import org.sonar.server.qualityprofile.ActiveRule; -import org.sonar.server.qualityprofile.ActiveRuleService; +import org.sonar.server.qualityprofile.QProfileService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.rule.Rule; import org.sonar.server.rule.index.RuleQuery; @@ -38,9 +39,9 @@ import java.util.Map; * web services. */ public class ActiveRuleCompleter implements ServerComponent { - private final ActiveRuleService service; + private final QProfileService service; - public ActiveRuleCompleter(ActiveRuleService service) { + public ActiveRuleCompleter(QProfileService service) { this.service = service; } @@ -51,7 +52,7 @@ public class ActiveRuleCompleter implements ServerComponent { // Load details of active rules on the selected profile QualityProfileKey profileKey = QualityProfileKey.parse(query.getQProfileKey()); for (Rule rule : rules) { - ActiveRule activeRule = service.getByKey(ActiveRuleKey.of(profileKey, rule.key())); + ActiveRule activeRule = service.getActiveRule(ActiveRuleKey.of(profileKey, rule.key())); if (activeRule != null) { writeActiveRules(rule.key(), Arrays.asList(activeRule), json); } @@ -59,7 +60,7 @@ public class ActiveRuleCompleter implements ServerComponent { } else { // Load details of all active rules for (Rule rule : rules) { - writeActiveRules(rule.key(), service.findByRuleKey(rule.key()), json); + writeActiveRules(rule.key(), service.findActiveRulesByRule(rule.key()), json); } } json.endObject(); @@ -67,7 +68,7 @@ public class ActiveRuleCompleter implements ServerComponent { void completeShow(Rule rule, JsonWriter json) { json.name("actives").beginArray(); - for (ActiveRule activeRule : service.findByRuleKey(rule.key())) { + for (ActiveRule activeRule : service.findActiveRulesByRule(rule.key())) { writeActiveRule(activeRule, json); } json.endArray(); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java deleted file mode 100644 index 11186909948..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesMediumTest.java +++ /dev/null @@ -1,146 +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.Ignore; -import org.junit.Test; -import org.sonar.api.profiles.ProfileDefinition; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rule.Severity; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.RuleParam; -import org.sonar.api.rules.RulePriority; -import org.sonar.api.server.rule.RuleParamType; -import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.utils.ValidationMessages; -import org.sonar.core.permission.GlobalPermissions; -import org.sonar.core.qualityprofile.db.QualityProfileKey; -import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.tester.ServerTester; -import org.sonar.server.user.MockUserSession; - -import static com.google.common.collect.Lists.newArrayList; -import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; - -@Ignore -@Deprecated -public class QProfilesMediumTest { - - @org.junit.Rule - public ServerTester serverTester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class); - - @Test - public void recreate_built_in_profile_from_language() throws Exception { -// MockUserSession.set().setLogin("julien").setName("Julien").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); -// -// QProfiles qProfiles = tester.get(QProfiles.class); -// QProfileBackup qProfileBackup = tester.get(QProfileBackup.class); -// Rules rules = tester.get(Rules.class); -// -// QProfile profile = qProfiles.profile("Basic", "xoo"); -// -// // Update rule x1 : update severity and update param value -// Rule rule1 = rules.find(RuleQuery.builder().searchQuery("x1").build()).results().iterator().next(); -// qProfiles.updateActiveRuleParam(qProfiles.findByProfileAndRule(profile.id(), rule1.id()).activeRuleId(), "acceptWhitespace", "false"); -// qProfiles.activateRule(profileKey(profile), rule1.ruleKey(), "INFO"); -// -// // Disable rule x2 -// Rule rule2 = rules.find(RuleQuery.builder().searchQuery("x2").build()).results().iterator().next(); -// qProfiles.deactivateRule(QualityProfileKey.of(profile.name(), profile.language()), rule2.ruleKey()); -// -// assertThat(qProfileBackup.findDefaultProfileNamesByLanguage("xoo")).hasSize(1); -// -// // Renamed profile -// qProfiles.renameProfile(profile.id(), "Old Basic"); -// -// // Restore default profiles of xoo -// qProfileBackup.recreateBuiltInProfilesByLanguage("xoo"); -// -// // Reload profile -// profile = qProfiles.profile("Basic", "xoo"); -// -// // Verify rule x1 -// QProfileRule qProfileRule = qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()).setNameOrKey("x1"), Paging.create(10, 1)).rules().get(0); -// assertThat(qProfileRule.severity()).isEqualTo("MAJOR"); -// QProfileRuleParam qProfileRuleParam = qProfileRule.params().get(0); -// assertThat(qProfileRuleParam.key()).isEqualTo("acceptWhitespace"); -// assertThat(qProfileRuleParam.value()).isEqualTo("true"); -// -// // Verify rule x2 -// assertThat(qProfiles.searchProfileRules(ProfileRuleQuery.create(profile.id()).setNameOrKey("x2"), Paging.create(10, 1)).rules().get(0)).isNotNull(); - } - - @Test - public void fail_to_recreate_built_in_profile_from_language_if_default_profile_already_exists() throws Exception { - MockUserSession.set().setLogin("julien").setName("Julien").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); - - QProfileBackup qProfileBackup = serverTester.get(QProfileBackup.class); - - try { - // Restore default profiles of xoo -> fail as it already exists - qProfileBackup.recreateBuiltInProfilesByLanguage("xoo"); - fail(); - } catch (BadRequestException e) { - assertThat(e.l10nKey()).isEqualTo("quality_profiles.profile_x_already_exists"); - assertThat(e.l10nParams()).containsOnly("Basic"); - } - } - - public static class XooProfileDefinition extends ProfileDefinition { - @Override - public RulesProfile createProfile(ValidationMessages validation) { - final RulesProfile profile = RulesProfile.create("Basic", "xoo"); - ActiveRule activeRule1 = profile.activateRule( - org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), - RulePriority.MAJOR); - activeRule1.setParameter("acceptWhitespace", "true"); - - profile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x2"), RulePriority.BLOCKER); - return profile; - } - } - - public static class XooRulesDefinition implements RulesDefinition { - @Override - public void define(Context context) { - NewRepository repository = context.createRepository("xoo", "xoo").setName("Xoo Repo"); - repository.createRule("x1") - .setName("x1 name") - .setHtmlDescription("x1 desc") - .setSeverity(Severity.MINOR) - .createParam("acceptWhitespace") - .setDefaultValue("false") - .setType(RuleParamType.BOOLEAN) - .setDescription("Accept whitespaces on the line"); - - repository.createRule("x2") - .setName("x2 name") - .setHtmlDescription("x2 desc") - .setSeverity(Severity.MAJOR); - repository.done(); - } - } - - private QualityProfileKey profileKey(QProfile profile){ - return QualityProfileKey.of(profile.name(), profile.language()); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java similarity index 72% rename from sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java rename to sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index 1197f4a162c..d412bbcc00a 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -46,27 +46,28 @@ import java.util.List; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; -public class ActiveRuleServiceMediumTest { +public class RuleActivatorMediumTest { @Rule public ServerTester tester = new ServerTester() .addComponents(XooRulesDefinition.class, JavaRulesDefinition.class); - QualityProfileKey profileKey = QualityProfileKey.of("MyProfile", "xoo"); - DbClient dbClient; + static final QualityProfileKey PROFILE_KEY = QualityProfileKey.of("MyProfile", "xoo"); + + DbClient db; DbSession dbSession; - ActiveRuleService service; + RuleActivator ruleActivator; ActiveRuleIndex index; @Before public void before() { - dbClient = tester.get(DbClient.class); - dbSession = dbClient.openSession(false); - service = tester.get(ActiveRuleService.class); + db = tester.get(DbClient.class); + dbSession = db.openSession(false); + ruleActivator = tester.get(RuleActivator.class); index = tester.get(ActiveRuleIndex.class); // create quality profile - dbClient.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor("MyProfile", "xoo")); + db.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor("MyProfile", "xoo")); dbSession.commit(); } @@ -74,17 +75,17 @@ public class ActiveRuleServiceMediumTest { public void activate() throws Exception { grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); activation.setSeverity(Severity.BLOCKER); activation.setParameter("max", "7"); - service.activate(activation); + ruleActivator.activate(activation); // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(PROFILE_KEY, dbSession); assertThat(activeRuleDtos).hasSize(1); assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.BLOCKER); assertThat(activeRuleDtos.get(0).getInheritance()).isNull(); - List params = dbClient.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); + List params = db.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); assertThat(params).hasSize(1); assertThat(params.get(0).getValue()).isEqualTo("7"); @@ -101,15 +102,15 @@ public class ActiveRuleServiceMediumTest { @Test public void activate_with_default_severity_and_parameter() throws Exception { grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); - service.activate(activation); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); + ruleActivator.activate(activation); // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(PROFILE_KEY, dbSession); assertThat(activeRuleDtos).hasSize(1); assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.MINOR); assertThat(activeRuleDtos.get(0).getInheritance()).isNull(); - List params = dbClient.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); + List params = db.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); assertThat(params).hasSize(1); assertThat(params.get(0).getValue()).isEqualTo("10"); @@ -127,22 +128,22 @@ public class ActiveRuleServiceMediumTest { public void update_activation_severity_and_parameters() throws Exception { // initial activation grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); activation.setSeverity(Severity.BLOCKER); - service.activate(activation); + ruleActivator.activate(activation); // update - RuleActivation update = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation update = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); update.setSeverity(Severity.CRITICAL); update.setParameter("max", "42"); - service.activate(update); + ruleActivator.activate(update); // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(PROFILE_KEY, dbSession); assertThat(activeRuleDtos).hasSize(1); assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.CRITICAL); assertThat(activeRuleDtos.get(0).getInheritance()).isNull(); - List params = dbClient.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); + List params = db.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); assertThat(params).hasSize(1); assertThat(params.get(0).getValue()).isEqualTo("42"); @@ -160,31 +161,31 @@ public class ActiveRuleServiceMediumTest { public void update_activation_but_new_parameter() throws Exception { // initial activation grantPermission(); - ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")); + ActiveRuleKey activeRuleKey = ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1")); RuleActivation activation = new RuleActivation(activeRuleKey); activation.setSeverity(Severity.BLOCKER); - service.activate(activation); + ruleActivator.activate(activation); - assertThat(dbClient.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull(); - dbClient.activeRuleDao().removeParamByKeyAndName(activeRuleKey, "max", dbSession); + assertThat(db.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull(); + db.activeRuleDao().removeParamByKeyAndName(activeRuleKey, "max", dbSession); dbSession.commit(); - assertThat(dbClient.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNull(); + assertThat(db.activeRuleDao().getParamsByKeyAndName(activeRuleKey, "max", dbSession)).isNull(); // update - RuleActivation update = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation update = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); update.setSeverity(Severity.CRITICAL); update.setParameter("max", "42"); // contrary to activerule, the param 'max' is supposed to be inserted but not updated - service.activate(update); + ruleActivator.activate(update); // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(PROFILE_KEY, dbSession); assertThat(activeRuleDtos).hasSize(1); assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.CRITICAL); assertThat(activeRuleDtos.get(0).getInheritance()).isNull(); - List params = dbClient.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); + List params = db.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); assertThat(params).hasSize(1); assertThat(params.get(0).getValue()).isEqualTo("42"); @@ -202,21 +203,21 @@ public class ActiveRuleServiceMediumTest { public void revert_activation_to_default_severity_and_parameters() throws Exception { // initial activation grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); activation.setSeverity(Severity.BLOCKER); activation.setParameter("max", "7"); - service.activate(activation); + ruleActivator.activate(activation); // update - RuleActivation update = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); - service.activate(update); + RuleActivation update = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); + ruleActivator.activate(update); // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(profileKey, dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(PROFILE_KEY, dbSession); assertThat(activeRuleDtos).hasSize(1); assertThat(activeRuleDtos.get(0).getSeverityString()).isEqualTo(Severity.MINOR); assertThat(activeRuleDtos.get(0).getInheritance()).isNull(); - List params = dbClient.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); + List params = db.activeRuleDao().findParamsByActiveRule(activeRuleDtos.get(0), dbSession); assertThat(params).hasSize(1); assertThat(params.get(0).getValue()).isEqualTo("10"); @@ -233,24 +234,38 @@ public class ActiveRuleServiceMediumTest { @Test public void fail_to_activate_if_not_granted() throws Exception { MockUserSession.set().setLogin("marius"); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); try { - service.activate(activation); + ruleActivator.activate(activation); fail(); } catch (ForbiddenException e) { verifyZeroActiveRules(activation.getKey()); } } + @Test + public void fail_to_activate_if_template() throws Exception { + grantPermission(); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "template1"))); + + try { + ruleActivator.activate(activation); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("A rule template can't be activated on a Quality profile: xoo:template1"); + verifyZeroActiveRules(activation.getKey()); + } + } + @Test public void fail_to_activate_if_different_languages() throws Exception { // profile and rule have different languages grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("squid", "j1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("squid", "j1"))); try { - service.activate(activation); + ruleActivator.activate(activation); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Rule squid:j1 and profile MyProfile:xoo have different languages"); @@ -262,10 +277,10 @@ public class ActiveRuleServiceMediumTest { public void fail_to_activate_if_unknown_rule() throws Exception { // profile and rule have different languages grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x3"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x3"))); try { - service.activate(activation); + ruleActivator.activate(activation); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Rule not found: xoo:x3"); @@ -280,7 +295,7 @@ public class ActiveRuleServiceMediumTest { RuleActivation activation = new RuleActivation(ActiveRuleKey.of(QualityProfileKey.of("other", "js"), RuleKey.of("xoo", "x1"))); try { - service.activate(activation); + ruleActivator.activate(activation); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Quality profile not found: other:js"); @@ -290,11 +305,11 @@ public class ActiveRuleServiceMediumTest { @Test public void fail_to_activate_if_invalid_parameter() throws Exception { grantPermission(); - RuleActivation activation = new RuleActivation(ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1"))); + RuleActivation activation = new RuleActivation(ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1"))); activation.setParameter("max", "foo"); try { - service.activate(activation); + ruleActivator.activate(activation); fail(); } catch (BadRequestException e) { assertThat(e.l10nKey()).isEqualTo("errors.type.notInteger"); @@ -306,14 +321,14 @@ public class ActiveRuleServiceMediumTest { public void deactivate() throws Exception { // activation grantPermission(); - ActiveRuleKey key = ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")); + ActiveRuleKey key = ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1")); RuleActivation activation = new RuleActivation(key); activation.setSeverity(Severity.BLOCKER); activation.setParameter("max", "7"); - service.activate(activation); + ruleActivator.activate(activation); // deactivation - service.deactivate(key); + ruleActivator.deactivate(key); verifyZeroActiveRules(key); } @@ -323,8 +338,8 @@ public class ActiveRuleServiceMediumTest { grantPermission(); // deactivation - ActiveRuleKey key = ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x1")); - service.deactivate(key); + ActiveRuleKey key = ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x1")); + ruleActivator.deactivate(key); verifyZeroActiveRules(key); } @@ -332,9 +347,9 @@ public class ActiveRuleServiceMediumTest { @Test public void deactivation_fails_if_rule_not_found() throws Exception { grantPermission(); - ActiveRuleKey key = ActiveRuleKey.of(profileKey, RuleKey.of("xoo", "x3")); + ActiveRuleKey key = ActiveRuleKey.of(PROFILE_KEY, RuleKey.of("xoo", "x3")); try { - service.deactivate(key); + ruleActivator.deactivate(key); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Rule not found: xoo:x3"); @@ -347,7 +362,7 @@ public class ActiveRuleServiceMediumTest { grantPermission(); ActiveRuleKey key = ActiveRuleKey.of(QualityProfileKey.of("other", "js"), RuleKey.of("xoo", "x1")); try { - service.deactivate(key); + ruleActivator.deactivate(key); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Quality profile not found: other:js"); @@ -357,13 +372,13 @@ public class ActiveRuleServiceMediumTest { @Test public void synchronize_index() throws Exception { QualityProfileDto profile1 = QualityProfileDto.createFor("p1", "java"); - dbClient.qualityProfileDao().insert(dbSession, profile1); + db.qualityProfileDao().insert(dbSession, profile1); RuleDto rule1 = RuleDto.createFor(RuleKey.of("java", "r1")).setSeverity(Severity.MAJOR); - dbClient.ruleDao().insert(dbSession, rule1); + db.ruleDao().insert(dbSession, rule1); ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile1, rule1).setSeverity("BLOCKER"); - dbClient.activeRuleDao().insert(dbSession, activeRule); + db.activeRuleDao().insert(dbSession, activeRule); dbSession.commit(); tester.clearIndexes(); @@ -372,7 +387,7 @@ public class ActiveRuleServiceMediumTest { assertThat(index.getByKey(activeRule.getKey())).isNull(); // 1. Synchronize since 0 - dbClient.activeRuleDao().synchronizeAfter(dbSession, 0); + db.activeRuleDao().synchronizeAfter(dbSession, 0); // 2. Assert that we have the rule in Index assertThat(index.getByKey(activeRule.getKey())).isNotNull(); @@ -383,41 +398,41 @@ public class ActiveRuleServiceMediumTest { public void find_active_rules() throws Exception { QualityProfileDto profile1 = QualityProfileDto.createFor("p1", "java"); QualityProfileDto profile2 = QualityProfileDto.createFor("p2", "java"); - dbClient.qualityProfileDao().insert(dbSession, profile1, profile2); + db.qualityProfileDao().insert(dbSession, profile1, profile2); RuleDto rule1 = RuleDto.createFor(RuleKey.of("java", "r1")).setSeverity(Severity.MAJOR); RuleDto rule2 = RuleDto.createFor(RuleKey.of("java", "r2")).setSeverity(Severity.MAJOR); - dbClient.ruleDao().insert(dbSession, rule1); - dbClient.ruleDao().insert(dbSession, rule2); + db.ruleDao().insert(dbSession, rule1); + db.ruleDao().insert(dbSession, rule2); - dbClient.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR)); - dbClient.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER)); - dbClient.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL)); + db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR)); + db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER)); + db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL)); dbSession.commit(); // find by rule key - List activeRules = service.findByRuleKey(RuleKey.of("java", "r1")); + List activeRules = index.findByRule(RuleKey.of("java", "r1")); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r1")); - activeRules = service.findByRuleKey(RuleKey.of("java", "r2")); + activeRules = index.findByRule(RuleKey.of("java", "r2")); assertThat(activeRules).hasSize(2); assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleKey.of("java", "r2")); - activeRules = service.findByRuleKey(RuleKey.of("java", "r3")); + activeRules = index.findByRule(RuleKey.of("java", "r3")); assertThat(activeRules).isEmpty(); // find by profile - activeRules = service.findByQProfileKey(profile1.getKey()); + activeRules = index.findByQProfile(profile1.getKey()); assertThat(activeRules).hasSize(2); assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile1.getKey()); assertThat(activeRules.get(1).key().qProfile()).isEqualTo(profile1.getKey()); - activeRules = service.findByQProfileKey(profile2.getKey()); + activeRules = index.findByQProfile(profile2.getKey()); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile2.getKey()); - activeRules = service.findByQProfileKey(QualityProfileKey.of("unknown", "unknown")); + activeRules = index.findByQProfile(QualityProfileKey.of("unknown", "unknown")); assertThat(activeRules).isEmpty(); } @@ -427,7 +442,7 @@ public class ActiveRuleServiceMediumTest { private void verifyZeroActiveRules(ActiveRuleKey key) { // verify db - List activeRuleDtos = dbClient.activeRuleDao().findByProfileKey(key.qProfile(), dbSession); + List activeRuleDtos = db.activeRuleDao().findByProfileKey(key.qProfile(), dbSession); assertThat(activeRuleDtos).isEmpty(); //TODO test params @@ -448,6 +463,13 @@ public class ActiveRuleServiceMediumTest { .setDefaultValue("10") .setType(RuleParamType.INTEGER) .setDescription("Maximum"); + + repository.createRule("template1") + .setName("Template1") + .setHtmlDescription("Template one") + .setSeverity(Severity.MINOR) + .setTemplate(true); + repository.done(); } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java index ebf04a98e8e..1ee43775aab 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRecreateBuiltInActionTest.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.server.qualityprofile.ActiveRuleService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.QProfileBackup; import org.sonar.server.qualityprofile.QProfileResult; import org.sonar.server.rule.RuleService; @@ -45,12 +45,12 @@ public class QProfileRecreateBuiltInActionTest { @Before public void setUp() throws Exception { - ActiveRuleService activeRuleService = mock(ActiveRuleService.class); + RuleActivator ruleActivator = mock(RuleActivator.class); RuleService ruleService = mock(RuleService.class); tester = new WsTester(new QProfilesWs( new QProfileRecreateBuiltInAction(qProfileBackup), - new RuleActivationActions(activeRuleService), - new BulkRuleActivationActions(activeRuleService, ruleService))); + new RuleActivationActions(ruleActivator), + new BulkRuleActivationActions(ruleActivator, ruleService))); } @Test diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 83a3563b567..aeee6954e54 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -23,7 +23,7 @@ package org.sonar.server.qualityprofile.ws; import org.junit.Before; import org.junit.Test; import org.sonar.api.server.ws.WebService; -import org.sonar.server.qualityprofile.ActiveRuleService; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.QProfileBackup; import org.sonar.server.rule.RuleService; import org.sonar.server.ws.WsTester; @@ -37,12 +37,12 @@ public class QProfilesWsTest { @Before public void setUp() { - ActiveRuleService activeRuleService = mock(ActiveRuleService.class); + RuleActivator ruleActivator = mock(RuleActivator.class); RuleService ruleService = mock(RuleService.class); controller = new WsTester(new QProfilesWs(new QProfileRecreateBuiltInAction( mock(QProfileBackup.class)), - new RuleActivationActions(activeRuleService), - new BulkRuleActivationActions(activeRuleService, ruleService) + new RuleActivationActions(ruleActivator), + new BulkRuleActivationActions(ruleActivator, ruleService) )).controller("api/qualityprofiles"); } -- 2.39.5