diff options
author | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-06-20 17:40:56 +0200 |
---|---|---|
committer | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-06-20 17:40:56 +0200 |
commit | 3207a389ee843d30591cc806ac2de9f24f7fd756 (patch) | |
tree | fae145470e778b6f96efcb49ce70e38d072f9cbc /sonar-ws-client | |
parent | f699d4683bfb31f0cd95d277cde92b4edaa3995f (diff) | |
download | sonarqube-3207a389ee843d30591cc806ac2de9f24f7fd756.tar.gz sonarqube-3207a389ee843d30591cc806ac2de9f24f7fd756.zip |
SONAR-4411 Added new user management operations in WS client
Diffstat (limited to 'sonar-ws-client')
9 files changed, 160 insertions, 32 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/DefaultUserClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/DefaultUserClient.java index b7c60866991..4b21fe35839 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/DefaultUserClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/DefaultUserClient.java @@ -31,6 +31,12 @@ import java.util.Map; */ 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 DELETE_URL = BASE_URL + "delete"; + private final HttpRequestFactory requestFactory; /** @@ -42,7 +48,7 @@ public class DefaultUserClient implements UserClient { @Override public List<User> find(UserQuery query) { - String json = requestFactory.get(UserQuery.BASE_URL, query.urlParams()); + String json = requestFactory.get(SEARCH_URL, query.urlParams()); List<User> result = new ArrayList<User>(); Map jsonRoot = (Map) JSONValue.parse(json); List<Map> jsonUsers = (List<Map>) jsonRoot.get("users"); @@ -54,4 +60,24 @@ public class DefaultUserClient implements UserClient { 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 delete(UserParameters userParameters) { + requestFactory.post(DELETE_URL, userParameters.urlParams()); + } } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java index d3893f89ef0..fe0e620f5c7 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java @@ -28,4 +28,9 @@ public interface UserClient { List<User> find(UserQuery query); + User create(UserParameters userParameters); + + User update(UserParameters userParameters); + + void delete(UserParameters userParameters); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java new file mode 100644 index 00000000000..79afaeb9661 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java @@ -0,0 +1,45 @@ +package org.sonar.wsclient.user; + +import java.util.HashMap; +import java.util.Map; + +public class UserParameters { + + private final Map<String, Object> params = new HashMap<String, Object>(); + + 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; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java index 43d0e33c6ac..5060a2fee2a 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java @@ -29,7 +29,6 @@ import java.util.Map; * @since 3.6 */ public class UserQuery { - static final String BASE_URL = "/api/users/search"; private final Map<String, Object> params = new HashMap<String, Object>(); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java index 442e999bce1..01d700d8c70 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java @@ -39,12 +39,12 @@ public final class MockHttpServerInterceptor extends ExternalResource { server.stop(); } - public MockHttpServerInterceptor doReturnBody(String body) { + public MockHttpServerInterceptor stubResponseBody(String body) { server.doReturnBody(body); return this; } - public MockHttpServerInterceptor doReturnStatus(int status) { + public MockHttpServerInterceptor stubStatusCode(int status) { server.doReturnStatus(status); return this; } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java index fdf537b1fa7..79f399cb7ea 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java @@ -41,7 +41,7 @@ public class HttpRequestFactoryTest { @Test public void test_get() { - httpServer.doReturnStatus(200).doReturnBody("{'issues': []}"); + httpServer.stubStatusCode(200).stubResponseBody("{'issues': []}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); String json = factory.get("/api/issues", Collections.<String, Object> emptyMap()); @@ -65,7 +65,7 @@ public class HttpRequestFactoryTest { @Test public void test_post() { - httpServer.doReturnStatus(200).doReturnBody("{}"); + httpServer.stubStatusCode(200).stubResponseBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); String json = factory.post("/api/issues/change", Collections.<String, Object> emptyMap()); @@ -76,7 +76,7 @@ public class HttpRequestFactoryTest { @Test public void test_authentication() { - httpServer.doReturnStatus(200).doReturnBody("{}"); + httpServer.stubStatusCode(200).stubResponseBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie"); String json = factory.get("/api/issues", Collections.<String, Object> emptyMap()); @@ -103,7 +103,7 @@ public class HttpRequestFactoryTest { @Test public void should_encode_characters() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); + httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); IssueClient client = new DefaultIssueClient(requestFactory); client.find(IssueQuery.create().issues("ABC DE")); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java index 540253a9bd5..3e51e4245d5 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java @@ -46,7 +46,7 @@ public class DefaultActionPlanClientTest { @Test public void should_find_action_plans() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" + + httpServer.stubResponseBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" + "\"name\": \"Long term\",\n" + "\"desc\": \"Long term acton plan\",\n" + "\"status\": \"CLOSED\",\n" + @@ -80,7 +80,7 @@ public class DefaultActionPlanClientTest { @Test public void should_create_action_plan() throws Exception { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); + httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); ActionPlanClient client = new DefaultActionPlanClient(requestFactory); ActionPlan result = client.create( @@ -93,7 +93,7 @@ public class DefaultActionPlanClientTest { @Test public void should_update_action_plan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); + httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); ActionPlanClient client = new DefaultActionPlanClient(requestFactory); ActionPlan result = client.update( @@ -116,7 +116,7 @@ public class DefaultActionPlanClientTest { @Test public void should_fail_to_delete_action_plan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnStatus(500); + httpServer.stubStatusCode(500); ActionPlanClient client = new DefaultActionPlanClient(requestFactory); try { @@ -132,7 +132,7 @@ public class DefaultActionPlanClientTest { @Test public void should_open_action_plan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); + httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); ActionPlanClient client = new DefaultActionPlanClient(requestFactory); ActionPlan result = client.open("382f6f2e-ad9d-424a-b973-9b065e04348a"); @@ -144,7 +144,7 @@ public class DefaultActionPlanClientTest { @Test public void should_close_action_plan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); + httpServer.stubResponseBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}"); ActionPlanClient client = new DefaultActionPlanClient(requestFactory); ActionPlan result = client.close("382f6f2e-ad9d-424a-b973-9b065e04348a"); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java index d262073e26e..9ad9f130e53 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java @@ -26,7 +26,6 @@ import org.sonar.wsclient.MockHttpServerInterceptor; import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.internal.HttpRequestFactory; import org.sonar.wsclient.issue.*; -import org.sonar.wsclient.issue.internal.DefaultIssueClient; import java.util.List; @@ -40,7 +39,7 @@ public class DefaultIssueClientTest { @Test public void should_find_issues() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); + httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); IssueClient client = new DefaultIssueClient(requestFactory); IssueQuery query = IssueQuery.create().issues("ABCDE"); @@ -54,7 +53,7 @@ public class DefaultIssueClientTest { @Test public void should_fail_to_find_issues() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnStatus(500); + httpServer.stubStatusCode(500); IssueClient client = new DefaultIssueClient(requestFactory); try { @@ -70,7 +69,7 @@ public class DefaultIssueClientTest { @Test public void should_set_severity() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.setSeverity("ABCDE", "BLOCKER"); @@ -82,7 +81,7 @@ public class DefaultIssueClientTest { @Test public void should_assign() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.assign("ABCDE", "emmerik"); @@ -94,7 +93,7 @@ public class DefaultIssueClientTest { @Test public void should_unassign() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.assign("ABCDE", null); @@ -106,7 +105,7 @@ public class DefaultIssueClientTest { @Test public void should_plan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.plan("ABCDE", "DEFGH"); @@ -118,7 +117,7 @@ public class DefaultIssueClientTest { @Test public void should_unplan() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.plan("ABCDE", null); @@ -130,7 +129,7 @@ public class DefaultIssueClientTest { @Test public void should_create_issue() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.create(NewIssue.create().component("Action.java").rule("squid:AvoidCycle")); @@ -142,7 +141,7 @@ public class DefaultIssueClientTest { @Test public void should_get_transitions() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\n" + + httpServer.stubResponseBody("{\n" + " \"transitions\": [\n" + " \"resolve\",\n" + " \"falsepositive\"\n" + @@ -160,7 +159,7 @@ public class DefaultIssueClientTest { @Test public void should_apply_transition() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.doTransition("ABCDE", "resolve"); @@ -172,7 +171,7 @@ public class DefaultIssueClientTest { @Test public void should_add_comment() throws Exception { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody(IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/DefaultIssueClientTest/add_comment_result.json"))); + httpServer.stubResponseBody(IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/DefaultIssueClientTest/add_comment_result.json"))); IssueClient client = new DefaultIssueClient(requestFactory); IssueComment comment = client.addComment("ISSUE-1", "this is my comment"); @@ -188,7 +187,7 @@ public class DefaultIssueClientTest { @Test public void should_get_actions() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\n" + + httpServer.stubResponseBody("{\n" + " \"actions\": [\n" + " \"link-to-jira\",\n" + " \"tweet\"\n" + @@ -206,7 +205,7 @@ public class DefaultIssueClientTest { @Test public void should_apply_action() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}"); + httpServer.stubResponseBody("{\"issue\": {\"key\": \"ABCDE\"}}"); IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.doAction("ABCDE", "tweet"); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java index 716802c6fde..2a7ea302491 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java @@ -19,29 +19,39 @@ */ 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 java.util.Arrays; import java.util.List; import static org.fest.assertions.Assertions.assertThat; public class DefaultUserClientTest { + + private HttpRequestFactory requestFactory; + private 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() { - HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.doReturnBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}"); + httpServer.stubResponseBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}"); - UserClient client = new DefaultUserClient(requestFactory); UserQuery query = UserQuery.create().logins("simon", "loic"); List<User> users = client.find(query); - assertThat(httpServer.requestedPath()).isEqualTo("/api/users/search?logins=simon,loic"); + assertThatRequestUrlContains("/api/users/search?", "logins=simon,loic"); assertThat(users).hasSize(1); User simon = users.get(0); assertThat(simon.login()).isEqualTo("simon"); @@ -49,4 +59,48 @@ public class DefaultUserClientTest { assertThat(simon.email()).isNull(); assertThat(simon.active()).isTrue(); } + + @Test + public void should_create_user() throws Exception { + httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":null}}"); + + UserParameters params = UserParameters.create().login("daveloper").password("pass1").passwordConfirmation("pass1"); + User createdUser = client.create(params); + + assertThatRequestUrlContains("/api/users/create?", "login=daveloper", "password=pass1", "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() throws Exception { + httpServer.stubResponseBody("{\"user\":{\"login\":\"daveloper\",\"name\":\"daveloper\",\"email\":\"new_email\"}}"); + + UserParameters params = UserParameters.create().login("daveloper").email("new_email"); + User updatedUser = client.update(params); + + assertThatRequestUrlContains("/api/users/update?", "login=daveloper", "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_delete_user() throws Exception { + httpServer.stubStatusCode(200); + + UserParameters params = UserParameters.create().login("daveloper"); + client.delete(params); + + assertThatRequestUrlContains("/api/users/delete?", "login=daveloper"); + } + + private void assertThatRequestUrlContains(String baseUrl, String... parameters) { + assertThat(httpServer.requestedPath()).startsWith(baseUrl); + List<String> requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&")); + assertThat(requestParameters).containsOnly(parameters); + } } |