]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7968 Create client for list_definitions WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Aug 2016 14:58:15 +0000 (16:58 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Aug 2016 09:29:40 +0000 (11:29 +0200)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java
server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java
server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java
server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java
server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java [new file with mode: 0644]
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java [new file with mode: 0644]
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java [new file with mode: 0644]

index db554d019fe7f9844ceb3d1ecaae62a3383ea1ee..714a383e636f6ee6e424bf3e00c11c83263a21aa 100644 (file)
@@ -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.<br>" +
         "Either '%s' or '%s' can be provided, not both.<br> " +
         "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<String> 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<String> getQualifier(ListDefinitionsRequest wsRequest) {
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      Optional<ComponentDto> component = getComponent(dbSession, wsRequest);
+      checkAdminPermission(component);
+      return component.isPresent() ? Optional.of(component.get().qualifier()) : Optional.empty();
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
+  }
+
+  private Optional<ComponentDto> 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<ComponentDto> 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);
-    }
-  }
-
 }
index 6ed340e0884f286687474fd5b4a8f8aeb1fb486f..546e8c7db8e740b860521ecaa1798ed743eec146 100644 (file)
@@ -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) {
index 40a3e5fb9e13256972d27720d36cf041b68ea262..6251ce9cbbfc368e63ecf3f5ecfddc53dc27e555 100644 (file)
  */
 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());
-    }
-  }
-
 }
index 3ca00245a23b5411ec46313ed4142e8eb15f3926..79f65c935ab5fc4ca9bbe6b53c3d2cf2c72af0a8 100644 (file)
@@ -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 {
 
index c670f788ad5a1b3b90fb3e9bd6724c7511b7f0b9..b49851d2517e205155e9d04b3a60526d599f68ad 100644 (file)
@@ -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 {
index 28eb7b0d8b0979f431c4bac2de079e63225e48cb..a5d98cff324985a551301809c88e72e407236949 100644 (file)
@@ -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;
+  }
 }
index 869b871c869a0dafd12a31293e1c75a9f8991e8a..c470bfa4b4c0f1d8685b17395139b98076727f68 100644 (file)
@@ -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 (file)
index 0000000..bace689
--- /dev/null
@@ -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 (file)
index 0000000..ff5e53d
--- /dev/null
@@ -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 (file)
index 0000000..e8b9f51
--- /dev/null
@@ -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 (file)
index 0000000..f457506
--- /dev/null
@@ -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 (file)
index 0000000..66eb079
--- /dev/null
@@ -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 (file)
index 0000000..e8578b5
--- /dev/null
@@ -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<SettingsService> 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();
+  }
+
+}