assertThat(rule.params().get("min")).isEqualTo("minimum");
}
@Test
public void find_active_rules() throws Exception {
QualityProfileDto profile1 = QProfileTesting.newXooP1();
QualityProfileDto profile2 = QProfileTesting.newXooP2();
db.qualityProfileDao().insert(dbSession, profile1, profile2);
RuleDto rule1 = RuleTesting.newXooX1().setSeverity(Severity.MAJOR);
RuleDto rule2 = RuleTesting.newXooX2().setSeverity(Severity.MAJOR);
RuleDto removedRule = RuleTesting.newDto(RuleKey.of("xoo", "removed")).setSeverity(Severity.MAJOR).setStatus(RuleStatus.REMOVED);
db.ruleDao().insert(dbSession, rule1, rule2, removedRule);
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule1).setSeverity(Severity.MINOR));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile1, rule2).setSeverity(Severity.BLOCKER));
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, rule2).setSeverity(Severity.CRITICAL));
// 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.
db.activeRuleDao().insert(dbSession, ActiveRuleDto.createFor(profile2, removedRule).setSeverity(Severity.MAJOR));
dbSession.commit();
// 1. find by rule key
// in db
dbSession.clearCache();
assertThat(db.activeRuleDao().findByRule(dbSession, rule1)).hasSize(1);
assertThat(db.activeRuleDao().findByRule(dbSession, rule2)).hasSize(2);
// in es
List<ActiveRule> activeRules = index.get(ActiveRuleIndex.class).findByRule(RuleTesting.XOO_X1);
assertThat(activeRules).hasSize(1);
assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleTesting.XOO_X1);
activeRules = index.get(ActiveRuleIndex.class).findByRule(RuleTesting.XOO_X2);
assertThat(activeRules).hasSize(2);
assertThat(activeRules.get(0).key().ruleKey()).isEqualTo(RuleTesting.XOO_X2);
activeRules = index.get(ActiveRuleIndex.class).findByRule(RuleTesting.XOO_X3);
assertThat(activeRules).isEmpty();
// 2. find by profile
activeRules = index.get(ActiveRuleIndex.class).findByProfile(profile1.getKey());
assertThat(activeRules).hasSize(2);
assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile1.getKey());
assertThat(activeRules.get(1).key().qProfile()).isEqualTo(profile1.getKey());
activeRules = index.get(ActiveRuleIndex.class).findByProfile(profile2.getKey());
assertThat(activeRules).hasSize(1);
assertThat(activeRules.get(0).key().qProfile()).isEqualTo(profile2.getKey());
activeRules = index.get(ActiveRuleIndex.class).findByProfile("unknown");
assertThat(activeRules).isEmpty();
}