From 5b1e7ed7ed26c291b60ea9e2f70644d34a6354c0 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 30 Nov 2021 15:13:08 -0600 Subject: [PATCH] SONAR-15745 Create 'views/remove_portfolio' and 'views/remove_application' --- .../org/sonar/db/portfolio/PortfolioDao.java | 14 ++++++- .../sonar/db/portfolio/PortfolioMapper.java | 4 +- .../sonar/db/portfolio/PortfolioMapper.xml | 18 ++++----- .../sonar/db/portfolio/PortfolioDaoTest.java | 38 ++++++++++++++++++- 4 files changed, 62 insertions(+), 12 deletions(-) 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 c7e2fc968bf..0b8209472f8 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 @@ -22,6 +22,7 @@ package org.sonar.db.portfolio; import java.util.List; import java.util.Optional; import java.util.Set; +import javax.annotation.CheckForNull; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactory; @@ -158,8 +159,19 @@ public class PortfolioDao implements Dao { return mapper(dbSession).deleteReference(portfolioUuid, referenceUuid); } + @CheckForNull public ReferenceDetailsDto selectReference(DbSession dbSession, String portfolioUuid, String referenceKey) { - return mapper(dbSession).selectReference(portfolioUuid, referenceKey); + return selectReferenceToApp(dbSession, portfolioUuid, referenceKey) + .or(() -> selectReferenceToPortfolio(dbSession, portfolioUuid, referenceKey)) + .orElse(null); + } + + public Optional selectReferenceToApp(DbSession dbSession, String portfolioUuid, String referenceKey) { + return Optional.ofNullable(mapper(dbSession).selectReferenceToApplication(portfolioUuid, referenceKey)); + } + + public Optional selectReferenceToPortfolio(DbSession dbSession, String portfolioUuid, String referenceKey) { + return Optional.ofNullable(mapper(dbSession).selectReferenceToPortfolio(portfolioUuid, referenceKey)); } /* 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 6abdcca9acb..0554a04213d 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 @@ -82,7 +82,9 @@ public interface PortfolioMapper { int deleteReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceUuid") String referenceUuid); - ReferenceDetailsDto selectReference(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey); + ReferenceDetailsDto selectReferenceToPortfolio(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey); + + ReferenceDetailsDto selectReferenceToApplication(@Param("portfolioUuid") String portfolioUuid, @Param("referenceKey") String referenceKey); void deleteAllProjects(); 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 64eff0fb448..08789406dd4 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 @@ -188,34 +188,34 @@ 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.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 + 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} + - UNION - + 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 3ded65b6e54..b4ca9c42ee7 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 @@ -249,7 +249,7 @@ public class PortfolioDaoTest { } @Test - public void add_and_select_references() { + public void add_and_select_references_by_uuid() { assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty(); portfolioDao.addReference(session, "portfolio1", "app1"); portfolioDao.addReference(session, "portfolio1", "app2"); @@ -265,6 +265,42 @@ public class PortfolioDaoTest { .containsExactlyInAnyOrder(1L, 2L, 3L); } + @Test + public void select_reference_to_app_by_key() { + PortfolioDto portfolio = db.components().insertPrivatePortfolioDto("portfolio1"); + ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1")); + db.components().addPortfolioReference(portfolio, app1.getUuid()); + + assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio.getUuid(), app1.getKey())) + .get() + .extracting(ReferenceDto::getTargetUuid) + .isEqualTo(app1.getUuid()); + + assertThat(portfolioDao.selectReference(db.getSession(), portfolio.getUuid(), app1.getKey())) + .extracting(ReferenceDto::getTargetUuid) + .isEqualTo(app1.getUuid()); + + assertThat(portfolioDao.selectReferenceToPortfolio(db.getSession(), portfolio.getUuid(), app1.getKey())).isEmpty(); + } + + @Test + public void select_reference_to_portfolio_by_key() { + PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1"); + PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2"); + db.components().addPortfolioReference(portfolio1, portfolio2); + + assertThat(portfolioDao.selectReferenceToPortfolio(db.getSession(), portfolio1.getUuid(), portfolio2.getKey())) + .get() + .extracting(ReferenceDto::getTargetUuid) + .isEqualTo(portfolio2.getUuid()); + + assertThat(portfolioDao.selectReference(db.getSession(), portfolio1.getUuid(), portfolio2.getKey())) + .extracting(ReferenceDto::getTargetUuid) + .isEqualTo(portfolio2.getUuid()); + + assertThat(portfolioDao.selectReferenceToApp(db.getSession(), portfolio1.getUuid(), portfolio2.getKey())).isEmpty(); + } + @Test public void selectAllReferencesInHierarchy() { PortfolioDto root1 = db.components().insertPrivatePortfolioDto("root1"); -- 2.39.5