From 36a39747d81b74279448e001a301a3726f5b4638 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Thu, 7 Dec 2017 17:55:49 +0100 Subject: [PATCH] SONAR-10147 Remove class QualityGates --- .../org/sonar/db/component/ComponentDao.java | 4 +- .../db/organization/OrganizationMapper.xml | 2 + .../db/organization/OrganizationTesting.java | 2 +- .../db/qualitygate/QualityGateDaoTest.java | 8 +- .../db/qualitygate/QualityGateDbTester.java | 34 +++---- .../server/qualitygate/QualityGateFinder.java | 46 +++------- .../server/qualitygate/QualityGateModule.java | 1 - .../qualitygate/QualityGateUpdater.java | 14 +++ .../server/qualitygate/QualityGates.java | 82 ----------------- .../qualitygate/RegisterQualityGates.java | 8 +- .../server/qualitygate/ws/DeselectAction.java | 2 +- .../server/qualitygate/ws/DestroyAction.java | 5 +- .../qualitygate/ws/GetByProjectAction.java | 13 +-- .../server/qualitygate/ws/ListAction.java | 5 +- .../server/qualitygate/ws/SelectAction.java | 2 +- .../qualitygate/ws/SetAsDefaultAction.java | 8 +- .../server/qualitygate/ws/ShowAction.java | 7 +- .../sonar/server/ui/ws/ComponentAction.java | 13 ++- .../server/metric/ws/DeleteActionTest.java | 4 +- .../qualitygate/QgateProjectFinderTest.java | 2 +- .../QualityGateConditionsUpdaterTest.java | 2 +- .../qualitygate/QualityGateFinderTest.java | 55 +++++++----- .../qualitygate/QualityGateModuleTest.java | 2 +- .../server/qualitygate/QualityGatesTest.java | 86 ------------------ .../qualitygate/RegisterQualityGatesTest.java | 9 +- .../server/qualitygate/ws/CopyActionTest.java | 2 +- .../qualitygate/ws/DeselectActionTest.java | 3 +- .../qualitygate/ws/DestroyActionTest.java | 22 ++++- .../ws/GetByProjectActionTest.java | 78 ++++------------ .../server/qualitygate/ws/ListActionTest.java | 30 +++---- .../qualitygate/ws/SelectActionTest.java | 2 +- .../server/qualitygate/ws/ShowActionTest.java | 15 +++- .../server/ui/ws/ComponentActionTest.java | 90 ++++++++++--------- 33 files changed, 221 insertions(+), 437 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index b52280819cf..b91fb92b620 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -240,8 +240,8 @@ public class ComponentDao implements Dao { } /** - * Returns all projects (Scope {@link org.sonar.api.resources.Scopes#PROJECT} and qualifier - * {@link org.sonar.api.resources.Qualifiers#PROJECT}) which are enabled. + * Returns all projects (Scope {@link Scopes#PROJECT} and qualifier + * {@link Qualifiers#PROJECT}) which are enabled. * * Branches are not returned. * diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index 41d758d9536..d48bcec8cfb 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -7,6 +7,7 @@ org.kee as "key", org.name as "name", org.description as "description", + org.default_quality_gate_uuid as "defaultQualityGateUuid", org.url as "url", org.avatar_url as "avatarUrl", org.guarded as "guarded", @@ -190,6 +191,7 @@ name = #{organization.name, jdbcType=VARCHAR}, description = #{organization.description, jdbcType=VARCHAR}, url = #{organization.url, jdbcType=VARCHAR}, + default_quality_gate_uuid = #{organization.defaultQualityGateUuid, jdbcType=VARCHAR}, avatar_url = #{organization.avatarUrl, jdbcType=VARCHAR}, updated_at = #{organization.updatedAt, jdbcType=BIGINT} where diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationTesting.java index 28e35ed2eb9..be98e22154b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationTesting.java @@ -37,7 +37,7 @@ public class OrganizationTesting { .setName(randomAlphanumeric(64)) .setDescription(randomAlphanumeric(256)) .setAvatarUrl(randomAlphanumeric(256)) - .setDefaultQualityGateUuid("1") // TODO check that using it directly is ok + .setDefaultQualityGateUuid("_NOT_SET_") .setUrl(randomAlphanumeric(256)); } } 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 ac27151a5bf..380ff784281 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 @@ -63,7 +63,7 @@ public class QualityGateDaoTest { @Test public void associate() { - QualityGateDto qgate = db.qualityGates().insertQualityGate(); + QualityGateDto qgate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); OrganizationDto org = db.organizations().insert(); underTest.associate(dbSession, Uuids.createFast(), org, qgate); @@ -212,8 +212,8 @@ public class QualityGateDaoTest { } private void insertQualityGates() { - qualityGateDbTester.insertQualityGate(g -> g.setName("Very strict").setBuiltIn(false)); - qualityGateDbTester.insertQualityGate(g -> g.setName("Balanced").setBuiltIn(false)); - qualityGateDbTester.insertQualityGate(g -> g.setName("Lenient").setBuiltIn(false)); + qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Very strict").setBuiltIn(false)); + qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Balanced").setBuiltIn(false)); + qualityGateDbTester.insertQualityGate(db.getDefaultOrganization(), g -> g.setName("Lenient").setBuiltIn(false)); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java index 77bc2d19462..bf96e855297 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java @@ -46,10 +46,6 @@ public class QualityGateDbTester { this.dbSession = db.getSession(); } - public QualityGateDto insertQualityGate(String name) { - return insertQualityGate(qualityGate -> qualityGate.setName(name)); - } - public QualityGateDto insertBuiltInQualityGate() { QualityGateDto builtin = dbClient.qualityGateDao().insert(dbSession, new QualityGateDto() .setName("Sonar way") @@ -60,9 +56,13 @@ public class QualityGateDbTester { return builtin; } - @SafeVarargs - public final QualityGateDto insertQualityGate(Consumer... dtoPopulators) { - return insertQualityGate(db.getDefaultOrganization(), dtoPopulators); + public void setBuiltInAsDefaultOn(OrganizationDto organizationDto) { + QualityGateDto builtinQG = dbClient.qualityGateDao().selectBuiltIn(dbSession); + dbClient.organizationDao().update(dbSession, organizationDto.setDefaultQualityGateUuid(builtinQG.getUuid())); + if (dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organizationDto, builtinQG.getUuid()) == null) { + associateQualityGateToOrganization(builtinQG, organizationDto); + } + dbSession.commit(); } @SafeVarargs @@ -86,17 +86,21 @@ public class QualityGateDbTester { db.commit(); } - public QualityGateDto createDefaultQualityGate(String qualityGateName) { - QualityGateDto defaultQGate = insertQualityGate(qualityGateName); - setDefaultQualityGate(defaultQGate); + public void associateQualityGateToOrganization(QualityGateDto qualityGate, OrganizationDto organization) { + dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organization, qualityGate); + dbSession.commit(); + } + + @SafeVarargs + public final QualityGateDto createDefaultQualityGate(OrganizationDto organization, Consumer... dtoPopulators) { + QualityGateDto defaultQGate = insertQualityGate(organization, dtoPopulators); + setDefaultQualityGate(organization, defaultQGate); return defaultQGate; } - public void setDefaultQualityGate(QualityGateDto qualityGate) { - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() - .setKey("sonar.qualitygate") - .setValue(String.valueOf(qualityGate.getId()))); - db.commit(); + public void setDefaultQualityGate(OrganizationDto organization, QualityGateDto qualityGate) { + dbClient.organizationDao().update(dbSession, organization.setDefaultQualityGateUuid(qualityGate.getUuid())); + dbSession.commit(); } @SafeVarargs diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java index 7bafdce2aa4..b09c72866a1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java @@ -20,20 +20,19 @@ package org.sonar.server.qualitygate; import java.util.Optional; -import org.apache.commons.lang.StringUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateDto; import static com.google.common.base.Preconditions.checkState; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.ws.WsUtils.checkFound; public class QualityGateFinder { + public static final String SONAR_QUALITYGATE_PROPERTY = "sonar.qualitygate"; + private final DbClient dbClient; public QualityGateFinder(DbClient dbClient) { @@ -43,20 +42,17 @@ public class QualityGateFinder { /** * Return effective quality gate of a project. * - * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate. - * As it's possible to have no default quality gate, this method can return {@link Optional#empty()} + * It will first try to get the quality gate explicitly defined on a project, if none it will try to return default quality gate ofI the organization */ - public Optional getQualityGate(DbSession dbSession, long componentId) { + public QualityGateData getQualityGate(DbSession dbSession, OrganizationDto organization, long componentId) { Optional qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, componentId); if (qualityGateId.isPresent()) { QualityGateDto qualityGate = checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId.get()), "No quality gate has been found for id %s", qualityGateId); - return Optional.of(new QualityGateData(qualityGate, false)); + return new QualityGateData(qualityGate, false); } else { - Optional defaultQualityGate = getDefault(dbSession); - if (!defaultQualityGate.isPresent()) { - return Optional.empty(); - } - return Optional.of(new QualityGateData(defaultQualityGate.get(), true)); + QualityGateDto defaultQualityGate = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid()); + checkState(defaultQualityGate != null, "Unable to find the quality gate [%s] for organization [%s]", organization.getDefaultQualityGateUuid(), organization.getUuid()); + return new QualityGateData(defaultQualityGate, true); } } @@ -65,17 +61,10 @@ public class QualityGateFinder { "No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName()); } - // TODO As there is always a default quality gate, this method should not return an optional - public Optional getDefault(DbSession dbSession) { - Optional defaultQualityGateId = getDefaultId(dbSession); - - if (!defaultQualityGateId.isPresent()) { - // For the moment, it's possible to have no default quality gate, but it will change with SONAR-8507 - return Optional.empty(); - } else { - return Optional.ofNullable( - dbClient.qualityGateDao().selectById(dbSession, defaultQualityGateId.get())); - } + public QualityGateDto getDefault(DbSession dbSession, OrganizationDto organization) { + QGateWithOrgDto qgate = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, organization.getDefaultQualityGateUuid()); + checkState(qgate != null, "Default quality gate [%s] is missing on organization [%s]", organization.getDefaultQualityGateUuid(), organization.getUuid()); + return qgate; } public QualityGateDto getBuiltInQualityGate(DbSession dbSession) { @@ -84,17 +73,6 @@ public class QualityGateFinder { return builtIn; } - private Optional getDefaultId(DbSession dbSession) { - PropertyDto defaultQualityGateId = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY); - - if (defaultQualityGateId == null || StringUtils.isBlank(defaultQualityGateId.getValue())) { - // For the moment, it's possible to have no default quality gate, but it will change with SONAR-8507 - return Optional.empty(); - } - - return Optional.of(Long.valueOf(defaultQualityGateId.getValue())); - } - public static class QualityGateData { private final QualityGateDto qualityGate; private final boolean isDefault; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java index a0fd5298e84..03d1e9c3492 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java @@ -42,7 +42,6 @@ public class QualityGateModule extends Module { @Override protected void configureModule() { add( - QualityGates.class, QualityGateUpdater.class, QualityGateConditionsUpdater.class, QgateProjectFinder.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java index 32744f1de84..f9309f764d9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java @@ -22,11 +22,14 @@ package org.sonar.server.qualitygate; import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import static com.google.common.base.Preconditions.checkArgument; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.util.Validation.IS_ALREADY_USED_MESSAGE; public class QualityGateUpdater { @@ -73,6 +76,17 @@ public class QualityGateUpdater { dbClient.qualityGateDao().update(qualityGateDto, dbSession); } + public void dissociateProject(DbSession dbSession, ComponentDto project) { + dbClient.propertiesDao().deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession); + } + + public void associateProject(DbSession dbSession, ComponentDto project, QualityGateDto qualityGate) { + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() + .setKey(SONAR_QUALITYGATE_PROPERTY) + .setResourceId(project.getId()) + .setValue(String.valueOf(qualityGate.getId()))); + } + private void checkQualityGateDoesNotAlreadyExist(DbSession dbSession, OrganizationDto organizationDto, String name) { QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organizationDto, name); checkArgument(existingQgate == null, IS_ALREADY_USED_MESSAGE, "Name"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java deleted file mode 100644 index 0df8928ded6..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.server.qualitygate; - -import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.permission.OrganizationPermission; -import org.sonar.db.property.PropertiesDao; -import org.sonar.db.qualitygate.QualityGateDao; -import org.sonar.db.qualitygate.QualityGateDto; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.user.UserSession; - -import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; - -/** - * Methods from this class should be moved to {@link QualityGateUpdater} and to classes QualityGateFinder / QualityGateConditionsUpdater / etc. - * in order to have classes with clearer responsibilities and more easily testable (without having to use too much mocks) - */ -public class QualityGates { - - public static final String SONAR_QUALITYGATE_PROPERTY = "sonar.qualitygate"; - - private final DbClient dbClient; - private final QualityGateDao dao; - private final PropertiesDao propertiesDao; - private final UserSession userSession; - private final DefaultOrganizationProvider organizationProvider; - - public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider) { - this.dbClient = dbClient; - this.dao = dbClient.qualityGateDao(); - this.propertiesDao = dbClient.propertiesDao(); - this.userSession = userSession; - this.organizationProvider = organizationProvider; - } - - public void dissociateProject(DbSession dbSession, ComponentDto project) { - checkProjectAdmin(project); - propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession); - dbSession.commit(); - } - - private QualityGateDto getNonNullQgate(DbSession dbSession, long id) { - QualityGateDto qGate = dao.selectById(dbSession, id); - if (qGate == null) { - throw new NotFoundException("There is no quality gate with id=" + id); - } - return qGate; - } - - private void checkIsQualityGateAdministrator() { - userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); - } - - private void checkProjectAdmin(ComponentDto project) { - if (!userSession.hasPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, project.getOrganizationUuid()) - && !userSession.hasComponentPermission(UserRole.ADMIN, project)) { - throw insufficientPrivilegesException(); - } - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java index 97d2bdb3347..3e56b8e5700 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java @@ -68,19 +68,15 @@ public class RegisterQualityGates implements Startable { private final DbClient dbClient; private final QualityGateConditionsUpdater qualityGateConditionsUpdater; - private final QualityGateFinder qualityGateFinder; - private final QualityGateUpdater qualityGateUpdater; private final QualityGateDao qualityGateDao; private final QualityGateConditionDao qualityGateConditionDao; private final UuidFactory uuidFactory; private final System2 system2; - public RegisterQualityGates(DbClient dbClient, QualityGateUpdater qualityGateUpdater, - QualityGateConditionsUpdater qualityGateConditionsUpdater, QualityGateFinder qualityGateFinder, UuidFactory uuidFactory, System2 system2) { + public RegisterQualityGates(DbClient dbClient, + QualityGateConditionsUpdater qualityGateConditionsUpdater, UuidFactory uuidFactory, System2 system2) { this.dbClient = dbClient; this.qualityGateConditionsUpdater = qualityGateConditionsUpdater; - this.qualityGateUpdater = qualityGateUpdater; - this.qualityGateFinder = qualityGateFinder; this.qualityGateDao = dbClient.qualityGateDao(); this.qualityGateConditionDao = dbClient.gateConditionDao(); this.uuidFactory = uuidFactory; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeselectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeselectAction.java index c8d4504066b..77d948e7c1a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeselectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeselectAction.java @@ -32,7 +32,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.component.ComponentFinder; -import static org.sonar.server.qualitygate.QualityGateUpdater.SONAR_QUALITYGATE_PROPERTY; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java index 959ecb787d8..cd58fa69122 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.qualitygate.ws; -import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -67,8 +66,8 @@ public class DestroyAction implements QualityGatesWsAction { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); QGateWithOrgDto qualityGate = finder.getByOrganizationAndId(dbSession, organization, qualityGateId); - Optional defaultQualityGate = finder.getDefault(dbSession); - checkArgument(!defaultQualityGate.isPresent() || !defaultQualityGate.get().getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); + QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); + checkArgument(!defaultQualityGate.getId().equals(qualityGate.getId()), "The default quality gate cannot be removed"); wsSupport.checkCanEdit(qualityGate); dbClient.qualityGateDao().delete(qualityGate, dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java index 1762a640c24..a4d481696db 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.qualitygate.ws; -import java.util.Optional; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -100,24 +99,20 @@ public class GetByProjectAction implements QualityGatesWsAction { throw insufficientPrivilegesException(); } - Optional data = qualityGateFinder.getQualityGate(dbSession, project.getId()); + QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project.getId()); writeProtobuf(buildResponse(data), request, response); } } - private static GetByProjectResponse buildResponse(Optional data) { - if (!data.isPresent()) { - return GetByProjectResponse.getDefaultInstance(); - } - - QualityGateDto qualityGate = data.get().getQualityGate(); + private static GetByProjectResponse buildResponse(QualityGateData data) { + QualityGateDto qualityGate = data.getQualityGate(); GetByProjectResponse.Builder response = GetByProjectResponse.newBuilder(); response.getQualityGateBuilder() .setId(qualityGate.getId()) .setName(qualityGate.getName()) - .setDefault(data.get().isDefault()); + .setDefault(data.isDefault()); return response.build(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java index 64d341bfb52..245c20791d3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java @@ -21,7 +21,6 @@ package org.sonar.server.qualitygate.ws; import com.google.common.io.Resources; import java.util.Collection; -import java.util.Optional; import javax.annotation.Nullable; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; @@ -70,9 +69,9 @@ public class ListAction implements QualityGatesWsAction { public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); - Optional defaultQualityGate = finder.getDefault(dbSession); + QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization); Collection qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization); - writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate.orElse(null)), request, response); + writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java index 79dde6012a1..63366d1fa57 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java @@ -35,7 +35,7 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.ComponentFinder.ParamNames; import org.sonar.server.qualitygate.QualityGateFinder; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_SELECT; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java index 54f1f9d0cbe..9ac53b56ec1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java @@ -27,29 +27,23 @@ import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.qualitygate.QualityGateFinder; -import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.user.UserSession; -import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.server.qualitygate.ws.QualityGatesWs.parseId; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; -import static org.sonar.server.ws.WsUtils.writeProtobuf; -import static org.sonarqube.ws.Qualitygates.QualityGate.newBuilder; public class SetAsDefaultAction implements QualityGatesWsAction { private final DbClient dbClient; private final UserSession userSession; - private final QualityGateUpdater qualityGateUpdater; private final QualityGateFinder qualityGateFinder; private final QualityGatesWsSupport wsSupport; - public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGateUpdater qualityGateUpdater, + public SetAsDefaultAction(DbClient dbClient, UserSession userSession, QualityGateFinder qualityGateFinder, QualityGatesWsSupport qualityGatesWsSupport) { this.dbClient = dbClient; this.userSession = userSession; - this.qualityGateUpdater = qualityGateUpdater; this.qualityGateFinder = qualityGateFinder; this.wsSupport = qualityGatesWsSupport; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index b6a08d62f45..d18508f2b2e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -22,7 +22,6 @@ package org.sonar.server.qualitygate.ws; import com.google.common.io.Resources; import java.util.Collection; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.function.Function; import javax.annotation.Nullable; @@ -95,8 +94,8 @@ public class ShowAction implements QualityGatesWsAction { QualityGateDto qualityGate = getByNameOrId(dbSession, organization, name, id); Collection conditions = getConditions(dbSession, qualityGate); Map metricsById = getMetricsById(dbSession, conditions); - Optional defaultQualityGate = qualityGateFinder.getDefault(dbSession); - writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response); + QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession, organization); + writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate, conditions, metricsById), request, response); } } @@ -121,7 +120,7 @@ public class ShowAction implements QualityGatesWsAction { .collect(uniqueIndex(MetricDto::getId)); } - private ShowWsResponse buildResponse(OrganizationDto organization, QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate, + private ShowWsResponse buildResponse(OrganizationDto organization, QualityGateDto qualityGate, QualityGateDto defaultQualityGate, Collection conditions, Map metricsById) { return ShowWsResponse.newBuilder() .setId(qualityGate.getId()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index 34156db7759..0570cecff8c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -144,7 +144,7 @@ public class ComponentAction implements NavigationWsAction { json.beginObject(); writeComponent(json, session, component, org, analysis.orElse(null)); writeProfiles(json, session, component); - writeQualityGate(json, session, component); + writeQualityGate(json, session, org, component); if (userSession.hasComponentPermission(ADMIN, component) || userSession.hasPermission(ADMINISTER_QUALITY_PROFILES, org) || userSession.hasPermission(ADMINISTER_QUALITY_GATES, org)) { @@ -206,16 +206,13 @@ public class ComponentAction implements NavigationWsAction { json.endArray(); } - private void writeQualityGate(JsonWriter json, DbSession session, ComponentDto component) { - Optional qualityGateData = qualityGateFinder.getQualityGate(session, component.getId()); - if (!qualityGateData.isPresent()) { - return; - } - QualityGateDto qualityGateDto = qualityGateData.get().getQualityGate(); + private void writeQualityGate(JsonWriter json, DbSession session, OrganizationDto organization, ComponentDto component) { + QualityGateFinder.QualityGateData qualityGateData = qualityGateFinder.getQualityGate(session, organization, component.getId()); + QualityGateDto qualityGateDto = qualityGateData.getQualityGate(); json.name("qualityGate").beginObject() .prop("key", qualityGateDto.getId()) .prop("name", qualityGateDto.getName()) - .prop("isDefault", qualityGateData.get().isDefault()) + .prop("isDefault", qualityGateData.isDefault()) .endObject(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java index 45e3aaaa946..03ff0778868 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java @@ -110,9 +110,9 @@ public class DeleteActionTest { loggedAsSystemAdministrator(); MetricDto customMetric = insertCustomMetric("custom-key"); MetricDto nonCustomMetric = insertMetric(newMetricDto().setEnabled(true).setUserManaged(false).setKey("non-custom")); - QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(); + QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); db.qualityGates().addCondition(qualityGate1, customMetric); - QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(); + QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); db.qualityGates().addCondition(qualityGate2, customMetric); db.qualityGates().addCondition(qualityGate2, nonCustomMetric); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QgateProjectFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QgateProjectFinderTest.java index 2e0e79bafe0..93f491c659a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QgateProjectFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QgateProjectFinderTest.java @@ -51,7 +51,7 @@ import static org.sonar.db.component.ComponentTesting.newPublicProjectDto; import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.IN; import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.OUT; import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.builder; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; public class QgateProjectFinderTest { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java index 6bb3284df74..8b1cd6c40bb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java @@ -78,7 +78,7 @@ public class QualityGateConditionsUpdaterTest { @Before public void setUp() throws Exception { - qualityGateDto = qualityGateDbTester.insertQualityGate(); + qualityGateDto = qualityGateDbTester.insertQualityGate(db.getDefaultOrganization()); dbClient.metricDao().insert(dbSession, coverageMetricDto, ratingMetricDto); dbSession.commit(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java index fcac7f74ff6..7f900c3d737 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.qualitygate; -import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -30,6 +29,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.NotFoundException; +import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; public class QualityGateFinderTest { @@ -47,56 +47,63 @@ public class QualityGateFinderTest { @Test public void return_default_quality_gate_for_project() { ComponentDto project = db.components().insertPrivateProject(); - QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate("Sonar way"); + QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); - Optional result = underTest.getQualityGate(dbSession, project.getId()); + QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId()); - assertThat(result).isPresent(); - assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); - assertThat(result.get().isDefault()).isTrue(); + assertThat(result).isNotNull(); + assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); + assertThat(result.isDefault()).isTrue(); } @Test public void return_project_quality_gate_over_default() { ComponentDto project = db.components().insertPrivateProject(); - db.qualityGates().createDefaultQualityGate("Sonar way"); - QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate("My team QG"); + db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(),qg -> qg.setName("Sonar way")); + QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); - Optional result = underTest.getQualityGate(dbSession, project.getId()); + QualityGateFinder.QualityGateData result = underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId()); - assertThat(result).isPresent(); - assertThat(result.get().getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); - assertThat(result.get().isDefault()).isFalse(); + assertThat(result).isNotNull(); + assertThat(result.getQualityGate().getId()).isEqualTo(dbQualityGate.getId()); + assertThat(result.isDefault()).isFalse(); } @Test - public void return_nothing_when_no_default_qgate_and_no_qgate_defined_for_project() { + public void fail_when_default_qgate_defined_does_not_exists() { ComponentDto project = db.components().insertPrivateProject(); + QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); + db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); + db.commit(); - Optional result = underTest.getQualityGate(dbSession, project.getId()); + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage(format("Unable to find the quality gate [%s] for organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid())); - assertThat(result).isNotPresent(); + underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId()); } @Test - public void fail_when_default_qgate_defined_in_properties_does_not_exists() { + public void fail_when_project_qgate_defined_does_not_exists() { ComponentDto project = db.components().insertPrivateProject(); - QualityGateDto dbQualityGate = db.qualityGates().createDefaultQualityGate("Sonar way"); + QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); + db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); - assertThat(underTest.getQualityGate(dbSession, project.getId())).isEmpty(); + expectedException.expect(NotFoundException.class); + underTest.getQualityGate(dbSession, db.getDefaultOrganization(), project.getId()); } @Test - public void fail_when_project_qgate_defined_in_properties_does_not_exists() { - ComponentDto project = db.components().insertPrivateProject(); - QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate("My team QG"); - db.qualityGates().associateProjectToQualityGate(project, dbQualityGate); + public void fail_when_default_quality_gate_does_not_exists() { + QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); + db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate); db.getDbClient().qualityGateDao().delete(dbQualityGate, dbSession); - expectedException.expect(NotFoundException.class); - underTest.getQualityGate(dbSession, project.getId()); + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage(format("Default quality gate [%s] is missing on organization [%s]", dbQualityGate.getUuid(), db.getDefaultOrganization().getUuid())); + + underTest.getDefault(dbSession, db.getDefaultOrganization()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java index 0abae931b0b..afb06808535 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java @@ -29,6 +29,6 @@ public class QualityGateModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new QualityGateModule().configure(container); - assertThat(container.size()).isEqualTo(22 + 2); + assertThat(container.size()).isEqualTo(21 + 2); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java deleted file mode 100644 index 03e795f5ecb..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 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.server.qualitygate; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.ArgumentCaptor; -import org.sonar.core.util.Uuids; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDao; -import org.sonar.db.organization.OrganizationTesting; -import org.sonar.db.permission.OrganizationPermission; -import org.sonar.db.property.PropertiesDao; -import org.sonar.db.property.PropertyDto; -import org.sonar.db.qualitygate.QualityGateConditionDao; -import org.sonar.db.qualitygate.QualityGateDao; -import org.sonar.db.qualitygate.QualityGateDto; -import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.tester.UserSessionRule; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; - -public class QualityGatesTest { - - private static final long QUALITY_GATE_ID = 42L; - private static final String PROJECT_KEY = "SonarQube"; - private static final String PROJECT_UUID = Uuids.UUID_EXAMPLE_01; - private static final String ORG_UUID = "ORG_UUID"; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.fromUuid(ORG_UUID); - private DbSession dbSession = mock(DbSession.class); - private DbClient dbClient = mock(DbClient.class); - private QualityGateDao dao = mock(QualityGateDao.class); - private QualityGateConditionDao conditionDao = mock(QualityGateConditionDao.class); - private PropertiesDao propertiesDao = mock(PropertiesDao.class); - private ComponentDao componentDao = mock(ComponentDao.class); - private QualityGates underTest; - - @Before - public void setUp() { - when(dbClient.openSession(false)).thenReturn(dbSession); - when(dbClient.qualityGateDao()).thenReturn(dao); - when(dbClient.gateConditionDao()).thenReturn(conditionDao); - when(dbClient.propertiesDao()).thenReturn(propertiesDao); - when(dbClient.componentDao()).thenReturn(componentDao); - - when(componentDao.selectOrFailById(eq(dbSession), anyLong())).thenReturn( - newPrivateProjectDto(OrganizationTesting.newOrganizationDto(), PROJECT_UUID).setId(1L).setDbKey(PROJECT_KEY)); - - underTest = new QualityGates(dbClient, userSession, organizationProvider); - - userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java index 21f576a81a4..8a848f2bd9f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java @@ -75,7 +75,7 @@ public class RegisterQualityGatesTest { private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()); private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); - private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateUpdater, qualityGateConditionsUpdater, qualityGateFinder, + private RegisterQualityGates underTest = new RegisterQualityGates(dbClient, qualityGateConditionsUpdater, UuidFactoryFast.getInstance(), System2.INSTANCE); private QualityGateDto builtInQG; @@ -215,11 +215,8 @@ public class RegisterQualityGatesTest { underTest.start(); - assertThat(qualityGateFinder.getDefault(dbSession)).isPresent(); - assertThat(qualityGateFinder.getDefault(dbSession).get().getId()).isEqualTo(builtInQG.getId()); - - assertThat( - logTester.logs(LoggerLevel.INFO).contains("Built-in quality gate [Sonar way] has been set as default")).isTrue(); + assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession)).isNotNull(); + assertThat(qualityGateFinder.getBuiltInQualityGate(dbSession).getId()).isEqualTo(builtInQG.getId()); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java index 1e3d33ae893..5819388872e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java @@ -151,7 +151,7 @@ public class CopyActionTest { @Test public void response_contains_quality_gate() { userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); QualityGate response = ws.newRequest() .setParam(PARAM_ID, qualityGate.getId().toString()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java index f6d740f74a0..4315421bb25 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java @@ -46,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; public class DeselectActionTest { @@ -314,5 +314,4 @@ public class DeselectActionTest { private void assertSelected(String qGateId, long projectId) { assertThat(dbClient.propertiesDao().selectProjectProperty(projectId, SONAR_QUALITYGATE_PROPERTY).getValue()).isEqualTo(qGateId); } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java index 496eabce710..65feb0d94f3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java @@ -29,6 +29,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.qualitygate.QGateWithOrgDto; +import org.sonar.db.qualitygate.QualityGateDbTester; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -59,6 +60,7 @@ public class DestroyActionTest { private DbClient dbClient = db.getDbClient(); private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db); private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); + private QualityGateDbTester qualityGateDbTester = new QualityGateDbTester(db); private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider); private DbSession dbSession = db.getSession(); @@ -71,13 +73,15 @@ public class DestroyActionTest { userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + qualityGateDbTester.setBuiltInAsDefaultOn(organization); + ws.newRequest() .setParam(PARAM_ID, qualityGate.getId().toString()) .setParam(PARAM_ORGANIZATION, organization.getKey()) .execute(); assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull(); - assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero(); + assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isEqualTo(1); // built-in quality gate } @Test @@ -86,8 +90,10 @@ public class DestroyActionTest { userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + qualityGateDbTester.setBuiltInAsDefaultOn(organization); + + QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); ws.newRequest() .setParam(PARAM_ID, valueOf(qualityGate.getId())) @@ -100,6 +106,8 @@ public class DestroyActionTest { @Test public void does_not_delete_built_in_quality_gate() { OrganizationDto organization = db.organizations().insert(); + qualityGateDbTester.setBuiltInAsDefaultOn(organization); + userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); @@ -115,6 +123,9 @@ public class DestroyActionTest { @Test public void default_organization_is_used_when_no_organization_parameter() { userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); + + qualityGateDbTester.setBuiltInAsDefaultOn(db.getDefaultOrganization()); + QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); OrganizationDto otherOrganization = db.organizations().insert(); QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); @@ -160,7 +171,7 @@ public class DestroyActionTest { OrganizationDto organization = db.organizations().insert(); userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - db.qualityGates().setDefaultQualityGate(qualityGate); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The default quality gate cannot be removed"); @@ -187,6 +198,9 @@ public class DestroyActionTest { @Test public void fail_when_not_quality_gates_administer() { OrganizationDto organization = db.organizations().insert(); + + qualityGateDbTester.setBuiltInAsDefaultOn(organization); + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java index c053edc7e8c..2514159a3f2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java @@ -31,7 +31,6 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QGateWithOrgDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.component.TestComponentFinder; @@ -86,7 +85,7 @@ public class GetByProjectActionTest { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My team QG")); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); + db.qualityGates().associateProjectToQualityGate(project, qualityGate); logInAsProjectUser(project); String result = ws.newRequest() @@ -100,27 +99,12 @@ public class GetByProjectActionTest { .isSimilarTo(getClass().getResource("get_by_project-example.json")); } - @Test - public void empty_response() { - OrganizationDto organization = db.organizations().insert(); - ComponentDto project = db.components().insertPrivateProject(organization); - db.qualityGates().insertQualityGate(organization); - logInAsProjectUser(project); - - String result = ws.newRequest() - .setParam("project", project.getKey()) - .setParam("organization", organization.getKey()) - .execute().getInput(); - - assertThat(result).isEqualToIgnoringWhitespace("{}"); - } - @Test public void default_quality_gate() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); - QGateWithOrgDto dbQualityGate = db.qualityGates().insertQualityGate(organization); - setDefaultQualityGate(dbQualityGate.getId()); + QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, dbQualityGate); logInAsProjectUser(project); GetByProjectResponse result = ws.newRequest() @@ -139,9 +123,9 @@ public class GetByProjectActionTest { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); QGateWithOrgDto defaultDbQualityGate = db.qualityGates().insertQualityGate(organization); - setDefaultQualityGate(defaultDbQualityGate.getId()); + db.qualityGates().setDefaultQualityGate(organization, defaultDbQualityGate); QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); + db.qualityGates().associateProjectToQualityGate(project, qualityGate); logInAsProjectUser(project); GetByProjectResponse result = ws.newRequest() @@ -158,8 +142,9 @@ public class GetByProjectActionTest { public void get_by_project_key() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); - QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); + + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("My team QG")); + db.qualityGates().associateProjectToQualityGate(project, qualityGate); logInAsProjectUser(project); GetByProjectResponse result = ws.newRequest() @@ -174,9 +159,9 @@ public class GetByProjectActionTest { public void get_with_project_admin_permission() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); - QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); GetByProjectResponse result = ws.newRequest() .setParam("project", project.getKey()) @@ -190,9 +175,9 @@ public class GetByProjectActionTest { public void get_with_project_user_permission() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); userSession.logIn().addProjectPermission(UserRole.USER, project); - QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); GetByProjectResponse result = ws.newRequest() .setParam("project", project.getKey()) @@ -207,8 +192,8 @@ public class GetByProjectActionTest { OrganizationDto organization = db.getDefaultOrganization(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); ComponentDto project = db.components().insertPrivateProject(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); userSession.logIn().addProjectPermission(UserRole.USER, project); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); GetByProjectResponse result = ws.newRequest() .setParam("project", project.getKey()) @@ -217,30 +202,12 @@ public class GetByProjectActionTest { assertThat(result.getQualityGate().getName()).isEqualTo(qualityGate.getName()); } - @Test - public void fail_when_project_does_not_not_belong_to_organization() { - OrganizationDto organization = db.organizations().insert(); - OrganizationDto otherOrganization = db.organizations().insert(); - ComponentDto project = db.components().insertPrivateProject(otherOrganization); - userSession.logIn().addProjectPermission(UserRole.USER, project); - QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); - - expectedException.expect(NotFoundException.class); - expectedException.expectMessage(String.format("Project '%s' doesn't exist in organization '%s'", project.getKey(), organization.getKey())); - - ws.newRequest() - .setParam("project", project.getKey()) - .setParam("organization", organization.getKey()) - .execute(); - } - @Test public void fail_when_insufficient_permission() { OrganizationDto organization = db.organizations().insert(); + QualityGateDto dbQualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); + db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), dbQualityGate); ComponentDto project = db.components().insertPrivateProject(organization); - QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - associateProjectToQualityGate(project.getId(), qualityGate.getId()); userSession.logIn(); expectedException.expect(ForbiddenException.class); @@ -291,21 +258,6 @@ public class GetByProjectActionTest { .execute(); } - private void associateProjectToQualityGate(long componentId, long qualityGateId) { - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() - .setKey("sonar.qualitygate") - .setResourceId(componentId) - .setValue(String.valueOf(qualityGateId))); - db.commit(); - } - - private void setDefaultQualityGate(long qualityGateId) { - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() - .setKey("sonar.qualitygate") - .setValue(String.valueOf(qualityGateId))); - db.commit(); - } - private void logInAsProjectUser(ComponentDto project) { userSession.logIn().addProjectPermission(UserRole.USER, project); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java index 21639462b2f..b43d806bde6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java @@ -83,7 +83,7 @@ public class ListActionTest { userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, organization); QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way").setBuiltIn(true)); db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setName("Sonar way - Without Coverage").setBuiltIn(false)); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); String response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -99,7 +99,7 @@ public class ListActionTest { OrganizationDto organization = db.organizations().insert(); QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization); QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -115,8 +115,11 @@ public class ListActionTest { @Test public void default_organization_is_used_when_no_organization_parameter() { QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); + db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate); + OrganizationDto otherOrganization = db.organizations().insert(); QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); + db.qualityGates().setDefaultQualityGate(otherOrganization, otherQualityGate); ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class); @@ -130,6 +133,7 @@ public class ListActionTest { OrganizationDto organization = db.organizations().insert(); QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(true)); QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qualityGate -> qualityGate.setBuiltIn(false)); + db.qualityGates().setDefaultQualityGate(organization, qualityGate1); ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -146,7 +150,7 @@ public class ListActionTest { public void test_deprecated_default_field() { OrganizationDto organization = db.organizations().insert(); QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -160,13 +164,12 @@ public class ListActionTest { OrganizationDto organization = db.organizations().insert(); QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); + expectedException.expect(IllegalStateException.class); + ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) .executeProtobuf(ListWsResponse.class); - assertThat(response.getQualitygatesList()) - .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault) - .containsExactlyInAnyOrder(tuple(qualityGate.getId(), qualityGate.getName(), false)); } @Test @@ -176,7 +179,7 @@ public class ListActionTest { QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Default").setBuiltIn(false)); QualityGateDto builtInQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true)); QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false)); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -201,7 +204,7 @@ public class ListActionTest { userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization); QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way").setBuiltIn(true)); QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("Sonar way - Without Coverage").setBuiltIn(false)); - db.qualityGates().setDefaultQualityGate(defaultQualityGate); + db.qualityGates().setDefaultQualityGate(organization, defaultQualityGate); ListWsResponse response = ws.newRequest() .setParam("organization", organization.getKey()) @@ -218,15 +221,4 @@ public class ListActionTest { tuple(defaultQualityGate.getName(), false, false, false, false, false, false), tuple(otherQualityGate.getName(), false, false, false, false, false, false)); } - - @Test - public void empty() { - OrganizationDto organization = db.organizations().insert(); - - ListWsResponse response = ws.newRequest() - .setParam("organization", organization.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getQualitygatesList()).isEmpty(); - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java index cbb7e770f67..46b753ea430 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java @@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.ISSUE_ADMIN; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; -import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; +import static org.sonar.server.qualitygate.QualityGateFinder.SONAR_QUALITYGATE_PROPERTY; public class SelectActionTest { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java index d5ee72e1cd1..3f588441d49 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java @@ -85,6 +85,8 @@ public class ShowActionTest { OrganizationDto organization = db.organizations().insert(); userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate")); + QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate 2")); + db.qualityGates().setDefaultQualityGate(organization, qualityGate2); MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations")); MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("critical_violations")); db.qualityGates().addCondition(qualityGate, blockerViolationsMetric, c -> c.setOperator("GT").setPeriod(null).setErrorThreshold("0").setWarningThreshold(null)); @@ -104,6 +106,7 @@ public class ShowActionTest { public void show() { OrganizationDto organization = db.organizations().insert(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); MetricDto metric = db.measures().insertMetric(); QualityGateConditionDto condition1 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("GT").setPeriod(null)); QualityGateConditionDto condition2 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("LT").setPeriod(1)); @@ -129,6 +132,7 @@ public class ShowActionTest { QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization()); OrganizationDto otherOrganization = db.organizations().insert(); QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization); + db.qualityGates().setDefaultQualityGate(db.getDefaultOrganization(), qualityGate); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -141,6 +145,7 @@ public class ShowActionTest { public void show_built_in() { OrganizationDto organization = db.organizations().insert(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -154,6 +159,7 @@ public class ShowActionTest { public void show_by_id() { OrganizationDto organization = db.organizations().insert(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); ShowWsResponse response = ws.newRequest() .setParam("id", qualityGate.getId().toString()) @@ -168,6 +174,7 @@ public class ShowActionTest { public void no_condition() { OrganizationDto organization = db.organizations().insert(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -184,6 +191,8 @@ public class ShowActionTest { OrganizationDto organization = db.organizations().insert(); userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate2); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -204,7 +213,7 @@ public class ShowActionTest { OrganizationDto organization = db.organizations().insert(); userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); - db.qualityGates().setDefaultQualityGate(qualityGate); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -225,6 +234,8 @@ public class ShowActionTest { OrganizationDto organization = db.organizations().insert(); userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); + QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(false)); + db.qualityGates().setDefaultQualityGate(organization, qualityGate2); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -245,6 +256,7 @@ public class ShowActionTest { OrganizationDto organization = db.organizations().insert(); userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true)); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); ShowWsResponse response = ws.newRequest() .setParam("name", qualityGate.getName()) @@ -292,6 +304,7 @@ public class ShowActionTest { public void fail_when_condition_is_on_disabled_metric() { OrganizationDto organization = db.organizations().insert(); QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); + db.qualityGates().setDefaultQualityGate(organization, qualityGate); MetricDto metric = db.measures().insertMetric(); db.qualityGates().addCondition(qualityGate, metric); db.getDbClient().metricDao().disableCustomByKey(db.getSession(), metric.getKey()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index 6c376653434..1492e5840ea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -100,19 +100,22 @@ public class ComponentActionTest { private ComponentDto project; private WsActionTester ws; + private OrganizationDto organization; @Before public void before() { - OrganizationDto organization = dbTester.organizations().insertForKey("my-org"); + organization = dbTester.organizations().insertForKey("my-org"); project = newPrivateProjectDto(organization, "abcd") .setDbKey("polop") .setName("Polop") .setDescription("test project") .setLanguage("xoo"); + dbTester.qualityGates().setBuiltInAsDefaultOn(organization); + dbTester.qualityGates().setBuiltInAsDefaultOn(dbTester.getDefaultOrganization()); } @Test - public void check_definition() throws Exception { + public void check_definition() { init(); WebService.Action action = ws.getDef(); @@ -133,7 +136,7 @@ public class ComponentActionTest { } @Test - public void fail_on_missing_parameters() throws Exception { + public void fail_on_missing_parameters() { init(); expectedException.expect(IllegalArgumentException.class); @@ -141,7 +144,7 @@ public class ComponentActionTest { } @Test - public void fail_on_unknown_component_key() throws Exception { + public void fail_on_unknown_component_key() { init(); expectedException.expect(NotFoundException.class); @@ -149,7 +152,7 @@ public class ComponentActionTest { } @Test - public void throw_ForbiddenException_if_required_permission_is_not_granted() throws Exception { + public void throw_ForbiddenException_if_required_permission_is_not_granted() { init(); componentDbTester.insertComponent(project); userSession.logIn(); @@ -159,7 +162,7 @@ public class ComponentActionTest { } @Test - public void return_info_if_user_has_browse_permission_on_project() throws Exception { + public void return_info_if_user_has_browse_permission_on_project() { init(); componentDbTester.insertComponent(project); userSession.logIn().addProjectPermission(UserRole.USER, project); @@ -168,7 +171,7 @@ public class ComponentActionTest { } @Test - public void return_info_if_user_has_administration_permission_on_project() throws Exception { + public void return_info_if_user_has_administration_permission_on_project() { init(); componentDbTester.insertComponent(project); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); @@ -177,7 +180,7 @@ public class ComponentActionTest { } @Test - public void return_info_if_user_is_system_administrator() throws Exception { + public void return_info_if_user_is_system_administrator() { init(); componentDbTester.insertComponent(project); userSession.logIn().setSystemAdministrator(); @@ -186,7 +189,7 @@ public class ComponentActionTest { } @Test - public void return_component_info_when_anonymous_no_snapshot() throws Exception { + public void return_component_info_when_anonymous_no_snapshot() { init(); componentDbTester.insertComponent(project); userSession.addProjectPermission(UserRole.USER, project); @@ -195,7 +198,7 @@ public class ComponentActionTest { } @Test - public void return_component_info_with_favourite() throws Exception { + public void return_component_info_with_favourite() { init(); UserDto user = dbTester.users().insertUser("obiwan"); componentDbTester.insertComponent(project); @@ -206,7 +209,7 @@ public class ComponentActionTest { } @Test - public void return_component_info_when_snapshot() throws Exception { + public void return_component_info_when_snapshot() { init(); componentDbTester.insertComponent(project); componentDbTester.insertSnapshot(newAnalysis(project) @@ -219,9 +222,10 @@ public class ComponentActionTest { } @Test - public void return_component_info_when_file_on_master() throws Exception { + public void return_component_info_when_file_on_master() { init(); OrganizationDto organization = dbTester.organizations().insertForKey("my-org2"); + dbTester.qualityGates().setBuiltInAsDefaultOn(organization); ComponentDto main = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample")); userSession.addProjectPermission(UserRole.USER, main); @@ -236,9 +240,10 @@ public class ComponentActionTest { } @Test - public void return_component_info_when_file_on_branch() throws Exception { + public void return_component_info_when_file_on_branch() { init(); OrganizationDto organization = dbTester.organizations().insertForKey("my-org2"); + dbTester.qualityGates().setBuiltInAsDefaultOn(organization); ComponentDto project = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample")); ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1")); userSession.addProjectPermission(UserRole.USER, project); @@ -259,7 +264,7 @@ public class ComponentActionTest { } @Test - public void return_quality_profiles() throws Exception { + public void return_quality_profiles() { init(); componentDbTester.insertComponent(project); addQualityProfiles(project, @@ -271,7 +276,7 @@ public class ComponentActionTest { } @Test - public void return_empty_quality_profiles_when_no_measure() throws Exception { + public void return_empty_quality_profiles_when_no_measure() { init(); componentDbTester.insertComponent(project); userSession.addProjectPermission(UserRole.USER, project); @@ -280,10 +285,10 @@ public class ComponentActionTest { } @Test - public void return_quality_gate_defined_on_project() throws Exception { + public void return_quality_gate_defined_on_project() { init(); componentDbTester.insertComponent(project); - QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way"); + QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way")); dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto); userSession.addProjectPermission(UserRole.USER, project); @@ -291,27 +296,17 @@ public class ComponentActionTest { } @Test - public void return_default_quality_gate() throws Exception { + public void return_default_quality_gate() { init(); componentDbTester.insertComponent(project); - dbTester.qualityGates().createDefaultQualityGate("Sonar way"); + dbTester.qualityGates().createDefaultQualityGate(organization, qg -> qg.setName("Sonar way")); userSession.addProjectPermission(UserRole.USER, project); executeAndVerify(project.getDbKey(), "return_default_quality_gate.json"); } @Test - public void return_no_quality_gate_when_not_defined_on_project_and_no_default_one() throws Exception { - init(); - componentDbTester.insertComponent(project); - userSession.addProjectPermission(UserRole.USER, project); - - String json = execute(project.getDbKey()); - assertThat(json).doesNotContain("qualityGate"); - } - - @Test - public void return_extensions() throws Exception { + public void return_extensions() { init(createPages()); componentDbTester.insertComponent(project); userSession.anonymous().addProjectPermission(UserRole.USER, project); @@ -320,7 +315,7 @@ public class ComponentActionTest { } @Test - public void return_extensions_for_application() throws Exception { + public void return_extensions_for_application() { Page page = Page.builder("my_plugin/app_page") .setName("App Page") .setScope(COMPONENT) @@ -329,6 +324,8 @@ public class ComponentActionTest { init(page); ComponentDto application = componentDbTester.insertPublicApplication(dbTester.getDefaultOrganization()); + QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way")); + dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto); userSession.registerComponents(application); String result = ws.newRequest() @@ -339,7 +336,7 @@ public class ComponentActionTest { } @Test - public void return_extensions_for_admin() throws Exception { + public void return_extensions_for_admin() { init(createPages()); componentDbTester.insertComponent(project); userSession.anonymous() @@ -350,7 +347,7 @@ public class ComponentActionTest { } @Test - public void return_configuration_for_admin() throws Exception { + public void return_configuration_for_admin() { UserDto user = dbTester.users().insertUser(); componentDbTester.insertComponent(project); userSession.logIn(user) @@ -375,7 +372,7 @@ public class ComponentActionTest { } @Test - public void return_configuration_with_all_properties() throws Exception { + public void return_configuration_with_all_properties() { init(); componentDbTester.insertComponent(project); userSession.anonymous() @@ -397,7 +394,7 @@ public class ComponentActionTest { } @Test - public void return_breadcrumbs_on_module() throws Exception { + public void return_breadcrumbs_on_module() { init(); ComponentDto project = componentDbTester.insertComponent(this.project); ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setDbKey("palap").setName("Palap")); @@ -409,7 +406,7 @@ public class ComponentActionTest { } @Test - public void return_configuration_for_quality_profile_admin() throws Exception { + public void return_configuration_for_quality_profile_admin() { init(); componentDbTester.insertComponent(project); userSession.logIn() @@ -420,7 +417,7 @@ public class ComponentActionTest { } @Test - public void return_configuration_for_quality_gate_admin() throws Exception { + public void return_configuration_for_quality_gate_admin() { init(); componentDbTester.insertComponent(project); userSession.logIn() @@ -431,7 +428,7 @@ public class ComponentActionTest { } @Test - public void return_bread_crumbs_on_several_levels() throws Exception { + public void return_bread_crumbs_on_several_levels() { init(); ComponentDto project = componentDbTester.insertComponent(this.project); ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setDbKey("palap").setName("Palap")); @@ -445,7 +442,7 @@ public class ComponentActionTest { } @Test - public void project_administrator_is_allowed_to_get_information() throws Exception { + public void project_administrator_is_allowed_to_get_information() { init(createPages()); componentDbTester.insertProjectAndSnapshot(project); userSession.addProjectPermission(UserRole.ADMIN, project); @@ -454,7 +451,7 @@ public class ComponentActionTest { } @Test - public void test_example_response() throws Exception { + public void test_example_response() { init(createPages()); OrganizationDto organizationDto = dbTester.organizations().insertForKey("my-org-1"); ComponentDto project = newPrivateProjectDto(organizationDto, "ABCD") @@ -473,7 +470,7 @@ public class ComponentActionTest { addQualityProfiles(project, createQProfile("qp1", "Sonar Way Java", "java"), createQProfile("qp2", "Sonar Way Xoo", "xoo")); - QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate("Sonar way"); + QualityGateDto qualityGateDto = dbTester.qualityGates().insertQualityGate(dbTester.getDefaultOrganization(), qg -> qg.setName("Sonar way")); dbTester.qualityGates().associateProjectToQualityGate(project, qualityGateDto); userSession.logIn(user) .addProjectPermission(UserRole.USER, project) @@ -484,9 +481,10 @@ public class ComponentActionTest { } @Test - public void should_return_private_flag_for_project() throws Exception { + public void should_return_private_flag_for_project() { init(); OrganizationDto org = dbTester.organizations().insert(); + dbTester.qualityGates().setBuiltInAsDefaultOn(org); ComponentDto project = dbTester.components().insertPrivateProject(org); userSession.logIn() @@ -496,9 +494,10 @@ public class ComponentActionTest { } @Test - public void should_return_public_flag_for_project() throws Exception { + public void should_return_public_flag_for_project() { init(); OrganizationDto org = dbTester.organizations().insert(); + dbTester.qualityGates().setBuiltInAsDefaultOn(org); ComponentDto project = dbTester.components().insertPublicProject(org); userSession.logIn() @@ -508,9 +507,10 @@ public class ComponentActionTest { } @Test - public void should_not_return_private_flag_for_module() throws Exception { + public void should_not_return_private_flag_for_module() { init(); OrganizationDto org = dbTester.organizations().insert(); + dbTester.qualityGates().setBuiltInAsDefaultOn(org); ComponentDto project = dbTester.components().insertPrivateProject(org); ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project)); @@ -525,6 +525,7 @@ public class ComponentActionTest { public void canApplyPermissionTemplate_is_true_if_logged_in_as_organization_administrator() { init(createPages()); OrganizationDto org = dbTester.organizations().insert(); + dbTester.qualityGates().setBuiltInAsDefaultOn(org); ComponentDto project = dbTester.components().insertPrivateProject(org); userSession.logIn() @@ -542,6 +543,7 @@ public class ComponentActionTest { public void canUpdateProjectVisibilityToPrivate_is_true_if_logged_in_as_project_administrator_and_extension_returns_false() { init(createPages()); OrganizationDto org = dbTester.organizations().insert(); + dbTester.qualityGates().setBuiltInAsDefaultOn(org); ComponentDto project = dbTester.components().insertPublicProject(org); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); -- 2.39.5