@Test
  public void bulk_index_active_rules_from_ids() throws IOException {
    when(myBatis.openSession(false)).thenReturn(session);

    List<Integer> ids = newArrayList(1);
    when(activeRuleDao.selectByIds(ids, session))
        .thenReturn(
            newArrayList(
                new ActiveRuleDto()
                    .setId(1)
                    .setProfileId(10)
                    .setRuleId(1)
                    .setSeverity(Severity.MAJOR)
                    .setParentId(5)));
    when(activeRuleDao.selectParamsByActiveRuleIds(ids, session))
        .thenReturn(
            newArrayList(
                new ActiveRuleParamDto()
                    .setId(1)
                    .setActiveRuleId(1)
                    .setRulesParameterId(1)
                    .setKey("key")
                    .setValue("RuleWithParameters")));

    esActiveRule.bulkIndexActiveRules(ids);
    assertThat(esSetup.exists("rules", "active_rule", "1"));

    SearchHit[] parentHit =
        esSetup
            .client()
            .prepareSearch("rules")
            .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10)))
            .execute()
            .actionGet()
            .getHits()
            .getHits();
    assertThat(parentHit).hasSize(1);
    assertThat(parentHit[0].getId()).isEqualTo("1");

    SearchHit[] childHit =
        esSetup
            .client()
            .prepareSearch("rules")
            .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters")))
            .execute()
            .actionGet()
            .getHits()
            .getHits();
    assertThat(childHit).hasSize(1);
    assertThat(childHit[0].getId()).isEqualTo("1");
  }
  @Test
  public void bulk_index_active_rules_checking_into_db() throws IOException {
    List<ActiveRuleDto> activeRules =
        newArrayList(
            new ActiveRuleDto()
                .setId(1)
                .setProfileId(10)
                .setRuleId(1)
                .setSeverity(Severity.MAJOR)
                .setParentId(5)
                .setNoteData("polop")
                .setNoteCreatedAt(new Date())
                .setNoteUserLogin("godin"));

    DbSession session = mock(DbSession.class);
    when(myBatis.openSession(false)).thenReturn(session);
    when(activeRuleDao.selectAll(session)).thenReturn(activeRules);
    when(activeRuleDao.selectAllParams(session))
        .thenReturn(Lists.<ActiveRuleParamDto>newArrayList());

    esActiveRule.bulkRegisterActiveRules();
    assertThat(esSetup.exists("rules", "active_rule", "1"));

    SearchHit[] parentHit =
        esSetup
            .client()
            .prepareSearch("rules")
            .setPostFilter(hasChildFilter("active_rule", termFilter("profileId", 10)))
            .execute()
            .actionGet()
            .getHits()
            .getHits();
    assertThat(parentHit).hasSize(1);
    assertThat(parentHit[0].getId()).isEqualTo("1");

    SearchHit[] childHit =
        esSetup
            .client()
            .prepareSearch("rules")
            .setPostFilter(hasParentFilter("rule", termFilter("key", "RuleWithParameters")))
            .execute()
            .actionGet()
            .getHits()
            .getHits();
    assertThat(childHit).hasSize(1);
    assertThat(childHit[0].getId()).isEqualTo("1");
  }
  @Override
  public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
    ProjectReferentials ref = new ProjectReferentials();

    String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP);

    for (Language language : languages.all()) {
      org.sonar.batch.protocol.input.QProfile profile = null;
      if (StringUtils.isNotBlank(defaultName)) {
        profile = loadDefaultQProfile(defaultName, language.getKey());
      }
      if (profile == null) {
        profile = loadQProfile(settings, language.getKey());
      }
      if (profile != null) {
        ref.addQProfile(profile);
      }
    }

    for (QProfile qProfile : ref.qProfiles()) {
      ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId =
          ArrayListMultimap.create();
      for (ActiveRuleParamDto dto : activeRuleDao.selectParamsByProfileKey(qProfile.key())) {
        paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
      }

      for (ActiveRuleDto activeDto : activeRuleDao.selectByProfileKey(qProfile.key())) {
        Rule rule = ruleFinder.findById(activeDto.getRuleId());
        if (rule != null) {
          String internalKey;
          Rule template = rule.getTemplate();
          if (template != null) {
            internalKey = template.getConfigKey();
          } else {
            internalKey = rule.getConfigKey();
          }
          ActiveRule activeRule =
              new ActiveRule(
                  rule.ruleKey().repository(),
                  rule.ruleKey().rule(),
                  rule.getName(),
                  activeDto.getSeverityString(),
                  internalKey,
                  rule.getLanguage());

          // load parameter values
          for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
            activeRule.params().put(paramDto.getKey(), paramDto.getValue());
          }

          // load default values
          for (RuleParam param : rule.getParams()) {
            if (!activeRule.params().containsKey(param.getKey())) {
              activeRule.params().put(param.getKey(), param.getDefaultValue());
            }
          }

          ref.addActiveRule(activeRule);
        }
      }
    }

    return ref;
  }