@Override
  public void triggerOnIndexCreation() {

    try (DbSession dbSession = dbClient.openSession(false)) {

      // remove already existing indexing task, if any
      removeExistingIndexationTasks(dbSession);

      dbClient.branchDao().updateAllNeedIssueSync(dbSession);
      List<BranchDto> branchInNeedOfIssueSync = dbClient.branchDao().selectBranchNeedingIssueSync(dbSession);
      LOG.info("{} branch found in need of issue sync.", branchInNeedOfIssueSync.size());

      if (branchInNeedOfIssueSync.isEmpty()) {
        return;
      }

      List<String> projectUuids = branchInNeedOfIssueSync.stream().map(BranchDto::getProjectUuid).distinct().collect(toCollection(ArrayList<String>::new));
      LOG.info("{} projects found in need of issue sync.", projectUuids.size());

      sortProjectUuids(dbSession, projectUuids);

      Map<String, List<BranchDto>> branchesByProject = branchInNeedOfIssueSync.stream()
        .collect(Collectors.groupingBy(BranchDto::getProjectUuid));

      List<CeTaskSubmit> tasks = new ArrayList<>();
      for (String projectUuid : projectUuids) {
        List<BranchDto> branches = branchesByProject.get(projectUuid);
        for (BranchDto branch : branches) {
          tasks.add(buildTaskSubmit(branch));
        }
      }

      ceQueue.massSubmit(tasks);
      dbSession.commit();
    }
  }
@Test
  public void characteristics_are_defined() {
    BranchDto dto = new BranchDto()
      .setBranchType(BRANCH)
      .setKey("branch_1")
      .setUuid("branch_uuid1")
      .setProjectUuid("project_uuid1")
      .setIsMain(false);
    dbClient.branchDao().insert(dbTester.getSession(), dto);
    dbTester.commit();
    insertSnapshot("analysis_1", "project_uuid1", 1);

    BranchDto dto2 = new BranchDto()
      .setBranchType(PULL_REQUEST)
      .setKey("pr_1")
      .setUuid("pr_uuid_1")
      .setProjectUuid("project_uuid2")
      .setIsMain(false);
    dbClient.branchDao().insert(dbTester.getSession(), dto2);
    dbTester.commit();
    insertSnapshot("analysis_2", "project_uuid2", 2);

    underTest.triggerOnIndexCreation();

    ArgumentCaptor<Collection<CeTaskSubmit>> captor = ArgumentCaptor.forClass(Collection.class);
    verify(ceQueue, times(1)).massSubmit(captor.capture());
    List<Collection<CeTaskSubmit>> captures = captor.getAllValues();
    assertThat(captures).hasSize(1);
    Collection<CeTaskSubmit> tasks = captures.get(0);
    assertThat(tasks).hasSize(2);

    assertThat(tasks)
      .extracting(p -> p.getCharacteristics().get(BRANCH_TYPE),
        p -> p.getCharacteristics().get(CeTaskCharacteristics.BRANCH),
        p -> p.getCharacteristics().get(CeTaskCharacteristics.PULL_REQUEST))
      .containsExactlyInAnyOrder(
        tuple("BRANCH", "branch_1", null),
        tuple("PULL_REQUEST", null, "pr_1"));
  }