]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Use portfolios table in MoveAction WS
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 24 Aug 2021 16:17:20 +0000 (11:17 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 8 Sep 2021 20:03:35 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java

index d779c6845fa847a6c6ff5b01b22da958e109e0c4..dffc171354290caec1bf73336b8cfa42c0521c0a 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.project.ProjectDto;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Collections.singleton;
 import static java.util.stream.Collectors.groupingBy;
 import static java.util.stream.Collectors.mapping;
@@ -64,6 +65,7 @@ public class PortfolioDao implements Dao {
   }
 
   public void insert(DbSession dbSession, PortfolioDto portfolio) {
+    checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
     mapper(dbSession).insert(portfolio);
   }
 
@@ -80,16 +82,23 @@ public class PortfolioDao implements Dao {
     mapper(dbSession).deleteByUuids(portfolioUuids);
   }
 
-  public List<ReferenceDto> selectAllReferencesToPortfolios(DbSession dbSession) {
-    return mapper(dbSession).selectAllReferencesToPortfolios();
+  public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
+    return mapper(dbSession).selectTree(portfolioUuid);
   }
 
-  public List<ReferenceDto> selectAllReferencesToApplications(DbSession dbSession) {
-    return mapper(dbSession).selectAllReferencesToApplications();
+  public void update(DbSession dbSession, PortfolioDto portfolio) {
+    checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
+    portfolio.setUpdatedAt(system2.now());
+    mapper(dbSession).update(portfolio);
   }
 
-  public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
-    return mapper(dbSession).selectTree(portfolioUuid);
+  public Map<String, String> selectKeysByUuids(DbSession dbSession, Collection<String> uuids) {
+    return executeLargeInputs(uuids, uuids1 -> mapper(dbSession).selectByUuids(uuids1)).stream()
+      .collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey));
+  }
+
+  public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
+    mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
   }
 
   public void addReference(DbSession dbSession, String portfolioUuid, String referenceUuid) {
@@ -100,6 +109,14 @@ public class PortfolioDao implements Dao {
       .setCreatedAt(system2.now()));
   }
 
+  public List<ReferenceDto> selectAllReferencesToPortfolios(DbSession dbSession) {
+    return mapper(dbSession).selectAllReferencesToPortfolios();
+  }
+
+  public List<ReferenceDto> selectAllReferencesToApplications(DbSession dbSession) {
+    return mapper(dbSession).selectAllReferencesToApplications();
+  }
+
   public Set<String> getReferences(DbSession dbSession, String portfolioUuid) {
     return mapper(dbSession).selectReferences(portfolioUuid);
   }
@@ -112,6 +129,22 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectRootOfReferencers(referenceUuid);
   }
 
+  public void deleteReferencesTo(DbSession dbSession, String referenceUuid) {
+    mapper(dbSession).deleteReferencesTo(referenceUuid);
+  }
+
+  public void deleteAllReferences(DbSession dbSession) {
+    mapper(dbSession).deleteAllReferences();
+  }
+
+  public int deleteReference(DbSession dbSession, String portfolioUuid, String referenceUuid) {
+    return mapper(dbSession).deleteReference(portfolioUuid, referenceUuid);
+  }
+
+  public ReferenceDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) {
+    return mapper(dbSession).selectReference(portfolioUuid, referenceKey);
+  }
+
   public List<ProjectDto> getProjects(DbSession dbSession, String portfolioUuid) {
     return mapper(dbSession).selectProjects(portfolioUuid);
   }
@@ -132,15 +165,6 @@ public class PortfolioDao implements Dao {
       .setCreatedAt(system2.now()));
   }
 
-  public void update(DbSession dbSession, PortfolioDto portfolio) {
-    portfolio.setUpdatedAt(system2.now());
-    mapper(dbSession).update(portfolio);
-  }
-
-  private static PortfolioMapper mapper(DbSession session) {
-    return session.getMapper(PortfolioMapper.class);
-  }
-
   public void deleteProjects(DbSession dbSession, String portfolioUuid) {
     mapper(dbSession).deleteProjects(portfolioUuid);
   }
@@ -149,30 +173,16 @@ public class PortfolioDao implements Dao {
     mapper(dbSession).deleteProject(portfolioUuid, projectUuid);
   }
 
