From: Sébastien Lesaint Date: Tue, 30 Jan 2018 16:09:38 +0000 (+0100) Subject: SONAR-10357 add ruleId to RuleActivation X-Git-Tag: 7.5~1695 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7f656ec61c7557f1856edb3d5c5d967293b6f754;p=sonarqube.git SONAR-10357 add ruleId to RuleActivation --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileUpdateImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileUpdateImpl.java index 04b83baa134..9e77286eaab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileUpdateImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQProfileUpdateImpl.java @@ -21,10 +21,10 @@ package org.sonar.server.qualityprofile; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; import org.sonar.api.rule.RuleKey; import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule; @@ -33,8 +33,11 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.RulesProfileDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; +import static org.sonar.core.util.stream.MoreCollectors.toSet; + public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate { private final DbClient dbClient; @@ -54,17 +57,19 @@ public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate { .map(ActiveRuleDto::getRuleKey) .collect(MoreCollectors.toHashSet()); + Set ruleKeys = Stream.concat( + deactivatedKeys.stream(), + builtIn.getActiveRules().stream().map(ar -> RuleKey.of(ar.repoKey(), ar.ruleKey()))) + .collect(toSet()); + RuleActivationContext context = ruleActivator.createContextForBuiltInProfile(dbSession, rulesProfile, ruleKeys); + Collection activations = new ArrayList<>(); - Collection ruleKeys = new HashSet<>(deactivatedKeys); for (BuiltInActiveRule ar : builtIn.getActiveRules()) { - RuleActivation activation = convert(ar); + RuleActivation activation = convert(ar, context); activations.add(activation); - ruleKeys.add(activation.getRuleKey()); deactivatedKeys.remove(activation.getRuleKey()); } - RuleActivationContext context = ruleActivator.createContextForBuiltInProfile(dbSession, rulesProfile, ruleKeys); - List changes = new ArrayList<>(); for (RuleActivation activation : activations) { changes.addAll(ruleActivator.activate(dbSession, activation, context)); @@ -77,10 +82,13 @@ public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate { return changes; } - private static RuleActivation convert(BuiltInActiveRule ar) { + private static RuleActivation convert(BuiltInActiveRule ar, RuleActivationContext context) { + RuleKey ruleKey = RuleKey.of(ar.repoKey(), ar.ruleKey()); + context.reset(ruleKey); + RuleDefinitionDto ruleDefinition = context.getRule().get(); Map params = ar.overriddenParams().stream() .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); - return RuleActivation.create(RuleKey.of(ar.repoKey(), ar.ruleKey()), ar.overriddenSeverity(), params); + return RuleActivation.create(ruleDefinition.getId(), ruleKey, ar.overriddenSeverity(), params); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java index dff9e728e1e..14e80f6812d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java @@ -30,6 +30,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Function; import javax.annotation.Nullable; @@ -43,6 +44,7 @@ import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.rule.RuleKey; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.text.XmlWriter; +import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; @@ -50,6 +52,7 @@ import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.OrgActiveRuleDto; import org.sonar.db.qualityprofile.QProfileDto; +import org.sonar.db.rule.RuleDefinitionDto; import static com.google.common.base.Preconditions.checkArgument; @@ -140,7 +143,7 @@ public class QProfileBackuperImpl implements QProfileBackuper { try { String profileLang = null; String profileName = null; - List ruleActivations = Lists.newArrayList(); + List rules = Lists.newArrayList(); SMInputFactory inputFactory = initStax(); SMHierarchicCursor rootC = inputFactory.rootElementCursor(backup); rootC.advance(); // @@ -158,12 +161,13 @@ public class QProfileBackuperImpl implements QProfileBackuper { } else if (StringUtils.equals(ATTRIBUTE_RULES, nodeName)) { SMInputCursor rulesCursor = cursor.childElementCursor("rule"); - ruleActivations = parseRuleActivations(rulesCursor); + rules = parseRuleActivations(rulesCursor); } } QProfileName targetName = new QProfileName(profileLang, profileName); QProfileDto targetProfile = profileLoader.apply(targetName); + List ruleActivations = toRuleActivations(dbSession, rules); BulkChangeResult changes = profileReset.reset(dbSession, targetProfile, ruleActivations); return new QProfileRestoreSummary(targetProfile, changes); } catch (XMLStreamException e) { @@ -171,8 +175,40 @@ public class QProfileBackuperImpl implements QProfileBackuper { } } - private static List parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException { - List activations = new ArrayList<>(); + private List toRuleActivations(DbSession dbSession, List rules) { + List ruleKeys = rules.stream() + .map(r -> r.ruleKey) + .collect(MoreCollectors.toList()); + Map ruleDefinitionsByKey = db.ruleDao().selectDefinitionByKeys(dbSession, ruleKeys) + .stream() + .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey)); + + return rules.stream() + .map(r -> { + RuleDefinitionDto ruleDefinition = ruleDefinitionsByKey.get(r.ruleKey); + if (ruleDefinition == null) { + return null; + } + return RuleActivation.create(ruleDefinition.getId(), ruleDefinition.getKey(), r.severity, r.parameters); + }) + .filter(Objects::nonNull) + .collect(MoreCollectors.toList(rules.size())); + } + + private static final class Rule { + private final RuleKey ruleKey; + private final String severity; + private final Map parameters; + + private Rule(RuleKey ruleKey, String severity, Map parameters) { + this.ruleKey = ruleKey; + this.severity = severity; + this.parameters = parameters; + } + } + + private static List parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException { + List activations = new ArrayList<>(); Set activatedKeys = new HashSet<>(); List duplicatedKeys = new ArrayList<>(); while (rulesCursor.getNext() != null) { @@ -202,7 +238,7 @@ public class QProfileBackuperImpl implements QProfileBackuper { duplicatedKeys.add(ruleKey); } activatedKeys.add(ruleKey); - activations.add(RuleActivation.create(ruleKey, severity, parameters)); + activations.add(new Rule(ruleKey, severity, parameters)); } if (!duplicatedKeys.isEmpty()) { throw new IllegalArgumentException("The quality profile cannot be restored as it contains duplicates for the following rules: " + diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java index 3cbaa9f121c..11ccb29d71f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java @@ -30,6 +30,8 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; +import javax.annotation.CheckForNull; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.profiles.ProfileExporter; @@ -50,6 +52,7 @@ import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.OrgActiveRuleDto; import org.sonar.db.qualityprofile.QProfileDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; @@ -156,9 +159,13 @@ public class QProfileExporters { } private List importProfile(QProfileDto profile, RulesProfile definition, DbSession dbSession) { + Map rulesByRuleKey = dbClient.ruleDao().selectAllDefinitions(dbSession) + .stream() + .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey)); List activeRules = definition.getActiveRules(); List activations = activeRules.stream() - .map(QProfileExporters::toRuleActivation) + .map(activeRule -> toRuleActivation(activeRule, rulesByRuleKey)) + .filter(Objects::nonNull) .collect(MoreCollectors.toArrayList(activeRules.size())); return qProfileRules.activateAndCommit(dbSession, profile, activations); } @@ -178,12 +185,17 @@ public class QProfileExporters { result.addInfos(messages.getInfos()); } - private static RuleActivation toRuleActivation(ActiveRule activeRule) { + @CheckForNull + private static RuleActivation toRuleActivation(ActiveRule activeRule, Map rulesByRuleKey) { RuleKey ruleKey = activeRule.getRule().ruleKey(); + RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey); + if (ruleDefinition == null) { + return null; + } String severity = activeRule.getSeverity().name(); Map params = activeRule.getActiveRuleParams().stream() .collect(MoreCollectors.uniqueIndex(ActiveRuleParam::getKey, ActiveRuleParam::getValue)); - return RuleActivation.create(ruleKey, severity, params); + return RuleActivation.create(ruleDefinition.getId(), ruleKey, severity, params); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRulesImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRulesImpl.java index 554db53075d..b082c30d1bc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRulesImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRulesImpl.java @@ -71,8 +71,8 @@ public class QProfileRulesImpl implements QProfileRules { @Override public BulkChangeResult bulkActivateAndCommit(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, @Nullable String severity) { verifyNotBuiltIn(profile); - return doBulk(dbSession, profile, ruleQuery, (context, ruleKey) -> { - RuleActivation activation = RuleActivation.create(ruleKey, severity, null); + return doBulk(dbSession, profile, ruleQuery, (context, ruleDefinition) -> { + RuleActivation activation = RuleActivation.create(ruleDefinition.getId(), ruleDefinition.getKey(), severity, null); return ruleActivator.activate(dbSession, activation, context); }); } @@ -93,7 +93,7 @@ public class QProfileRulesImpl implements QProfileRules { @Override public BulkChangeResult bulkDeactivateAndCommit(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery) { verifyNotBuiltIn(profile); - return doBulk(dbSession, profile, ruleQuery, (context, ruleKey) -> ruleActivator.deactivate(dbSession, context, ruleKey, false)); + return doBulk(dbSession, profile, ruleQuery, (context, ruleDefinition) -> ruleActivator.deactivate(dbSession, context, ruleDefinition.getKey(), false)); } @Override @@ -115,14 +115,16 @@ public class QProfileRulesImpl implements QProfileRules { checkArgument(!profile.isBuiltIn(), "The built-in profile %s is read-only and can't be updated", profile.getName()); } - private BulkChangeResult doBulk(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, BiFunction> fn) { + private BulkChangeResult doBulk(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, BiFunction> fn) { BulkChangeResult result = new BulkChangeResult(); Collection ruleKeys = Sets.newHashSet(ruleIndex.searchAll(ruleQuery)); RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleKeys); for (RuleKey ruleKey : ruleKeys) { try { - List changes = fn.apply(context, ruleKey); + context.reset(ruleKey); + RuleDefinitionDto ruleDefinition = context.getRule().get(); + List changes = fn.apply(context, ruleDefinition); result.addChanges(changes); if (!changes.isEmpty()) { result.incrementSucceeded(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileTreeImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileTreeImpl.java index 09f346c03b8..1549b74e834 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileTreeImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileTreeImpl.java @@ -86,7 +86,7 @@ public class QProfileTreeImpl implements QProfileTree { for (ActiveRuleDto parentActiveRule : parentActiveRules) { try { - RuleActivation activation = RuleActivation.create(parentActiveRule.getRuleKey(), null, null); + RuleActivation activation = RuleActivation.create(parentActiveRule.getRuleId(), parentActiveRule.getRuleKey(), null, null); changes.addAll(ruleActivator.activate(dbSession, activation, context)); } catch (BadRequestException e) { // for example because rule status is REMOVED diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java index bede1be50bd..fba67f21db7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivation.java @@ -35,11 +35,13 @@ import org.sonar.api.rule.Severity; public class RuleActivation { private final RuleKey ruleKey; + private final int ruleId; private final boolean reset; private final String severity; private final Map parameters = new HashMap<>(); - private RuleActivation(RuleKey ruleKey, boolean reset, @Nullable String severity, @Nullable Map parameters) { + private RuleActivation(int ruleId, RuleKey ruleKey, boolean reset, @Nullable String severity, @Nullable Map parameters) { + this.ruleId = ruleId; this.ruleKey = ruleKey; this.reset = reset; this.severity = severity; @@ -53,16 +55,16 @@ public class RuleActivation { } } - public static RuleActivation createReset(RuleKey ruleKey) { - return new RuleActivation(ruleKey, true, null, null); + public static RuleActivation createReset(int ruleId, RuleKey ruleKey) { + return new RuleActivation(ruleId, ruleKey, true, null, null); } - public static RuleActivation create(RuleKey ruleKey, @Nullable String severity, @Nullable Map parameters) { - return new RuleActivation(ruleKey, false, severity, parameters); + public static RuleActivation create(int ruleId, RuleKey ruleKey, @Nullable String severity, @Nullable Map parameters) { + return new RuleActivation(ruleId, ruleKey, false, severity, parameters); } - public static RuleActivation create(RuleKey ruleKey) { - return create(ruleKey, null, null); + public static RuleActivation create(int ruleId, RuleKey ruleKey) { + return create(ruleId, ruleKey, null, null); } /** @@ -73,6 +75,10 @@ public class RuleActivation { return severity; } + public int getRuleId() { + return ruleId; + } + public RuleKey getRuleKey() { return ruleKey; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java index 1f620be292c..85028f242ef 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java @@ -30,6 +30,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualityprofile.QProfileDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.server.qualityprofile.QProfileRules; import org.sonar.server.qualityprofile.RuleActivation; import org.sonar.server.user.UserSession; @@ -104,18 +105,20 @@ public class ActivateRuleAction implements QProfileWsAction { QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey)); OrganizationDto organization = wsSupport.getOrganization(dbSession, profile); wsSupport.checkCanEdit(dbSession, organization, profile); - RuleActivation activation = readActivation(request); + RuleActivation activation = readActivation(dbSession, request); ruleActivator.activateAndCommit(dbSession, profile, singletonList(activation)); } response.noContent(); } - private static RuleActivation readActivation(Request request) { + private RuleActivation readActivation(DbSession dbSession, Request request) { RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE)); + RuleDefinitionDto ruleDefinition = dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey) + .orElseThrow(() -> new IllegalArgumentException(format("Rule '%s' not found", ruleKey))); boolean reset = Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET)); if (reset) { - return RuleActivation.createReset(ruleKey); + return RuleActivation.createReset(ruleDefinition.getId(), ruleKey); } String severity = request.param(PARAM_SEVERITY); Map params = null; @@ -123,7 +126,7 @@ public class ActivateRuleAction implements QProfileWsAction { if (paramsAsString != null) { params = KeyValueFormat.parse(paramsAsString); } - return RuleActivation.create(ruleKey, severity, params); + return RuleActivation.create(ruleDefinition.getId(), ruleKey, severity, params); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java index 7da0368869d..3e51539b2f7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java @@ -156,6 +156,7 @@ public class QProfileBackuperImplTest { @Test public void restore_resets_the_activated_rules() { + db.rules().insert(RuleKey.of("sonarjs", "s001")); OrganizationDto organization = db.organizations().insert(); Reader backup = new StringReader("" + "foo" + diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileComparisonTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileComparisonTest.java index dbcb29e488b..764962d5d9c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileComparisonTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileComparisonTest.java @@ -115,7 +115,7 @@ public class QProfileComparisonTest { @Test public void compare_same() { - RuleActivation commonActivation = RuleActivation.create(xooRule1.getKey(), Severity.CRITICAL, + RuleActivation commonActivation = RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), Severity.CRITICAL, ImmutableMap.of("min", "7", "max", "42")); qProfileRules.activateAndCommit(dbSession, left, singleton(commonActivation)); qProfileRules.activateAndCommit(dbSession, right, singleton(commonActivation)); @@ -132,7 +132,7 @@ public class QProfileComparisonTest { @Test public void compare_only_left() { - RuleActivation activation = RuleActivation.create(xooRule1.getKey()); + RuleActivation activation = RuleActivation.create(xooRule1.getId(), xooRule1.getKey()); qProfileRules.activateAndCommit(dbSession, left, singleton(activation)); QProfileComparisonResult result = comparison.compare(dbSession, left, right); @@ -147,7 +147,7 @@ public class QProfileComparisonTest { @Test public void compare_only_right() { - qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getKey()))); + qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey()))); QProfileComparisonResult result = comparison.compare(dbSession, left, right); assertThat(result.left().getKee()).isEqualTo(left.getKee()); @@ -161,8 +161,8 @@ public class QProfileComparisonTest { @Test public void compare_disjoint() { - qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getKey()))); - qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule2.getKey()))); + qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey()))); + qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule2.getId(), xooRule2.getKey()))); QProfileComparisonResult result = comparison.compare(dbSession, left, right); assertThat(result.left().getKee()).isEqualTo(left.getKee()); @@ -176,8 +176,8 @@ public class QProfileComparisonTest { @Test public void compare_modified_severity() { - qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getKey(), Severity.CRITICAL, null))); - qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getKey(), Severity.BLOCKER, null))); + qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), Severity.CRITICAL, null))); + qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule2.getId(), xooRule1.getKey(), Severity.BLOCKER, null))); QProfileComparisonResult result = comparison.compare(dbSession, left, right); assertThat(result.left().getKee()).isEqualTo(left.getKee()); @@ -196,8 +196,8 @@ public class QProfileComparisonTest { @Test public void compare_modified_param() { - qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getKey(), null, ImmutableMap.of("max", "20")))); - qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getKey(), null, ImmutableMap.of("max", "30")))); + qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), null, ImmutableMap.of("max", "20")))); + qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), null, ImmutableMap.of("max", "30")))); QProfileComparisonResult result = comparison.compare(dbSession, left, right); assertThat(result.left().getKee()).isEqualTo(left.getKee()); @@ -219,8 +219,8 @@ public class QProfileComparisonTest { @Test public void compare_different_params() { - qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getKey(), null, ImmutableMap.of("max", "20")))); - qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getKey(), null, ImmutableMap.of("min", "5")))); + qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), null, ImmutableMap.of("max", "20")))); + qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getId(), xooRule1.getKey(), null, ImmutableMap.of("min", "5")))); QProfileComparisonResult result = comparison.compare(dbSession, left, right); assertThat(result.left().getKee()).isEqualTo(left.getKee()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java index 130fecf0201..677c7e7c921 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java @@ -67,10 +67,10 @@ public class QProfileResetImplTest { public void reset() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE)); RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); - qProfileRules.activateAndCommit(db.getSession(), profile, singleton(RuleActivation.create(existingRule.getKey()))); + qProfileRules.activateAndCommit(db.getSession(), profile, singleton(RuleActivation.create(existingRule.getId(), existingRule.getKey()))); RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); - BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getKey()))); + BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getId(), newRule.getKey()))); assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)) .extracting(OrgActiveRuleDto::getRuleKey) @@ -87,11 +87,11 @@ public class QProfileResetImplTest { QProfileDto childProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE)); qProfileTree.setParentAndCommit(db.getSession(), childProfile, parentProfile); RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); - qProfileRules.activateAndCommit(db.getSession(), parentProfile, singleton(RuleActivation.create(existingRule.getKey()))); - qProfileRules.activateAndCommit(db.getSession(), childProfile, singleton(RuleActivation.create(existingRule.getKey()))); + qProfileRules.activateAndCommit(db.getSession(), parentProfile, singleton(RuleActivation.create(existingRule.getId(), existingRule.getKey()))); + qProfileRules.activateAndCommit(db.getSession(), childProfile, singleton(RuleActivation.create(existingRule.getId(), existingRule.getKey()))); RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); - underTest.reset(db.getSession(), childProfile, singletonList(RuleActivation.create(newRule.getKey()))); + underTest.reset(db.getSession(), childProfile, singletonList(RuleActivation.create(newRule.getId(), newRule.getKey()))); assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), childProfile)) .extracting(OrgActiveRuleDto::getRuleKey) @@ -106,7 +106,7 @@ public class QProfileResetImplTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee())); - underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getKey()))); + underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getId(), defaultRule.getKey()))); } @Test @@ -117,6 +117,6 @@ public class QProfileResetImplTest { expectedException.expect(NullPointerException.class); expectedException.expectMessage("Quality profile must be persisted"); - underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getKey()))); + underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getId(), defaultRule.getKey()))); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java index cd562543f93..e331bc6e4f2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleImplTest.java @@ -95,7 +95,7 @@ public class QProfileRuleImplTest { public void system_activates_rule_without_parameters() { RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, BLOCKER, null, emptyMap()); @@ -107,7 +107,7 @@ public class QProfileRuleImplTest { userSession.logIn(); RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, BLOCKER, null, emptyMap()); @@ -120,7 +120,7 @@ public class QProfileRuleImplTest { RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10")); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of("min", "10")); @@ -133,7 +133,7 @@ public class QProfileRuleImplTest { RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10")); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of(ruleParam.getName(), "15")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(ruleParam.getName(), "15")); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of("min", "15")); @@ -145,7 +145,7 @@ public class QProfileRuleImplTest { RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, emptyMap()); @@ -161,7 +161,7 @@ public class QProfileRuleImplTest { RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10")); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of("min", "")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of("min", "")); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of("min", "10")); @@ -178,7 +178,7 @@ public class QProfileRuleImplTest { RuleParamDto paramWithDefault = db.rules().insertRuleParam(rule, p -> p.setName("max").setDefaultValue("10")); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of(paramWithoutDefault.getName(), "-10")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(paramWithoutDefault.getName(), "-10")); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, @@ -192,7 +192,7 @@ public class QProfileRuleImplTest { RuleParamDto param = db.rules().insertRuleParam(rule, p -> p.setName("max").setDefaultValue("10")); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of("xxx", "yyy")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of("xxx", "yyy")); List changes = activate(profile, activation); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of(param.getName(), param.getDefaultValue())); @@ -206,11 +206,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation - RuleActivation activation = RuleActivation.create(rule.getKey(), MAJOR, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, null); activate(profile, activation); // update - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "20")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "20")); List changes = activate(profile, updateActivation); assertThatRuleIsUpdated(profile, rule, CRITICAL, null, of(param.getName(), "20")); @@ -225,11 +225,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation -> param "max" has a default value - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); activate(profile, activation); // update param "min", which has no default value - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), MAJOR, of(paramWithoutDefault.getName(), "3")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(paramWithoutDefault.getName(), "3")); List changes = activate(profile, updateActivation); assertThatRuleIsUpdated(profile, rule, MAJOR, null, of(paramWithDefault.getName(), "10", paramWithoutDefault.getName(), "3")); @@ -243,11 +243,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation -> param "max" has a default value - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of(paramWithDefault.getName(), "20")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(paramWithDefault.getName(), "20")); activate(profile, activation); // reset to default_value - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), null, of(paramWithDefault.getName(), "")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(paramWithDefault.getName(), "")); List changes = activate(profile, updateActivation); assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(paramWithDefault.getName(), "10")); @@ -262,11 +262,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation -> param "max" has a default value - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of(paramWithoutDefault.getName(), "20")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(paramWithoutDefault.getName(), "20")); activate(profile, activation); // remove parameter - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), null, of(paramWithoutDefault.getName(), "")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(paramWithoutDefault.getName(), "")); List changes = activate(profile, updateActivation); assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(paramWithDefault.getName(), paramWithDefault.getDefaultValue())); @@ -280,13 +280,13 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation -> param "max" has a default value - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(profile, activation); db.getDbClient().activeRuleDao().deleteParametersByRuleProfileUuids(db.getSession(), asList(profile.getRulesProfileUuid())); assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, emptyMap()); // contrary to activerule, the param is supposed to be inserted but not updated - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), null, of(param.getName(), "")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(param.getName(), "")); changes = activate(profile, updateActivation); assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(param.getName(), param.getDefaultValue())); @@ -299,11 +299,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); activate(profile, activation); // update with exactly the same severity and params - activation = RuleActivation.create(rule.getKey()); + activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(profile, activation); assertThat(changes).isEmpty(); @@ -316,11 +316,11 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(rule); // initial activation -> param "max" has a default value - RuleActivation activation = RuleActivation.create(rule.getKey(), BLOCKER, of(param.getName(), "20")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, of(param.getName(), "20")); activate(profile, activation); // update without any severity or params => keep - RuleActivation update = RuleActivation.create(rule.getKey()); + RuleActivation update = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(profile, update); assertThat(changes).isEmpty(); @@ -331,7 +331,7 @@ public class QProfileRuleImplTest { RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); RuleKey ruleKey = RuleKey.parse("unknown:xxx"); - RuleActivation activation = RuleActivation.create(ruleKey); + RuleActivation activation = RuleActivation.create(rule.getId(), ruleKey); expectFailure("Rule not found: " + ruleKey, () -> activate(profile, activation)); } @@ -340,7 +340,7 @@ public class QProfileRuleImplTest { public void fail_to_activate_rule_if_profile_is_on_different_languages() { RuleDefinitionDto rule = createJavaRule(); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage("js")); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); expectFailure("java rule " + rule.getKey() + " cannot be activated on js profile " + profile.getKee(), () -> activate(profile, activation)); } @@ -349,7 +349,7 @@ public class QProfileRuleImplTest { public void fail_to_activate_rule_if_rule_has_REMOVED_status() { RuleDefinitionDto rule = db.rules().insert(r -> r.setStatus(RuleStatus.REMOVED)); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); expectFailure("Rule was removed: " + rule.getKey(), () -> activate(profile, activation)); } @@ -358,7 +358,7 @@ public class QProfileRuleImplTest { public void fail_to_activate_if_template() { RuleDefinitionDto rule = db.rules().insert(r -> r.setIsTemplate(true)); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); expectFailure("Rule template can't be activated on a Quality profile: " + rule.getKey(), () -> activate(profile, activation)); } @@ -369,7 +369,7 @@ public class QProfileRuleImplTest { RuleParamDto param = db.rules().insertRuleParam(rule, p -> p.setName("max").setDefaultValue("10").setType(PropertyType.INTEGER.name())); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey(), null, of(param.getName(), "foo")); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), null, of(param.getName(), "foo")); expectFailure("Value 'foo' must be an integer.", () -> activate(profile, activation)); } @@ -382,12 +382,12 @@ public class QProfileRuleImplTest { QProfileDto profile = createProfile(customRule); // initial activation - RuleActivation activation = RuleActivation.create(customRule.getKey(), MAJOR, emptyMap()); + RuleActivation activation = RuleActivation.create(customRule.getId(), customRule.getKey(), MAJOR, emptyMap()); activate(profile, activation); assertThatRuleIsActivated(profile, customRule, null, MAJOR, null, of("format", "txt")); // update -> parameter is not changed - RuleActivation updateActivation = RuleActivation.create(customRule.getKey(), BLOCKER, of("format", "xml")); + RuleActivation updateActivation = RuleActivation.create(customRule.getId(), customRule.getKey(), BLOCKER, of("format", "xml")); activate(profile, updateActivation); assertThatRuleIsActivated(profile, customRule, null, BLOCKER, null, of("format", "txt")); } @@ -397,7 +397,7 @@ public class QProfileRuleImplTest { userSession.logIn(); RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); activate(profile, activation); List changes = deactivate(profile, rule); @@ -411,7 +411,7 @@ public class QProfileRuleImplTest { public void system_deactivates_a_rule() { RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); activate(profile, activation); List changes = deactivate(profile, rule); @@ -450,7 +450,7 @@ public class QProfileRuleImplTest { public void deactivate_rule_that_has_REMOVED_status() { RuleDefinitionDto rule = createRule(); QProfileDto profile = createProfile(rule); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); activate(profile, activation); rule.setStatus(RuleStatus.REMOVED); @@ -468,7 +468,7 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - List changes = activate(childProfile, RuleActivation.create(rule.getKey())); + List changes = activate(childProfile, RuleActivation.create(rule.getId(), rule.getKey())); assertThatProfileHasNoActiveRules(parentProfile); assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsActivated(grandChildProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap()); @@ -482,10 +482,10 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation initialActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation initialActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(childProfile, initialActivation); - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "bar")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "bar")); List changes = activate(childProfile, updateActivation); assertThatProfileHasNoActiveRules(parentProfile); @@ -502,10 +502,10 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation initialActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation initialActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(childProfile, initialActivation); - RuleActivation overrideActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "bar")); + RuleActivation overrideActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "bar")); List changes = activate(grandChildProfile, overrideActivation); assertThatProfileHasNoActiveRules(parentProfile); @@ -522,14 +522,14 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation initialActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation initialActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(childProfile, initialActivation); - RuleActivation overrideActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "bar")); + RuleActivation overrideActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "bar")); activate(grandChildProfile, overrideActivation); // update child --> do not touch grandChild - RuleActivation updateActivation = RuleActivation.create(rule.getKey(), BLOCKER, of(param.getName(), "baz")); + RuleActivation updateActivation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, of(param.getName(), "baz")); List changes = activate(childProfile, updateActivation); assertThatProfileHasNoActiveRules(parentProfile); @@ -546,14 +546,14 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation initialActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation initialActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(parentProfile, initialActivation); - RuleActivation overrideActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "bar")); + RuleActivation overrideActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "bar")); activate(grandChildProfile, overrideActivation); // reset parent --> touch child but not grandChild - RuleActivation updateActivation = RuleActivation.createReset(rule.getKey()); + RuleActivation updateActivation = RuleActivation.createReset(rule.getId(), rule.getKey()); List changes = activate(parentProfile, updateActivation); assertThatRuleIsUpdated(parentProfile, rule, rule.getSeverityString(), null, of(param.getName(), param.getDefaultValue())); @@ -569,10 +569,10 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation childActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation childActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(childProfile, childActivation); - RuleActivation parentActivation = RuleActivation.create(rule.getKey(), CRITICAL, of(param.getName(), "bar")); + RuleActivation parentActivation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, of(param.getName(), "bar")); List changes = activate(parentProfile, parentActivation); assertThatRuleIsUpdated(parentProfile, rule, CRITICAL, null, of(param.getName(), "bar")); @@ -587,10 +587,10 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation parentActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation parentActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); activate(parentProfile, parentActivation); - RuleActivation overrideActivation = RuleActivation.create(rule.getKey(), MAJOR, of(param.getName(), "foo")); + RuleActivation overrideActivation = RuleActivation.create(rule.getId(), rule.getKey(), MAJOR, of(param.getName(), "foo")); List changes = activate(childProfile, overrideActivation); assertThatRuleIsUpdated(childProfile, rule, MAJOR, INHERITED, of(param.getName(), "foo")); @@ -603,7 +603,7 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(parentProfile, activation); assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap()); @@ -620,12 +620,12 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(parentProfile, activation); assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap()); - activation = RuleActivation.create(rule.getKey(), CRITICAL, null); + activation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, null); activate(childProfile, activation); changes = deactivate(parentProfile, rule); @@ -640,7 +640,7 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List changes = activate(parentProfile, activation); assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap()); @@ -656,18 +656,18 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation activation = RuleActivation.create(rule.getKey(), CRITICAL, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, null); List changes = activate(parentProfile, activation); assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, INHERITED, emptyMap()); assertThat(changes).hasSize(2); - RuleActivation childActivation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation childActivation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); changes = activate(childProfile, childActivation); assertThatRuleIsUpdated(childProfile, rule, BLOCKER, ActiveRule.Inheritance.OVERRIDES, emptyMap()); assertThat(changes).hasSize(1); - RuleActivation resetActivation = RuleActivation.createReset(rule.getKey()); + RuleActivation resetActivation = RuleActivation.createReset(rule.getId(), rule.getKey()); changes = activate(childProfile, resetActivation); assertThatRuleIsUpdated(childProfile, rule, CRITICAL, INHERITED, emptyMap()); assertThatRuleIsUpdated(parentProfile, rule, CRITICAL, null, emptyMap()); @@ -681,21 +681,21 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(baseProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation activation = RuleActivation.create(rule.getKey(), CRITICAL, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, null); List changes = activate(baseProfile, activation); assertThatRuleIsActivated(baseProfile, rule, changes, CRITICAL, null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, INHERITED, emptyMap()); assertThatRuleIsActivated(grandChildProfile, rule, changes, CRITICAL, INHERITED, emptyMap()); assertThat(changes).hasSize(3); - RuleActivation childActivation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation childActivation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); changes = activate(childProfile, childActivation); assertThatRuleIsUpdated(childProfile, rule, BLOCKER, ActiveRule.Inheritance.OVERRIDES, emptyMap()); assertThatRuleIsUpdated(grandChildProfile, rule, BLOCKER, INHERITED, emptyMap()); assertThat(changes).hasSize(2); // Reset on parent do not change child nor grandchild - RuleActivation resetActivation = RuleActivation.createReset(rule.getKey()); + RuleActivation resetActivation = RuleActivation.createReset(rule.getId(), rule.getKey()); changes = activate(baseProfile, resetActivation); assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsUpdated(childProfile, rule, BLOCKER, ActiveRule.Inheritance.OVERRIDES, emptyMap()); @@ -703,7 +703,7 @@ public class QProfileRuleImplTest { assertThat(changes).hasSize(1); // Reset on child change grandchild - resetActivation = RuleActivation.createReset(rule.getKey()); + resetActivation = RuleActivation.createReset(rule.getId(), rule.getKey()); changes = activate(childProfile, resetActivation); assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsUpdated(childProfile, rule, rule.getSeverityString(), INHERITED, emptyMap()); @@ -717,7 +717,7 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - RuleActivation resetActivation = RuleActivation.createReset(rule.getKey()); + RuleActivation resetActivation = RuleActivation.createReset(rule.getId(), rule.getKey()); List changes = activate(parentProfile, resetActivation); verifyNoActiveRules(); assertThat(changes).hasSize(0); @@ -791,7 +791,7 @@ public class QProfileRuleImplTest { QProfileDto parentProfile = createProfile(rule); QProfileDto childProfile = createChildProfile(parentProfile); - List changes = activate(parentProfile, RuleActivation.create(rule.getKey())); + List changes = activate(parentProfile, RuleActivation.create(rule.getId(), rule.getKey())); assertThatRuleIsActivated(parentProfile, rule, null, rule.getSeverityString(), null, emptyMap()); assertThatRuleIsActivated(childProfile, rule, null, rule.getSeverityString(), INHERITED, emptyMap()); @@ -816,8 +816,8 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandchildProfile = createChildProfile(childProfile); - activate(parentProfile, RuleActivation.create(rule1.getKey())); - activate(parentProfile, RuleActivation.create(rule2.getKey())); + activate(parentProfile, RuleActivation.create(rule1.getId(), rule1.getKey())); + activate(parentProfile, RuleActivation.create(rule2.getId(), rule2.getKey())); ruleIndexer.indexOnStartup(ruleIndexer.getIndexTypes()); @@ -848,10 +848,10 @@ public class QProfileRuleImplTest { QProfileDto childProfile = createChildProfile(parentProfile); QProfileDto grandChildProfile = createChildProfile(childProfile); - RuleActivation activation = RuleActivation.create(rule.getKey(), CRITICAL, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), CRITICAL, null); activate(parentProfile, activation); - RuleActivation overrideActivation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation overrideActivation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); activate(grandChildProfile, overrideActivation); // Reset on parent do not change child nor grandchild @@ -874,7 +874,7 @@ public class QProfileRuleImplTest { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The built-in profile " + builtInProfile.getName() + " is read-only and can't be updated"); - underTest.activateAndCommit(db.getSession(), builtInProfile, singleton(RuleActivation.create(rule.getKey()))); + underTest.activateAndCommit(db.getSession(), builtInProfile, singleton(RuleActivation.create(rule.getId(), rule.getKey()))); } private void assertThatProfileHasNoActiveRules(QProfileDto profile) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTreeImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTreeImplTest.java index 0012109e938..36718e93ac4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTreeImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileTreeImplTest.java @@ -110,11 +110,11 @@ public class QProfileTreeImplTest { RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("bar")); QProfileDto parentProfile = createProfile(rule1); - List changes = activate(parentProfile, RuleActivation.create(rule1.getKey())); + List changes = activate(parentProfile, RuleActivation.create(rule1.getId(), rule1.getKey())); assertThat(changes).hasSize(1); QProfileDto childProfile = createProfile(rule2); - changes = activate(childProfile, RuleActivation.create(rule2.getKey())); + changes = activate(childProfile, RuleActivation.create(rule2.getId(), rule2.getKey())); assertThat(changes).hasSize(1); expectedException.expect(BadRequestException.class); @@ -129,11 +129,11 @@ public class QProfileTreeImplTest { RuleDefinitionDto rule2 = createJavaRule(); QProfileDto profile1 = createProfile(rule1); - List changes = activate(profile1, RuleActivation.create(rule1.getKey())); + List changes = activate(profile1, RuleActivation.create(rule1.getId(), rule1.getKey())); assertThat(changes).hasSize(1); QProfileDto profile2 = createProfile(rule2); - changes = activate(profile2, RuleActivation.create(rule2.getKey())); + changes = activate(profile2, RuleActivation.create(rule2.getId(), rule2.getKey())); assertThat(changes).hasSize(1); changes = underTest.setParentAndCommit(db.getSession(), profile2, profile1); @@ -152,11 +152,11 @@ public class QProfileTreeImplTest { RuleDefinitionDto rule1 = createJavaRule(); RuleDefinitionDto rule2 = createJavaRule(); QProfileDto profile1 = createProfile(rule1); - List changes = activate(profile1, RuleActivation.create(rule1.getKey())); + List changes = activate(profile1, RuleActivation.create(rule1.getId(), rule1.getKey())); assertThat(changes).hasSize(1); QProfileDto profile2 = createProfile(rule2); - changes = activate(profile2, RuleActivation.create(rule2.getKey())); + changes = activate(profile2, RuleActivation.create(rule2.getId(), rule2.getKey())); assertThat(changes).hasSize(1); changes = underTest.setParentAndCommit(db.getSession(), profile2, profile1); @@ -164,7 +164,7 @@ public class QProfileTreeImplTest { assertThatRuleIsActivated(profile2, rule1, changes, rule1.getSeverityString(), INHERITED, emptyMap()); assertThatRuleIsActivated(profile2, rule2, null, rule2.getSeverityString(), null, emptyMap()); - RuleActivation activation = RuleActivation.create(rule1.getKey(), BLOCKER, null); + RuleActivation activation = RuleActivation.create(rule1.getId(), rule1.getKey(), BLOCKER, null); changes = activate(profile2, activation); assertThat(changes).hasSize(1); assertThatRuleIsUpdated(profile2, rule1, BLOCKER, ActiveRule.Inheritance.OVERRIDES, emptyMap()); @@ -182,8 +182,8 @@ public class QProfileTreeImplTest { RuleDefinitionDto rule1 = createJavaRule(); RuleDefinitionDto rule2 = createJavaRule(); QProfileDto parentProfile = createProfile(rule1); - activate(parentProfile, RuleActivation.create(rule1.getKey())); - activate(parentProfile, RuleActivation.create(rule2.getKey())); + activate(parentProfile, RuleActivation.create(rule1.getId(), rule1.getKey())); + activate(parentProfile, RuleActivation.create(rule2.getId(), rule2.getKey())); rule1.setStatus(RuleStatus.REMOVED); db.rules().update(rule1); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java index 2d599340789..cc2983410bf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java @@ -260,7 +260,7 @@ public class RegisterQualityProfilesNotificationTest { db.qualityProfiles().activateRule(builtInQProfileDto, rule); QProfileDto childQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee())); - qProfileRules.activateAndCommit(db.getSession(), childQProfileDto, singleton(RuleActivation.create(rule.getKey()))); + qProfileRules.activateAndCommit(db.getSession(), childQProfileDto, singleton(RuleActivation.create(rule.getId(), rule.getKey()))); db.commit(); addPluginProfile(builtInQProfileDto); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java index ab1f04ad2b1..044f81e78d0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java @@ -35,6 +35,7 @@ import org.sonar.db.DbTester; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.qualityprofile.QProfileDto; +import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.BadRequestException; @@ -140,10 +141,10 @@ public class ActivateRuleActionTest { public void activate_rule_in_default_organization() { userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization); - RuleKey ruleKey = RuleTesting.randomRuleKey(); + RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey()); TestRequest request = ws.newRequest() .setMethod("POST") - .setParam(PARAM_RULE, ruleKey.toString()) + .setParam(PARAM_RULE, rule.getKey().toString()) .setParam(PARAM_KEY, qualityProfile.getKee()) .setParam("severity", "BLOCKER") .setParam("params", "key1=v1;key2=v2") @@ -160,7 +161,7 @@ public class ActivateRuleActionTest { assertThat(activations).hasSize(1); RuleActivation activation = activations.iterator().next(); - assertThat(activation.getRuleKey()).isEqualTo(ruleKey); + assertThat(activation.getRuleKey()).isEqualTo(rule.getKey()); assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER); assertThat(activation.isReset()).isFalse(); } @@ -170,6 +171,7 @@ public class ActivateRuleActionTest { userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization); QProfileDto qualityProfile = db.qualityProfiles().insert(organization); RuleKey ruleKey = RuleTesting.randomRuleKey(); + db.rules().insert(ruleKey); TestRequest request = ws.newRequest() .setMethod("POST") .setParam(PARAM_RULE, ruleKey.toString()) @@ -200,6 +202,7 @@ public class ActivateRuleActionTest { db.qualityProfiles().addUserPermission(qualityProfile, user); userSession.logIn(user); RuleKey ruleKey = RuleTesting.randomRuleKey(); + db.rules().insert(ruleKey); ws.newRequest() .setMethod("POST") diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java index 7f8d7e50e33..0bb3a68e938 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java @@ -280,7 +280,7 @@ public class InheritanceActionTest { } private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) { - qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getKey(), severity, null))); + qProfileRules.activateAndCommit(dbSession, profile, singleton(RuleActivation.create(rule.getId(), rule.getKey(), severity, null))); // dbSession.commit(); // activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index f59614b4f03..01e9923df95 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -613,7 +613,7 @@ public class SearchActionTest { OrganizationDto organization = db.organizations().insert(); QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage("java")); RuleDefinitionDto rule = createJavaRule(); - RuleActivation activation = RuleActivation.create(rule.getKey(), BLOCKER, null); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey(), BLOCKER, null); qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation)); indexRules(); @@ -660,7 +660,7 @@ public class SearchActionTest { .setDescription("Empty Param") .setName("empty_var")); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List activeRuleChanges1 = qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation)); qProfileRules.activateAndCommit(db.getSession(), waterproofProfile, singleton(activation)); @@ -716,7 +716,7 @@ public class SearchActionTest { .setDescription("My small description") .setName("my_var")); - RuleActivation activation = RuleActivation.create(rule.getKey()); + RuleActivation activation = RuleActivation.create(rule.getId(), rule.getKey()); List activeRuleChanges = qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation)); // Insert directly in database a rule parameter with a null value @@ -779,7 +779,7 @@ public class SearchActionTest { .setSeverity("MAJOR") .setStatus(RuleStatus.DEPRECATED) .setType(RuleType.VULNERABILITY)); - RuleActivation activation = RuleActivation.create(rule2.getKey(), null, null); + RuleActivation activation = RuleActivation.create(rule2.getId(), rule2.getKey(), null, null); qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation)); // on other language, not activated => no match