From 3e8478588517ca8caee9f538e586543268579752 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 23 Feb 2016 20:55:17 +0100 Subject: [PATCH] SONAR-7330 QProfileLoader is now using new ActiveRuleIndex --- .../qualityprofile/QProfileComparison.java | 24 ++++++------- .../qualityprofile/QProfileExporters.java | 3 +- .../server/qualityprofile/QProfileLoader.java | 35 +++++++++---------- .../QProfileBackuperMediumTest.java | 13 +++---- .../qualityprofile/QProfileExportersTest.java | 12 +++---- .../QProfileServiceMediumTest.java | 5 +-- .../qualityprofile/ws/ExportActionTest.java | 17 ++++----- 7 files changed, 53 insertions(+), 56 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileComparison.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileComparison.java index 0deacd4f669..5fada8ad564 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileComparison.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileComparison.java @@ -23,16 +23,16 @@ import com.google.common.base.Preconditions; import com.google.common.collect.MapDifference; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.sonar.api.server.ServerSide; -import org.sonar.api.rule.RuleKey; -import org.sonar.db.DbSession; -import org.sonar.db.qualityprofile.QualityProfileDto; -import org.sonar.core.util.NonNullInputFunction; -import org.sonar.db.DbClient; - import java.util.Collection; import java.util.Map; import java.util.Set; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.server.ServerSide; +import org.sonar.core.util.NonNullInputFunction; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.qualityprofile.QualityProfileDto; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; @ServerSide public class QProfileComparison { @@ -66,8 +66,8 @@ public class QProfileComparison { result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey); Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey)); - Map leftActiveRulesByRuleKey = loadActiveRules(leftKey); - Map rightActiveRulesByRuleKey = loadActiveRules(rightKey); + Map leftActiveRulesByRuleKey = loadActiveRules(leftKey); + Map rightActiveRulesByRuleKey = loadActiveRules(rightKey); Set allRules = Sets.newHashSet(); allRules.addAll(leftActiveRulesByRuleKey.keySet()); @@ -99,10 +99,10 @@ public class QProfileComparison { } } - private Map loadActiveRules(String profileKey) { - return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction() { + private Map loadActiveRules(String profileKey) { + return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction() { @Override - protected RuleKey doApply(ActiveRule input) { + protected RuleKey doApply(ActiveRuleDoc input) { return input.key().ruleKey(); } }); 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 d154f217697..192349dba9a 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 @@ -45,6 +45,7 @@ import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; @ServerSide public class QProfileExporters { @@ -119,7 +120,7 @@ public class QProfileExporters { private RulesProfile wrap(QualityProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); - for (Iterator activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) { + for (Iterator activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) { ActiveRule activeRule = activeRuleIterator.next(); Rule rule = ruleFinder.findByKey(activeRule.key().ruleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.severity())); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java index 7d1b475e926..36343f1410c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLoader.java @@ -33,25 +33,24 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.QualityProfileDto; -import org.sonar.server.qualityprofile.index.ActiveRuleIndex; -import org.sonar.server.rule.index.RuleIndex; +import org.sonar.server.es.SearchOptions; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex2; +import org.sonar.server.rule.index.RuleIndex2; import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.search.FacetValue; -import org.sonar.server.search.IndexClient; -import org.sonar.server.search.QueryContext; -import org.sonar.server.user.UserSession; @ServerSide public class QProfileLoader { private final DbClient dbClient; - private final IndexClient index; - private final UserSession userSession; + private final ActiveRuleIndex2 activeRuleIndex; + private final RuleIndex2 ruleIndex; - public QProfileLoader(DbClient dbClient, IndexClient index, UserSession userSession) { + public QProfileLoader(DbClient dbClient, ActiveRuleIndex2 activeRuleIndex, RuleIndex2 ruleIndex) { this.dbClient = dbClient; - this.index = index; - this.userSession = userSession; + this.activeRuleIndex = activeRuleIndex; + this.ruleIndex = ruleIndex; } /** @@ -89,20 +88,20 @@ public class QProfileLoader { @CheckForNull public ActiveRule getActiveRule(ActiveRuleKey key) { - return index.get(ActiveRuleIndex.class).getNullableByKey(key); + return activeRuleIndex.getNullableByKey(key); } public List findActiveRulesByRule(RuleKey key) { - return index.get(ActiveRuleIndex.class).findByRule(key); + return activeRuleIndex.findByRule(key); } - public Iterator findActiveRulesByProfile(String key) { - return index.get(ActiveRuleIndex.class).findByProfile(key); + public Iterator findActiveRulesByProfile(String key) { + return activeRuleIndex.findByProfile(key); } public Map countAllActiveRules() { Map counts = new HashMap<>(); - for (Map.Entry entry : index.get(ActiveRuleIndex.class).countAllByQualityProfileKey().entrySet()) { + for (Map.Entry entry : activeRuleIndex.countAllByQualityProfileKey().entrySet()) { counts.put(entry.getKey(), entry.getValue()); } return counts; @@ -113,16 +112,16 @@ public class QProfileLoader { for (QualityProfileDto profile : this.findAll()) { keys.add(profile.getKey()); } - return index.get(ActiveRuleIndex.class).getStatsByProfileKeys(keys); + return activeRuleIndex.getStatsByProfileKeys(keys); } public long countDeprecatedActiveRulesByProfile(String key) { - return index.get(RuleIndex.class).search( + return ruleIndex.search( new RuleQuery() .setQProfileKey(key) .setActivation(true) .setStatuses(Lists.newArrayList(RuleStatus.DEPRECATED)), - new QueryContext(userSession).setLimit(0)).getTotal(); + new SearchOptions().setLimit(0)).getTotal(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java index 1adb893bb52..1a25af93adf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java @@ -44,6 +44,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.rule.RuleTesting; import org.sonar.server.db.DbClient; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; @@ -133,7 +134,7 @@ public class QProfileBackuperMediumTest { QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession); assertThat(profile).isNotNull(); - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey())); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey())); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER"); assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); @@ -160,7 +161,7 @@ public class QProfileBackuperMediumTest { tester.get(QProfileBackuper.class).restore(new StringReader( Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null); - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER"); assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); @@ -188,7 +189,7 @@ public class QProfileBackuperMediumTest { Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null); // parent profile is unchanged - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).severity()).isEqualTo("INFO"); assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); @@ -223,7 +224,7 @@ public class QProfileBackuperMediumTest { Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null); // parent profile is updated - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); assertThat(activeRules).hasSize(1); assertThat(activeRules.get(0).severity()).isEqualTo("BLOCKER"); assertThat(activeRules.get(0).inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); @@ -258,7 +259,7 @@ public class QProfileBackuperMediumTest { Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P2_NAME); // x1 and x2 - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY)); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY)); assertThat(activeRules).hasSize(2); } @@ -302,7 +303,7 @@ public class QProfileBackuperMediumTest { Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P3_NAME); - List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); + List activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY)); assertThat(activeRules).hasSize(0); QualityProfileDto target = db.qualityProfileDao().selectByNameAndLanguage("P3", "xoo", dbSession); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java index f0d3972bbd9..35ef9aba34c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java @@ -20,6 +20,10 @@ package org.sonar.server.qualityprofile; import com.google.common.collect.Lists; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -40,12 +44,8 @@ import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; import org.sonar.server.tester.ServerTester; - -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; -import java.util.List; import org.sonar.server.tester.UserSessionRule; import static org.assertj.core.api.Assertions.assertThat; @@ -138,7 +138,7 @@ public class QProfileExportersTest { exporters.importXml(profileDto, "XooProfileImporter", "", dbSession); dbSession.commit(); - List activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey())); + List activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey())); assertThat(activeRules).hasSize(1); ActiveRule activeRule = activeRules.get(0); assertThat(activeRule.key().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1")); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index aca431d0b84..dbb70389642 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -45,13 +45,14 @@ import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; +import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; import org.sonar.server.activity.Activity; import org.sonar.server.activity.ActivityService; import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer; -import org.sonar.db.rule.RuleTesting; import org.sonar.server.search.FacetValue; import org.sonar.server.search.Result; import org.sonar.server.tester.ServerTester; @@ -123,7 +124,7 @@ public class QProfileServiceMediumTest { assertThat(loader.getByKey(profile.getKey()).getLanguage()).isEqualTo("xoo"); assertThat(loader.getByKey(profile.getKey()).getName()).isEqualTo("New Profile"); - List activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey())); + List activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey())); assertThat(activeRules).hasSize(1); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java index 7123e6da435..d2cbc287147 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java @@ -41,15 +41,14 @@ import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.language.LanguageTesting; -import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.qualityprofile.QProfileBackuper; import org.sonar.server.qualityprofile.QProfileExporters; import org.sonar.server.qualityprofile.QProfileFactory; import org.sonar.server.qualityprofile.QProfileLoader; import org.sonar.server.qualityprofile.QProfileTesting; -import org.sonar.server.qualityprofile.index.ActiveRuleIndex; -import org.sonar.server.search.IndexClient; -import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.qualityprofile.index.ActiveRuleDoc; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex2; +import org.sonar.server.rule.index.RuleIndex2; import org.sonar.server.ws.WsTester; import org.sonar.server.ws.WsTester.Result; @@ -62,8 +61,6 @@ public class ExportActionTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public UserSessionRule userSessionRule = UserSessionRule.standalone(); WsTester wsTester; @@ -89,12 +86,10 @@ public class ExportActionTest { ProfileExporter exporter1 = newExporter("polop"); ProfileExporter exporter2 = newExporter("palap"); - IndexClient indexClient = mock(IndexClient.class); - ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class); - when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.newHashSet().iterator()); + ActiveRuleIndex2 activeRuleIndex = mock(ActiveRuleIndex2.class); + when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.newHashSet().iterator()); - when(indexClient.get(ActiveRuleIndex.class)).thenReturn(activeRuleIndex); - exporters = new QProfileExporters(new QProfileLoader(dbClient, indexClient, userSessionRule), null, null, new ProfileExporter[] {exporter1, exporter2}, null); + exporters = new QProfileExporters(new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex2.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null); wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class), -- 2.39.5