Search in sources :

Example 26 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class RuleIndexTest method search_by_profile_and_inheritance.

@Test
public void search_by_profile_and_inheritance() {
    indexRules(newDoc(RULE_KEY_1), newDoc(RULE_KEY_2), newDoc(RULE_KEY_3), newDoc(RULE_KEY_4));
    ActiveRuleKey activeRuleKey1 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1);
    ActiveRuleKey activeRuleKey2 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2);
    ActiveRuleKey activeRuleKey3 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_3);
    indexActiveRules(ActiveRuleDocTesting.newDoc(activeRuleKey1), ActiveRuleDocTesting.newDoc(activeRuleKey2), ActiveRuleDocTesting.newDoc(activeRuleKey3), // Profile 2 is a child a profile 1
    ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1)).setInheritance(INHERITED.name()), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_2)).setInheritance(OVERRIDES.name()), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_3)).setInheritance(INHERITED.name()));
    // 0. get all rules
    assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).hasSize(4);
    // 1. get all active rules
    assertThat(index.search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).hasSize(3);
    // 2. get all inactive rules.
    assertThat(index.search(new RuleQuery().setActivation(false), new SearchOptions()).getIds()).containsOnly(RULE_KEY_4);
    // 3. get Inherited Rules on profile1
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY1).setInheritance(ImmutableSet.of(INHERITED.name())), new SearchOptions()).getIds()).isEmpty();
    // 4. get Inherited Rules on profile2
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY2).setInheritance(ImmutableSet.of(INHERITED.name())), new SearchOptions()).getIds()).hasSize(2);
    // 5. get Overridden Rules on profile1
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY1).setInheritance(ImmutableSet.of(OVERRIDES.name())), new SearchOptions()).getIds()).isEmpty();
    // 6. get Overridden Rules on profile2
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY2).setInheritance(ImmutableSet.of(OVERRIDES.name())), new SearchOptions()).getIds()).hasSize(1);
    // 7. get Inherited AND Overridden Rules on profile1
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY1).setInheritance(ImmutableSet.of(INHERITED.name(), OVERRIDES.name())), new SearchOptions()).getIds()).isEmpty();
    // 8. get Inherited AND Overridden Rules on profile2
    assertThat(index.search(new RuleQuery().setActivation(true).setQProfileKey(QUALITY_PROFILE_KEY2).setInheritance(ImmutableSet.of(INHERITED.name(), OVERRIDES.name())), new SearchOptions()).getIds()).hasSize(3);
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Test(org.junit.Test)

Aggregations

ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)26 Test (org.junit.Test)18 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)9 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)7 RuleKey (org.sonar.api.rule.RuleKey)6 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 BadRequestException (org.sonar.server.exceptions.BadRequestException)3 HashMap (java.util.HashMap)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 RuleDto (org.sonar.db.rule.RuleDto)2 SearchOptions (org.sonar.server.es.SearchOptions)2 RuleQuery (org.sonar.server.rule.index.RuleQuery)2 Multimap (com.google.common.collect.Multimap)1 RuleParam (org.sonar.api.rules.RuleParam)1 DbSession (org.sonar.db.DbSession)1 Platform (org.sonar.server.platform.Platform)1 ServerTester (org.sonar.server.tester.ServerTester)1