-  public void deleteReferencesTo(DbSession dbSession, String referenceUuid) {
-    mapper(dbSession).deleteReferencesTo(referenceUuid);
-  }
-
-  public Map<String, String> selectKeysByUuids(DbSession dbSession, Collection<String> uuids) {
-    return executeLargeInputs(uuids, uuids1 -> mapper(dbSession).selectByUuids(uuids1)).stream()
-      .collect(Collectors.toMap(PortfolioDto::getUuid, PortfolioDto::getKey));
-  }
-
-  public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
-    mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
-  }
-
-  public void deleteAllReferences(DbSession dbSession) {
-    mapper(dbSession).deleteAllReferences();
-
-  }
-
   public void deleteAllProjects(DbSession dbSession) {
     mapper(dbSession).deleteAllProjects();
   }
-
+  
   public List<PortfolioProjectDto> selectAllProjectsOfPortfolios(DbSession dbSession) {
     return mapper(dbSession).selectAllProjectsOfPortfolios();
   }
 
+  private static PortfolioMapper mapper(DbSession session) {
+    return session.getMapper(PortfolioMapper.class);
+  }
+
 }
index 6587896849bacb5f4f9f418378d1a9c3abd4d119..34c9f699659b67e7c86c1c726a2d24a61c5a2592 100644 (file)
@@ -81,6 +81,10 @@ public interface PortfolioMapper {
 
   void deleteAllReferences();
 
+  int deleteReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceUuid") String referenceUuid);
+
+  ReferenceDto selectReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey);
+
   void deleteAllProjects();
 
   List<PortfolioProjectDto> selectAllProjectsOfPortfolios();
index 938e314e1294c3da3deb648ed02f4bded953a432..089e4bad195a4c2aaaa8c4607294ba0f426bb42c 100644 (file)
     WHERE pj.kee=#{referenceKey,jdbcType=VARCHAR}
   </select>
 
