}
@Test
public void search_by_profile_and_inheritance() throws InterruptedException {
QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
QualityProfileDto qualityProfileDto2 = QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY);
db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);
RuleDto rule1 = RuleTesting.newDto(RuleKey.of("xoo", "S001"));
RuleDto rule2 = RuleTesting.newDto(RuleKey.of("xoo", "S002"));
RuleDto rule3 = RuleTesting.newDto(RuleKey.of("xoo", "S003"));
RuleDto rule4 = RuleTesting.newDto(RuleKey.of("xoo", "S004"));
dao.insert(dbSession, rule1, rule2, rule3, rule4);
db.activeRuleDao().insert(
dbSession,
ActiveRuleDto.createFor(qualityProfileDto1, rule1)
.setSeverity("BLOCKER"),
ActiveRuleDto.createFor(qualityProfileDto1, rule2)
.setSeverity("BLOCKER"),
ActiveRuleDto.createFor(qualityProfileDto1, rule3)
.setSeverity("BLOCKER"),
ActiveRuleDto.createFor(qualityProfileDto2, rule1)
.setSeverity("MINOR")
.setInheritance(ActiveRule.Inheritance.INHERITED.name()),
ActiveRuleDto.createFor(qualityProfileDto2, rule2)
.setSeverity("BLOCKER")
.setInheritance(ActiveRule.Inheritance.OVERRIDES.name()),
ActiveRuleDto.createFor(qualityProfileDto2, rule3)
.setSeverity("BLOCKER")
.setInheritance(ActiveRule.Inheritance.INHERITED.name())
);
dbSession.commit();
// 0. get all rules
Result<Rule> result = index.search(new RuleQuery(),
new QueryContext());
assertThat(result.getHits()).hasSize(4);
// 1. get all active rules
result = index.search(new RuleQuery().setActivation(true),
new QueryContext());
assertThat(result.getHits()).hasSize(3);
// 2. get all inactive rules.
result = index.search(new RuleQuery().setActivation(false),
new QueryContext());
assertThat(result.getHits()).hasSize(1);
assertThat(result.getHits().get(0).name()).isEqualTo(rule4.getName());
// 3. get Inherited Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(0);
// 4. get Inherited Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(2);
// 5. get Overridden Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(0);
// 6. get Overridden Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(1);
// 7. get Inherited AND Overridden Rules on profile1
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto1.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(0);
// 8. get Inherited AND Overridden Rules on profile2
result = index.search(new RuleQuery().setActivation(true)
.setQProfileKey(qualityProfileDto2.getKey())
.setInheritance(ImmutableSet.of(
ActiveRule.Inheritance.INHERITED.name(), ActiveRule.Inheritance.OVERRIDES.name())),
new QueryContext()
);
assertThat(result.getHits()).hasSize(3);