From 08d3297f4a786d1763f7f6475561307339fe6985 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 17 Nov 2015 12:05:00 +0100 Subject: [PATCH] SONAR-6947 Add tests and bug fixes --- it/it-tests/pom.xml | 6 + .../java/it/actionPlan/ActionPlanTest.java | 24 ++- .../it/authorisation/IssuePermissionTest.java | 26 ++- .../java/it/authorisation/PermissionTest.java | 184 ++++++++++++++++++ .../it/measureFilter/MeasureFiltersTest.java | 32 ++- it/it-tests/src/test/java/util/ItUtils.java | 30 +-- .../server/permission/ws/AddGroupAction.java | 2 +- .../server/permission/ws/GroupsAction.java | 8 +- .../ws/SearchGlobalPermissionsActionTest.java | 12 ++ .../sonarqube/ws/client/HttpConnector.java | 3 +- .../ws/client/HttpRequestFactory.java | 20 +- .../permission/PermissionsWsParameters.java | 4 + .../org/sonarqube/ws/client/WsClientTest.java | 34 ---- 13 files changed, 294 insertions(+), 91 deletions(-) create mode 100644 it/it-tests/src/test/java/it/authorisation/PermissionTest.java diff --git a/it/it-tests/pom.xml b/it/it-tests/pom.xml index f6c16fc5731..e3dee2cfe1e 100644 --- a/it/it-tests/pom.xml +++ b/it/it-tests/pom.xml @@ -36,6 +36,12 @@ ${project.version} provided + + org.sonarsource.sonarqube + sonar-ws + ${project.version} + provided + org.sonarsource.sonarqube it-plugins diff --git a/it/it-tests/src/test/java/it/actionPlan/ActionPlanTest.java b/it/it-tests/src/test/java/it/actionPlan/ActionPlanTest.java index 1fc446b34d4..914ed9eb526 100644 --- a/it/it-tests/src/test/java/it/actionPlan/ActionPlanTest.java +++ b/it/it-tests/src/test/java/it/actionPlan/ActionPlanTest.java @@ -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 { diff --git a/it/it-tests/src/test/java/it/authorisation/IssuePermissionTest.java b/it/it-tests/src/test/java/it/authorisation/IssuePermissionTest.java index 3ab8ff8ebaa..5a45198f444 100644 --- a/it/it-tests/src/test/java/it/authorisation/IssuePermissionTest.java +++ b/it/it-tests/src/test/java/it/authorisation/IssuePermissionTest.java @@ -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 index 00000000000..e3dfe05fdb0 --- /dev/null +++ b/it/it-tests/src/test/java/it/authorisation/PermissionTest.java @@ -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)); + } +} diff --git a/it/it-tests/src/test/java/it/measureFilter/MeasureFiltersTest.java b/it/it-tests/src/test/java/it/measureFilter/MeasureFiltersTest.java index 1dc0f5acb44..17f4fb9de52 100644 --- a/it/it-tests/src/test/java/it/measureFilter/MeasureFiltersTest.java +++ b/it/it-tests/src/test/java/it/measureFilter/MeasureFiltersTest.java @@ -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)); } } diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index 7ecb5fc0f61..0f293e3b4ce 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -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 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()); } /** diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java index bdd8563b258..19eac6a5489 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupAction.java @@ -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()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java index 1ca8bf8f4b2..2d3183d94f6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java @@ -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 project = dependenciesFinder.searchProject(dbSession, newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey())); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java index 94df2895d9e..5c05e25314a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java @@ -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); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java index 2b3bcaf8978..66ac3cc89cd 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java @@ -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() { } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java index 97b26436b0c..cda4c549b74 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java @@ -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 execute(WsRequest wsRequest, Parser 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) { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java index c2821a26970..1557e97b632 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java @@ -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 + } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java index a3bdf4967f6..13ff1fa14f3 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java @@ -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"); - } } -- 2.39.5