From 7b93c4de175621ce43236126a4a239534bed229e Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Fri, 29 Jul 2016 17:41:48 +0200 Subject: [PATCH] SONAR-7924 Modify WS api/qualitygates/select to accept project key (#1126) --- .../server/component/ComponentFinder.java | 20 +- .../server/qualitygate/ws/SelectAction.java | 90 ++++++-- .../server/component/ComponentFinderTest.java | 59 +++++ .../server/qualitygate/ws/AppActionTest.java | 7 +- .../qualitygate/ws/QualityGatesWsTest.java | 22 +- .../qualitygate/ws/SelectActionTest.java | 210 ++++++++++++++++++ .../qualitygate/QualityGatesService.java | 9 + .../client/qualitygate/SelectWsRequest.java | 57 +++++ .../qualitygate/QualityGatesServiceTest.java | 63 ++++++ 9 files changed, 506 insertions(+), 31 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/SelectActionTest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/qualitygate/SelectWsRequest.java create mode 100644 sonar-ws/src/test/java/org/sonarqube/ws/client/qualitygate/QualityGatesServiceTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java index 41554dd51ce..1f4efe50b40 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java @@ -37,6 +37,7 @@ import static org.sonar.server.ws.WsUtils.checkRequest; public class ComponentFinder { private static final String MSG_COMPONENT_ID_OR_KEY_TEMPLATE = "Either '%s' or '%s' must be provided, not both"; + public static final String MSG_PARAMETER_MUST_NOT_BE_EMPTY = "The '%s' parameter must not be empty"; private final DbClient dbClient; @@ -48,11 +49,22 @@ public class ComponentFinder { checkArgument(componentUuid != null ^ componentKey != null, MSG_COMPONENT_ID_OR_KEY_TEMPLATE, parameterNames.getUuidParam(), parameterNames.getKeyParam()); if (componentUuid != null) { - checkArgument(!componentUuid.isEmpty(), "The '%s' parameter must not be empty", parameterNames.getUuidParam()); + checkArgument(!componentUuid.isEmpty(), MSG_PARAMETER_MUST_NOT_BE_EMPTY, parameterNames.getUuidParam()); return getByUuid(dbSession, componentUuid); } - checkArgument(!componentKey.isEmpty(), "The '%s' parameter must not be empty", parameterNames.getKeyParam()); + checkArgument(!componentKey.isEmpty(), MSG_PARAMETER_MUST_NOT_BE_EMPTY, parameterNames.getKeyParam()); + return getByKey(dbSession, componentKey); + } + + public ComponentDto getByIdOrKey(DbSession dbSession, @Nullable Long componentId, @Nullable String componentKey, ParamNames parameterNames) { + checkArgument(componentId != null ^ componentKey != null, MSG_COMPONENT_ID_OR_KEY_TEMPLATE, parameterNames.getUuidParam(), parameterNames.getKeyParam()); + + if (componentId != null) { + return getById(dbSession, componentId); + } + + checkArgument(!componentKey.isEmpty(), MSG_PARAMETER_MUST_NOT_BE_EMPTY, parameterNames.getKeyParam()); return getByKey(dbSession, componentKey); } @@ -64,6 +76,10 @@ public class ComponentFinder { return getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, uuid), format("Component id '%s' not found", uuid)); } + public ComponentDto getById(DbSession dbSession, long id) { + return getIfPresentOrFail(dbClient.componentDao().selectById(dbSession, id), format("Component id '%s' not found", id)); + } + /** * A project can be: *