import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ServerSide;
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.es.SearchOptions;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleQuery;
}
}
- @CheckForNull
- public ActiveRule getActiveRule(ActiveRuleKey key) {
- return activeRuleIndex.getNullableByKey(key);
- }
-
- public List<ActiveRule> findActiveRulesByRule(RuleKey key) {
- return activeRuleIndex.findByRule(key);
- }
-
- public Iterator<ActiveRuleDoc> findActiveRulesByProfile(String key) {
- return activeRuleIndex.findByProfile(key);
- }
-
public Map<String, Long> countAllActiveRules() {
return activeRuleIndex.countAllByQualityProfileKey();
}
*/
package org.sonar.server.qualityprofile.index;
-import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-import org.elasticsearch.action.get.GetRequestBuilder;
-import org.elasticsearch.action.get.GetResponse;
+import javax.annotation.Nullable;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.search.SearchType;
-import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
-import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
-import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.server.es.BaseIndex;
import org.sonar.server.es.EsClient;
-import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.search.FacetValue;
-import static org.sonar.server.es.EsUtils.SCROLL_TIME_IN_MINUTES;
-import static org.sonar.server.es.EsUtils.scroll;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY;
import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_SEVERITY;
super(client);
}
- /**
- * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
- */
- @Deprecated
- @CheckForNull
- public ActiveRule getNullableByKey(ActiveRuleKey key) {
- GetRequestBuilder request = getClient().prepareGet()
- .setIndex(RuleIndexDefinition.INDEX)
- .setType(RuleIndexDefinition.TYPE_ACTIVE_RULE)
- .setId(key.toString())
- .setFetchSource(true)
- .setRouting(key.ruleKey().repository());
-
- GetResponse response = request.get();
- if (response.isExists()) {
- return new ActiveRuleDoc(response.getSource());
- }
- return null;
- }
-
- /**
- * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
- */
- @Deprecated
- public List<ActiveRule> findByRule(RuleKey key) {
- SearchRequestBuilder request = getClient().prepareSearch(RuleIndexDefinition.INDEX)
- .setQuery(QueryBuilders
- .hasParentQuery(RuleIndexDefinition.TYPE_RULE,
- QueryBuilders.idsQuery(RuleIndexDefinition.TYPE_RULE)
- .addIds(key.toString())
- ))
- .setRouting(key.repository())
- .setSize(Integer.MAX_VALUE);
-
- SearchResponse response = request.get();
-
- List<ActiveRule> activeRules = new ArrayList<>();
- for (SearchHit hit : response.getHits()) {
- activeRules.add(new ActiveRuleDoc(hit.getSource()));
- }
- return activeRules;
- }
-
- /**
- * @deprecated since 5.5, use {@link org.sonar.db.qualityprofile.ActiveRuleDao instead}
- */
- @Deprecated
- public Iterator<ActiveRuleDoc> findByProfile(String key) {
- SearchRequestBuilder request = getClient().prepareSearch(RuleIndexDefinition.INDEX)
- .setTypes(RuleIndexDefinition.TYPE_ACTIVE_RULE)
- .setSearchType(SearchType.SCAN)
- .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES))
- .setSize(100)
- .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.boolFilter()
- .must(FilterBuilders.termFilter(RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_KEY, key))
- .mustNot(FilterBuilders.hasParentFilter(RuleIndexDefinition.TYPE_RULE,
- FilterBuilders.termFilter(RuleIndexDefinition.FIELD_RULE_STATUS, RuleStatus.REMOVED.name())))));
-
- SearchResponse response = request.get();
- return scroll(getClient(), response.getScrollId(), ToDoc.INSTANCE);
- }
-
public Map<String, Long> countAllByQualityProfileKey() {
return countByField(FIELD_ACTIVE_RULE_PROFILE_KEY,
FilterBuilders.hasParentFilter(TYPE_RULE,
SearchResponse response = request.get();
- Terms values =
- response.getAggregations().get(indexField);
+ Terms values = response.getAggregations().get(indexField);
for (Terms.Bucket value : values.getBuckets()) {
counts.put(value.getKey(), value.getDocCount());
return stats;
}
- private Multimap<String, FacetValue> processAggregations(Aggregations aggregations) {
+ private Multimap<String, FacetValue> processAggregations(@Nullable Aggregations aggregations) {
Multimap<String, FacetValue> stats = ArrayListMultimap.create();
if (aggregations != null) {
for (Aggregation aggregation : aggregations.asList()) {
return stats;
}
- private enum ToDoc implements Function<Map<String, Object>, ActiveRuleDoc> {
- INSTANCE;
-
- @Override
- public ActiveRuleDoc apply(@Nonnull Map<String, Object> input) {
- return new ActiveRuleDoc(input);
- }
- }
-
}
*/
package org.sonar.server.qualityprofile;
-import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import java.io.StringReader;
import java.io.StringWriter;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
-import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
+import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.sonar.db.qualityprofile.ActiveRuleDto.INHERITED;
+import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
+import static org.sonar.db.rule.RuleTesting.XOO_X1;
+import static org.sonar.db.rule.RuleTesting.XOO_X2;
+import static org.sonar.db.rule.RuleTesting.newDto;
+import static org.sonar.db.rule.RuleTesting.newXooX1;
+import static org.sonar.db.rule.RuleTesting.newXooX2;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_NAME;
+import static org.sonar.server.qualityprofile.QProfileTesting.newXooP1;
+import static org.sonar.server.qualityprofile.QProfileTesting.newXooP2;
public class QProfileBackuperMediumTest {
activeRuleIndexer.setEnabled(true);
// create pre-defined rules
- RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR").setLanguage("xoo");
- RuleDto xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR").setLanguage("xoo");
+ RuleDto xooRule1 = newXooX1().setSeverity("MINOR").setLanguage("xoo");
+ RuleDto xooRule2 = newXooX2().setSeverity("MAJOR").setLanguage("xoo");
db.ruleDao().insert(dbSession, xooRule1);
db.ruleDao().insert(dbSession, xooRule2);
db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
@Test
public void backup() throws Exception {
RuleKey blahRuleKey = RuleKey.of("blah", "my-rule");
- RuleDto blahRule = RuleTesting.newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
+ RuleDto blahRule = newDto(blahRuleKey).setSeverity("INFO").setLanguage("xoo");
db.ruleDao().insert(dbSession, blahRule);
dbSession.commit();
dbSession.clearCache();
ruleIndexer.index();
// create profile P1 with rules x2 and x1 activated
- db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
- RuleActivation activation1 = new RuleActivation(RuleTesting.XOO_X2).setSeverity("MINOR");
- RuleActivation activation2 = new RuleActivation(RuleTesting.XOO_X1);
+ db.qualityProfileDao().insert(dbSession, newXooP1());
+ RuleActivation activation1 = new RuleActivation(XOO_X2).setSeverity("MINOR");
+ RuleActivation activation2 = new RuleActivation(XOO_X1);
RuleActivation activation3 = new RuleActivation(blahRuleKey);
activation2.setSeverity(Severity.BLOCKER);
activation2.setParameter("max", "7");
- tester.get(RuleActivator.class).activate(dbSession, activation1, QProfileTesting.XOO_P1_NAME);
- tester.get(RuleActivator.class).activate(dbSession, activation2, QProfileTesting.XOO_P1_NAME);
- tester.get(RuleActivator.class).activate(dbSession, activation3, QProfileTesting.XOO_P1_NAME);
+ tester.get(RuleActivator.class).activate(dbSession, activation1, XOO_P1_NAME);
+ tester.get(RuleActivator.class).activate(dbSession, activation2, XOO_P1_NAME);
+ tester.get(RuleActivator.class).activate(dbSession, activation3, XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
StringWriter output = new StringWriter();
- tester.get(QProfileBackuper.class).backup(QProfileTesting.XOO_P1_KEY, output);
+ tester.get(QProfileBackuper.class).backup(XOO_P1_KEY, output);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
null);
+ // Check in db
QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession);
assertThat(profile).isNotNull();
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(profile.getKey()));
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
assertThat(activeRules).hasSize(1);
- ActiveRuleDoc activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ ActiveRuleDto activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.getInheritance()).isNull();
- ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
+
+ // Check in es
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profile.getKey()).setActivation(true))).containsOnly(XOO_X1);
}
@Test
public void restore_and_update_profile() throws Exception {
// create profile P1 with rules x1 and x2 activated
- db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
- RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+ db.qualityProfileDao().insert(dbSession, newXooP1());
+ RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
- tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
+ tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
- activation = new RuleActivation(RuleTesting.XOO_X2);
+ activation = new RuleActivation(XOO_X2);
activation.setSeverity(Severity.INFO);
- tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
+ tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
tester.get(QProfileBackuper.class).restore(new StringReader(
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)), null);
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ // Check in db
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
- ActiveRuleDoc activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ ActiveRuleDto activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.getInheritance()).isNull();
- ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
+
+ // Check in es
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).containsOnly(XOO_X1);
}
@Test
public void restore_child_profile() throws Exception {
// define two parent/child profiles
db.qualityProfileDao().insert(dbSession,
- QProfileTesting.newXooP1(),
- QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+ newXooP1(),
+ newXooP2().setParentKee(XOO_P1_KEY));
dbSession.commit();
// rule x1 is activated on parent profile (so inherited by child profile)
- RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+ RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
- tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+ tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-child.xml"), StandardCharsets.UTF_8)), null);
// parent profile is unchanged
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
- ActiveRuleDoc activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("INFO");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ ActiveRuleDto activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("INFO");
+ assertThat(activeRuleDoc.getInheritance()).isNull();
- ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("10");
// child profile overrides parent
- activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+ activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
assertThat(activeRules).hasSize(1);
activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.OVERRIDES);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.getInheritance()).isEqualTo(OVERRIDES);
- activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
public void restore_parent_profile() throws Exception {
// define two parent/child profiles
db.qualityProfileDao().insert(dbSession,
- QProfileTesting.newXooP1(),
- QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+ newXooP1(),
+ newXooP2().setParentKee(XOO_P1_KEY));
dbSession.commit();
// rule x1 is activated on parent profile (so inherited by child profile)
- RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+ RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
- tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+ tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore-parent.xml"), StandardCharsets.UTF_8)), null);
// parent profile is updated
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(1);
- ActiveRuleDoc activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
+ ActiveRuleDto activeRuleDoc = activeRules.get(0);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.getInheritance()).isNull();
- ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
assertThat(params.get(0).getValue()).isEqualTo("7");
// child profile is inherited
- activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
+ activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY);
assertThat(activeRules).hasSize(1);
activeRuleDoc = activeRules.get(0);
- assertThat(activeRuleDoc.severity()).isEqualTo("BLOCKER");
- assertThat(activeRuleDoc.inheritance()).isEqualTo(ActiveRule.Inheritance.INHERITED);
+ assertThat(activeRuleDoc.getSeverityString()).isEqualTo("BLOCKER");
+ assertThat(activeRuleDoc.getInheritance()).isEqualTo(INHERITED);
- activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.key());
+ activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRuleDoc.getKey());
params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("max");
public void keep_other_inherited_rules() throws Exception {
// define two parent/child profiles
db.qualityProfileDao().insert(dbSession,
- QProfileTesting.newXooP1(),
- QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY));
+ newXooP1(),
+ newXooP2().setParentKee(XOO_P1_KEY));
dbSession.commit();
// rule x1 is activated on parent profile and is inherited by child profile
- RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+ RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(Severity.INFO);
activation.setParameter("max", "10");
- tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_KEY);
+ tester.get(RuleActivator.class).activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
activeRuleIndexer.index();
// backup of child profile contains x2 but not x1
tester.get(QProfileBackuper.class).restore(new StringReader(
- Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), QProfileTesting.XOO_P2_NAME);
+ Resources.toString(getClass().getResource("QProfileBackuperMediumTest/keep_other_inherited_rules.xml"), StandardCharsets.UTF_8)), XOO_P2_NAME);
// x1 and x2
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P2_KEY));
- assertThat(activeRules).hasSize(2);
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY)).hasSize(2);
}
@Test
public void restore_and_override_profile_name() throws Exception {
tester.get(QProfileBackuper.class).restore(new StringReader(
Resources.toString(getClass().getResource("QProfileBackuperMediumTest/restore.xml"), StandardCharsets.UTF_8)),
- QProfileTesting.XOO_P3_NAME);
+ XOO_P3_NAME);
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(QProfileTesting.XOO_P1_KEY));
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY);
assertThat(activeRules).hasSize(0);
QualityProfileDto target = db.qualityProfileDao().selectByNameAndLanguage("P3", "xoo", dbSession);
assertThat(target).isNotNull();
- activeRules = Lists.newArrayList(tester.get(QProfileLoader.class).findActiveRulesByProfile(target.getKey()));
- assertThat(activeRules).hasSize(1);
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, target.getKey())).hasSize(1);
}
@Test
package org.sonar.server.qualityprofile;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
+import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
verifyOneActiveRule(dto.getKey(), expectedSeverity, expectedInheritance, expectedParams);
}
- private void verifyOneActiveRule(String profileKey, String expectedSeverity,
- @Nullable String expectedInheritance, Map<String, String> expectedParams) {
+ private void verifyOneActiveRule(String profileKey, String expectedSeverity, @Nullable String expectedInheritance, Map<String, String> expectedParams) {
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(index.findByProfile(profileKey));
+ // check in db
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileKey);
assertThat(activeRules).hasSize(1);
- ActiveRuleDoc activeRule = activeRules.get(0);
- assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
- assertThat(activeRule.inheritance()).isEqualTo(expectedInheritance == null ? ActiveRule.Inheritance.NONE : ActiveRule.Inheritance.valueOf(expectedInheritance));
+ ActiveRuleDto activeRule = activeRules.get(0);
+ assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
+ assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance);
// verify parameters
- ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.key());
+ ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, activeRule.getKey());
List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(expectedParams.size());
Map<String, ActiveRuleParamDto> paramsByKey = ActiveRuleParamDto.groupByKey(params);
String value = paramsByKey.get(entry.getKey()).getValue();
assertThat(value).isEqualTo(entry.getValue());
}
+
+ // check in es
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileKey).setActivation(true))).hasSize(1);
}
}
*/
package org.sonar.server.qualityprofile;
-import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
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.QualityProfileDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
- assertThat(loader.findActiveRulesByProfile(profileDto.getKey())).isEmpty();
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())).isEmpty();
QProfileResult result = exporters.importXml(profileDto, "XooProfileImporter", "<xml/>", dbSession);
dbSession.commit();
activeRuleIndexer.index(result.getChanges());
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profileDto.getKey()));
+ // Check in db
+ List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey());
assertThat(activeRules).hasSize(1);
- ActiveRule activeRule = activeRules.get(0);
- assertThat(activeRule.key().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
- assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
+ ActiveRuleDto activeRule = activeRules.get(0);
+ assertThat(activeRule.getKey().ruleKey()).isEqualTo(RuleKey.of("xoo", "R1"));
+ assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
+
+ // Check in es
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).containsOnly(RuleKey.of("xoo", "R1"));
}
@Test
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
+import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.MockUserSession;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
- assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(XOO_P1_KEY).setActivation(true))).isEmpty();
}
@Test
assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
- assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
- assertThat(activeRuleIndex.findByProfile(XOO_P2_KEY)).isEmpty();
- assertThat(activeRuleIndex.findByProfile(XOO_P3_KEY)).isEmpty();
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).isEmpty();
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P2_KEY)).isEmpty();
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P3_KEY)).isEmpty();
}
@Test
import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
defProfile.activateRule(
org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))),
- RulePriority.CRITICAL
- ).setParameter("acceptWhitespace", "true");
+ RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
register(new Rules() {
- @Override
- public void init(RulesDefinition.NewRepository repository) {
- RulesDefinition.NewRule x1 = repository.createRule("x1")
- .setName("x1 name")
- .setHtmlDescription("x1 desc")
- .setSeverity(MINOR);
- x1.createParam("acceptWhitespace")
- .setDefaultValue("false")
- .setType(RuleParamType.BOOLEAN)
- .setDescription("Accept whitespaces on the line");
- }},
- defProfile
- );
+ @Override
+ public void init(RulesDefinition.NewRepository repository) {
+ RulesDefinition.NewRule x1 = repository.createRule("x1")
+ .setName("x1 name")
+ .setHtmlDescription("x1 desc")
+ .setSeverity(MINOR);
+ x1.createParam("acceptWhitespace")
+ .setDefaultValue("false")
+ .setType(RuleParamType.BOOLEAN)
+ .setDescription("Accept whitespaces on the line");
+ }
+ },
+ defProfile);
RuleKey ruleKey = RuleKey.of("xoo", "x1");
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
tester.get(RuleActivator.class).activate(dbSession,
new RuleActivation(ruleKey).setSeverity(BLOCKER)
.setParameter("acceptWhitespace", "false"),
- profile.getKey()
- );
+ profile.getKey());
dbSession.commit();
-
// Verify severity and param has changed
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
// Severity and parameter value come back to origin after reset
activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
- ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
- assertThat(activeRule.severity()).isEqualTo(CRITICAL);
activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
.setDefaultValue("false")
.setType(RuleParamType.BOOLEAN)
.setDescription("Accept whitespaces on the line");
- }}, defProfile);
+ }
+ }, defProfile);
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
.setDefaultValue("true")
.setType(RuleParamType.BOOLEAN)
.setDescription("Accept whitespaces on the line");
- }}, defProfile);
+ }
+ }, defProfile);
reset.resetLanguage(ServerTester.Xoo.KEY);
package org.sonar.server.qualityprofile;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.io.Reader;
import org.sonar.server.activity.Activity;
import org.sonar.server.activity.ActivityService;
import org.sonar.server.es.SearchOptions;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;
+import org.sonar.server.rule.index.RuleQuery;
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<ActiveRuleDoc> activeRules = Lists.newArrayList(loader.findActiveRulesByProfile(profile.getKey()));
- assertThat(activeRules).hasSize(1);
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())).hasSize(1);
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey( profile.getKey()).setActivation(true))).hasSize(1);
}
@Test
RuleKey ruleKey = RuleKey.of("xoo", "deleted_rule");
tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, ActiveRuleKey.of(XOO_P1_KEY, ruleKey))
- .setParameter("max", "10").toActivity()
- );
+ .setParameter("max", "10").toActivity());
Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
assertThat(activities.getHits()).hasSize(1);
ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
.setSeverity(Severity.MAJOR)
.setParameter("max", "10")
- .toActivity()
- );
+ .toActivity());
Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
assertThat(activities.getHits()).hasSize(1);
tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, ruleKey))
.setSeverity(Severity.MAJOR)
.setParameter("max", "10")
- .toActivity()
- );
+ .toActivity());
dbSession.commit();
Result<QProfileActivity> activities = service.searchActivities(new QProfileActivityQuery(), new SearchOptions());
package org.sonar.server.qualityprofile;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.Message;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
+import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.sonar.api.rule.Severity.BLOCKER;
// 2. assert that all activation has been commit to DB and ES
dbSession.clearCache();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
- assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).hasSize(bulkSize);
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
assertThat(result.countSucceeded()).isEqualTo(bulkSize);
assertThat(result.countFailed()).isEqualTo(0);
}
// -> xoo rules x1, x2 and custom1
dbSession.clearCache();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
- assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).hasSize(3);
+ assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
assertThat(result.countSucceeded()).isEqualTo(3);
assertThat(result.countFailed()).isGreaterThan(0);
}
assertThat(found).as("Rule is not activated in db").isTrue();
}
- private void verifyHasActiveRuleInIndex(ActiveRuleKey activeRuleKey, String expectedSeverity,
- @Nullable String expectedInheritance) {
+ private void verifyHasActiveRuleInIndex(ActiveRuleKey activeRuleKey, String expectedSeverity, @Nullable String expectedInheritance) {
// verify es
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(activeRuleIndex.findByProfile(activeRuleKey.qProfile()));
- boolean found = false;
- for (ActiveRuleDoc activeRule : activeRules) {
- if (activeRule.key().equals(activeRuleKey)) {
- found = true;
- assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
- assertThat(activeRule.inheritance()).isEqualTo(expectedInheritance == null ? ActiveRule.Inheritance.NONE : ActiveRule.Inheritance.valueOf(expectedInheritance));
-
- // Dates should be set
- assertThat(activeRule.createdAt()).isNotNull();
- assertThat(activeRule.updatedAt()).isNotNull();
- }
- }
- assertThat(found).as("Rule is not activated in index").isTrue();
+ List<RuleKey> ruleKeys = newArrayList(tester.get(RuleIndex.class).searchAll(
+ new RuleQuery()
+ .setKey(activeRuleKey.ruleKey().toString())
+ .setQProfileKey(activeRuleKey.qProfile())
+ .setActivation(true)
+ .setInheritance(singleton(expectedInheritance == null ? ActiveRule.Inheritance.NONE.name() : ActiveRule.Inheritance.valueOf(expectedInheritance).name()))
+ .setActiveSeverities(singleton(expectedSeverity))));
+ assertThat(ruleKeys).as("Rule is not activated in index").hasSize(1);
}
private void verifyHasActiveRuleInDbAndIndex(ActiveRuleKey activeRuleKey, String expectedSeverity,
assertThat(activeRuleDtos).isEmpty();
// verify es
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(activeRuleIndex.findByProfile(key));
- assertThat(activeRules).isEmpty();
+ assertThat(tester.get(RuleIndex.class).searchAll(
+ new RuleQuery()
+ .setQProfileKey(key)
+ .setActivation(true))).isEmpty();
}
}
package org.sonar.server.qualityprofile.index;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.es.EsTester;
-import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.rule.index.RuleDoc;
import org.sonar.server.rule.index.RuleDocTesting;
import org.sonar.server.rule.index.RuleIndexDefinition;
assertThat(stats).hasSize(30);
}
- @Test
- public void get_by_key() {
- indexRules(RuleDocTesting.newDoc(RULE_KEY_1));
- ActiveRuleKey key = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1);
- indexActiveRules(ActiveRuleDocTesting.newDoc(key));
-
- assertThat(index.getNullableByKey(key)).isNotNull();
- assertThat(index.getNullableByKey(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2))).isNull();
- }
-
- @Test
- public void find_active_rules() {
- indexRules(
- RuleDocTesting.newDoc(RULE_KEY_1),
- RuleDocTesting.newDoc(RULE_KEY_2),
- RuleDocTesting.newDoc(RuleKey.of("xoo", "removed")).setStatus(RuleStatus.REMOVED.name()));
-
- indexActiveRules(
- ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)),
- ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2)),
- ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_2)),
- // Removed rule can still be activated for instance when removing the checkstyle plugin, active rules related on checkstyle are not
- // removed
- // because if the plugin is re-install, quality profiles using these rule are not changed.
- ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RuleKey.of("xoo", "removed"))));
-
- // 1. find by rule key
-
- // in es
- List<ActiveRule> activeRules = index.findByRule(RULE_KEY_1);
- assertThat(activeRules).hasSize(1);
- assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RULE_KEY_1);
-
- activeRules = index.findByRule(RULE_KEY_2);
- assertThat(activeRules).hasSize(2);
- assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RULE_KEY_2);
-
- activeRules = index.findByRule(RuleKey.of("unknown", "unknown"));
- assertThat(activeRules).isEmpty();
-
- // 2. find by profile
- List<ActiveRuleDoc> activeRuleDocs = Lists.newArrayList(index.findByProfile(QUALITY_PROFILE_KEY1));
- assertThat(activeRuleDocs).hasSize(2);
- assertThat(activeRuleDocs.get(0).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY1);
- assertThat(activeRuleDocs.get(1).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY1);
-
- activeRuleDocs = Lists.newArrayList(index.findByProfile(QUALITY_PROFILE_KEY2));
- assertThat(activeRuleDocs).hasSize(1);
- assertThat(activeRuleDocs.get(0).key().qProfile()).isEqualTo(QUALITY_PROFILE_KEY2);
-
- activeRuleDocs = Lists.newArrayList(index.findByProfile("unknown"));
- assertThat(activeRuleDocs).isEmpty();
- }
-
- @Test
- public void find_many_active_rules_by_profile() {
- int nb = 150;
- RuleDoc[] ruleDocs = new RuleDoc[nb];
- ActiveRuleDoc[] activeRuleDocs = new ActiveRuleDoc[nb];
- for (int i = 0; i < nb; i++) {
- RuleKey ruleKey = RuleKey.of("xoo", "S00" + i);
- ruleDocs[i] = RuleDocTesting.newDoc(ruleKey);
- activeRuleDocs[i] = ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, ruleKey));
- }
- indexRules(ruleDocs);
- indexActiveRules(activeRuleDocs);
-
- // verify index
- assertThat(index.findByProfile(QUALITY_PROFILE_KEY1)).hasSize(nb);
- }
-
- @Test
- public void find_many_active_rules_by_rule() {
- indexRules(RuleDocTesting.newDoc(RULE_KEY_1));
-
- int nb = 150;
- ActiveRuleDoc[] activeRuleDocs = new ActiveRuleDoc[nb];
- for (int i = 0; i < nb; i++) {
- activeRuleDocs[i] = ActiveRuleDocTesting.newDoc(ActiveRuleKey.of("qp" + i, RULE_KEY_1));
- }
- indexActiveRules(activeRuleDocs);
-
- // verify index
- assertThat(index.findByRule(RULE_KEY_1)).hasSize(nb);
- }
-
private void indexActiveRules(ActiveRuleDoc... docs) {
activeRuleIndexer.index(asList(docs).iterator());
}
*/
package org.sonar.server.qualityprofile.ws;
-import com.google.common.collect.Sets;
import java.io.IOException;
import java.io.Writer;
import org.apache.commons.lang.StringUtils;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.qualityprofile.QProfileTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.ws.WsTester;
import org.sonar.server.ws.WsTester.Result;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class ExportActionTest {
ProfileExporter exporter1 = newExporter("polop");
ProfileExporter exporter2 = newExporter("palap");
- ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
- when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>newHashSet().iterator());
-
- exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2},
+ exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, null, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2},
null);
wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
package org.sonar.server.qualityprofile.ws;
import com.google.common.collect.ImmutableSet;
+import java.util.Collections;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.QProfileTesting;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
// 0. Assert No Active Rule for profile
assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
- assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(0);
// 2. Assert ActiveRule with BLOCKER severity
assertThat(tester.get(RuleIndex.class).search(
session.commit();
// 2. Assert ActiveRule with MINOR severity
- assertThat(tester.get(ActiveRuleIndex.class).findByRule(rule0.getKey()).get(0).severity()).isEqualTo("MINOR");
+ assertThat(tester.get(ActiveRuleDao.class).selectByRule(session, rule0).get(0).getSeverityString()).isEqualTo("MINOR");
+ assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery()
+ .setQProfileKey(profile.getKey())
+ .setKey(rule0.getKey().toString())
+ .setActiveSeverities(Collections.singleton("MINOR"))
+ .setActivation(true))).hasSize(1);
}
@Test
*/
package org.sonar.server.rule;
-import com.google.common.collect.Lists;
-import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.es.SearchOptions;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.RuleActivator;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
// Verify there's no more active rule from custom rule
- List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey()));
- assertThat(activeRules).isEmpty();
+ assertThat(index.searchAll(new RuleQuery().setQProfileKey(profileDto.getKey()).setActivation(true))).isEmpty();
// Verify in index
- assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(templateRule.getKey());
+ assertThat(index.searchAll(new RuleQuery())).containsOnly(templateRule.getKey());
}
@Test