]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4411 Added new user management operations in WS client
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 20 Jun 2013 15:40:56 +0000 (17:40 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 20 Jun 2013 15:40:56 +0000 (17:40 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboards_controller.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/user/DefaultUserClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserParameters.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/user/UserQuery.java
sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java
sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java

index faee218f71c5515894e14d04b59faddf286d05ef..ed206bab77c9fe5307074e7385553f949fab522c 100644 (file)
@@ -76,7 +76,7 @@ class DashboardsController < ApplicationController
     if @dashboard.editable_by?(current_user)
       render :partial => 'edit_form', :resource => params[:resource]
     else
-      redirect_to :action => 'index', :resource => params[:resource], :status => 401
+      access_denied
     end
   end
 
@@ -91,7 +91,7 @@ class DashboardsController < ApplicationController
         render :partial => 'dashboards/edit_form', :status => 400, :resource => params[:resource]
       end
     else
-      render :text => @dashboard.id.to_s, :resource => params[:resource], :status => 401
+      access_denied
     end
   end
 
index b7c6086699159d89b832f8f0125864ead00009a0..4b21fe35839fe0806fd43971cd5ce3a39c2c7d7a 100644 (file)
@@ -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());
+  }
 }
index d3893f89ef01cb1a828a378ea91fe905cfbff70f..fe0e620f5c762a2b1d98c49105530b5e5713c057 100644 (file)
@@ -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 (file)
index 0000000..79afaeb
--- /dev/null
@@ -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;
+  }
+}
index 43d0e33c6ac528b46fe8d12adf8309a852f5fa79..5060a2fee2a542fcefe412ea537b42c6a7034fe8 100644 (file)
@@ -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>();
 
index 442e999bce16b46bb0d69380d04fe019e4401de3..01d700d8c70212f6c073a3bbb1ebf3e6c09f25a1 100644 (file)
@@ -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;
   }
index fdf537b1fa7a1f998fc3dfd0f352206b09db0167..79f399cb7ea6baa94e0f47dae559ded2dd7b18f6 100644 (file)
@@ -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"));
index 540253a9bd58e7def4d1ab2d1c99067283fb5575..3e51e4245d5745b4e6103be9dd23b030cecff171 100644 (file)
@@ -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");
index d262073e26e84b9bb848c911a367240f311b6be7..9ad9f130e53bdf1e9cf7af229c50a59f28030c97 100644 (file)
@@ -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");
index 716802c6fde886c5eb7af2977b8952b9d7b68054..2a7ea3024919876a6ffcc3106823db3ca8806487 100644 (file)
  */
 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);
+  }
 }