From 4beb2af88d3e1378d19bbaec1859c4c59acb6252 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 23 Aug 2016 16:58:15 +0200 Subject: [PATCH] SONAR-7968 Create client for list_definitions WS --- .../settings/ws/ListDefinitionsAction.java | 76 +++++++++++++------ .../sonar/server/settings/ws/SettingsWs.java | 4 +- .../ws/SettingsWsComponentParameters.java | 39 +--------- .../server/settings/ws/ValuesAction.java | 4 +- .../ws/ListDefinitionsActionTest.java | 3 +- .../sonarqube/ws/client/DefaultWsClient.java | 8 ++ .../org/sonarqube/ws/client/WsClient.java | 6 ++ .../setting/ListDefinitionsRequest.java | 73 ++++++++++++++++++ .../ws/client/setting/SettingsService.java | 44 +++++++++++ .../client/setting/SettingsWsParameters.java | 31 ++++++++ .../ws/client/setting/package-info.java | 27 +++++++ .../setting/ListDefinitionsRequestTest.java | 60 +++++++++++++++ .../client/setting/SettingsServiceTest.java | 56 ++++++++++++++ 13 files changed, 369 insertions(+), 62 deletions(-) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java create mode 100644 sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java create mode 100644 sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java index db554d019fe..714a383e636 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java @@ -20,7 +20,7 @@ package org.sonar.server.settings.ws; import java.util.List; -import javax.annotation.CheckForNull; +import java.util.Optional; import org.sonar.api.PropertyType; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.config.PropertyDefinitions; @@ -28,33 +28,42 @@ import org.sonar.api.config.PropertyFieldDefinition; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.api.web.UserRole; +import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; +import org.sonar.server.component.ComponentFinder; +import org.sonar.server.user.UserSession; import org.sonarqube.ws.Settings; import org.sonarqube.ws.Settings.ListDefinitionsWsResponse; +import org.sonarqube.ws.client.setting.ListDefinitionsRequest; import static org.elasticsearch.common.Strings.isNullOrEmpty; -import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID; -import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY; +import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY; import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; public class ListDefinitionsAction implements SettingsWsAction { private final DbClient dbClient; - private final SettingsWsComponentParameters settingsWsComponentParameters; + private final ComponentFinder componentFinder; + private final UserSession userSession; private final PropertyDefinitions propertyDefinitions; - public ListDefinitionsAction(DbClient dbClient, SettingsWsComponentParameters settingsWsComponentParameters, PropertyDefinitions propertyDefinitions) { + public ListDefinitionsAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, PropertyDefinitions propertyDefinitions) { this.dbClient = dbClient; - this.settingsWsComponentParameters = settingsWsComponentParameters; + this.componentFinder = componentFinder; + this.userSession = userSession; this.propertyDefinitions = propertyDefinitions; } @Override public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("list_definitions") + WebService.NewAction action = context.createAction(ACTION_LIST_DEFINITIONS) .setDescription(String.format("List settings definitions.
" + "Either '%s' or '%s' can be provided, not both.
" + "Requires one of the following permissions: " + @@ -65,7 +74,6 @@ public class ListDefinitionsAction implements SettingsWsAction { .setResponseExample(getClass().getResource("list_definitions-example.json")) .setSince("6.1") .setHandler(this); - addComponentParameters(action); } @@ -75,16 +83,52 @@ public class ListDefinitionsAction implements SettingsWsAction { } private ListDefinitionsWsResponse doHandle(Request request) { - String qualifier = getQualifier(request); + ListDefinitionsRequest wsRequest = toWsRequest(request); + Optional qualifier = getQualifier(wsRequest); ListDefinitionsWsResponse.Builder wsResponse = ListDefinitionsWsResponse.newBuilder(); propertyDefinitions.getAll().stream() - .filter(definition -> qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) + .filter(definition -> qualifier.isPresent() ? definition.qualifiers().contains(qualifier.get()) : definition.global()) .filter(definition -> !definition.type().equals(PropertyType.LICENSE)) .forEach(definition -> addDefinition(definition, wsResponse)); return wsResponse.build(); } + private static ListDefinitionsRequest toWsRequest(Request request) { + return ListDefinitionsRequest.builder() + .setComponentId(request.param(PARAM_COMPONENT_ID)) + .setComponentKey(request.param(PARAM_COMPONENT_KEY)) + .build(); + } + + private Optional getQualifier(ListDefinitionsRequest wsRequest) { + DbSession dbSession = dbClient.openSession(false); + try { + Optional component = getComponent(dbSession, wsRequest); + checkAdminPermission(component); + return component.isPresent() ? Optional.of(component.get().qualifier()) : Optional.empty(); + } finally { + dbClient.closeSession(dbSession); + } + } + + private Optional getComponent(DbSession dbSession, ListDefinitionsRequest wsRequest) { + String componentId = wsRequest.getComponentId(); + String componentKey = wsRequest.getComponentKey(); + if (componentId != null || componentKey != null) { + return Optional.of(componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, ID_AND_KEY)); + } + return Optional.empty(); + } + + private void checkAdminPermission(Optional component) { + if (component.isPresent()) { + userSession.checkComponentUuidPermission(UserRole.ADMIN, component.get().uuid()); + } else { + userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN); + } + } + private void addDefinition(PropertyDefinition definition, ListDefinitionsWsResponse.Builder wsResponse) { String key = definition.key(); Settings.Definition.Builder builder = wsResponse.addDefinitionsBuilder() @@ -133,16 +177,4 @@ public class ListDefinitionsAction implements SettingsWsAction { .build(); } - @CheckForNull - private String getQualifier(Request request) { - DbSession dbSession = dbClient.openSession(false); - try { - ComponentDto component = settingsWsComponentParameters.getComponent(dbSession, request); - settingsWsComponentParameters.checkAdminPermission(component); - return component == null ? null : component.qualifier(); - } finally { - dbClient.closeSession(dbSession); - } - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java index 6ed340e0884..546e8c7db8e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java @@ -21,6 +21,8 @@ package org.sonar.server.settings.ws; import org.sonar.api.server.ws.WebService; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS; + public class SettingsWs implements WebService { private final SettingsWsAction[] actions; @@ -31,7 +33,7 @@ public class SettingsWs implements WebService { @Override public void define(Context context) { - NewController controller = context.createController("api/settings") + NewController controller = context.createController(CONTROLLER_SETTINGS) .setDescription("Manage settings.") .setSince("6.1"); for (SettingsWsAction action : actions) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java index 40a3e5fb9e1..6251ce9cbbf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java @@ -19,32 +19,17 @@ */ package org.sonar.server.settings.ws; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.WebService; -import org.sonar.api.web.UserRole; -import org.sonar.core.permission.GlobalPermissions; -import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.user.UserSession; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; -import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; public class SettingsWsComponentParameters { - static final String PARAM_COMPONENT_ID = "componentId"; - static final String PARAM_COMPONENT_KEY = "componentKey"; - - private final ComponentFinder componentFinder; - private final UserSession userSession; - - public SettingsWsComponentParameters(ComponentFinder componentFinder, UserSession userSession) { - this.componentFinder = componentFinder; - this.userSession = userSession; + private SettingsWsComponentParameters() { + // Only static methods } static void addComponentParameters(WebService.NewAction action) { @@ -57,20 +42,4 @@ public class SettingsWsComponentParameters { .setExampleValue(KEY_PROJECT_EXAMPLE_001); } - @CheckForNull - ComponentDto getComponent(DbSession dbSession, Request request) { - if (request.hasParam(PARAM_COMPONENT_ID) || request.hasParam(PARAM_COMPONENT_KEY)) { - return componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_COMPONENT_ID), request.param(PARAM_COMPONENT_KEY), ID_AND_KEY); - } - return null; - } - - void checkAdminPermission(@Nullable ComponentDto component) { - if (component == null) { - userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN); - } else { - userSession.checkComponentUuidPermission(UserRole.ADMIN, component.uuid()); - } - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java index 3ca00245a23..79f65c935ab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java @@ -43,10 +43,10 @@ import org.sonarqube.ws.Settings.ValuesWsResponse; import static org.elasticsearch.common.Strings.isNullOrEmpty; import static org.sonar.api.PropertyType.PROPERTY_SET; -import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID; -import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY; import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; public class ValuesAction implements SettingsWsAction { diff --git a/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java index c670f788ad5..b49851d2517 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java @@ -79,9 +79,8 @@ public class ListDefinitionsActionTest { ComponentDto project; PropertyDefinitions propertyDefinitions = new PropertyDefinitions(); - SettingsWsComponentParameters settingsWsComponentParameters = new SettingsWsComponentParameters(new ComponentFinder(dbClient), userSession); - WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, settingsWsComponentParameters, propertyDefinitions)); + WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, new ComponentFinder(dbClient), userSession, propertyDefinitions)); @Before public void setUp() throws Exception { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java index 28eb7b0d8b0..a5d98cff324 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java @@ -29,6 +29,7 @@ import org.sonarqube.ws.client.projectlinks.ProjectLinksService; import org.sonarqube.ws.client.qualitygate.QualityGatesService; import org.sonarqube.ws.client.qualityprofile.QualityProfilesService; import org.sonarqube.ws.client.rule.RulesService; +import org.sonarqube.ws.client.setting.SettingsService; import org.sonarqube.ws.client.system.SystemService; import org.sonarqube.ws.client.usertoken.UserTokensService; @@ -53,6 +54,7 @@ class DefaultWsClient implements WsClient { private final RulesService rulesService; private final ProjectsService projectsService; private final ProjectLinksService projectLinksService; + private final SettingsService settingsService; DefaultWsClient(WsConnector wsConnector) { this.wsConnector = wsConnector; @@ -68,6 +70,7 @@ class DefaultWsClient implements WsClient { this.rulesService = new RulesService(wsConnector); this.projectsService = new ProjectsService(wsConnector); this.projectLinksService = new ProjectLinksService(wsConnector); + this.settingsService = new SettingsService(wsConnector); } @Override @@ -134,4 +137,9 @@ class DefaultWsClient implements WsClient { public ProjectLinksService projectLinks() { return projectLinksService; } + + @Override + public SettingsService settingsService() { + return settingsService; + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java index 869b871c869..c470bfa4b4c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java @@ -29,6 +29,7 @@ import org.sonarqube.ws.client.projectlinks.ProjectLinksService; import org.sonarqube.ws.client.qualitygate.QualityGatesService; import org.sonarqube.ws.client.qualityprofile.QualityProfilesService; import org.sonarqube.ws.client.rule.RulesService; +import org.sonarqube.ws.client.setting.SettingsService; import org.sonarqube.ws.client.system.SystemService; import org.sonarqube.ws.client.usertoken.UserTokensService; @@ -82,4 +83,9 @@ public interface WsClient { * @since 6.1 */ ProjectLinksService projectLinks(); + + /** + * @since 6.1 + */ + SettingsService settingsService(); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java new file mode 100644 index 00000000000..bace689f91a --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarqube.ws.client.setting; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public class ListDefinitionsRequest { + + private final String componentId; + private final String componentKey; + + private ListDefinitionsRequest(Builder builder) { + this.componentId = builder.componentId; + this.componentKey = builder.componentKey; + } + + @CheckForNull + public String getComponentId() { + return componentId; + } + + @CheckForNull + public String getComponentKey() { + return componentKey; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String componentId; + private String componentKey; + + private Builder() { + // enforce factory method use + } + + public Builder setComponentId(@Nullable String componentId) { + this.componentId = componentId; + return this; + } + + public Builder setComponentKey(@Nullable String componentKey) { + this.componentKey = componentKey; + return this; + } + + public ListDefinitionsRequest build() { + return new ListDefinitionsRequest(this); + } + } + +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java new file mode 100644 index 00000000000..ff5e53d669e --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarqube.ws.client.setting; + +import org.sonarqube.ws.Settings.ListDefinitionsWsResponse; +import org.sonarqube.ws.client.BaseService; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsConnector; + +import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_ID; +import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_KEY; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS; + +public class SettingsService extends BaseService { + public SettingsService(WsConnector wsConnector) { + super(wsConnector, CONTROLLER_SETTINGS); + } + + public ListDefinitionsWsResponse listDefinitions(ListDefinitionsRequest request) { + GetRequest getRequest = new GetRequest(path(ACTION_LIST_DEFINITIONS)) + .setParam(PARAM_COMPONENT_ID, request.getComponentId()) + .setParam(PARAM_COMPONENT_KEY, request.getComponentKey()); + return call(getRequest, ListDefinitionsWsResponse.parser()); + } + +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java new file mode 100644 index 00000000000..e8b9f515cbc --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java @@ -0,0 +1,31 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarqube.ws.client.setting; + +public class SettingsWsParameters { + public static final String CONTROLLER_SETTINGS = "api/settings"; + + public static final String ACTION_LIST_DEFINITIONS = "list_definitions"; + + public static final String PARAM_COMPONENT_ID = "componentId"; + public static final String PARAM_COMPONENT_KEY = "componentKey"; + +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java new file mode 100644 index 00000000000..f4575065bee --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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. + */ + + + +@ParametersAreNonnullByDefault +package org.sonarqube.ws.client.setting; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java new file mode 100644 index 00000000000..66eb07952f7 --- /dev/null +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarqube.ws.client.setting; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ListDefinitionsRequestTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + ListDefinitionsRequest.Builder underTest = ListDefinitionsRequest.builder(); + + @Test + public void create_request_with_no_component() { + ListDefinitionsRequest result = underTest.build(); + + assertThat(result.getComponentId()).isNull(); + assertThat(result.getComponentKey()).isNull(); + } + + @Test + public void create_request_with_component_id() { + ListDefinitionsRequest result = underTest.setComponentId("projectId").build(); + + assertThat(result.getComponentId()).isEqualTo("projectId"); + assertThat(result.getComponentKey()).isNull(); + } + + @Test + public void create_request_with_component_key() { + ListDefinitionsRequest result = underTest.setComponentKey("projectKey").build(); + + assertThat(result.getComponentId()).isNull(); + assertThat(result.getComponentKey()).isEqualTo("projectKey"); + } + +} diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java new file mode 100644 index 00000000000..e8578b56990 --- /dev/null +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.sonarqube.ws.client.setting; + +import org.junit.Rule; +import org.junit.Test; +import org.sonarqube.ws.Settings; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.ServiceTester; +import org.sonarqube.ws.client.WsConnector; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY; + +public class SettingsServiceTest { + + @Rule + public ServiceTester serviceTester = new ServiceTester<>(new SettingsService(mock(WsConnector.class))); + + private SettingsService underTest = serviceTester.getInstanceUnderTest(); + + @Test + public void list_definitions() { + ListDefinitionsRequest request = ListDefinitionsRequest.builder() + .setComponentKey("KEY") + .build(); + + underTest.listDefinitions(request); + GetRequest getRequest = serviceTester.getGetRequest(); + + assertThat(serviceTester.getGetParser()).isSameAs(Settings.ListDefinitionsWsResponse.parser()); + serviceTester.assertThat(getRequest) + .hasParam(PARAM_COMPONENT_KEY, "KEY") + .andNoOtherParam(); + } + +} -- 2.39.5