@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 remove_existing_indexation_task() {
    String reportTaskUuid = persistReportTasks();

    CeQueueDto task = new CeQueueDto();
    task.setUuid("uuid_2");
    task.setTaskType(BRANCH_ISSUE_SYNC);
    dbClient.ceQueueDao().insert(dbTester.getSession(), task);
    CeActivityDto activityDto = new CeActivityDto(task);
    activityDto.setStatus(Status.SUCCESS);
    dbClient.ceActivityDao().insert(dbTester.getSession(), activityDto);
    dbTester.commit();

    underTest.triggerOnIndexCreation();

    assertThat(dbClient.ceQueueDao().selectAllInAscOrder(dbTester.getSession())).extracting("uuid").containsExactly(reportTaskUuid);
    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), BRANCH_ISSUE_SYNC)).isEmpty();
    assertThat(dbClient.ceActivityDao().selectByTaskType(dbTester.getSession(), REPORT)).hasSize(1);
    assertThat(dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), new HashSet<>(List.of("uuid_2")))).isEmpty();

    assertThat(logTester.logs(Level.INFO))
      .contains(
        "1 pending indexing task found to be deleted...",
        "1 completed indexing task found to be deleted...",
        "Indexing task deletion complete.",
        "Deleting tasks characteristics...",
        "Tasks characteristics deletion complete.");
  }