]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Improve unit tests and fix Move WS
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 31 Aug 2021 15:46:42 +0000 (10:46 -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/java/org/sonar/db/portfolio/ReferenceDetailsDto.java [new file with mode: 0644]
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-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java

index 14162c0b7c55653c7a6c5e150e7f9a344f62df03..14b757459c429e1bc340ba6f91ee04d81b82f72f 100644 (file)
@@ -25,24 +25,29 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.audit.AuditPersister;
+import org.sonar.db.audit.model.ComponentNewValue;
 import org.sonar.db.project.ProjectDto;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.emptyList;
 import static java.util.Collections.singleton;
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
 
 public class PortfolioDao implements Dao {
   private final System2 system2;
   private final UuidFactory uuidFactory;
+  private final AuditPersister auditPersister;
 
-  public PortfolioDao(System2 system2, UuidFactory uuidFactory) {
-    // TODO: Audits missing
+  public PortfolioDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) {
     this.system2 = system2;
     this.uuidFactory = uuidFactory;
+    this.auditPersister = auditPersister;
   }
 
   public List<PortfolioDto> selectAllRoots(DbSession dbSession) {
@@ -53,6 +58,10 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectAll();
   }
 
+  public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
+    return mapper(dbSession).selectTree(portfolioUuid);
+  }
+
   public Optional<PortfolioDto> selectByKey(DbSession dbSession, String key) {
     return Optional.ofNullable(mapper(dbSession).selectByKey(key));
   }
@@ -61,32 +70,45 @@ public class PortfolioDao implements Dao {
     return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid));
   }
 
+  public List<PortfolioDto> selectByUuids(DbSession dbSession, Set<String> uuids) {
+    if (uuids.isEmpty()) {
+      return emptyList();
+    }
+    return mapper(dbSession).selectByUuids(uuids);
+  }
+
   public void insert(DbSession dbSession, PortfolioDto portfolio) {
     checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
     mapper(dbSession).insert(portfolio);
+    auditPersister.addComponent(dbSession, toComponentNewValue(portfolio));
   }
 
-  public void delete(DbSession dbSession, String portfolioUuid) {
-    mapper(dbSession).deletePortfoliosByUuids(singleton(portfolioUuid));
-    mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolioUuid));
-    mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolioUuid));
-  }
-
-  public void deleteByUuids(DbSession dbSession, Set<String> portfolioUuids) {
-    if (portfolioUuids.isEmpty()) {
-      return;
-    }
-    mapper(dbSession).deleteByUuids(portfolioUuids);
+  public void delete(DbSession dbSession, PortfolioDto portfolio) {
+    mapper(dbSession).deletePortfoliosByUuids(singleton(portfolio.getUuid()));
+    mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolio.getUuid()));
+    mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolio.getUuid()));
+    auditPersister.deleteComponent(dbSession, toComponentNewValue(portfolio));
   }
 