+  <delete id="deleteReference">
+    DELETE FROM portfolio_references
+    WHERE
+      portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+    AND reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
+  </delete>
+
+  <select id="selectReference" resultType="org.sonar.db.portfolio.ReferenceDto">
+    (SELECT
+      source.uuid as sourceUuid,
+      source.root_uuid as sourceRootUuid,
+      target.uuid as targetUuid,
+      target.root_uuid as targetRootUuid
+    FROM portfolio_references pr
+     INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
+     INNER JOIN portfolios target ON pr.reference_uuid = target.uuid
+     WHERE pr.portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+     AND target.kee=#{referenceKey,jdbcType=VARCHAR}
+
+    UNION
+
+    SELECT
+      source.uuid as sourceUuid,
+      source.root_uuid as sourceRootUuid,
+      target.uuid as targetUuid,
+      target.uuid as targetRootUuid
+    FROM portfolio_references pr
+     INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
+     INNER JOIN projects target ON pr.reference_uuid = target.uuid AND target.qualifier = 'APP'
+     WHERE pr.portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+     AND target.kee=#{referenceKey,jdbcType=VARCHAR})
+  </select>
+
    <insert id="insert" parameterType="Portfolio">
     INSERT INTO portfolios (
       kee,
     description = #{description,jdbcType=VARCHAR},
     selection_mode = #{selectionMode,jdbcType=VARCHAR},
     selection_expression = #{selectionExpression,jdbcType=VARCHAR},
+    parent_uuid = #{parentUuid,jdbcType=VARCHAR},
+    root_uuid = #{rootUuid,jdbcType=VARCHAR},
     updated_at = #{updatedAt,jdbcType=BIGINT}
     where
     uuid = #{uuid,jdbcType=VARCHAR}
index afbd38a25b7d3b9dc74f22b2c44dd57d7b48088d..fb40b95eb77d458f997e6cdb82fbb2fd4f924c39 100644 (file)
@@ -26,23 +26,25 @@ import org.junit.Test;
 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.project.ProjectDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.assertj.core.api.Assertions.tuple;
 
 public class PortfolioDaoTest {
   private final System2 system2 = new AlwaysIncreasingSystem2(1L, 1);
-
   @Rule
   public DbTester db = DbTester.create(system2);
 
   private final PortfolioDao portfolioDao = new PortfolioDao(system2, UuidFactoryFast.getInstance());
+  private final DbSession session = db.getSession();
 
   @Test
   public void selectByKey_returns_empty_if_no_match() {
-    assertThat(portfolioDao.selectByKey(db.getSession(), "nonexisting")).isEmpty();
+    assertThat(portfolioDao.selectByKey(session, "nonexisting")).isEmpty();
   }
 
   @Test
@@ -52,129 +54,174 @@ public class PortfolioDaoTest {
     createPortfolio("p111", "p11", "p1");
     createPortfolio("p2", null, "p2");
 
-    assertThat(portfolioDao.selectAllRoots(db.getSession())).extracting("uuid")
+    assertThat(portfolioDao.selectAllRoots(session)).extracting("uuid")
       .containsExactlyInAnyOrder("p1", "p2");
   }
 
+  @Test
+  public void selectAll() {
+    createPortfolio("p1", null, "p1");
+    createPortfolio("p11", "p1", "p1");
+    createPortfolio("p111", "p11", "p1");
+    createPortfolio("p2", null, "p2");
+
+    assertThat(portfolioDao.selectAll(session)).extracting("uuid")
+      .containsExactlyInAnyOrder("p1", "p2", "p11", "p111");
+  }
+
   @Test
   public void selectByKey_returns_match() {
-    createPortfolio("name");
-    assertThat(portfolioDao.selectByKey(db.getSession(), "key_name")).isNotEmpty();
+    PortfolioDto portfolio = db.components().insertPrivatePortfolioDto();
+    assertThat(portfolioDao.selectByKey(session, portfolio.getKey())).isNotEmpty();
   }
 
   @Test
   public void selectByUuid_returns_empty_if_no_match() {
-    assertThat(portfolioDao.selectByUuid(db.getSession(), "nonexisting")).isEmpty();
+    assertThat(portfolioDao.selectByUuid(session, "nonexisting")).isEmpty();
   }
 
   @Test
   public void selectByUuid_returns_match() {
-    createPortfolio("name");
-    assertThat(portfolioDao.selectByUuid(db.getSession(), "name")).isNotEmpty();
-    assertThat(portfolioDao.selectByUuid(db.getSession(), "name").get())
-      .extracting("name", "key", "uuid", "description", "rootUuid", "parentUuid", "selectionMode", "selectionExpression", "createdAt", "updatedAt")
-      .containsExactly("name_name", "key_name", "name", "desc_name", "root", "parent", "mode", "exp", 1000L, 2000L);
+    db.components().insertPrivatePortfolioDto("name");
+    assertThat(portfolioDao.selectByUuid(session, "name")).isNotEmpty();
+    assertThat(portfolioDao.selectByUuid(session, "name").get())
+      .extracting("name", "key", "uuid", "description", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
+      .containsExactly("NAME_name", "KEY_name", "name", "DESCRIPTION_name", "name", null, "NONE", null);
+  }
+
+  @Test
+  public void insert_fails_if_root_is_inconsistent_with_parent() {
+    PortfolioDto portfolio = new PortfolioDto()
+      .setUuid("uuid")
+      .setParentUuid(null)
+      .setRootUuid("root");
+    assertThatThrownBy(() -> portfolioDao.insert(session, portfolio))
+      .isInstanceOf(IllegalArgumentException.class);
+
+    PortfolioDto portfolio2 = new PortfolioDto()
+      .setUuid("uuid")
+      .setParentUuid("parent")
+      .setRootUuid("uuid");
+    assertThatThrownBy(() -> portfolioDao.insert(session, portfolio2))
+      .isInstanceOf(IllegalArgumentException.class);
   }
 
   @Test
   public void update_portfolio() {
-    createPortfolio("name");
-    PortfolioDto dto = portfolioDao.selectByUuid(db.getSession(), "name").get();
+    db.components().insertPrivatePortfolioDto("name");
+    PortfolioDto dto = portfolioDao.selectByUuid(session, "name").get();
     dto.setSelectionMode("newMode");
     dto.setSelectionExpression("newExp");
     dto.setDescription("newDesc");
     dto.setName("newName");
-    portfolioDao.update(db.getSession(), dto);
+    dto.setRootUuid("root");
+    dto.setParentUuid("parent");
+    portfolioDao.update(session, dto);
 
-    assertThat(portfolioDao.selectByUuid(db.getSession(), "name").get())
-      .extracting("name", "key", "uuid", "description", "private", "rootUuid", "parentUuid", "selectionMode", "selectionExpression", "createdAt", "updatedAt")
-      .containsExactly("newName", "key_name", "name", "newDesc", true, "root", "parent", "newMode", "newExp", 1000L, 1L);
+    assertThat(portfolioDao.selectByUuid(session, "name").get())
+      .extracting("name", "key", "uuid", "description", "private", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
+      .containsExactly("newName", "KEY_name", "name", "newDesc", true, "root", "parent", "newMode", "newExp");
   }
 
   @Test
   public void selectTree() {
-    createPortfolio("p1", null, "p1");
-    createPortfolio("p11", "p1", "p1");
-    createPortfolio("p111", "p11", "p1");
-    createPortfolio("p12", "p1", "p1");
-
-    createPortfolio("p2", null, "p2");
-
-    assertThat(portfolioDao.selectTree(db.getSession(), "p1")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
-    assertThat(portfolioDao.selectTree(db.getSession(), "p11")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
-    assertThat(portfolioDao.selectTree(db.getSession(), "p111")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
-    assertThat(portfolioDao.selectTree(db.getSession(), "p2")).extracting("uuid").containsOnly("p2");
+    PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+    PortfolioDto p11 = addPortfolio(p1, "p11");
+    PortfolioDto p111 = addPortfolio(p11, "p111");
+    PortfolioDto p12 = addPortfolio(p1, "p12");
+    PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
+
+    assertThat(portfolioDao.selectTree(session, "p1")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+    assertThat(portfolioDao.selectTree(session, "p11")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+    assertThat(portfolioDao.selectTree(session, "p111")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12");
+    assertThat(portfolioDao.selectTree(session, "p2")).extracting("uuid").containsOnly("p2");
   }
 
   @Test
   public void deleteByUuids() {
-    createPortfolio("p1");
-    createPortfolio("p2");
-    createPortfolio("p3");
-    createPortfolio("p4");
+    PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+    PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
+    PortfolioDto p3 = db.components().insertPrivatePortfolioDto("p3");
+    PortfolioDto p4 = db.components().insertPrivatePortfolioDto("p4");
 
-    portfolioDao.addProject(db.getSession(), "p1", "proj1");
-    portfolioDao.addProject(db.getSession(), "p2", "proj1");
+    portfolioDao.addProject(session, "p1", "proj1");
+    portfolioDao.addProject(session, "p2", "proj1");
 
-    portfolioDao.addReference(db.getSession(), "p1", "app1");
-    portfolioDao.addReference(db.getSession(), "p2", "app1");
-    portfolioDao.addReference(db.getSession(), "p4", "p1");
+    portfolioDao.addReference(session, "p1", "app1");
+    portfolioDao.addReference(session, "p2", "app1");
+    portfolioDao.addReference(session, "p4", "p1");
 
-    portfolioDao.deleteByUuids(db.getSession(), Set.of("p1", "p3"));
-    assertThat(db.select(db.getSession(), "select uuid from portfolios")).extracting(m -> m.values().iterator().next())
+    portfolioDao.deleteByUuids(session, Set.of("p1", "p3"));
+    assertThat(db.select(session, "select uuid from portfolios")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2", "p4");
-    assertThat(db.select(db.getSession(), "select portfolio_uuid from portfolio_references")).extracting(m -> m.values().iterator().next())
+    assertThat(db.select(session, "select portfolio_uuid from portfolio_references")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2");
-    assertThat(db.select(db.getSession(), "select portfolio_uuid from portfolio_projects")).extracting(m -> m.values().iterator().next())
+    assertThat(db.select(session, "select portfolio_uuid from portfolio_projects")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2");
-
   }
 
   @Test
   public void selectReferencersByKey() {
-    createPortfolio("portfolio1");
-    createPortfolio("portfolio2");
+    PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
+    PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
+
     ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
-    portfolioDao.addReference(db.getSession(), "portfolio1", "portfolio2");
-    portfolioDao.addReference(db.getSession(), "portfolio1", app1.getUuid());
-    portfolioDao.addReference(db.getSession(), "portfolio2", app1.getUuid());
+    portfolioDao.addReference(session, "portfolio1", "portfolio2");
+    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
+    portfolioDao.addReference(session, "portfolio2", app1.getUuid());
 
-    assertThat(portfolioDao.selectReferencersByKey(db.getSession(), "key_portfolio2"))
+    assertThat(portfolioDao.selectReferencersByKey(session, portfolio2.getKey()))
       .extracting("uuid").containsOnly("portfolio1");
 
-    assertThat(portfolioDao.selectReferencersByKey(db.getSession(), "app1"))
+    assertThat(portfolioDao.selectReferencersByKey(session, "app1"))
       .extracting("uuid").containsOnly("portfolio1", "portfolio2");
   }
 
   @Test
-  public void selectAllReferences() {
-    createPortfolio("portfolio1");
-    createPortfolio("portfolio2");
-    createPortfolio("portfolio3");
+  public void selectAllReferencesToPortfolios() {
+    db.components().insertPrivatePortfolioDto("portfolio1");
+    db.components().insertPrivatePortfolioDto("portfolio2");
+    db.components().insertPrivatePortfolioDto("portfolio3");
     ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
 
-    portfolioDao.addReference(db.getSession(), "portfolio1", "portfolio2");
-    portfolioDao.addReference(db.getSession(), "portfolio2", "portfolio3");
-    portfolioDao.addReference(db.getSession(), "portfolio3", "app1");
+    portfolioDao.addReference(session, "portfolio1", "portfolio2");
+    portfolioDao.addReference(session, "portfolio2", "portfolio3");
+    portfolioDao.addReference(session, "portfolio3", "app1");
 
-    assertThat(portfolioDao.selectAllReferencesToPortfolios(db.getSession()))
+    assertThat(portfolioDao.selectAllReferencesToPortfolios(session))
       .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
       .containsOnly(tuple("portfolio1", "portfolio2"), tuple("portfolio2", "portfolio3"));
   }
 
+  @Test
+  public void selectAllReferencesToApplications() {
+    db.components().insertPrivatePortfolioDto("portfolio1");
+    db.components().insertPrivatePortfolioDto("portfolio2");
+    db.components().insertPrivatePortfolioDto("portfolio3");
+    ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
+
+    portfolioDao.addReference(session, "portfolio1", "portfolio2");
+    portfolioDao.addReference(session, "portfolio2", "portfolio3");
+    portfolioDao.addReference(session, "portfolio3", app1.getUuid());
+
+    assertThat(portfolioDao.selectAllReferencesToApplications(session))
+      .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+      .containsOnly(tuple("portfolio3", app1.getUuid()));
+  }
+
   @Test
   public void insert_and_select_references() {
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).isEmpty();
-    portfolioDao.addReference(db.getSession(), "portfolio1", "app1");
-    portfolioDao.addReference(db.getSession(), "portfolio1", "app2");
-    portfolioDao.addReference(db.getSession(), "portfolio2", "app3");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    portfolioDao.addReference(session, "portfolio1", "app1");
+    portfolioDao.addReference(session, "portfolio1", "app2");
+    portfolioDao.addReference(session, "portfolio2", "app3");
     db.commit();
-    assertThat(portfolioDao.getReferences(db.getSession(), "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
-    assertThat(portfolioDao.getReferences(db.getSession(), "portfolio2")).containsExactlyInAnyOrder("app3");
-    assertThat(portfolioDao.getReferences(db.getSession(), "portfolio3")).isEmpty();
+    assertThat(portfolioDao.getReferences(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
+    assertThat(portfolioDao.getReferences(session, "portfolio2")).containsExactlyInAnyOrder("app3");
+    assertThat(portfolioDao.getReferences(session, "portfolio3")).isEmpty();
 
     assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(3);
-    assertThat(db.select(db.getSession(), "select created_at from portfolio_references"))
+    assertThat(db.select(session, "select created_at from portfolio_references"))
       .extracting(m -> m.values().iterator().next())
       .containsExactlyInAnyOrder(1L, 2L, 3L);
   }
@@ -184,19 +231,19 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProject("project1");
     db.components().insertPrivateProject("project2");
 
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).isEmpty();
-    portfolioDao.addProject(db.getSession(), "portfolio1", "project1");
-    portfolioDao.addProject(db.getSession(), "portfolio1", "project2");
-    portfolioDao.addProject(db.getSession(), "portfolio2", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    portfolioDao.addProject(session, "portfolio1", "project1");
+    portfolioDao.addProject(session, "portfolio1", "project2");
+    portfolioDao.addProject(session, "portfolio2", "project2");
     db.commit();
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).extracting(ProjectDto::getUuid).containsExactlyInAnyOrder("project1", "project2");
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio2")).extracting(ProjectDto::getUuid).containsExactlyInAnyOrder("project2");
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio3")).isEmpty();
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio3")).isEmpty();
 
     assertThat(db.countRowsOfTable("portfolio_projects")).isEqualTo(3);
-    assertThat(db.select(db.getSession(), "select created_at from portfolio_projects"))
+    assertThat(db.select(session, "select created_at from portfolio_projects"))
       .extracting(m -> m.values().iterator().next())
-      .containsExactlyInAnyOrder(3L, 4L, 5L);
+      .containsOnly(3L, 4L, 5L);
   }
 
   @Test
@@ -204,15 +251,31 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProject("project1");
     db.components().insertPrivateProject("project2");
 
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).isEmpty();
-    portfolioDao.addProject(db.getSession(), "portfolio1", "project1");
-    portfolioDao.addProject(db.getSession(), "portfolio1", "project2");
-    portfolioDao.addProject(db.getSession(), "portfolio2", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    portfolioDao.addProject(session, "portfolio1", "project1");
+    portfolioDao.addProject(session, "portfolio1", "project2");
+    portfolioDao.addProject(session, "portfolio2", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
+
+    portfolioDao.deleteProjects(session, "portfolio1");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+  }
+
+  @Test
+  public void delete_project() {
+    db.components().insertPrivateProject("project1");
+    db.components().insertPrivateProject("project2");
+
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    portfolioDao.addProject(session, "portfolio1", "project1");
+    portfolioDao.addProject(session, "portfolio1", "project2");
+    portfolioDao.addProject(session, "portfolio2", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
 
-    portfolioDao.deleteProjects(db.getSession(), "portfolio1");
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio1")).isEmpty();
-    assertThat(portfolioDao.getProjects(db.getSession(), "portfolio2")).extracting(ProjectDto::getUuid)
-      .containsExactlyInAnyOrder("project2");
+    portfolioDao.deleteProject(session, "portfolio1", "project2");
+    assertThat(portfolioDao.getProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1");
+    assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
   }
 
   @Test
@@ -222,74 +285,73 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProject("p3");
     db.components().insertPrivateProject("p4");
 
-    createPortfolio("root", null, "root");
-    createPortfolio("child1", null, "root");
-    createPortfolio("child11", "child1", "root");
-
-    createPortfolio("root2", null, "root2");
+    PortfolioDto root = db.components().insertPrivatePortfolioDto("root");
+    PortfolioDto child1 = addPortfolio(root, "child1");
+    PortfolioDto child11 = addPortfolio(child1, "child11");
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto("root2");
 
-    portfolioDao.addProject(db.getSession(), "root", "p1");
-    portfolioDao.addProject(db.getSession(), "child1", "p2");
-    portfolioDao.addProject(db.getSession(), "child11", "p3");
-    portfolioDao.addProject(db.getSession(), "root2", "p4");
+    portfolioDao.addProject(session, root.getUuid(), "p1");
+    portfolioDao.addProject(session, child1.getUuid(), "p2");
+    portfolioDao.addProject(session, child11.getUuid(), "p3");
+    portfolioDao.addProject(session, root2.getUuid(), "p4");
+    session.commit();
 
-    assertThat(portfolioDao.getAllProjectsInHierarchy(db.getSession(), "root").keySet()).containsExactly("p1", "p2", "p3");
-    assertThat(portfolioDao.getAllProjectsInHierarchy(db.getSession(), "nonexisting")).isEmpty();
+    assertThat(portfolioDao.getAllProjectsInHierarchy(session, root.getUuid()).keySet()).containsExactly("p1", "p2", "p3");
+    assertThat(portfolioDao.getAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
   }
 
   @Test
   public void deleteAllDescendantPortfolios() {
-    createPortfolio("root", null, "root");
-    createPortfolio("child1", null, "root");
-    createPortfolio("child11", "child1", "root");
-
-    createPortfolio("root2", null, "root2");
+    PortfolioDto root = db.components().insertPrivatePortfolioDto();
+    PortfolioDto child1 = addPortfolio(root);
+    PortfolioDto child11 = addPortfolio(child1);
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
 
-    portfolioDao.deleteAllDescendantPortfolios(db.getSession(), "root");
-    portfolioDao.deleteAllDescendantPortfolios(db.getSession(), "root2");
+    portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid());
+    portfolioDao.deleteAllDescendantPortfolios(session, root2.getUuid());
 
-    assertThat(db.countSql(db.getSession(), "select count(*) from portfolios where parent_uuid is not null")).isZero();
+    assertThat(db.countSql(session, "select count(*) from portfolios where parent_uuid is not null")).isZero();
   }
 
   @Test
   public void deleteAllReferences() {
-    createPortfolio("root", null, "root");
-    createPortfolio("child1", null, "root");
-    createPortfolio("child11", "child1", "root");
-
-    createPortfolio("root2", null, "root2");
-    createPortfolio("root3", null, "root3");
-    createPortfolio("root4", null, "root4");
-
-    portfolioDao.addReference(db.getSession(), "child1", "root2");
-    portfolioDao.addReference(db.getSession(), "root3", "root4");
-
-    portfolioDao.deleteAllReferences(db.getSession());
-
-    assertThat(db.countSql(db.getSession(), "select count(*) from portfolio_references")).isZero();
+    PortfolioDto root = db.components().insertPrivatePortfolioDto();
+    PortfolioDto child1 = addPortfolio(root);
+    PortfolioDto child11 = addPortfolio(child1);
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
+    PortfolioDto root3 = db.components().insertPrivatePortfolioDto();
+    PortfolioDto root4 = db.components().insertPrivatePortfolioDto();
+
+    portfolioDao.addReference(session, child1.getUuid(), root2.getUuid());
+    portfolioDao.addReference(session, root3.getUuid(), root4.getUuid());
+    session.commit();
+    assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(2);
+
+    portfolioDao.deleteAllReferences(session);
+    session.commit();
+    assertThat(db.countRowsOfTable("portfolio_references")).isZero();
   }
 
   @Test
   public void deleteAllProjects() {
-    db.components().insertPrivateProject("p1");
+    db.components().insertPrivateProjectDto("p1");
     db.components().insertPrivateProject("p2");
     db.components().insertPrivateProject("p3");
     db.components().insertPrivateProject("p4");
 
-    createPortfolio("root", null, "root");
-    createPortfolio("child1", null, "root");
-    createPortfolio("child11", "child1", "root");
-
-    createPortfolio("root2", null, "root2");
+    PortfolioDto root = db.components().insertPrivatePortfolioDto();
+    PortfolioDto child1 = addPortfolio(root);
+    PortfolioDto child11 = addPortfolio(child1);
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
 
-    portfolioDao.addProject(db.getSession(), "root", "p1");
-    portfolioDao.addProject(db.getSession(), "child1", "p2");
-    portfolioDao.addProject(db.getSession(), "child11", "p3");
-    portfolioDao.addProject(db.getSession(), "root2", "p4");
+    portfolioDao.addProject(session, root.getUuid(), "p1");
+    portfolioDao.addProject(session, child1.getUuid(), "p2");
+    portfolioDao.addProject(session, child11.getUuid(), "p3");
+    portfolioDao.addProject(session, root2.getUuid(), "p4");
+    assertThat(db.countRowsOfTable(session, "portfolio_projects")).isEqualTo(4);
 
-    portfolioDao.deleteAllProjects(db.getSession());
-
-    assertThat(db.countSql(db.getSession(), "select count(*) from portfolio_projects")).isZero();
+    portfolioDao.deleteAllProjects(session);
+    assertThat(db.countRowsOfTable(session, "portfolio_projects")).isZero();
   }
 
   private PortfolioDto createPortfolio(String uuid, @Nullable String parentUuid, String rootUuid) {
@@ -306,11 +368,24 @@ public class PortfolioDaoTest {
       .setCreatedAt(1000L)
       .setUpdatedAt(2000L);
 
-    portfolioDao.insert(db.getSession(), p);
+    portfolioDao.insert(session, p);
     return p;
   }
 
-  private PortfolioDto createPortfolio(String name) {
-    return createPortfolio(name, "parent", "root");
+  private PortfolioDto addPortfolio(PortfolioDto parent) {
+    return addPortfolio(parent, UuidFactoryFast.getInstance().create());
+  }
+
+  private PortfolioDto addPortfolio(PortfolioDto parent, String uuid) {
+    PortfolioDto portfolio = new PortfolioDto()
+      .setKey("key_" + uuid)
+      .setName("name_" + uuid)
+      .setSelectionMode(PortfolioDto.SelectionMode.NONE)
+      .setRootUuid(parent.getRootUuid())
+      .setParentUuid(parent.getUuid())
+      .setUuid(uuid);
+    db.getDbClient().portfolioDao().insert(session, portfolio);
+    session.commit();
+    return portfolio;
   }
 }
index 79e61335dd6bb837571c807c113643c1215c4fc3..ed3f1498fd9d766332ba3d2c64db1058a8e2c009 100644 (file)
@@ -123,6 +123,11 @@ public class ComponentDbTester {
     return getProjectDto(componentDto);
   }
 
+  public final ProjectDto insertPrivateProjectDto(String uuid) {
+    ComponentDto componentDto = insertPrivateProject(uuid);
+    return getProjectDto(componentDto);
+  }
+
   public final ProjectDto insertPrivateProjectDto(Consumer<ComponentDto> componentDtoPopulator) {
     return insertPrivateProjectDto(componentDtoPopulator, defaults());
   }
@@ -174,18 +179,32 @@ public class ComponentDbTester {
     return insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(false), false, dtoPopulator, portfolioPopulator);
   }
 
+  public final PortfolioDto insertPublicPortfolioDto() {
+    return insertPublicPortfolioDto(defaults());
+  }
+
   public final PortfolioDto insertPublicPortfolioDto(Consumer<ComponentDto> dtoPopulator) {
     ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(false), false, dtoPopulator, defaults());
     return getPortfolioDto(component);
   }
 
+  public final PortfolioDto insertPrivatePortfolioDto(String uuid) {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio(uuid).setPrivate(true), true, defaults(), defaults());
+    return getPortfolioDto(component);
+  }
+
+  public final PortfolioDto insertPrivatePortfolioDto() {
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, defaults(), defaults());
+    return getPortfolioDto(component);
+  }
+
   public final PortfolioDto insertPrivatePortfolioDto(Consumer<ComponentDto> dtoPopulator) {
     ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator, defaults());
     return getPortfolioDto(component);
   }
 
   public final PortfolioDto insertPrivatePortfolioDto(Consumer<ComponentDto> dtoPopulator, Consumer<PortfolioDto> portfolioPopulator) {
-    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator,portfolioPopulator);
+    ComponentDto component = insertComponentAndPortfolio(ComponentTesting.newPortfolio().setPrivate(true), true, dtoPopulator, portfolioPopulator);
     return getPortfolioDto(component);
   }
 
@@ -243,6 +262,13 @@ public class ComponentDbTester {
     db.commit();
   }
 
+  public void addPortfolioReference(PortfolioDto portfolio, String... referencerUuids) {
+    for (String uuid : referencerUuids) {
+      dbClient.portfolioDao().addReference(dbSession, portfolio.getUuid(), uuid);
+    }
+    db.commit();
+  }
+
   public void addPortfolioProject(ComponentDto portfolio, String... projectUuids) {
     for (String uuid : projectUuids) {
       dbClient.portfolioDao().addProject(dbSession, portfolio.uuid(), uuid);
@@ -251,10 +277,7 @@ public class ComponentDbTester {
   }
 
   public void addPortfolioProject(ComponentDto portfolio, ComponentDto... projects) {
-    for (ComponentDto project : projects) {
-      dbClient.portfolioDao().addProject(dbSession, portfolio.uuid(), project.uuid());
-    }
-    db.commit();
+    addPortfolioProject(portfolio, Arrays.stream(projects).map(ComponentDto::uuid).toArray(String[]::new));
   }
 
   public void addPortfolioProject(PortfolioDto portfolioDto, ProjectDto... projects) {
@@ -479,7 +502,7 @@ public class ComponentDbTester {
       .setName(componentDto.name());
   }
 
-  private static <T> Consumer<T> defaults() {
+  public static <T> Consumer<T> defaults() {
     return t -> {
     };
   }
index d2a9c4736ce4de3f10f217cc680b402f3d08fae4..f1840280bd1bd1382d659a2b95e643f5dd71ca0f 100644 (file)
@@ -173,10 +173,10 @@ public class SearchMyProjectsActionTest {
   @Test
   public void does_not_return_views() {
     ComponentDto jdk7 = insertJdk7();
-    ComponentDto view = insertPortfolio();
+    ComponentDto portfolio = insertPortfolio();
 
     db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
-    db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, view);
+    db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, portfolio);
 
     SearchMyProjectsWsResponse result = callWs();
 
@@ -260,9 +260,11 @@ public class SearchMyProjectsActionTest {
   }
 
   private ComponentDto insertPortfolio() {
+    String uuid = "752d8bfd-420c-4a83-a4e5-8ab19b13c8fc";
     return db.components().insertPublicPortfolio(p -> p.setUuid("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc")
-      .setName("Java")
-      .setDbKey("Java"));
+        .setName("Java")
+        .setDbKey("Java"),
+      p -> p.setRootUuid(uuid));
   }
 
   private SearchMyProjectsWsResponse callWs() {