]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10147 Remove class QualityGates
authorEric Hartmann <hartmann.eric@gmail.com>
Thu, 7 Dec 2017 16:55:49 +0000 (17:55 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
33 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationTesting.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DeselectAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/DestroyAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SelectAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SetAsDefaultAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java
server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QgateProjectFinderTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateConditionsUpdaterTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/qualitygate/RegisterQualityGatesTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/GetByProjectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java
server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java

index b52280819cfa0e7d63022ece5c73231c8d31364b..b91fb92b620532979e3edaab9bc9b279a555d189 100644 (file)
@@ -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.
    *
index 41d758d95362dedfb908bce15534b6cb22ffa356..d48bcec8cfb2c1c0fff83d707376b9af3ae36113 100644 (file)
@@ -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",
       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
index 28e35ed2eb9d60d5539b1fa7b75a18d4a2217a4b..be98e22154bb8a27a0916457a632cf9e7af1892c 100644 (file)
@@ -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));
   }
 }
index ac27151a5bfdef7cfab1f3de83b8eff2bbb63767..380ff784281baf508c9c0d366412436d002277cb 100644 (file)
@@ -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));
   }
 }
index 77bc2d194629d60d8b87d0d8ea8d58c54d9e8d26..bf96e855297e1d05231a65ca8b1e48eea9d51fea 100644 (file)
@@ -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<QualityGateDto>... 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<QualityGateDto>... 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
index 7bafdce2aa4d37ee30762d1cc94eb72daa9bcea9..b09c72866a10d0ea5ef35eea72f81e75c812c449 100644 (file)
 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<QualityGateData> getQualityGate(DbSession dbSession, long componentId) {
+  public QualityGateData getQualityGate(DbSession dbSession, OrganizationDto organization, long componentId) {
     Optional<Long> 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<QualityGateDto> 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<QualityGateDto> getDefault(DbSession dbSession) {
-    Optional<Long> 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<Long> 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;
index a0fd5298e84d45fbb2190790c02571ae19764d27..03d1e9c34920a3806a53fa15f404ed3cf08e331f 100644 (file)
@@ -42,7 +42,6 @@ public class QualityGateModule extends Module {
   @Override
   protected void configureModule() {
     add(
-      QualityGates.class,
       QualityGateUpdater.class,
       QualityGateConditionsUpdater.class,
       QgateProjectFinder.class,
index 32744f1de84cb23e4ca50180662faf24ccc3c8fd..f9309f764d952c20954b7b9c4d1e91bf96508b08 100644 (file)
@@ -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 (file)
index 0df8928..0000000
+++ /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();
-    }
-  }
-}
index 97d2bdb33475d89b6a89ccae425c2789bf66fcfb..3e56b8e57005e7b6aae4b93f92a8c6db87f103fc 100644 (file)
@@ -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;
index c8d4504066b3209d245e1c6b8435b451ea9ad3e6..77d948e7c1af144e77c0fcc7362aaafbd74276cb 100644 (file)
@@ -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;
index 959ecb787d8ff6481cbef80533cf6a9c5fd0df18..cd58fa69122b7fc763421bd419ee057f995418e8 100644 (file)
@@ -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<QualityGateDto> 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);
index 1762a640c24fe6f595d5b2e210e5e9cf38a4af5f..a4d481696db1c109bb184e6c368bb6edd1e5f0ec 100644 (file)
@@ -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<QualityGateData> data = qualityGateFinder.getQualityGate(dbSession, project.getId());
+      QualityGateData data = qualityGateFinder.getQualityGate(dbSession, organization, project.getId());
 
       writeProtobuf(buildResponse(data), request, response);
     }
   }
 
-  private static GetByProjectResponse buildResponse(Optional<QualityGateData> 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();
   }
index 64d341bfb52e662b91a7a5679fe2111034872142..245c20791d318faf260b73d0cfd1e5dd82235708 100644 (file)
@@ -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<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
+      QualityGateDto defaultQualityGate = finder.getDefault(dbSession, organization);
       Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession, organization);