-  public List<PortfolioDto> selectTree(DbSession dbSession, String portfolioUuid) {
-    return mapper(dbSession).selectTree(portfolioUuid);
+  public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
+    // not audited but it's part of DefineWs
+    mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
   }
 
   public void update(DbSession dbSession, PortfolioDto portfolio) {
     checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
     portfolio.setUpdatedAt(system2.now());
     mapper(dbSession).update(portfolio);
+    auditPersister.updateComponent(dbSession, toComponentNewValue(portfolio));
+  }
+
+  private static ComponentNewValue toComponentNewValue(PortfolioDto portfolio) {
+    return new ComponentNewValue(portfolio.getUuid(), portfolio.getName(), portfolio.getKey(), portfolio.isPrivate(),
+      portfolio.getDescription(), qualifier(portfolio));
+  }
+
+  private static String qualifier(PortfolioDto portfolioDto) {
+    return portfolioDto.isRoot() ? Qualifiers.VIEW : Qualifiers.SUBVIEW;
   }
 
   public Map<String, String> selectKeysByUuids(DbSession dbSession, Collection<String> uuids) {
@@ -94,10 +116,6 @@ public class PortfolioDao implements Dao {
       .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) {
     mapper(dbSession).insertReference(new PortfolioReferenceDto()
       .setUuid(uuidFactory.create())
@@ -114,12 +132,16 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectAllReferencesToApplications();
   }
 
-  public Set<String> getReferences(DbSession dbSession, String portfolioUuid) {
-    return mapper(dbSession).selectReferences(portfolioUuid);
+  public Set<String> selectReferenceUuids(DbSession dbSession, String portfolioUuid) {
+    return mapper(dbSession).selectReferenceUuids(portfolioUuid);
   }
 
-  public List<PortfolioDto> selectReferencersByKey(DbSession dbSession, String referenceKey) {
-    return mapper(dbSession).selectReferencersByKey(referenceKey);
+  public List<ReferenceDto> selectAllReferencesInHierarchy(DbSession dbSession, String uuid) {
+    return mapper(dbSession).selectAllReferencesInHierarchy(uuid);
+  }
+
+  public List<PortfolioDto> selectReferencers(DbSession dbSession, String referenceUuid) {
+    return mapper(dbSession).selectReferencers(referenceUuid);
   }
 
   public List<PortfolioDto> selectRootOfReferencers(DbSession dbSession, String referenceUuid) {
@@ -138,18 +160,22 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).deleteReference(portfolioUuid, referenceUuid);
   }
 
-  public ReferenceDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) {
+  public ReferenceDetailsDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) {
     return mapper(dbSession).selectReference(portfolioUuid, referenceKey);
   }
 
-  public List<ProjectDto> getProjects(DbSession dbSession, String portfolioUuid) {
+  public List<ProjectDto> selectProjects(DbSession dbSession, String portfolioUuid) {
     return mapper(dbSession).selectProjects(portfolioUuid);
   }
 
-  public List<PortfolioProjectDto> getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
+  public List<PortfolioProjectDto> selectAllProjectsInHierarchy(DbSession dbSession, String rootUuid) {
     return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid);
   }
 
+  public List<PortfolioProjectDto> selectAllPortfolioProjects(DbSession dbSession) {
+    return mapper(dbSession).selectAllPortfolioProjects();
+  }
+
   public void addProject(DbSession dbSession, String portfolioUuid, String projectUuid) {
     mapper(dbSession).insertProject(new PortfolioProjectDto()
       .setUuid(uuidFactory.create())
@@ -170,18 +196,6 @@ public class PortfolioDao implements Dao {
     mapper(dbSession).deleteAllProjects();
   }
 
-  public List<PortfolioProjectDto> selectAllProjectsOfPortfolios(DbSession dbSession) {
-    return mapper(dbSession).selectAllProjectsOfPortfolios();
-  }
-
-  public List<ReferenceDto> selectAllReferencesInHierarchy(DbSession dbSession, String uuid) {
-    return mapper(dbSession).selectAllReferencesInHierarchy(uuid);
-  }
-
-  public List<PortfolioDto> selectByUuids(DbSession dbSession, Set<String> uuidsToRefresh) {
-    return mapper(dbSession).selectByUuids(uuidsToRefresh);
-  }
-
   private static PortfolioMapper mapper(DbSession session) {
     return session.getMapper(PortfolioMapper.class);
   }
index 49cc54ba540d50026b7b74c2381d1fae09f24cb0..4340b395686e69f2d3e87064a05bfa76ae3336a0 100644 (file)
@@ -49,9 +49,9 @@ public interface PortfolioMapper {
 
   List<PortfolioDto> selectTree(String portfolioUuid);
 
-  Set<String> selectReferences(String portfolioUuid);
+  Set<String> selectReferenceUuids(String portfolioUuid);
 
-  List<PortfolioDto> selectReferencersByKey(String referenceKey);
+  List<PortfolioDto> selectReferencers(String referenceUuid);
 
   List<ProjectDto> selectProjects(String portfolioUuid);
 
@@ -83,13 +83,11 @@ public interface PortfolioMapper {
 
   int deleteReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceUuid") String referenceUuid);
 
-  ReferenceDto selectReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey);
+  ReferenceDetailsDto selectReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey);
 
   void deleteAllProjects();
 
-  List<PortfolioProjectDto> selectAllProjectsOfPortfolios();
-
-  List<ReferenceDto> selectAllReferencesOfPortfolios();
+  List<PortfolioProjectDto> selectAllPortfolioProjects();
 
   List<ReferenceDto> selectAllReferencesInHierarchy(String rootUuid);
 }
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/ReferenceDetailsDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/ReferenceDetailsDto.java
new file mode 100644 (file)
index 0000000..7e75a7a
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.db.portfolio;
+
+public class ReferenceDetailsDto extends ReferenceDto {
+  private String targetName;
+  private String targetKey;
+
+  public String getTargetName() {
+    return targetName;
+  }
+
+  public ReferenceDetailsDto setTargetName(String targetName) {
+    this.targetName = targetName;
+    return this;
+  }
+
+  public String getTargetKey() {
+    return targetKey;
+  }
+
+  public ReferenceDetailsDto setTargetKey(String targetKey) {
+    this.targetKey = targetKey;
+    return this;
+  }
+}
index 8176cd6719aa43ae4bc31ab8a6a1db907217139f..1281dfbec3979465de81a3efa0b8a89821add448 100644 (file)
@@ -78,7 +78,7 @@
       pp.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
-  <select id="selectAllProjectsOfPortfolios" resultType="org.sonar.db.portfolio.PortfolioProjectDto">
+  <select id="selectAllPortfolioProjects" resultType="org.sonar.db.portfolio.PortfolioProjectDto">
     SELECT
        pp.uuid,
        pp.project_uuid as projectUuid,
        p.root_uuid = #{rootUuid,jdbcType=VARCHAR}
   </select>
 
-  <select id="selectReferences" resultType="String">
+  <select id="selectReferenceUuids" resultType="String">
     SELECT
       p.reference_uuid
     FROM portfolio_references p
       pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
   </select>
 
- <select id="selectTree" parameterType="String" resultType="Portfolio">
 <select id="selectTree" parameterType="String" resultType="Portfolio">
     SELECT <include refid="portfolioColumns"/>
     FROM portfolios p
     INNER JOIN portfolios p2 ON p.root_uuid = p2.root_uuid
     WHERE p2.uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
 <select id="selectReferencersByKey" resultType="Portfolio">
<select id="selectReferencers" resultType="Portfolio">
     SELECT
       <include refid="portfolioColumns"/>
     FROM portfolios p
     INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
-    INNER JOIN portfolios p2 ON pr.reference_uuid = p2.uuid
-    WHERE p2.kee=#{referenceKey,jdbcType=VARCHAR}
-
-    UNION
-
-    SELECT
-      <include refid="portfolioColumns"/>
-    FROM portfolios p
-    INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
-    INNER JOIN projects pj ON pr.reference_uuid = pj.uuid AND pj.qualifier = 'APP'
-    WHERE pj.kee=#{referenceKey,jdbcType=VARCHAR}
+    WHERE pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
   </select>
 
   <delete id="deleteReference">
     AND reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
   </delete>
 
-  <select id="selectReference" resultType="org.sonar.db.portfolio.ReferenceDto">
+  <select id="selectReference" resultType="org.sonar.db.portfolio.ReferenceDetailsDto">
     (SELECT
       source.uuid as sourceUuid,
       source.root_uuid as sourceRootUuid,
       target.uuid as targetUuid,
-      target.root_uuid as targetRootUuid
+      target.root_uuid as targetRootUuid,
+      target.name as targetName,
+      target.kee as targetKey
     FROM portfolio_references pr
      INNER JOIN portfolios source ON pr.portfolio_uuid = source.uuid
      INNER JOIN portfolios target ON pr.reference_uuid = target.uuid
       source.uuid as sourceUuid,
       source.root_uuid as sourceRootUuid,
       target.uuid as targetUuid,
-      target.uuid as targetRootUuid
+      target.uuid as targetRootUuid,
+      target.name as targetName,
+      target.kee as targetKey
     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'
index 7bbfe7b3a1c7dc36f8254c87cae7beab20a53012..593f311cad3811d767a54d0235c2d3d99eaaef66 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.db.portfolio;
 
 import java.util.Set;
-import javax.annotation.Nullable;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
@@ -28,31 +27,33 @@ 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.audit.AuditPersister;
 import org.sonar.db.project.ProjectDto;
 
+import static java.util.Collections.emptySet;
+import static java.util.Map.entry;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.assertj.core.api.Assertions.tuple;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 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 AuditPersister audit = mock(AuditPersister.class);
+  private final PortfolioDao portfolioDao = new PortfolioDao(system2, UuidFactoryFast.getInstance(), audit);
   private final DbSession session = db.getSession();
 
-  @Test
-  public void selectByKey_returns_empty_if_no_match() {
-    assertThat(portfolioDao.selectByKey(session, "nonexisting")).isEmpty();
-  }
-
   @Test
   public void selectAllRoots() {
-    createPortfolio("p1", null, "p1");
-    createPortfolio("p11", "p1", "p1");
-    createPortfolio("p111", "p11", "p1");
-    createPortfolio("p2", null, "p2");
+    PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+    PortfolioDto p11 = addPortfolio(p1, "p11");
+    PortfolioDto p111 = addPortfolio(p11, "p111");
+    PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
 
     assertThat(portfolioDao.selectAllRoots(session)).extracting("uuid")
       .containsExactlyInAnyOrder("p1", "p2");
@@ -60,15 +61,34 @@ public class PortfolioDaoTest {
 
   @Test
   public void selectAll() {
-    createPortfolio("p1", null, "p1");
-    createPortfolio("p11", "p1", "p1");
-    createPortfolio("p111", "p11", "p1");
-    createPortfolio("p2", null, "p2");
+    PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
+    PortfolioDto p11 = addPortfolio(p1, "p11");
+    PortfolioDto p111 = addPortfolio(p11, "p111");
+    PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
 
     assertThat(portfolioDao.selectAll(session)).extracting("uuid")
       .containsExactlyInAnyOrder("p1", "p2", "p11", "p111");
   }
 
+  @Test
+  public void selectTree() {
+    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 selectByKey_returns_empty_if_no_match() {
+    assertThat(portfolioDao.selectByKey(session, "nonexisting")).isEmpty();
+  }
+
   @Test
   public void selectByKey_returns_match() {
     PortfolioDto portfolio = db.components().insertPrivatePortfolioDto();
@@ -89,6 +109,31 @@ public class PortfolioDaoTest {
       .containsExactly("NAME_name", "KEY_name", "name", "DESCRIPTION_name", "name", null, "NONE", null);
   }
 
+  @Test
+  public void selectByUuids_returns_empty_if_no_match() {
+    db.components().insertPrivatePortfolioDto("name1");
+    assertThat(portfolioDao.selectByUuids(session, Set.of("name2"))).isEmpty();
+  }
+
+  @Test
+  public void selectByUuids_returns_empty_if_uuids_is_empty() {
+    db.components().insertPrivatePortfolioDto("name1");
+    assertThat(portfolioDao.selectByUuids(session, emptySet())).isEmpty();
+  }
+
+  @Test
+  public void selectByUuids_returns_matches() {
+    db.components().insertPrivatePortfolioDto("name1");
+    db.components().insertPrivatePortfolioDto("name2");
+    db.components().insertPrivatePortfolioDto("name3");
+
+    assertThat(portfolioDao.selectByUuids(session, Set.of("name1", "name2")))
+      .extracting("name", "key", "uuid", "description", "rootUuid", "parentUuid", "selectionMode", "selectionExpression")
+      .containsOnly(
+        tuple("NAME_name1", "KEY_name1", "name1", "DESCRIPTION_name1", "name1", null, "NONE", null),
+        tuple("NAME_name2", "KEY_name2", "name2", "DESCRIPTION_name2", "name2", null, "NONE", null));
+  }
+
   @Test
   public void insert_fails_if_root_is_inconsistent_with_parent() {
     PortfolioDto portfolio = new PortfolioDto()
@@ -107,38 +152,7 @@ public class PortfolioDaoTest {
   }
 
   @Test
-  public void update_portfolio() {
-    db.components().insertPrivatePortfolioDto("name");
-    PortfolioDto dto = portfolioDao.selectByUuid(session, "name").get();
-    dto.setSelectionMode("newMode");
-    dto.setSelectionExpression("newExp");
-    dto.setDescription("newDesc");
-    dto.setName("newName");
-    dto.setRootUuid("root");
-    dto.setParentUuid("parent");
-    portfolioDao.update(session, dto);
-
-    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() {
-    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() {
+  public void delete() {
     PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1");
     PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2");
     PortfolioDto p3 = db.components().insertPrivatePortfolioDto("p3");
@@ -151,30 +165,58 @@ public class PortfolioDaoTest {
     portfolioDao.addReference(session, "p2", "app1");
     portfolioDao.addReference(session, "p4", "p1");
 
-    portfolioDao.deleteByUuids(session, Set.of("p1", "p3"));
+    portfolioDao.delete(session, p1);
+    portfolioDao.delete(session, p3);
+
     assertThat(db.select(session, "select uuid from portfolios")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2", "p4");
     assertThat(db.select(session, "select portfolio_uuid from portfolio_references")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2");
     assertThat(db.select(session, "select portfolio_uuid from portfolio_projects")).extracting(m -> m.values().iterator().next())
       .containsOnly("p2");
+
+    verify(audit, times(2)).deleteComponent(any(), any());
   }
 
   @Test
-  public void selectReferencersByKey() {
-    PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
-    PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
+  public void deleteAllDescendantPortfolios() {
+    PortfolioDto root = db.components().insertPrivatePortfolioDto();
+    PortfolioDto child1 = addPortfolio(root);
+    PortfolioDto child11 = addPortfolio(child1);
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
 
-    ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
-    portfolioDao.addReference(session, "portfolio1", "portfolio2");
-    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
-    portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+    portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid());
+    portfolioDao.deleteAllDescendantPortfolios(session, root2.getUuid());
 
-    assertThat(portfolioDao.selectReferencersByKey(session, portfolio2.getKey()))
-      .extracting("uuid").containsOnly("portfolio1");
+    assertThat(db.countSql(session, "select count(*) from portfolios where parent_uuid is not null")).isZero();
+  }
 
-    assertThat(portfolioDao.selectReferencersByKey(session, "app1"))
-      .extracting("uuid").containsOnly("portfolio1", "portfolio2");
+  @Test
+  public void update_portfolio() {
+    db.components().insertPrivatePortfolioDto("name");
+    PortfolioDto dto = portfolioDao.selectByUuid(session, "name").get();
+    dto.setSelectionMode("newMode");
+    dto.setSelectionExpression("newExp");
+    dto.setDescription("newDesc");
+    dto.setName("newName");
+    dto.setRootUuid("root");
+    dto.setParentUuid("parent");
+    portfolioDao.update(session, dto);
+
+    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");
+    verify(audit).updateComponent(any(), any());
+
+  }
+
+  @Test
+  public void selectKeysByUuids() {
+    PortfolioDto root = db.components().insertPrivatePortfolioDto();
+    PortfolioDto child1 = addPortfolio(root);
+
+    assertThat(portfolioDao.selectKeysByUuids(session, Set.of(root.getUuid(), child1.getUuid())))
+      .containsOnly(entry(root.getUuid(), root.getKey()), entry(child1.getUuid(), child1.getKey()));
   }
 
   @Test
@@ -210,15 +252,15 @@ public class PortfolioDaoTest {
   }
 
   @Test
-  public void insert_and_select_references() {
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+  public void add_and_select_references() {
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
     portfolioDao.addReference(session, "portfolio1", "app1");
     portfolioDao.addReference(session, "portfolio1", "app2");
     portfolioDao.addReference(session, "portfolio2", "app3");
     db.commit();
-    assertThat(portfolioDao.getReferences(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
-    assertThat(portfolioDao.getReferences(session, "portfolio2")).containsExactlyInAnyOrder("app3");
-    assertThat(portfolioDao.getReferences(session, "portfolio3")).isEmpty();
+    assertThat(portfolioDao.selectReferenceUuids(session, "portfolio1")).containsExactlyInAnyOrder("app1", "app2");
+    assertThat(portfolioDao.selectReferenceUuids(session, "portfolio2")).containsExactlyInAnyOrder("app3");
+    assertThat(portfolioDao.selectReferenceUuids(session, "portfolio3")).isEmpty();
 
     assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(3);
     assertThat(db.select(session, "select created_at from portfolio_references"))
@@ -226,19 +268,127 @@ public class PortfolioDaoTest {
       .containsExactlyInAnyOrder(1L, 2L, 3L);
   }
 
+  @Test
+  public void selectAllReferencesInHierarchy() {
+    PortfolioDto root1 = db.components().insertPrivatePortfolioDto("root1");
+    PortfolioDto root2 = db.components().insertPrivatePortfolioDto("root2");
+    PortfolioDto sub1 = addPortfolio(root1, "sub1");
+    PortfolioDto sub11 = addPortfolio(sub1, "sub11");
+    ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setUuid("app1"));
+
+    db.components().addPortfolioReference("root1", "app1");
+    db.components().addPortfolioReference("root2", "app1");
+    db.components().addPortfolioReference("sub11", "root2");
+
+    assertThat(portfolioDao.selectAllReferencesInHierarchy(session, root1.getUuid()))
+      .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+      .containsOnly(
+        tuple(root1.getUuid(), app1.getUuid()),
+        tuple(sub11.getUuid(), root2.getUuid()));
+  }
+
+  @Test
+  public void selectReferencers() {
+    PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1");
+    PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2");
+
+    ProjectDto app1 = db.components().insertPrivateApplicationDto(c -> c.setUuid("app1"));
+    portfolioDao.addReference(session, "portfolio1", "portfolio2");
+    portfolioDao.addReference(session, "portfolio1", app1.getUuid());
+    portfolioDao.addReference(session, "portfolio2", app1.getUuid());
+
+    assertThat(portfolioDao.selectReferencers(session, portfolio2.getUuid()))
+      .extracting("uuid").containsOnly("portfolio1");
+
+    assertThat(portfolioDao.selectReferencers(session, "app1"))
+      .extracting("uuid").containsOnly("portfolio1", "portfolio2");
+  }
+
+  @Test
+  public void selectReferencers_for_non_existing_reference() {
+    assertThat(portfolioDao.selectReferencers(session, "unknown")).isEmpty();
+  }
+
+  @Test
+  public void selectRootOfReferencers_returns_root() {
+    PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("name1");
+    PortfolioDto sub = addPortfolio(portfolio1, "sub1");
+    PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("name2");
+    db.components().addPortfolioReference(sub, portfolio2.getUuid());
+
+    assertThat(portfolioDao.selectRootOfReferencers(session, portfolio2.getUuid()))
+      .extracting("uuid")
+      .containsOnly(portfolio1.getUuid());
+  }
+
+  @Test
+  public void selectRootOfReferencers_for_non_existing_reference() {
+    assertThat(portfolioDao.selectRootOfReferencers(session, "nonexisting")).isEmpty();
+  }
+
+  @Test
+  public void deleteReferencesTo() {
+    db.components().insertPrivatePortfolioDto("portfolio1");
+    db.components().insertPrivatePortfolioDto("portfolio2");
+    db.components().insertPrivatePortfolioDto("portfolio3");
+
+    db.components().addPortfolioReference("portfolio1", "portfolio3");
+    db.components().addPortfolioReference("portfolio2", "portfolio3");
+    portfolioDao.deleteReferencesTo(session, "portfolio3");
+    session.commit();
+    assertThat(db.countRowsOfTable("portfolio_references")).isZero();
+  }
+
+  @Test
+  public void deleteReferencesTo_with_non_existing_reference_doesnt_fail() {
+    portfolioDao.deleteReferencesTo(session, "portfolio3");
+  }
+
+  @Test
+  public void deleteAllReferences() {
+    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();
+
+    db.components().addPortfolioReference(child1.getUuid(), root2.getUuid());
+    db.components().addPortfolioReference(root3.getUuid(), root4.getUuid());
+    assertThat(db.countRowsOfTable("portfolio_references")).isEqualTo(2);
+
+    portfolioDao.deleteAllReferences(session);
+    session.commit();
+    assertThat(db.countRowsOfTable("portfolio_references")).isZero();
+  }
+
+  @Test
+  public void deleteReference() {
+    db.components().insertPrivatePortfolioDto("portfolio1");
+    db.components().insertPrivatePortfolioDto("portfolio2");
+    db.components().insertPrivatePortfolioDto("portfolio3");
+
+    db.components().addPortfolioReference("portfolio1", "portfolio3");
+    db.components().addPortfolioReference("portfolio2", "portfolio3");
+    portfolioDao.deleteReference(session, "portfolio1", "portfolio3");
+    assertThat(portfolioDao.selectAllReferencesToPortfolios(session))
+      .extracting(ReferenceDto::getSourceUuid, ReferenceDto::getTargetUuid)
+      .containsOnly(tuple("portfolio2", "portfolio3"));
+  }
+
   @Test
   public void insert_and_select_projects() {
     db.components().insertPrivateProject("project1");
     db.components().insertPrivateProject("project2");
 
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
     portfolioDao.addProject(session, "portfolio1", "project1");
     portfolioDao.addProject(session, "portfolio1", "project2");
     portfolioDao.addProject(session, "portfolio2", "project2");
     db.commit();
-    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(portfolioDao.selectProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1", "project2");
+    assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+    assertThat(portfolioDao.selectProjects(session, "portfolio3")).isEmpty();
 
     assertThat(db.countRowsOfTable("portfolio_projects")).isEqualTo(3);
     assertThat(db.select(session, "select created_at from portfolio_projects"))
@@ -251,15 +401,15 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProject("project1");
     db.components().insertPrivateProject("project2");
 
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
     portfolioDao.addProject(session, "portfolio1", "project1");
     portfolioDao.addProject(session, "portfolio1", "project2");
     portfolioDao.addProject(session, "portfolio2", "project2");
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isNotEmpty();
 
     portfolioDao.deleteProjects(session, "portfolio1");
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
-    assertThat(portfolioDao.getProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
   }
 
   @Test
@@ -267,19 +417,19 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProject("project1");
     db.components().insertPrivateProject("project2");
 
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isEmpty();
     portfolioDao.addProject(session, "portfolio1", "project1");
     portfolioDao.addProject(session, "portfolio1", "project2");
     portfolioDao.addProject(session, "portfolio2", "project2");
-    assertThat(portfolioDao.getProjects(session, "portfolio1")).isNotEmpty();
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).isNotEmpty();
 
     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");
+    assertThat(portfolioDao.selectProjects(session, "portfolio1")).extracting(ProjectDto::getUuid).containsOnly("project1");
+    assertThat(portfolioDao.selectProjects(session, "portfolio2")).extracting(ProjectDto::getUuid).containsOnly("project2");
   }
 
   @Test
-  public void getAllProjectsInHierarchy() {
+  public void selectAllProjectsInHierarchy() {
     db.components().insertPrivateProject("p1");
     db.components().insertPrivateProject("p2");
     db.components().insertPrivateProject("p3");
@@ -296,41 +446,9 @@ public class PortfolioDaoTest {
     portfolioDao.addProject(session, root2.getUuid(), "p4");
     session.commit();
 
-    assertThat(portfolioDao.getAllProjectsInHierarchy(session, root.getUuid()))
+    assertThat(portfolioDao.selectAllProjectsInHierarchy(session, root.getUuid()))
       .extracting(PortfolioProjectDto::getProjectUuid).containsExactly("p1", "p2", "p3");
-    assertThat(portfolioDao.getAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
-  }
-
-  @Test
-  public void deleteAllDescendantPortfolios() {
-    PortfolioDto root = db.components().insertPrivatePortfolioDto();
-    PortfolioDto child1 = addPortfolio(root);
-    PortfolioDto child11 = addPortfolio(child1);
-    PortfolioDto root2 = db.components().insertPrivatePortfolioDto();
-
-    portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid());
-    portfolioDao.deleteAllDescendantPortfolios(session, root2.getUuid());
-
-    assertThat(db.countSql(session, "select count(*) from portfolios where parent_uuid is not null")).isZero();
-  }
-
-  @Test
-  public void deleteAllReferences() {
-    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();
+    assertThat(portfolioDao.selectAllProjectsInHierarchy(session, "nonexisting")).isEmpty();
   }
 
   @Test
@@ -355,24 +473,6 @@ public class PortfolioDaoTest {
     assertThat(db.countRowsOfTable(session, "portfolio_projects")).isZero();
   }
 
-  private PortfolioDto createPortfolio(String uuid, @Nullable String parentUuid, String rootUuid) {
-    PortfolioDto p = new PortfolioDto()
-      .setName("name_" + uuid)
-      .setKey("key_" + uuid)
-      .setUuid(uuid)
-      .setDescription("desc_" + uuid)
-      .setRootUuid(rootUuid)
-      .setParentUuid(parentUuid)
-      .setPrivate(true)
-      .setSelectionExpression("exp")
-      .setSelectionMode("mode")
-      .setCreatedAt(1000L)
-      .setUpdatedAt(2000L);
-
-    portfolioDao.insert(session, p);
-    return p;
-  }
-
   private PortfolioDto addPortfolio(PortfolioDto parent) {
     return addPortfolio(parent, UuidFactoryFast.getInstance().create());
   }
index fe8b4374f268cb831263aff828f6f70199f1b029..e7ee9c0cc639da8e758b213ed9d276c44621d20e 100644 (file)
@@ -308,6 +308,10 @@ public class ComponentDbTester {
     return getProjectDto(insertPublicApplication());
   }
 
+  public final ProjectDto insertPublicApplicationDto(Consumer<ComponentDto> dtoPopulator) {
+    return getProjectDto(insertPublicApplication(dtoPopulator));
+  }
+
   public final ProjectDto insertPrivateApplicationDto(Consumer<ComponentDto> dtoPopulator) {
     return getProjectDto(insertPrivateApplication(dtoPopulator, defaults()));
   }
index bff8fb5665f937aa523ddeaff557c60c41fbbdc3..773c63fd757a18a7b601cf3ef834e568784df643 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.view.index;
 
+import java.util.ArrayList;
 import java.util.List;
 import org.elasticsearch.action.search.ClearScrollRequest;
 import org.elasticsearch.action.search.SearchRequest;
@@ -57,7 +58,7 @@ public class ViewIndex {
       .scroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
 
     SearchResponse response = esClient.search(esSearch);
-    List<String> result = newArrayList();
+    List<String> result = new ArrayList<>();
     while (true) {
       List<SearchHit> hits = newArrayList(response.getHits());
       for (SearchHit hit : hits) {