From 1a4c2501bff751e058f8ab0a8fca9551e13f0c31 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 13 May 2015 09:58:48 +0200 Subject: [PATCH] SONAR-6523 Remove user client from sonar-wsclient library --- .../java/org/sonar/wsclient/SonarClient.java | 9 -- .../org/sonar/wsclient/user/UserClient.java | 45 ------- .../sonar/wsclient/user/UserParameters.java | 65 ---------- .../org/sonar/wsclient/user/UserQuery.java | 64 ---------- .../user/internal/DefaultUserClient.java | 90 -------------- .../wsclient/user/internal/package-info.java | 22 ---- .../org/sonar/wsclient/SonarClientTest.java | 2 - .../wsclient/user/DefaultUserClientTest.java | 117 ------------------ .../sonar/wsclient/user/UserQueryTest.java | 60 --------- 9 files changed, 474 deletions(-) delete mode 100644 server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java delete mode 100644 server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java delete mode 100644 server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java delete mode 100644 server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/DefaultUserClient.java delete mode 100644 server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/package-info.java delete mode 100644 server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java delete mode 100644 server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java index 498a4c00cc5..efd5a2512cb 100644 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java +++ b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java @@ -34,8 +34,6 @@ import org.sonar.wsclient.qualitygate.QualityGateClient; 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; @@ -97,13 +95,6 @@ public class SonarClient { 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 */ diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java deleted file mode 100644 index 261745326bf..00000000000 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 find(UserQuery query); - - /** - * @since 3.7 - */ - User create(UserParameters userParameters); - - /** - * @since 3.7 - */ - User update(UserParameters userParameters); - - /** - * @since 3.7 - */ - void deactivate(String login); -} diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java deleted file mode 100644 index 025cea9a2c6..00000000000 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 params = new HashMap<>(); - - private UserParameters() { - } - - public static UserParameters create() { - return new UserParameters(); - } - - public Map 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; - } -} diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java deleted file mode 100644 index 2550ef713d6..00000000000 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 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 urlParams() { - return params; - } -} diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/DefaultUserClient.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/DefaultUserClient.java deleted file mode 100644 index 7b1af27b70b..00000000000 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/DefaultUserClient.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 find(UserQuery query) { - String json = requestFactory.get(SEARCH_URL, query.urlParams()); - List result = new ArrayList<>(); - Map jsonRoot = (Map) JSONValue.parse(json); - List jsonUsers = (List) 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 params = new HashMap<>(); - params.put("login", login); - requestFactory.post(DEACTIVATE_URL, params); - } -} diff --git a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/package-info.java b/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/package-info.java deleted file mode 100644 index 20e56b94013..00000000000 --- a/server/sonar-ws-client/src/main/java/org/sonar/wsclient/user/internal/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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; diff --git a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java b/server/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java index 7c1ed06d237..5b01598674e 100644 --- a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java +++ b/server/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java @@ -29,7 +29,6 @@ import org.sonar.wsclient.project.internal.DefaultProjectClient; 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; @@ -45,7 +44,6 @@ public class SonarClientTest { 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); diff --git a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java b/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java deleted file mode 100644 index d521d115053..00000000000 --- a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * 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 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 requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&")); - assertThat(requestParameters).containsOnly(parameters); - } -} diff --git a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java b/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java deleted file mode 100644 index 5cad47114ef..00000000000 --- a/server/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 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 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(); - } -} -- 2.39.5