@Test
  public void index_all_project() {
    componentDbTester.insertProjectAndSnapshot(newProjectDto());
    componentDbTester.insertProjectAndSnapshot(newProjectDto());
    componentDbTester.insertProjectAndSnapshot(newProjectDto());

    underTest.index();

    assertThat(esTester.countDocuments(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).isEqualTo(3);
  }
  @Test
  public void index_one_project() throws Exception {
    ComponentDto project = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project);
    componentDbTester.insertProjectAndSnapshot(newProjectDto());

    underTest.index(project.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project.uuid());
  }
  @Test
  public void delete_project() {
    ComponentDto project1 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project1);
    ComponentDto project2 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project2);
    ComponentDto project3 = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project3);
    underTest.index();
    authorizationIndexerTester.indexProjectPermission(project1.uuid(), emptyList(), emptyList());
    authorizationIndexerTester.indexProjectPermission(project2.uuid(), emptyList(), emptyList());
    authorizationIndexerTester.indexProjectPermission(project3.uuid(), emptyList(), emptyList());

    underTest.deleteProject(project1.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project2.uuid(), project3.uuid());
    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION))
        .containsOnly(project2.uuid(), project3.uuid());
  }
  @Test
  public void does_nothing_when_deleting_unknown_project() throws Exception {
    ComponentDto project = newProjectDto();
    componentDbTester.insertProjectAndSnapshot(project);
    underTest.index();
    authorizationIndexerTester.indexProjectPermission(project.uuid(), emptyList(), emptyList());

    underTest.deleteProject("UNKNOWN");

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES))
        .containsOnly(project.uuid());
    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION))
        .containsOnly(project.uuid());
  }
  @Test
  public void update_existing_document_when_indexing_one_project() throws Exception {
    String uuid = "PROJECT-UUID";
    esTester.putDocuments(
        INDEX_PROJECT_MEASURES,
        TYPE_PROJECT_MEASURES,
        new ProjectMeasuresDoc()
            .setId(uuid)
            .setKey("Old Key")
            .setName("Old Name")
            .setAnalysedAt(new Date(1_000_000L)));
    ComponentDto project = newProjectDto(uuid).setKey("New key").setName("New name");
    SnapshotDto analysis = componentDbTester.insertProjectAndSnapshot(project);

    underTest.index(project.uuid());

    assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).containsOnly(uuid);
    SearchRequestBuilder request =
        esTester
            .client()
            .prepareSearch(INDEX_PROJECT_MEASURES)
            .setTypes(TYPE_PROJECT_MEASURES)
            .setQuery(
                boolQuery()
                    .must(matchAllQuery())
                    .filter(
                        boolQuery()
                            .must(termQuery("_id", uuid))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_KEY, "New key"))
                            .must(termQuery(ProjectMeasuresIndexDefinition.FIELD_NAME, "New name"))
                            .must(
                                termQuery(
                                    ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT,
                                    new Date(analysis.getCreatedAt())))));
    assertThat(request.get().getHits()).hasSize(1);
  }