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 {
result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey);
Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey));
- Map<RuleKey, ActiveRule> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
- Map<RuleKey, ActiveRule> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
+ Map<RuleKey, ActiveRuleDoc> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
+ Map<RuleKey, ActiveRuleDoc> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
Set<RuleKey> allRules = Sets.newHashSet();
allRules.addAll(leftActiveRulesByRuleKey.keySet());
}
}
- private Map<RuleKey, ActiveRule> loadActiveRules(String profileKey) {
- return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRule, RuleKey>() {
+ private Map<RuleKey, ActiveRuleDoc> loadActiveRules(String profileKey) {
+ return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRuleDoc, RuleKey>() {
@Override
- protected RuleKey doApply(ActiveRule input) {
+ protected RuleKey doApply(ActiveRuleDoc input) {
return input.key().ruleKey();
}
});
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 {
private RulesProfile wrap(QualityProfileDto profile) {
RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
- for (Iterator<ActiveRule> activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) {
+ for (Iterator<ActiveRuleDoc> 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()));
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;
}
/**
@CheckForNull
public ActiveRule getActiveRule(ActiveRuleKey key) {
- return index.get(ActiveRuleIndex.class).getNullableByKey(key);
+ return activeRuleIndex.getNullableByKey(key);
}
public List<ActiveRule> findActiveRulesByRule(RuleKey key) {
- return index.get(ActiveRuleIndex.class).findByRule(key);
+ return activeRuleIndex.findByRule(key);
}
- public Iterator<ActiveRule> findActiveRulesByProfile(String key) {
- return index.get(ActiveRuleIndex.class).findByProfile(key);
+ public Iterator<ActiveRuleDoc> findActiveRulesByProfile(String key) {
+ return activeRuleIndex.findByProfile(key);
}
public Map<String, Long> countAllActiveRules() {
Map<String, Long> counts = new HashMap<>();
- for (Map.Entry<String, Long> entry : index.get(ActiveRuleIndex.class).countAllByQualityProfileKey().entrySet()) {
+ for (Map.Entry<String, Long> entry : activeRuleIndex.countAllByQualityProfileKey().entrySet()) {
counts.put(entry.getKey(), entry.getValue());
}
return counts;
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();
}
}
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;
QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
assertThat(profile).isNotNull();
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
+ List<ActiveRuleDoc> 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);
tester.get(QProfileBackuper.class).restore(new StringReader(
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDoc> 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);
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null);
// parent profile is unchanged
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDoc> 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);
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
// parent profile is updated
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDoc> 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);
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P2_NAME);
// x1 and x2
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+ List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
assertThat(activeRules).hasSize(2);
}
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
QProfileTesting.XOO_P3_NAME);
- List<ActiveRule> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
assertThat(activeRules).hasSize(0);
QualityProfileDto target = db.qualityProfileDao().selectByNameAndLanguage("P3", "xoo", dbSession);
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;
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;
exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
dbSession.commit();
- List<ActiveRule> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
+ List<ActiveRuleDoc> 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"));
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;
assertThat(loader.getByKey(profile.getKey()).getLanguage()).isEqualTo("xoo");
assertThat(loader.getByKey(profile.getKey()).getName()).isEqualTo("New Profile");
- List<ActiveRule> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
+ List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
assertThat(activeRules).hasSize(1);
}
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;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
WsTester wsTester;
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.<ActiveRule>newHashSet().iterator());
+ ActiveRuleIndex2 activeRuleIndex = mock(ActiveRuleIndex2.class);
+ when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>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),