From: Julien Lancelot Date: Tue, 28 Nov 2017 07:53:26 +0000 (+0100) Subject: SONAR-10096 Delete getDefault() from QualityGatesWsSupport X-Git-Tag: 7.0-RC1~212 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=70fd4ee3f448a9bd0ce7e97d269b2ca41355a8f0;p=sonarqube.git SONAR-10096 Delete getDefault() from QualityGatesWsSupport QualityGateFinder has already methods to get default quality gate --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java index e2930804118..e33623c5fd4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java @@ -21,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 defaultQualityGate = finder.getDefault(dbSession); Collection qualityGates = dbClient.qualityGateDao().selectAll(dbSession); - writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response); + writeProtobuf(buildResponse(qualityGates, defaultQualityGate.orElse(null)), request, response); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java index 6cbf5ab48a8..e0cf37f5798 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java @@ -20,24 +20,17 @@ 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()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index 6eee336e81a..50823e89f47 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -22,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 conditions = getConditions(dbSession, qualityGate); Map metricsById = getMetricsById(dbSession, conditions); - QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession); - writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsById), request, response); + Optional defaultQualityGate = qualityGateFinder.getDefault(dbSession); + writeProtobuf(buildResponse(qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java index 471da817602..f4f78c3f605 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java @@ -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() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java index 34c0667ffe6..07da702aeac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java @@ -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() {