<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>
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;
@ClassRule
public static final Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+ private static WsClient adminWsClient;
private static final String PROJECT_KEY = "sample";
@BeforeClass
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() {
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 {
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() {
}
}
- 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));
}
}
--- /dev/null
+/*
+ * 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));
+ }
+}
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");
}
// 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);
}
"/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);
}
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);
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);
}
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);
}
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);
}
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));
}
}
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;
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;
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 {
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());
}
/**
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());
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;
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()));
package org.sonar.server.permission.ws;
+import java.io.IOException;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
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;
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);
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() {
}
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;
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);
}
}
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:
}
}
+ 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) {
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
+ }
}
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
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");
- }
}