-      writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate.orElse(null)), request, response);
+      writeProtobuf(buildResponse(organization, qualityGates, defaultQualityGate), request, response);
     }
   }
 
index 79dde6012a12acb42f65116e6940e81c9181b718..63366d1fa5747c09a0d80c0a0e8495aea45b79dc 100644 (file)
@@ -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;
index 54f1f9d0cbe7392c3fec0629816b6dbb0d5a03e0..9ac53b56ec10ec04537ceafab193b4c224c1189f 100644 (file)
@@ -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;
   }
index b6a08d62f456d015dbb84873c5b99fc30daca374..d18508f2b2e35cf80003f91c8a037661c44eb90e 100644 (file)
@@ -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<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
       Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
-      Optional<QualityGateDto> 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<QualityGateConditionDto> conditions, Map<Integer, MetricDto> metricsById) {
     return ShowWsResponse.newBuilder()
       .setId(qualityGate.getId())
index 34156db7759b0d53316d71227e00f2711cb08635..0570cecff8c17e53a7ffc518595247eab87e808a 100644 (file)
@@ -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<QualityGateFinder.QualityGateData> 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();
   }
 
index 45e3aaaa946748bd54614b9cbd290d4878648f4c..03ff07788680f4b8b667629c50c6d2a1678e07e7 100644 (file)
@@ -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);
 
index 2e0e79bafe0164e69fc2c1197eb820769866888e..93f491c659afe2e8250e7a4803a196970c84a29d 100644 (file)
@@ -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 {
 
index 6bb3284df74197ca1045d46254b9eb0d5844321a..8b1cd6c40bb06fb5ad075ae2d60b4c2815998529 100644 (file)
@@ -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();
   }
index fcac7f74ff6ac1c1ebd70101804cc4341a816e62..7f900c3d7379275cec9559600ee13cd1bc64ce87 100644 (file)
@@ -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<QualityGateFinder.QualityGateData> 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<QualityGateFinder.QualityGateData> 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<QualityGateFinder.QualityGateData> 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());
   }
 
 }
index 0abae931b0b500df85353901e2660cfa1def678c..afb06808535e5cad1d4f0dc3ef749c3aff0d47a2 100644 (file)
@@ -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 (file)
index 03e795f..0000000
+++ /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());
-  }
-}
index 21f576a81a4f4eee5b9934ad380c8e9de55498cb..8a848f2bd9fac3a2efedf0f254227e7c430618b1 100644 (file)
@@ -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
index 1e3d33ae893ee1debe8867060319ea627442acfc..5819388872e67d2a6aacbaedb6fce14478e31cba 100644 (file)
@@ -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())
index f6d740f74a0097c2e2bf38a0f3031b4fc615a66b..4315421bb259bfcd0db1454f9f54f1d1d92016d2 100644 (file)
@@ -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);
   }
-
 }
index 496eabce71037d5feb5a9286b1fde6483981ed76..65feb0d94f332bb10363c0ad9e819c9de90b1a65 100644 (file)
@@ -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);
 
index c053edc7e8cf6f6b8365d1f0f701921c3c67310e..2514159a3f25bac803bfe820b5aafba16079aa41 100644 (file)
@@ -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);
   }
index 21639462b2f83459416af7a050f1ba0de2634832..b43d806bde670121f2e7e186603112428e3c512b 100644 (file)
@@ -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();
-  }
 }
index cbb7e770f67713ab9d555fdbe2794612e68c7580..46b753ea4305ae02ef08a6abc9276a8229c48523 100644 (file)
@@ -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 {
 
index d5ee72e1cd172f2289a2b498a9fdc01f26c3f6e8..3f588441d49d83ebd3880e903e58c2f5780f87c9 100644 (file)
@@ -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());
index 6c37665343490b82e326eb32f8be7f9d1ab81b09..1492e5840ea94c874f4924aa63d6abb5f312b79d 100644 (file)
@@ -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);