@Test
  public void update_existing_rules_and_forget_deleted_rules() {
    when(ruleDao.selectEnablesAndNonManual(session))
        .thenReturn(
            newArrayList(
                new RuleDto()
                    .setId(1)
                    .setRepositoryKey("xoo")
                    .setRuleKey("key1")
                    .setSeverity(Severity.MINOR),
                new RuleDto()
                    .setId(2)
                    .setRepositoryKey("xoo")
                    .setRuleKey("key2")
                    .setSeverity(Severity.MINOR)));
    assertThat(esSetup.exists("rules", "rule", "3")).isTrue();

    String[] ids = registry.reindex(session);
    registry.removeDeletedRules(ids);

    assertThat(registry.findIds(ImmutableMap.of("repositoryKey", "xoo")))
        .hasSize(2)
        .containsOnly(1, 2);
    assertThat(esSetup.exists("rules", "rule", "3")).isFalse();
  }
  @Test
  public void reindex_existing_rules() {
    assertThat(esSetup.exists("rules", "rule", "3")).isTrue();

    // Update severity to MAJOR
    when(ruleDao.selectEnablesAndNonManual(session))
        .thenReturn(
            newArrayList(
                new RuleDto()
                    .setId(3)
                    .setRepositoryKey("repo")
                    .setRuleKey("key")
                    .setSeverity(Severity.MAJOR)));

    registry.reindex();

    Map<String, Object> ruleDocument =
        esSetup
            .client()
            .prepareGet("rules", "rule", Integer.toString(3))
            .execute()
            .actionGet()
            .getSourceAsMap();
    assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3);
    assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo");
    assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key");
    assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MAJOR");
  }
  @Test
  public void
      index_overridden_function_if_both_default_and_overridden_functions_exists_when_indexing_rules() {
    when(ruleDao.selectEnablesAndNonManual(session))
        .thenReturn(
            newArrayList(
                new RuleDto()
                    .setId(10)
                    .setRepositoryKey("repo")
                    .setRuleKey("key1")
                    .setSeverity(Severity.MINOR)
                    // default and overridden debt values are set
                    .setDefaultSubCharacteristicId(11)
                    .setDefaultRemediationFunction("CONSTANT_ISSUE")
                    .setDefaultRemediationOffset("15min")
                    .setSubCharacteristicId(11)
                    .setRemediationFunction("LINEAR")
                    .setRemediationCoefficient("1h")));

    when(characteristicDao.selectCharacteristicsByIds(newHashSet(11), session))
        .thenReturn(
            newArrayList(
                new CharacteristicDto()
                    .setId(11)
                    .setKey("MODULARITY")
                    .setName("Modularity")
                    .setParentId(10)));
    when(characteristicDao.selectCharacteristicsByIds(newHashSet(10), session))
        .thenReturn(
            newArrayList(
                new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability")));

    registry.reindex();

    Map<String, Object> ruleDocument =
        esSetup
            .client()
            .prepareGet("rules", "rule", Integer.toString(10))
            .execute()
            .actionGet()
            .getSourceAsMap();
    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10);
    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11);
    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");

    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)).isEqualTo("LINEAR");
    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h");
    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isNull();
  }
  @Test
  public void index_one_rule() {
    RuleDto ruleDto =
        new RuleDto()
            .setId(3)
            .setRepositoryKey("repo")
            .setRuleKey("key")
            .setSeverity(Severity.MINOR)
            .setNoteData("noteData")
            .setNoteUserLogin("userLogin")
            .setDefaultSubCharacteristicId(11)
            .setDefaultRemediationFunction("LINEAR_OFFSET")
            .setDefaultRemediationCoefficient("1h")
            .setDefaultRemediationOffset("15min");

    when(ruleDao.selectParametersByRuleIds(newArrayList(3), session))
        .thenReturn(newArrayList(new RuleParamDto().setRuleId(3).setName("name")));

    when(ruleDao.selectTagsByRuleIds(newArrayList(3), session))
        .thenReturn(
            newArrayList(
                new RuleRuleTagDto().setRuleId(3).setTag("tag1").setType(RuleTagType.SYSTEM),
                new RuleRuleTagDto().setRuleId(3).setTag("tag2").setType(RuleTagType.SYSTEM),
                new RuleRuleTagDto().setRuleId(3).setTag("tag").setType(RuleTagType.ADMIN)));

    when(characteristicDao.selectById(11, session))
        .thenReturn(
            new CharacteristicDto()
                .setId(11)
                .setKey("MODULARITY")
                .setName("Modularity")
                .setParentId(10));
    when(characteristicDao.selectById(10, session))
        .thenReturn(new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"));

    registry.reindex(ruleDto);

    Map<String, Object> ruleDocument =
        esSetup
            .client()
            .prepareGet("rules", "rule", Integer.toString(3))
            .execute()
            .actionGet()
            .getSourceAsMap();
    assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3);
    assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo");
    assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key");
    assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MINOR");
    assertThat(ruleDocument.get(RuleDocument.FIELD_NOTE)).isNotNull();

    assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_PARAMS)).hasSize(1);
    assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_SYSTEM_TAGS)).hasSize(2);
    assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_ADMIN_TAGS)).hasSize(1);

    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10);
    assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11);
    assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");
    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION))
        .isEqualTo("LINEAR_OFFSET");
    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h");
    assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isEqualTo("15min");
  }