@Test
  public void stats_for_all() {
    indexRules(newDoc(RULE_KEY_1), newDoc(RULE_KEY_2));

    ActiveRuleKey activeRuleKey1 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1);
    ActiveRuleKey activeRuleKey2 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2);

    indexActiveRules(
        ActiveRuleDocTesting.newDoc(activeRuleKey1).setSeverity(BLOCKER),
        ActiveRuleDocTesting.newDoc(activeRuleKey2).setSeverity(MINOR),
        // Profile 2 is a child a profile 1
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1))
            .setSeverity(MAJOR)
            .setInheritance(INHERITED.name()),
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_2))
            .setSeverity(BLOCKER)
            .setInheritance(OVERRIDES.name()));

    // 0. Test base case
    assertThat(tester.countDocuments(INDEX, TYPE_ACTIVE_RULE)).isEqualTo(4);

    // 1. Assert by term aggregation;
    Map<String, Multimap<String, FacetValue>> stats =
        index.getStatsByProfileKeys(ImmutableList.of(QUALITY_PROFILE_KEY1, QUALITY_PROFILE_KEY2));
    assertThat(stats).hasSize(2);
  }
  @Test
  public void count_all_by_quality_profile_key() {
    indexRules(RuleDocTesting.newDoc(RULE_KEY_1));

    indexActiveRules(
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)),
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1)));

    // 0. Test base case
    assertThat(tester.countDocuments(INDEX, TYPE_ACTIVE_RULE)).isEqualTo(2);

    // 1. Assert by term aggregation;
    assertThat(index.countAllByQualityProfileKey())
        .containsOnly(entry(QUALITY_PROFILE_KEY1, 1L), entry(QUALITY_PROFILE_KEY2, 1L));
  }
  private void verifyOneActiveRule(
      String profileKey,
      String expectedSeverity,
      @Nullable String expectedInheritance,
      Map<String, String> expectedParams) {

    List<ActiveRule> activeRules = Lists.newArrayList(index.findByProfile(profileKey));
    assertThat(activeRules).hasSize(1);
    ActiveRule activeRule = activeRules.get(0);
    assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
    assertThat(activeRule.inheritance())
        .isEqualTo(
            expectedInheritance == null
                ? ActiveRule.Inheritance.NONE
                : ActiveRule.Inheritance.valueOf(expectedInheritance));

    // verify parameters
    assertThat(activeRule.params()).hasSize(expectedParams.size());
    for (Map.Entry<String, String> entry : expectedParams.entrySet()) {
      String value = activeRule.params().get(entry.getKey());
      assertThat(value).isEqualTo(entry.getValue());
    }
  }
  /** SONAR-5844 */
  @Test
  public void stats_for_all_with_lof_of_profiles() {
    indexRules(RuleDocTesting.newDoc(RULE_KEY_1), RuleDocTesting.newDoc(RULE_KEY_2));

    indexActiveRules(
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)),
        ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1)));

    List<String> profileKeys = new ArrayList<>();
    List<ActiveRuleDoc> docs = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
      String profileKey = "profile-" + i;
      profileKeys.add(profileKey);
      docs.add(
          ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(profileKey, RULE_KEY_1))
              .setSeverity(BLOCKER));
      docs.add(
          ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(profileKey, RULE_KEY_2)).setSeverity(MAJOR));
    }
    indexActiveRules(docs.toArray(new ActiveRuleDoc[] {}));

    Map<String, Multimap<String, FacetValue>> stats = index.getStatsByProfileKeys(profileKeys);
    assertThat(stats).hasSize(30);
  }