From c4e00bab051a72bf5da6bc9fc4cd791b37522a0b Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 31 Aug 2021 10:46:42 -0500 Subject: [PATCH] SONAR-15259 Improve unit tests and fix Move WS --- .../org/sonar/db/portfolio/PortfolioDao.java | 90 +++-- .../sonar/db/portfolio/PortfolioMapper.java | 10 +- .../db/portfolio/ReferenceDetailsDto.java | 43 +++ .../sonar/db/portfolio/PortfolioMapper.xml | 30 +- .../sonar/db/portfolio/PortfolioDaoTest.java | 360 +++++++++++------- .../sonar/db/component/ComponentDbTester.java | 4 + .../sonar/server/view/index/ViewIndex.java | 3 +- 7 files changed, 347 insertions(+), 193 deletions(-) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/ReferenceDetailsDto.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java index 14162c0b7c5..14b757459c4 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java @@ -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 selectAllRoots(DbSession dbSession) { @@ -53,6 +58,10 @@ public class PortfolioDao implements Dao { return mapper(dbSession).selectAll(); } + public List selectTree(DbSession dbSession, String portfolioUuid) { + return mapper(dbSession).selectTree(portfolioUuid); + } + public Optional 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 selectByUuids(DbSession dbSession, Set 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 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 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 selectKeysByUuids(DbSession dbSession, Collection 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 getReferences(DbSession dbSession, String portfolioUuid) { - return mapper(dbSession).selectReferences(portfolioUuid); + public Set selectReferenceUuids(DbSession dbSession, String portfolioUuid) { + return mapper(dbSession).selectReferenceUuids(portfolioUuid); } - public List selectReferencersByKey(DbSession dbSession, String referenceKey) { - return mapper(dbSession).selectReferencersByKey(referenceKey); + public List selectAllReferencesInHierarchy(DbSession dbSession, String uuid) { + return mapper(dbSession).selectAllReferencesInHierarchy(uuid); + } + + public List selectReferencers(DbSession dbSession, String referenceUuid) { + return mapper(dbSession).selectReferencers(referenceUuid); } public List 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 getProjects(DbSession dbSession, String portfolioUuid) { + public List selectProjects(DbSession dbSession, String portfolioUuid) { return mapper(dbSession).selectProjects(portfolioUuid); } - public List getAllProjectsInHierarchy(DbSession dbSession, String rootUuid) { + public List selectAllProjectsInHierarchy(DbSession dbSession, String rootUuid) { return mapper(dbSession).selectAllProjectsInHierarchy(rootUuid); } + public List 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 selectAllProjectsOfPortfolios(DbSession dbSession) { - return mapper(dbSession).selectAllProjectsOfPortfolios(); - } - - public List selectAllReferencesInHierarchy(DbSession dbSession, String uuid) { - return mapper(dbSession).selectAllReferencesInHierarchy(uuid); - } - - public List selectByUuids(DbSession dbSession, Set uuidsToRefresh) { - return mapper(dbSession).selectByUuids(uuidsToRefresh); - } - private static PortfolioMapper mapper(DbSession session) { return session.getMapper(PortfolioMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java index 49cc54ba540..4340b395686 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java @@ -49,9 +49,9 @@ public interface PortfolioMapper { List selectTree(String portfolioUuid); - Set selectReferences(String portfolioUuid); + Set selectReferenceUuids(String portfolioUuid); - List selectReferencersByKey(String referenceKey); + List selectReferencers(String referenceUuid); List 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 selectAllProjectsOfPortfolios(); - - List selectAllReferencesOfPortfolios(); + List selectAllPortfolioProjects(); List 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 index 00000000000..7e75a7ad6cd --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/ReferenceDetailsDto.java @@ -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; + } +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index 8176cd6719a..1281dfbec39 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -78,7 +78,7 @@ pp.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR} - SELECT pp.uuid, pp.project_uuid as projectUuid, @@ -103,7 +103,7 @@ p.root_uuid = #{rootUuid,jdbcType=VARCHAR} - SELECT p.reference_uuid FROM portfolio_references p @@ -120,29 +120,19 @@ pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR} - SELECT FROM portfolios p INNER JOIN portfolios p2 ON p.root_uuid = p2.root_uuid WHERE p2.uuid=#{portfolioUuid,jdbcType=VARCHAR} - SELECT 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 - - 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} @@ -152,12 +142,14 @@ AND reference_uuid = #{referenceUuid,jdbcType=VARCHAR} - (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 @@ -170,7 +162,9 @@ 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' diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java index 7bbfe7b3a1c..593f311cad3 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java @@ -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()); } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index fe8b4374f26..e7ee9c0cc63 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -308,6 +308,10 @@ public class ComponentDbTester { return getProjectDto(insertPublicApplication()); } + public final ProjectDto insertPublicApplicationDto(Consumer dtoPopulator) { + return getProjectDto(insertPublicApplication(dtoPopulator)); + } + public final ProjectDto insertPrivateApplicationDto(Consumer dtoPopulator) { return getProjectDto(insertPrivateApplication(dtoPopulator, defaults())); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java b/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java index bff8fb5665f..773c63fd757 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java @@ -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 result = newArrayList(); + List result = new ArrayList<>(); while (true) { List hits = newArrayList(response.getHits()); for (SearchHit hit : hits) { -- 2.39.5