]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10096 Delete getDefault() from QualityGatesWsSupport
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 28 Nov 2017 07:53:26 +0000 (08:53 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +0100)
QualityGateFinder has already methods to get default quality gate

server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.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/ShowActionTest.java

index e293080411810189d72dbc67ff9ddff6b2440293..e33623c5fd4ca083c7586d4960e5462be79f770b 100644 (file)
@@ -21,6 +21,7 @@ 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;
@@ -29,6 +30,7 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.qualitygate.QualityGateFinder;
 import org.sonarqube.ws.Qualitygates.ListWsResponse;
 import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
 
@@ -40,10 +42,12 @@ public class ListAction implements QualityGatesWsAction {
 
   private final DbClient dbClient;
   private final QualityGatesWsSupport wsSupport;
+  private final QualityGateFinder finder;
 
-  public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport) {
+  public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport, QualityGateFinder finder) {
     this.dbClient = dbClient;
     this.wsSupport = wsSupport;
+    this.finder = finder;
   }
 
   @Override
@@ -63,9 +67,9 @@ public class ListAction implements QualityGatesWsAction {
   @Override
   public void handle(Request request, Response response) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession);
+      Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
       Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
-      writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response);
+      writeProtobuf(buildResponse(qualityGates, defaultQualityGate.orElse(null)), request, response);
     }
   }
 
index 6cbf5ab48a8fc2cae161862965ff7090e4ad2cdd..e0cf37f5798cb19ff394333d4ee54323fb45cc90 100644 (file)
 
 package org.sonar.server.qualitygate.ws;
 
-import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-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.QualityGateConditionDto;
 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 org.sonarqube.ws.Qualitygates;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
 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.ws.WsUtils.checkFound;
 
 public class QualityGatesWsSupport {
@@ -56,12 +49,6 @@ public class QualityGatesWsSupport {
     return checkFound(dbClient.gateConditionDao().selectById(id, dbSession), "No quality gate condition with id '%d'", id);
   }
 
-  OrganizationDto getOrganization(DbSession dbSession) {
-    String organizationKey = defaultOrganizationProvider.get().getKey();
-    return dbClient.organizationDao().selectByKey(dbSession, organizationKey)
-      .orElseThrow(() -> new NotFoundException(format("No organization with key '%s'", organizationKey)));
-  }
-
   boolean isQualityGateAdmin() {
     return userSession.hasPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
   }
@@ -79,24 +66,6 @@ public class QualityGatesWsSupport {
       .build();
   }
 
-  @CheckForNull
-  QualityGateDto getDefault(DbSession dbSession) {
-    Long defaultId = getDefaultId(dbSession);
-    if (defaultId == null) {
-      return null;
-    }
-    return dbClient.qualityGateDao().selectById(dbSession, defaultId);
-  }
-
-  @CheckForNull
-  private Long getDefaultId(DbSession dbSession) {
-    PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY);
-    if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) {
-      return null;
-    }
-    return Long.valueOf(defaultQgate.getValue());
-  }
-
   void checkCanEdit(QualityGateDto qualityGate) {
     checkNotBuiltInt(qualityGate);
     userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
index 6eee336e81a0dfb4cedbd332e6ee16bb7e51e637..50823e89f474582e029226bd7d38c0542be78c46 100644 (file)
@@ -22,6 +22,7 @@ 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;
@@ -89,8 +90,8 @@ public class ShowAction implements QualityGatesWsAction {
       QualityGateDto qualityGate = qualityGateFinder.getByNameOrId(dbSession, name, id);
       Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
       Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
-      QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession);
-      writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsById), request, response);
+      Optional<QualityGateDto> defaultQualityGate = qualityGateFinder.getDefault(dbSession);
+      writeProtobuf(buildResponse(qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response);
     }
   }
 
index 471da817602741a7cc64b633d5fba1489f72d5d1..f4f78c3f60568972b2b5d753dc757759784f8bdf 100644 (file)
@@ -25,10 +25,12 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.qualitygate.QualityGateFinder;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
@@ -48,9 +50,12 @@ public class ListActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create();
-  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
 
-  private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
+  private DbClient dbClient = db.getDbClient();
+  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+  private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+  private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(),
+    new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider), qualityGateFinder));
 
   @Test
   public void verify_definition() {
index 34c0667ffe6d3fb6e9bb85cc69e4654ac15f45f2..07da702aeac0b298f22f8c4c85981e51dd546cc1 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.qualitygate.QualityGateConditionDto;
@@ -55,10 +56,11 @@ public class ShowActionTest {
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
+  private DbClient dbClient = db.getDbClient();
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
   private WsActionTester ws = new WsActionTester(
-    new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
+    new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()),
+      new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
 
   @Test
   public void verify_definition() {