@Test
  public void shouldNotGrantDefaultPermissionsIfExistingProject() {
    setupData("shared");

    ResourcePermissions permissions = mock(ResourcePermissions.class);
    when(permissions.hasRoles(singleProject)).thenReturn(true);

    ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions);
    persister.saveProject(singleProject, null);

    verify(permissions, never()).grantDefaultRoles(singleProject);
  }
  // SONAR-1700
  @Test
  public void shouldRemoveRootIndexIfResourceIsProject() {
    setupData("shouldRemoveRootIndexIfResourceIsProject");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(singleProject, null);

    checkTables(
        "shouldRemoveRootIndexIfResourceIsProject",
        new String[] {"build_date", "created_at"},
        "projects",
        "snapshots");
  }
  @Test
  public void shouldSaveNewProject() {
    setupData("shared");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(singleProject, null);

    checkTables(
        "shouldSaveNewProject", new String[] {"build_date", "created_at"}, "projects", "snapshots");

    // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table
    ResourceModel model =
        getSession().getSingleResult(ResourceModel.class, "key", singleProject.getKey());
    assertThat(model.getCreatedAt()).isNotNull();
  }
  @Test
  public void shouldUpdateExistingResource() {
    setupData("shouldUpdateExistingResource");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    singleProject.setName("new name");
    singleProject.setDescription("new description");
    persister.saveProject(singleProject, null);

    checkTables(
        "shouldUpdateExistingResource",
        new String[] {"build_date", "created_at"},
        "projects",
        "snapshots");
  }
  @Test
  public void shouldSaveNewMultiModulesProject() {
    setupData("shared");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(multiModuleProject, null);
    persister.saveProject(moduleA, multiModuleProject);
    persister.saveProject(moduleB, multiModuleProject);
    persister.saveProject(moduleB1, moduleB);

    checkTables(
        "shouldSaveNewMultiModulesProject",
        new String[] {"build_date", "created_at"},
        "projects",
        "snapshots");
  }
  @Test
  public void shouldSaveNewDirectory() {
    setupData("shared");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(singleProject, null);
    persister.saveResource(
        singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo"));

    // check that the directory is attached to the project
    checkTables(
        "shouldSaveNewDirectory",
        new String[] {"build_date", "created_at"},
        "projects",
        "snapshots");
  }
  @Test
  public void shouldSaveNewLibrary() {
    setupData("shared");

    ResourcePersister persister =
        new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class));
    persister.saveProject(singleProject, null);
    persister.saveResource(
        singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));
    persister.saveResource(
        singleProject,
        new Library("junit:junit", "4.8.2")
            .setEffectiveKey("junit:junit")); // do nothing, already saved
    persister.saveResource(
        singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit"));

    checkTables(
        "shouldSaveNewLibrary", new String[] {"build_date", "created_at"}, "projects", "snapshots");
  }