]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6947 Add tests and bug fixes 623/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Nov 2015 11:05:00 +0000 (12:05 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Nov 2015 12:41:34 +0000 (13:41 +0100)
13 files changed:
it/it-tests/pom.xml
it/it-tests/src/test/java/it/actionPlan/ActionPlanTest.java
it/it-tests/src/test/java/it/authorisation/IssuePermissionTest.java
it/it-tests/src/test/java/it/authorisation/PermissionTest.java [new file with mode: 0644]
it/it-tests/src/test/java/it/measureFilter/MeasureFiltersTest.java
it/it-tests/src/test/java/util/ItUtils.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java
sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java

index f6c16fc57315d9a671740e2c3f5129b9749c9b90..e3dee2cfe1ed3ef0c20dab14d225ba94d97e38e9 100644 (file)
       <version>${project.version}</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.sonarsource.sonarqube</groupId>
+      <artifactId>sonar-ws</artifactId>
+      <version>${project.version}</version>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>org.sonarsource.sonarqube</groupId>
       <artifactId>it-plugins</artifactId>
index 1fc446b34d46af8eb01aac2d9f980a4b69620c38..914ed9eb526b5d35e165bd7e248d8dfca8d7aa13 100644 (file)
@@ -38,9 +38,12 @@ import org.sonar.wsclient.issue.Issues;
 import org.sonar.wsclient.issue.NewActionPlan;
 import org.sonar.wsclient.issue.UpdateActionPlan;
 import org.sonar.wsclient.user.UserParameters;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.permission.AddUserWsRequest;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
+import static util.ItUtils.newAdminWsClient;
 import static util.ItUtils.runProjectAnalysis;
 import static util.ItUtils.toDate;
 import static util.ItUtils.verifyHttpException;
@@ -49,6 +52,7 @@ public class ActionPlanTest {
 
   @ClassRule
   public static final Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+  private static WsClient adminWsClient;
   private static final String PROJECT_KEY = "sample";
 
   @BeforeClass
@@ -59,6 +63,8 @@ public class ActionPlanTest {
     orchestrator.getServer().provisionProject("sample", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line-profile");
     runProjectAnalysis(orchestrator, "shared/xoo-sample");
+
+    adminWsClient = newAdminWsClient(orchestrator);
   }
 
   protected static ActionPlanClient adminActionPlanClient() {
@@ -131,17 +137,19 @@ public class ActionPlanTest {
     try {
       // Create a user having admin permission on the project
       adminClient.userClient().create(UserParameters.create().login(projectAdminUser).name(projectAdminUser).password("password").passwordConfirmation("password"));
-      orchestrator.getServer().adminWsClient().post("api/permissions/add_user",
-        "login", projectAdminUser,
-        "projectKey", PROJECT_KEY,
-        "permission", "admin");
+      adminWsClient.permissionsClient().addUser(
+        new AddUserWsRequest()
+          .setLogin(projectAdminUser)
+          .setProjectKey(PROJECT_KEY)
+          .setPermission("admin"));
 
       // Create a user having browse permission on the project
       adminClient.userClient().create(UserParameters.create().login(projectUser).name(projectUser).password("password").passwordConfirmation("password"));
-      orchestrator.getServer().adminWsClient().post("api/permissions/add_user",
-        "login", projectUser,
-        "projectKey", PROJECT_KEY,
-        "permission", "user");
+      adminWsClient.permissionsClient().addUser(
+        new AddUserWsRequest()
+          .setLogin(projectUser)
+          .setProjectKey(PROJECT_KEY)
+          .setPermission("user"));
 
       // Without project admin permission, a user cannot set action plan
       try {
index 3ab8ff8ebaaa958f877dd01a64bbe0017d11766c..5a45198f44497c651f1e0966d619663182681554 100644 (file)
@@ -33,15 +33,20 @@ import org.sonar.wsclient.issue.BulkChangeQuery;
 import org.sonar.wsclient.issue.Issue;
 import org.sonar.wsclient.issue.IssueQuery;
 import org.sonar.wsclient.user.UserParameters;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.permission.AddUserWsRequest;
+import org.sonarqube.ws.client.permission.RemoveGroupWsRequest;
 
 import static junit.framework.TestCase.fail;
 import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.newAdminWsClient;
 import static util.ItUtils.projectDir;
 
 public class IssuePermissionTest {
 
   @ClassRule
   public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+  public WsClient adminWsClient = newAdminWsClient(orchestrator);
 
   @Before
   public void init() {
@@ -223,17 +228,18 @@ public class IssuePermissionTest {
     }
   }
 
-  private static void addUserPermission(String login, String projectKey, String permission) {
-    orchestrator.getServer().adminWsClient().post("api/permissions/add_user",
-      "login", login,
-      "projectKey", projectKey,
-      "permission", permission);
+  private void addUserPermission(String login, String projectKey, String permission) {
+    adminWsClient.permissionsClient().addUser(
+      new AddUserWsRequest()
+        .setLogin(login)
+        .setProjectKey(projectKey)
+        .setPermission(permission));
   }
 
-  private static void removeGroupPermission(String groupName, String projectKey, String permission) {
-    orchestrator.getServer().adminWsClient().post("api/permissions/remove_group",
-      "groupName", groupName,
-      "projectKey", projectKey,
-      "permission", permission);
+  private void removeGroupPermission(String groupName, String projectKey, String permission) {
+    adminWsClient.permissionsClient().removeGroup(new RemoveGroupWsRequest()
+      .setGroupName(groupName)
+      .setProjectKey(projectKey)
+      .setPermission(permission));
   }
 }
diff --git a/it/it-tests/src/test/java/it/authorisation/PermissionTest.java b/it/it-tests/src/test/java/it/authorisation/PermissionTest.java
new file mode 100644 (file)
index 0000000..e3dfe05
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 it.authorisation;
+
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.build.SonarRunner;
+import com.sonar.orchestrator.locator.FileLocation;
+import it.Category1Suite;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonarqube.ws.WsPermissions;
+import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.permission.AddGroupToTemplateWsRequest;
+import org.sonarqube.ws.client.permission.AddGroupWsRequest;
+import org.sonarqube.ws.client.permission.AddUserToTemplateWsRequest;
+import org.sonarqube.ws.client.permission.AddUserWsRequest;
+import org.sonarqube.ws.client.permission.CreateTemplateWsRequest;
+import org.sonarqube.ws.client.permission.GroupsWsRequest;
+import org.sonarqube.ws.client.permission.PermissionsWsClient;
+import org.sonarqube.ws.client.permission.RemoveGroupFromTemplateWsRequest;
+import org.sonarqube.ws.client.permission.RemoveUserFromTemplateWsRequest;
+import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
+import org.sonarqube.ws.client.permission.UsersWsRequest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonarqube.ws.client.WsRequest.newPostRequest;
+import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.projectDir;
+
+public class PermissionTest {
+  @ClassRule
+  public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+  private static WsClient adminWsClient;
+  private static PermissionsWsClient permissionsWsClient;
+
+  private static final String PROJECT_KEY = "sample";
+  private static final String LOGIN = "george.orwell";
+  private static final String GROUP_NAME = "1984";
+
+  @BeforeClass
+  public static void analyzeProject() {
+    orchestrator.resetData();
+
+    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/authorisation/one-issue-per-line-profile.xml"));
+
+    orchestrator.getServer().provisionProject(PROJECT_KEY, "Sample");
+    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
+    SonarRunner sampleProject = SonarRunner.create(projectDir("shared/xoo-sample"));
+    orchestrator.executeBuild(sampleProject);
+
+    adminWsClient = newAdminWsClient(orchestrator);
+    permissionsWsClient = adminWsClient.permissionsClient();
+
+    createUser(LOGIN, "George Orwell");
+    createGroup(GROUP_NAME);
+  }
+
+  @AfterClass
+  public static void delete_data() {
+    deactivateUser(LOGIN);
+    deleteGroup(GROUP_NAME);
+  }
+
+  @Test
+  public void permission_web_services() {
+    permissionsWsClient.addUser(
+      new AddUserWsRequest()
+        .setPermission("admin")
+        .setLogin(LOGIN));
+    permissionsWsClient.addGroup(
+      new AddGroupWsRequest()
+        .setPermission("admin")
+        .setGroupName(GROUP_NAME));
+
+    WsPermissions.WsSearchGlobalPermissionsResponse searchGlobalPermissionsWsResponse = permissionsWsClient.searchGlobalPermissions();
+    assertThat(searchGlobalPermissionsWsResponse.getPermissionsList().get(0).getKey()).isEqualTo("admin");
+    assertThat(searchGlobalPermissionsWsResponse.getPermissionsList().get(0).getUsersCount()).isEqualTo(1);
+    // by default, a group has the global admin permission
+    assertThat(searchGlobalPermissionsWsResponse.getPermissionsList().get(0).getGroupsCount()).isEqualTo(2);
+
+    WsPermissions.UsersWsResponse users = permissionsWsClient
+      .users(new UsersWsRequest()
+        .setPermission("admin"));
+    assertThat(users.getUsersList()).extracting("login").contains(LOGIN);
+
+    WsPermissions.WsGroupsResponse groupsResponse = permissionsWsClient
+      .groups(new GroupsWsRequest()
+        .setPermission("admin"));
+    assertThat(groupsResponse.getGroupsList()).extracting("name").contains(GROUP_NAME);
+  }
+
+  @Test
+  public void template_permission_web_services() {
+    WsPermissions.CreateTemplateWsResponse createTemplateWsResponse = permissionsWsClient.createTemplate(
+      new CreateTemplateWsRequest()
+        .setName("my-new-template")
+        .setDescription("template-used-in-tests"));
+    assertThat(createTemplateWsResponse.getPermissionTemplate().getName()).isEqualTo("my-new-template");
+
+    permissionsWsClient.addUserToTemplate(
+      new AddUserToTemplateWsRequest()
+        .setPermission("admin")
+        .setTemplateName("my-new-template")
+        .setLogin(LOGIN));
+
+    permissionsWsClient.addGroupToTemplate(
+      new AddGroupToTemplateWsRequest()
+        .setPermission("admin")
+        .setTemplateName("my-new-template")
+        .setGroupName(GROUP_NAME));
+
+    SearchTemplatesWsResponse searchTemplatesWsResponse = permissionsWsClient.searchTemplates(
+      new SearchTemplatesWsRequest()
+        .setQuery("my-new-template"));
+    assertThat(searchTemplatesWsResponse.getPermissionTemplates(0).getName()).isEqualTo("my-new-template");
+    assertThat(searchTemplatesWsResponse.getPermissionTemplates(0).getPermissions(0).getKey()).isEqualTo("admin");
+    assertThat(searchTemplatesWsResponse.getPermissionTemplates(0).getPermissions(0).getUsersCount()).isEqualTo(1);
+    assertThat(searchTemplatesWsResponse.getPermissionTemplates(0).getPermissions(0).getGroupsCount()).isEqualTo(1);
+
+    permissionsWsClient.removeGroupFromTemplate(
+      new RemoveGroupFromTemplateWsRequest()
+        .setPermission("admin")
+        .setTemplateName("my-new-template")
+        .setGroupName(GROUP_NAME));
+
+    permissionsWsClient.removeUserFromTemplate(
+      new RemoveUserFromTemplateWsRequest()
+        .setPermission("admin")
+        .setTemplateName("my-new-template")
+        .setLogin(LOGIN));
+
+    SearchTemplatesWsResponse clearedSearchTemplatesWsResponse = permissionsWsClient.searchTemplates(
+      new SearchTemplatesWsRequest()
+        .setQuery("my-new-template"));
+    assertThat(clearedSearchTemplatesWsResponse.getPermissionTemplates(0).getPermissionsList()).isEmpty();
+  }
+
+  private static void createUser(String login, String name) {
+    adminWsClient.execute(
+      newPostRequest("api/users/create")
+        .setParam("login", login)
+        .setParam("name", name)
+        .setParam("password", "123456"));
+  }
+
+  private static void deactivateUser(String login) {
+    adminWsClient.execute(
+      newPostRequest("api/users/deactivate")
+        .setParam("login", login));
+  }
+
+  private static void createGroup(String groupName) {
+    adminWsClient.execute(
+      newPostRequest("api/user_groups/create")
+        .setParam("name", groupName));
+  }
+
+  private static void deleteGroup(String groupName) {
+    adminWsClient.execute(
+      newPostRequest("api/user_groups/delete")
+        .setParam("name", groupName));
+  }
+}
index 1dc0f5acb44ce65bd25526c8d65ef75d8ca323ac..17f4fb9de521fa6320dda51218096d2221aa8c6f 100644 (file)
@@ -29,19 +29,24 @@ import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.wsclient.SonarClient;
 import org.sonar.wsclient.user.UserParameters;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.permission.AddUserWsRequest;
 import util.selenium.SeleneseTest;
 
+import static util.ItUtils.newAdminWsClient;
 import static util.ItUtils.projectDir;
 
 public class MeasureFiltersTest {
 
   @ClassRule
   public static Orchestrator orchestrator = Category2Suite.ORCHESTRATOR;
+  public static WsClient adminWsClient;
 
   @BeforeClass
   public static void scanStruts() {
     orchestrator.resetData();
     orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-multi-modules-sample")));
+    adminWsClient = newAdminWsClient(orchestrator);
 
     createUser("user-measure-filters", "User Measure Filters");
   }
@@ -61,8 +66,7 @@ public class MeasureFiltersTest {
       // SONAR-4195
       "/measureFilter/MeasureFiltersTest/search-by-key.html",
       "/measureFilter/MeasureFiltersTest/search-by-name.html",
-      "/measureFilter/MeasureFiltersTest/empty_filter.html"
-      ).build();
+      "/measureFilter/MeasureFiltersTest/empty_filter.html").build();
     new SeleneseTest(selenese).runOn(orchestrator);
   }
 
@@ -73,8 +77,7 @@ public class MeasureFiltersTest {
       "/measureFilter/MeasureFiltersTest/list_delete_column.html",
       "/measureFilter/MeasureFiltersTest/list_move_columns.html",
       "/measureFilter/MeasureFiltersTest/list_sort_by_descending_name.html",
-      "/measureFilter/MeasureFiltersTest/list_sort_by_ncloc.html"
-      ).build();
+      "/measureFilter/MeasureFiltersTest/list_sort_by_ncloc.html").build();
     new SeleneseTest(selenese).runOn(orchestrator);
   }
 
@@ -87,8 +90,7 @@ public class MeasureFiltersTest {
     try {
       Selenese selenese = Selenese.builder().setHtmlTestsInClasspath("share_measure_filters",
         // SONAR-4469
-        "/measureFilter/MeasureFiltersTest/should-unshare-filter-remove-other-filters-favourite.html"
-        ).build();
+        "/measureFilter/MeasureFiltersTest/should-unshare-filter-remove-other-filters-favourite.html").build();
       new SeleneseTest(selenese).runOn(orchestrator);
     } finally {
       deactivateUser(user);
@@ -105,8 +107,7 @@ public class MeasureFiltersTest {
 
     try {
       new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("should_not_share_filter_when_user_have_no_sharing_permissions",
-        "/measureFilter/MeasureFiltersTest/should-not-share-filter-when-user-have-no-sharing-permissions.html"
-        ).build()).runOn(orchestrator);
+        "/measureFilter/MeasureFiltersTest/should-not-share-filter-when-user-have-no-sharing-permissions.html").build()).runOn(orchestrator);
     } finally {
       deactivateUser(user);
     }
@@ -116,16 +117,14 @@ public class MeasureFiltersTest {
   public void copy_measure_filters() {
     Selenese selenese = Selenese.builder().setHtmlTestsInClasspath("copy_measure_filters",
       "/measureFilter/MeasureFiltersTest/copy_measure_filter.html",
-      "/measureFilter/MeasureFiltersTest/copy_uniqueness_of_name.html"
-      ).build();
+      "/measureFilter/MeasureFiltersTest/copy_uniqueness_of_name.html").build();
     new SeleneseTest(selenese).runOn(orchestrator);
   }
 
   @Test
   public void manage_measure_filters() {
     Selenese selenese = Selenese.builder().setHtmlTestsInClasspath("manage_measure_filters",
-      "/measureFilter/MeasureFiltersTest/save_with_special_characters.html"
-      ).build();
+      "/measureFilter/MeasureFiltersTest/save_with_special_characters.html").build();
     new SeleneseTest(selenese).runOn(orchestrator);
   }
 
@@ -134,8 +133,7 @@ public class MeasureFiltersTest {
     Selenese selenese = Selenese.builder().setHtmlTestsInClasspath("measure_filter_list_widget",
       "/measureFilter/MeasureFiltersTest/list_widget.html",
       "/measureFilter/MeasureFiltersTest/list_widget_sort.html",
-      "/measureFilter/MeasureFiltersTest/list_widget_warning_if_missing_filter.html"
-      ).build();
+      "/measureFilter/MeasureFiltersTest/list_widget_warning_if_missing_filter.html").build();
     new SeleneseTest(selenese).runOn(orchestrator);
   }
 
@@ -149,9 +147,9 @@ public class MeasureFiltersTest {
     client.userClient().create(userCreationParameters);
 
     if (permission != null) {
-      client.post("api/permissions/add_user",
-        "login", login,
-        "permission", permission);
+      adminWsClient.permissionsClient().addUser(new AddUserWsRequest()
+        .setLogin(login)
+        .setPermission(permission));
     }
   }
 
index 7ecb5fc0f61de0d8615e8dbfa86cb51e2bcb4a25..0f293e3b4ce171f74ff22afd2c8d72cc001f020d 100644 (file)
@@ -1,13 +1,9 @@
 package util;/*
- * Copyright (C) 2009-2014 SonarSource SA
- * All rights reserved
- * mailto:contact AT sonarsource DOT com
- */
            * Copyright (C) 2009-2014 SonarSource SA
            * All rights reserved
            * mailto:contact AT sonarsource DOT com
            */
 
-import org.sonar.wsclient.issue.Issue;
-
-import org.sonar.wsclient.issue.IssueQuery;
-import org.sonar.wsclient.issue.IssueClient;
 import com.google.common.base.Supplier;
 import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableMap;
@@ -15,8 +11,8 @@ import com.google.common.collect.Iterables;
 import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.BuildResult;
 import com.sonar.orchestrator.build.SonarRunner;
+import com.sonar.orchestrator.container.Server;
 import com.sonar.orchestrator.locator.FileLocation;
-
 import java.io.File;
 import java.io.IOException;
 import java.text.ParseException;
@@ -25,20 +21,24 @@ import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-
 import javax.annotation.Nullable;
-
 import org.apache.commons.io.FileUtils;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
 import org.sonar.wsclient.base.HttpException;
+import org.sonar.wsclient.issue.Issue;
+import org.sonar.wsclient.issue.IssueClient;
+import org.sonar.wsclient.issue.IssueQuery;
 import org.sonar.wsclient.services.PropertyDeleteQuery;
 import org.sonar.wsclient.services.PropertyUpdateQuery;
+import org.sonarqube.ws.client.WsClient;
+
 import static com.google.common.collect.FluentIterable.from;
 import static java.util.Arrays.asList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.fail;
+import static org.sonarqube.ws.client.HttpConnector.newHttpConnector;
 
 public class ItUtils {
 
@@ -73,7 +73,15 @@ public class ItUtils {
   public static List<Issue> getAllServerIssues(Orchestrator orchestrator) {
     IssueClient issueClient = orchestrator.getServer().wsClient().issueClient();
     return issueClient.find(IssueQuery.create()).list();
+  }
 
+  public static WsClient newAdminWsClient(Orchestrator orchestrator) {
+    Server server = orchestrator.getServer();
+    return new WsClient(newHttpConnector()
+      .url(server.getUrl())
+      .login(server.ADMIN_LOGIN)
+      .password(server.ADMIN_PASSWORD)
+      .build());
   }
 
   /**
index bdd8563b25859c4b7413da896bcafc6a353d6701..19eac6a5489e26912ccdbc5e555c108635a90a92 100644 (file)
@@ -80,7 +80,7 @@ public class AddGroupAction implements PermissionsWsAction {
     response.noContent();
   }
 
-  private void doHandle(AddGroupWsRequest request) throws Exception {
+  private void doHandle(AddGroupWsRequest request) {
     DbSession dbSession = dbClient.openSession(false);
     try {
       Long groupId = request.getGroupId() == null ? null : Long.valueOf(request.getGroupId());
index 1ca8bf8f4b28eb2aea427c44f21c3590ff75bc0d..2d3183d94f64a52b64979a200029b21a8b9cbb69 100644 (file)
@@ -45,13 +45,13 @@ import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjec
 import static org.sonar.server.permission.ws.PermissionQueryParser.fromSelectionModeToMembership;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateGlobalPermission;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter;
 import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
 
 public class GroupsAction implements PermissionsWsAction {
   private final DbClient dbClient;
@@ -93,7 +93,7 @@ public class GroupsAction implements PermissionsWsAction {
     writeProtobuf(groupsResponse, wsRequest, wsResponse);
   }
 
-  private WsGroupsResponse doHandle(GroupsWsRequest request) throws Exception {
+  private WsGroupsResponse doHandle(GroupsWsRequest request) {
     DbSession dbSession = dbClient.openSession(false);
     try {
       Optional<ComponentDto> project = dependenciesFinder.searchProject(dbSession, newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()));
index 94df2895d9e13d1d87594ee03dccf4fe6597d8c3..5c05e25314a05ee09e3ffa1cbbb8649951ad6ad4 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.permission.ws;
 
+import java.io.IOException;
 import javax.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Rule;
@@ -38,6 +39,8 @@ import org.sonar.server.i18n.I18nRule;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
 import org.sonar.test.DbTests;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsPermissions;
 
 import static org.sonar.core.permission.GlobalPermissions.DASHBOARD_SHARING;
 import static org.sonar.core.permission.GlobalPermissions.PREVIEW_EXECUTION;
@@ -92,6 +95,15 @@ public class SearchGlobalPermissionsActionTest {
     assertJson(result).isSimilarTo(getClass().getResource("search_global_permissions-example.json"));
   }
 
+  @Test
+  public void protobuf_response() throws IOException {
+    WsPermissions.WsSearchGlobalPermissionsResponse wsSearchGlobalPermissionsResponse = WsPermissions.WsSearchGlobalPermissionsResponse.parseFrom(
+      ws.newRequest()
+        .setMediaType(MediaTypes.PROTOBUF)
+        .execute().getInputStream());
+    System.out.println(wsSearchGlobalPermissionsResponse.getPermissionsList());
+  }
+
   @Test
   public void fail_if_insufficient_privileges() {
     expectedException.expect(ForbiddenException.class);
index 2b3bcaf897828ed59ed29114c7efbbb65ca30143..66ac3cc89cdf9fab3df20ed14c7d25909812c20c 100644 (file)
@@ -80,7 +80,8 @@ public class HttpConnector implements WsConnector {
     private String proxyPassword;
     private int proxyPort = 0;
 
-    private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
+    private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
+    private int readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
 
     private Builder() {
     }
index 97b26436b0c267659ba7e0227883edcbb859406a..cda4c549b743e76fc9a07084517828357c19d5f8 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Throwables;
 import com.google.protobuf.InvalidProtocolBufferException;
 import com.google.protobuf.Message;
 import com.google.protobuf.Parser;
+import java.io.InputStream;
 import java.util.Arrays;
 import javax.annotation.Nullable;
 import org.sonarqube.ws.MediaTypes;
@@ -136,9 +137,9 @@ public class HttpRequestFactory {
 
   public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) {
     HttpRequest httpRequest = wsRequestToHttpRequest(wsRequest);
-    String response = execute(httpRequest);
+    InputStream response = executeWithStream(httpRequest);
     try {
-      return protobufParser.parseFrom(response.getBytes());
+      return protobufParser.parseFrom(response);
     } catch (InvalidProtocolBufferException e) {
       Throwables.propagate(e);
     }
@@ -147,9 +148,9 @@ public class HttpRequestFactory {
   }
 
   private HttpRequest wsRequestToHttpRequest(WsRequest wsRequest) {
-    HttpRequest httpRequest = wsRequest.getMethod().equals(WsRequest.Method.GET)
-      ? HttpRequest.post(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true)
-      : HttpRequest.get(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true);
+    HttpRequest httpRequest = WsRequest.Method.GET.equals(wsRequest.getMethod())
+      ? HttpRequest.get(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true)
+      : HttpRequest.post(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true);
     httpRequest = prepare(httpRequest);
     switch (wsRequest.getMediaType()) {
       case PROTOBUF:
@@ -188,6 +189,15 @@ public class HttpRequestFactory {
     }
   }
 
+  private static InputStream executeWithStream(HttpRequest request) {
+    try {
+      checkSuccess(request);
+      return request.stream();
+    } catch (HttpRequest.HttpRequestException e) {
+      throw new IllegalStateException("Fail to request " + request.url(), e);
+    }
+  }
+
   private static void checkSuccess(HttpRequest request) {
     boolean isSuccess = Arrays.binarySearch(RESPONSE_SUCCESS, request.code()) >= 0;
     if (!isSuccess) {
index c2821a269706c1f97b49c6f84de252167b4bcb4c..1557e97b632a9dd20bbce617c0b48410e1c8657b 100644 (file)
@@ -36,4 +36,8 @@ public class PermissionsWsParameters {
   public static final String PARAM_DESCRIPTION = "description";
   public static final String PARAM_PROJECT_KEY_PATTERN = "projectKeyPattern";
   public static final String PARAM_QUALIFIER = "qualifier";
+
+  private PermissionsWsParameters() {
+    // static utils only
+  }
 }
index a3bdf4967f6890ecacb925a64eee5fd81be35c95..13ff1fa14f34d65a17cdabbcc8a54c50e0191a9b 100644 (file)
@@ -23,21 +23,17 @@ package org.sonarqube.ws.client;
 import com.google.common.net.HttpHeaders;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonarqube.ws.MediaTypes;
 import org.sonarqube.ws.WsComponents;
-import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
-import org.sonarqube.ws.client.permission.GroupsWsRequest;
 
 import static java.net.HttpURLConnection.HTTP_OK;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonarqube.ws.client.HttpConnector.newDefaultHttpConnector;
 import static org.sonarqube.ws.client.HttpConnector.newHttpConnector;
 import static org.sonarqube.ws.client.WsRequest.newGetRequest;
-import static org.sonarqube.ws.client.WsRequest.newPostRequest;
 
 public class WsClientTest {
   @Rule
@@ -152,34 +148,4 @@ public class WsClientTest {
     assertThat(requestFactory.getProxyLogin()).isEqualTo("proxyLogin");
     assertThat(requestFactory.getProxyPassword()).isEqualTo("proxyPass");
   }
-
-  @Ignore
-  @Test
-  public void contact_localhost() {
-    /**
-     * This is a temporary test to have a simple end-to-end test
-     * To make it work launch a default sonar server locally
-     */
-    WsClient ws = new WsClient(newHttpConnector()
-      .url("http://localhost:9000")
-      .login("admin")
-      .password("admin")
-      .build());
-
-    // test with json response
-    String stringResponse = ws.execute(newGetRequest("api/webservices/list"));
-    assertThat(stringResponse).contains("webservices", "permissions");
-
-    // test with protobuf response
-    WsGroupsResponse protobufResponse = ws.execute(newPostRequest("api/permissions/groups")
-      .setMediaType(WsRequest.MediaType.PROTOBUF)
-      .setParam("permission", "admin"),
-      WsGroupsResponse.parser());
-    assertThat(protobufResponse.getGroups(0).getName()).contains("sonar-administrator");
-
-    // test with specific client
-    WsGroupsResponse groupsResponse = ws.permissionsClient().groups(new GroupsWsRequest()
-      .setPermission("admin"));
-    assertThat(groupsResponse.getGroups(0).getName()).contains("sonar-administrator");
-  }
 }