From 0976096bcedf7d54d49baee8ddf8ea2dfeedbcc7 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Fri, 8 Dec 2017 18:19:05 +0100 Subject: [PATCH] SONAR-10134 Fix test on QualityGateDao --- .../org/sonar/db/qualitygate/QualityGateDaoTest.java | 10 ++++++---- .../main/java/org/sonarqube/qa/util/QGateTester.java | 1 + .../org/sonar/server/organization/ws/DeleteAction.java | 5 ++++- .../sonar/server/organization/ws/DeleteActionTest.java | 5 +++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java index 8c950112625..ac27151a5bf 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java @@ -107,7 +107,7 @@ public class QualityGateDaoTest { public void testSelectById() { insertQualityGates(); assertThat(underTest.selectById(dbSession, underTest.selectByName(dbSession, "Very strict").getId()).getName()).isEqualTo("Very strict"); - assertThat(underTest.selectById(dbSession, 42L)).isNull(); + assertThat(underTest.selectById(dbSession, -1L)).isNull(); } @Test @@ -185,16 +185,18 @@ public class QualityGateDaoTest { underTest.deleteByUuids(dbSession, asList(qualityGate1.getUuid(), qualityGate2.getUuid())); dbSession.commit(); - assertThat(db.countRowsOfTable(dbSession, "quality_gates")).isZero(); - assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero(); + assertThat(underTest.selectAll(dbSession, organization).stream()) + .extracting(QualityGateDto::getUuid) + .doesNotContain(qualityGate1.getUuid(), qualityGate2.getUuid()); } @Test public void delete_by_uuids_does_nothing_on_empty_list() { + int nbOfQualityGates = db.countRowsOfTable(dbSession, "quality_gates"); underTest.deleteByUuids(dbSession, Collections.emptyList()); dbSession.commit(); - assertThat(db.countRowsOfTable(dbSession, "quality_gates")).isZero(); + assertThat(db.countRowsOfTable(dbSession, "quality_gates")).isEqualTo(nbOfQualityGates); } @Test diff --git a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/QGateTester.java b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/QGateTester.java index 86c330bee29..87eeca9a8da 100644 --- a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/QGateTester.java +++ b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/QGateTester.java @@ -57,6 +57,7 @@ public class QGateTester { } session.wsClient().qualitygates().list().getQualitygatesList().stream() .filter(qualityGate -> !qualityGate.getIsDefault()) + .filter(qualityGate -> !qualityGate.getIsBuiltIn()) .forEach(qualityGate -> session.wsClient().qualitygates().destroy(new DestroyRequest().setId(Long.toString(qualityGate.getId())))); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java index 6a50ee5ef12..0e25904e76a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java @@ -139,7 +139,10 @@ public class DeleteAction implements OrganizationsWsAction { private void deleteQualityGates(DbSession dbSession, OrganizationDto organization) { Collection qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization); - dbClient.qualityGateDao().deleteByUuids(dbSession, qualityGates.stream().map(QualityGateDto::getUuid).collect(MoreCollectors.toList())); + dbClient.qualityGateDao().deleteByUuids(dbSession, qualityGates.stream() + .filter(q -> !q.isBuiltIn()) + .map(QualityGateDto::getUuid) + .collect(MoreCollectors.toList())); } private void deleteOrganization(DbSession dbSession, OrganizationDto organization) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java index f6db373cd88..a03fce3d006 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java @@ -48,6 +48,7 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.organization.TestOrganizationFlags; +import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.qualityprofile.QProfileFactory; import org.sonar.server.qualityprofile.QProfileFactoryImpl; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; @@ -88,7 +89,7 @@ public class DeleteActionTest { private QProfileFactory qProfileFactory = new QProfileFactoryImpl(dbClient, mock(UuidFactory.class), System2.INSTANCE, mock(ActiveRuleIndexer.class)); private UserIndex userIndex = new UserIndex(es.client(), System2.INSTANCE); private UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); - + private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); private WsActionTester wsTester = new WsActionTester(new DeleteAction(userSession, dbClient, defaultOrganizationProvider, componentCleanerService, organizationFlags, userIndexer, qProfileFactory)); @Test @@ -362,7 +363,7 @@ public class DeleteActionTest { verifyOrganizationDoesNotExist(org); assertThat(db.select("select uuid as \"uuid\" from quality_gates")) .extracting(row -> (String) row.get("uuid")) - .containsOnly(qualityGateInOtherOrg.getUuid()); + .containsOnly(qualityGateInOtherOrg.getUuid(), qualityGateFinder.getBuiltInQualityGate(db.getSession()).getUuid()); assertThat(db.select("select organization_uuid as \"organizationUuid\" from org_quality_gates")) .extracting(row -> (String) row.get("organizationUuid")) .containsOnly(otherOrg.getUuid()); -- 2.39.5