From 57ef40dfa4071de478bfc184cc7f7846e76836b8 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 24 Feb 2016 21:18:21 +0100 Subject: [PATCH] SONAR-7330 QProfileReset is now using ActiveRuleIndexer --- .../server/qualityprofile/QProfileReset.java | 23 +++++--- .../QProfileResetMediumTest.java | 58 +++++++++++++------ 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java index 9b9bbc64297..beb198c2891 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java @@ -24,6 +24,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -35,13 +36,14 @@ import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.ValidationMessages; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleParamDto; -import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; @ServerSide public class QProfileReset { @@ -49,19 +51,22 @@ public class QProfileReset { private final DbClient db; private final QProfileFactory factory; private final RuleActivator activator; + private final ActiveRuleIndexer activeRuleIndexer; private final BuiltInProfiles builtInProfiles; private final ProfileDefinition[] definitions; - public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ProfileDefinition[] definitions) { + public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, BuiltInProfiles builtInProfiles, QProfileFactory factory, + ProfileDefinition[] definitions) { this.db = db; this.activator = activator; + this.activeRuleIndexer = activeRuleIndexer; this.builtInProfiles = builtInProfiles; this.factory = factory; this.definitions = definitions; } - public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory) { - this(db, activator, builtInProfiles, factory, new ProfileDefinition[0]); + public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) { + this(db, activator, activeRuleIndexer, builtInProfiles, factory, new ProfileDefinition[0]); } public Collection builtInProfileNamesForLanguage(String language) { @@ -89,7 +94,7 @@ public class QProfileReset { activation.setParameter(param.getParamKey(), param.getValue()); } } else { - for (RuleParamDto param : db.deprecatedRuleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) { + for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) { activation.setParameter(param.getName(), param.getDefaultValue()); } } @@ -97,7 +102,6 @@ public class QProfileReset { } } doReset(dbSession, profile, activations); - dbSession.commit(); } } finally { dbSession.close(); @@ -112,7 +116,6 @@ public class QProfileReset { try { QualityProfileDto profile = factory.getOrCreate(dbSession, profileName); BulkChangeResult result = doReset(dbSession, profile, activations); - dbSession.commit(); return result; } finally { dbSession.close(); @@ -147,13 +150,17 @@ public class QProfileReset { } } + List changes = new ArrayList<>(); + changes.addAll(result.getChanges()); for (RuleKey ruleKey : ruleToBeDeactivated) { try { - activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey)); + changes.addAll(activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey))); } catch (BadRequestException e) { // ignore, probably a rule inherited from parent that can't be deactivated } } + dbSession.commit(); + activeRuleIndexer.index(changes); return result; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java index 33f11227767..92e052ca286 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetMediumTest.java @@ -19,7 +19,8 @@ */ package org.sonar.server.qualityprofile; -import com.google.common.collect.ImmutableMap; +import java.util.List; +import javax.annotation.Nullable; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -28,34 +29,42 @@ import org.junit.Test; import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.Severity; 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.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.qualityprofile.ActiveRuleDao; +import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleKey; +import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.qualityprofile.QualityProfileDto; -import org.sonar.server.db.DbClient; import org.sonar.server.platform.Platform; -import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex2; import org.sonar.server.tester.ServerTester; - -import javax.annotation.Nullable; import org.sonar.server.tester.UserSessionRule; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.rule.Severity.BLOCKER; +import static org.sonar.api.rule.Severity.CRITICAL; +import static org.sonar.api.rule.Severity.MAJOR; +import static org.sonar.api.rule.Severity.MINOR; +// TODO Replace ServerTester by EsTester and DbTester public class QProfileResetMediumTest { static final XooRulesDefinition RULE_DEFS = new XooRulesDefinition(); static final XooProfileDefinition PROFILE_DEFS = new XooProfileDefinition(); @ClassRule - public static ServerTester tester = new ServerTester().addXoo().addComponents(PROFILE_DEFS, RULE_DEFS); + public static ServerTester tester = new ServerTester() + .withEsIndexes() + .addXoo().addComponents(PROFILE_DEFS, RULE_DEFS); + @Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); @@ -105,7 +114,7 @@ public class QProfileResetMediumTest { RulesDefinition.NewRule x1 = repository.createRule("x1") .setName("x1 name") .setHtmlDescription("x1 desc") - .setSeverity(Severity.MINOR); + .setSeverity(MINOR); x1.createParam("acceptWhitespace") .setDefaultValue("false") .setType(RuleParamType.BOOLEAN) @@ -120,23 +129,32 @@ public class QProfileResetMediumTest { // Change the severity and the value of the parameter in the active rule tester.get(RuleActivator.class).activate(dbSession, - new RuleActivation(ruleKey).setSeverity(Severity.BLOCKER) + new RuleActivation(ruleKey).setSeverity(BLOCKER) .setParameter("acceptWhitespace", "false"), profile.getKey() ); dbSession.commit(); + // Verify severity and param has changed - ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey); - assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER); - assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "false")); + ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey); + assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER); + List activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey); + assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace"); + assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false"); reset.resetLanguage(ServerTester.Xoo.KEY); + dbSession.commit(); // Severity and parameter value come back to origin after reset - activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey); - assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL); - assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true")); + activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey); + assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL); + ActiveRule activeRule = tester.get(ActiveRuleIndex2.class).getNullableByKey(activeRuleKey); + assertThat(activeRule.severity()).isEqualTo(CRITICAL); + + activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey); + assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace"); + assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true"); } @Test @@ -150,7 +168,7 @@ public class QProfileResetMediumTest { RulesDefinition.NewRule x1 = repository.createRule("x1") .setName("x1 name") .setHtmlDescription("x1 desc") - .setSeverity(Severity.MAJOR); + .setSeverity(MAJOR); x1.createParam("acceptWhitespace") .setDefaultValue("false") .setType(RuleParamType.BOOLEAN) @@ -167,7 +185,7 @@ public class QProfileResetMediumTest { RulesDefinition.NewRule x1 = repository.createRule("x1") .setName("x1 name") .setHtmlDescription("x1 desc") - .setSeverity(Severity.MAJOR); + .setSeverity(MAJOR); x1.createParam("acceptWhitespace") .setDefaultValue("true") .setType(RuleParamType.BOOLEAN) @@ -177,8 +195,10 @@ public class QProfileResetMediumTest { reset.resetLanguage(ServerTester.Xoo.KEY); // Parameter value come back to origin after reset - ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey); - assertThat(activeRule.params()).isEqualTo(ImmutableMap.of("acceptWhitespace", "true")); + List params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleKey(dbSession, activeRuleKey); + assertThat(params).hasSize(1); + assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace"); + assertThat(params.get(0).getValue()).isEqualTo("true"); } interface Rules { -- 2.39.5