@Override
  public void execute(ComputationStep.Context context) {
    try (DbSession dbSession = dbClient.openSession(false)) {
      branchPersister.persist(dbSession);
      projectPersister.persist(dbSession);

      String projectUuid = treeRootHolder.getRoot().getUuid();

      // safeguard, reset all rows to b-changed=false
      dbClient.componentDao().resetBChangedForBranchUuid(dbSession, projectUuid);

      Map<String, ComponentDto> existingDtosByUuids = indexExistingDtosByUuids(dbSession);
      boolean isRootPrivate = isRootPrivate(treeRootHolder.getRoot(), existingDtosByUuids);

      // Insert or update the components in database. They are removed from existingDtosByUuids
      // at the same time.
      new PathAwareCrawler<>(new PersistComponentStepsVisitor(existingDtosByUuids, dbSession))
        .visit(treeRootHolder.getRoot());

      disableRemainingComponents(dbSession, existingDtosByUuids.values());
      dbClient.componentDao().setPrivateForBranchUuidWithoutAudit(dbSession, projectUuid, isRootPrivate);
      dbSession.commit();
    }
  }
@Test
  public void should_fail_if_project_is_not_stored_in_database_yet() {
    TreeRootHolder treeRootHolder = mock(TreeRootHolder.class);
    Component component = mock(Component.class);
    DbClient dbClient = mock(DbClient.class);
    ComponentDao componentDao = mock(ComponentDao.class);
    String projectKey = randomAlphabetic(20);

    doReturn(component).when(treeRootHolder).getRoot();
    doReturn(projectKey).when(component).getKey();
    doReturn(componentDao).when(dbClient).componentDao();
    doReturn(emptyList()).when(componentDao).selectByBranchUuid(eq(projectKey), any(DbSession.class));

    assertThatThrownBy(() -> new PersistComponentsStep(
      dbClient,
      treeRootHolder,
      System2.INSTANCE,
      mock(MutableDisabledComponentsHolder.class),
      mock(BranchPersister.class),
      mock(ProjectPersister.class)).execute(new TestComputationStepContext()))
      .isInstanceOf(IllegalStateException.class)
      .hasMessageContaining("The project '" + projectKey + "' is not stored in the database, during a project analysis");
  }