import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
import org.sonar.wsclient.system.SystemClient;
import org.sonar.wsclient.system.internal.DefaultSystemClient;
-import org.sonar.wsclient.user.UserClient;
-import org.sonar.wsclient.user.internal.DefaultUserClient;
import javax.annotation.Nullable;
return new DefaultActionPlanClient(requestFactory);
}
- /**
- * New client to interact with web services related to users
- */
- public UserClient userClient() {
- return new DefaultUserClient(requestFactory);
- }
-
/**
* New client to interact with web services related to users and groups permissions
*/
+++ /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 org.sonar.wsclient.user;
-
-import java.util.List;
-
-/**
- * @since 3.6
- */
-public interface UserClient {
-
- List<User> find(UserQuery query);
-
- /**
- * @since 3.7
- */
- User create(UserParameters userParameters);
-
- /**
- * @since 3.7
- */
- User update(UserParameters userParameters);
-
- /**
- * @since 3.7
- */
- void deactivate(String login);
-}
+++ /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 org.sonar.wsclient.user;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public class UserParameters {
-
- private final Map<String, Object> params = new HashMap<>();
-
- private UserParameters() {
- }
-
- public static UserParameters create() {
- return new UserParameters();
- }
-
- public Map<String, Object> urlParams() {
- return params;
- }
-
- public UserParameters login(String s) {
- params.put("login", s);
- return this;
- }
-
- public UserParameters name(String s) {
- params.put("name", s);
- return this;
- }
-
- public UserParameters password(String s) {
- params.put("password", s);
- return this;
- }
-
- public UserParameters passwordConfirmation(String s) {
- params.put("password_confirmation", s);
- return this;
- }
-
- public UserParameters email(String s) {
- params.put("email", s);
- return this;
- }
-}
+++ /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 org.sonar.wsclient.user;
-
-import org.sonar.wsclient.internal.EncodingUtils;
-
-import javax.annotation.Nullable;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @since 3.6
- */
-public class UserQuery {
-
- private final Map<String, Object> params = new HashMap<>();
-
- private UserQuery() {
- }
-
- public static UserQuery create() {
- return new UserQuery();
- }
-
- public UserQuery includeDeactivated() {
- params.put("includeDeactivated", "true");
- return this;
- }
-
- public UserQuery logins(String... s) {
- params.put("logins", EncodingUtils.toQueryParam(s));
- return this;
- }
-
- public UserQuery searchText(@Nullable String s) {
- if (s != null) {
- params.put("s", s);
- } else {
- params.remove("s");
- }
- return this;
- }
-
- public Map<String, Object> urlParams() {
- return params;
- }
-}
+++ /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 org.sonar.wsclient.user.internal;
-
-import org.json.simple.JSONValue;
-import org.sonar.wsclient.internal.HttpRequestFactory;
-import org.sonar.wsclient.user.User;
-import org.sonar.wsclient.user.UserClient;
-import org.sonar.wsclient.user.UserParameters;
-import org.sonar.wsclient.user.UserQuery;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#userClient()}.
- */
-public class DefaultUserClient implements UserClient {
-
- private static final String BASE_URL = "/api/users/";
- private static final String SEARCH_URL = BASE_URL + "search";
- private static final String CREATE_URL = BASE_URL + "create";
- private static final String UPDATE_URL = BASE_URL + "update";
- private static final String DEACTIVATE_URL = BASE_URL + "deactivate";
-
- private final HttpRequestFactory requestFactory;
-
- /**
- * For internal use. Use {@link org.sonar.wsclient.SonarClient} to get an instance.
- */
- public DefaultUserClient(HttpRequestFactory requestFactory) {
- this.requestFactory = requestFactory;
- }
-
- @Override
- public List<User> find(UserQuery query) {
- String json = requestFactory.get(SEARCH_URL, query.urlParams());
- List<User> result = new ArrayList<>();
- Map jsonRoot = (Map) JSONValue.parse(json);
- List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
- if (jsonUsers != null) {
- for (Map jsonUser : jsonUsers) {
- result.add(new User(jsonUser));
- }
- }
- return result;
- }
-
- @Override
- public User create(UserParameters userParameters) {
- String json = requestFactory.post(CREATE_URL, userParameters.urlParams());
- Map jsonRoot = (Map) JSONValue.parse(json);
- Map jsonUser = (Map) jsonRoot.get("user");
- return new User(jsonUser);
- }
-
- @Override
- public User update(UserParameters userParameters) {
- String json = requestFactory.post(UPDATE_URL, userParameters.urlParams());
- Map jsonRoot = (Map) JSONValue.parse(json);
- Map jsonUser = (Map) jsonRoot.get("user");
- return new User(jsonUser);
- }
-
- @Override
- public void deactivate(String login) {
- Map<String, Object> params = new HashMap<>();
- params.put("login", login);
- requestFactory.post(DEACTIVATE_URL, params);
- }
-}
+++ /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.
- */
-
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.wsclient.user.internal;
import org.sonar.wsclient.qprofile.internal.DefaultQProfileClient;
import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
import org.sonar.wsclient.system.internal.DefaultSystemClient;
-import org.sonar.wsclient.user.internal.DefaultUserClient;
import java.util.Map;
SonarClient client = SonarClient.create("http://localhost:9000");
assertThat(client.issueClient()).isNotNull().isInstanceOf(DefaultIssueClient.class);
assertThat(client.actionPlanClient()).isNotNull().isInstanceOf(DefaultActionPlanClient.class);
- assertThat(client.userClient()).isNotNull().isInstanceOf(DefaultUserClient.class);
assertThat(client.permissionClient()).isNotNull().isInstanceOf(DefaultPermissionClient.class);
assertThat(client.projectClient()).isNotNull().isInstanceOf(DefaultProjectClient.class);
assertThat(client.qualityGateClient()).isNotNull().isInstanceOf(DefaultQualityGateClient.class);
+++ /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 org.sonar.wsclient.user;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.wsclient.MockHttpServerInterceptor;
-import org.sonar.wsclient.internal.HttpRequestFactory;
-import org.sonar.wsclient.user.internal.DefaultUserClient;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.entry;
-
-public class DefaultUserClientTest {
-
- HttpRequestFactory requestFactory;
- DefaultUserClient client;
-
- @Rule
- public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
-
- @Before
- public void setUp() {
- requestFactory = new HttpRequestFactory(httpServer.url());
- client = new DefaultUserClient(requestFactory);
- }
-
- @Test
- public void should_find_issues() {
- httpServer.stubResponseBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}");
-
- UserQuery query = UserQuery.create().logins("simon", "loic");
- List<User> users = client.find(query);
-
- assertThatGetRequestUrlContains("/api/users/search?", "logins=simon,loic");
- assertThat(users).hasSize(1);
- User simon = users.get(0);
- assertThat(simon.login()).isEqualTo("simon");
- assertThat(simon.name()).isEqualTo("Simon");
- assertThat(simon.email()).isNull();
- assertThat(simon.active()).isTrue();
- }
-
- @Test
- public void should_create_user() {
- httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":null}}");
-
- UserParameters params = UserParameters.create().login("daveloper").password("pass1").passwordConfirmation("pass1");
- User createdUser = client.create(params);
-
- assertThat(httpServer.requestedPath()).isEqualTo("/api/users/create");
- assertThat(httpServer.requestParams()).contains(
- entry("login", "daveloper"),
- entry("password", "pass1"),
- entry("password_confirmation", "pass1")
- );
- assertThat(createdUser).isNotNull();
- assertThat(createdUser.login()).isEqualTo("daveloper");
- assertThat(createdUser.name()).isEqualTo("daveloper");
- assertThat(createdUser.email()).isNull();
- }
-
- @Test
- public void should_update_user() {
- httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":\"new_email\"}}");
-
- UserParameters params = UserParameters.create().login("daveloper").email("new_email");
- User updatedUser = client.update(params);
-
- assertThat(httpServer.requestedPath()).isEqualTo("/api/users/update");
- assertThat(httpServer.requestParams()).contains(
- entry("login", "daveloper"),
- entry("email", "new_email")
- );
- assertThat(updatedUser).isNotNull();
- assertThat(updatedUser.login()).isEqualTo("daveloper");
- assertThat(updatedUser.name()).isEqualTo("daveloper");
- assertThat(updatedUser.email()).isEqualTo("new_email");
- }
-
- @Test
- public void should_deactivate_user() {
- httpServer.stubStatusCode(200);
-
- client.deactivate("daveloper");
-
- assertThat(httpServer.requestedPath()).isEqualTo("/api/users/deactivate");
- assertThat(httpServer.requestParams()).containsEntry("login", "daveloper");
- }
-
- private void assertThatGetRequestUrlContains(String baseUrl, String... parameters) {
- assertThat(httpServer.requestedPath()).startsWith(baseUrl);
- List<String> requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&"));
- assertThat(requestParameters).containsOnly(parameters);
- }
-}
+++ /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 org.sonar.wsclient.user;
-
-import org.junit.Test;
-
-import java.util.Map;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class UserQueryTest {
- @Test
- public void test_params() throws Exception {
- UserQuery query = UserQuery.create().includeDeactivated().logins("simon", "loic");
- Map<String, Object> params = query.urlParams();
-
- assertThat(params.get("includeDeactivated")).isEqualTo("true");
- assertThat(params.get("logins")).isEqualTo("simon,loic");
- }
-
- @Test
- public void test_empty_params() throws Exception {
- UserQuery query = UserQuery.create();
- Map<String, Object> params = query.urlParams();
-
- assertThat(params).isEmpty();
- }
-
- @Test
- public void should_replace_logins() {
- UserQuery query = UserQuery.create().logins("simon").logins("loic");
- assertThat(query.urlParams().get("logins")).isEqualTo("loic");
- }
-
- @Test
- public void should_search_by_text() {
- UserQuery query = UserQuery.create().searchText("sim");
- assertThat(query.urlParams().get("s")).isEqualTo("sim");
-
- query = UserQuery.create().searchText("sim").searchText(null);
- assertThat(query.urlParams().get("s")).isNull();
- }
-}