]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 complete wsclient for new actions
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 30 Apr 2013 10:00:30 +0000 (12:00 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 30 Apr 2013 10:00:39 +0000 (12:00 +0200)
sonar-ws-client/src/main/java/org/sonar/wsclient/internal/EncodingUtils.java
sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueChange.java [deleted file]
sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java
sonar-ws-client/src/test/java/org/sonar/wsclient/internal/EncodingUtilsTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueChangeTest.java [deleted file]

index d8b62839eefed6678d9e3b918da478b4a3483d18..1e1631c66980007fe3ad4ef33543f7cdd19ba9f7 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.wsclient.internal;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 /**
  * Not an API, please do not directly use this class.
@@ -34,6 +36,17 @@ public class EncodingUtils {
     // only static methods
   }
 
+  public static Map<String, Object> toMap(String... array) {
+    Map<String, Object> map = new LinkedHashMap<String, Object>();
+    for (int i = 0; i < array.length; i += 2) {
+      Object value = array[i + 1];
+      if (value != null) {
+        map.put(array[i], array[i + 1]);
+      }
+    }
+    return map;
+  }
+
   public static String toQueryParam(String[] strings) {
     StringBuilder sb = new StringBuilder();
     boolean first = true;
index d9670513957f368270305256378bea24561174f9..ecef1bf26772e576b14b390805cf4ff2190a537a 100644 (file)
 package org.sonar.wsclient.issue;
 
 import com.github.kevinsawicki.http.HttpRequest;
+import org.sonar.wsclient.internal.EncodingUtils;
 import org.sonar.wsclient.internal.HttpRequestFactory;
 
-import java.util.LinkedHashMap;
+import javax.annotation.Nullable;
 import java.util.List;
 import java.util.Map;
 
@@ -31,9 +32,6 @@ import java.util.Map;
  */
 public class DefaultIssueClient implements IssueClient {
 
-  private static final String TRANSITIONS_BASE_URL = "/api/issues/transitions";
-  private static final String DO_TRANSITION_BASE_URL = "/api/issues/do_transition";
-
   private final HttpRequestFactory requestFactory;
   private final IssueParser parser;
 
@@ -55,35 +53,35 @@ public class DefaultIssueClient implements IssueClient {
   }
 
   @Override
-  public void change(String issueKey, IssueChange change) {
-    if (!change.urlParams().isEmpty()) {
-      Map<String, Object> queryParams = new LinkedHashMap<String, Object>(change.urlParams());
-      queryParams.put("issue", issueKey);
-      HttpRequest request = requestFactory.post(IssueChange.BASE_URL, queryParams);
-      if (!request.ok()) {
-        throw new IllegalStateException("Fail to change issue " + issueKey + ".Bad HTTP response status: " + request.code());
-      }
+  public void create(NewIssue newIssue) {
+    HttpRequest request = requestFactory.post(NewIssue.BASE_URL, newIssue.urlParams());
+    if (!request.ok()) {
+      throw new IllegalStateException("Fail to create issue. Bad HTTP response status: " + request.code());
     }
   }
 
   @Override
-  public void create(NewIssue newIssue) {
-    HttpRequest request = requestFactory.post(NewIssue.BASE_URL, newIssue.urlParams());
+  public void setSeverity(String issueKey, String severity) {
+    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "severity", severity);
+    HttpRequest request = requestFactory.post("/api/issues/set_severity", params);
     if (!request.ok()) {
-      throw new IllegalStateException("Fail to create issue. Bad HTTP response status: " + request.code());
+      throw new IllegalStateException("Fail to set severity. Bad HTTP response status: " + request.code());
     }
   }
 
   @Override
-  public void comment(String issueKey, String comment) {
-    change(issueKey, IssueChange.create().comment(comment));
+  public void assign(String issueKey, @Nullable String assignee) {
+    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "assignee", assignee);
+    HttpRequest request = requestFactory.post("/api/issues/assign", params);
+    if (!request.ok()) {
+      throw new IllegalStateException("Fail to assign issue to user. Bad HTTP response status: " + request.code());
+    }
   }
 
   @Override
   public List<String> transitions(String issueKey) {
-    Map<String, Object> queryParams = new LinkedHashMap<String, Object>();
-    queryParams.put("issue", issueKey);
-    HttpRequest request = requestFactory.get(TRANSITIONS_BASE_URL, queryParams);
+    Map<String, Object> queryParams = EncodingUtils.toMap("issue", issueKey);
+    HttpRequest request = requestFactory.get("/api/issues/transitions", queryParams);
     if (!request.ok()) {
       throw new IllegalStateException("Fail to return transition for issue. Bad HTTP response status: " + request.code());
     }
@@ -93,12 +91,12 @@ public class DefaultIssueClient implements IssueClient {
 
   @Override
   public void doTransition(String issueKey, String transition) {
-    Map<String, Object> queryParams = new LinkedHashMap<String, Object>();
-    queryParams.put("issue", issueKey);
-    queryParams.put("transition", transition);
-    HttpRequest request = requestFactory.post(DO_TRANSITION_BASE_URL, queryParams);
+    Map<String, Object> params = EncodingUtils.toMap("issue", issueKey, "transition", transition);
+    HttpRequest request = requestFactory.post("/api/issues/do_transition", params);
     if (!request.ok()) {
       throw new IllegalStateException("Fail to execute transition on issue " + issueKey + ".Bad HTTP response status: " + request.code());
     }
   }
+
+
 }
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueChange.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueChange.java
deleted file mode 100644 (file)
index c4f2e16..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 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.issue;
-
-import javax.annotation.Nullable;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * @since 3.6
- */
-public class IssueChange {
-  static final String BASE_URL = "/api/issues/change";
-  private final Map<String, Object> params = new LinkedHashMap<String, Object>();
-
-  private IssueChange() {
-  }
-
-  public static IssueChange create() {
-    return new IssueChange();
-  }
-
-  Map<String, Object> urlParams() {
-    return params;
-  }
-
-  public IssueChange severity(String s) {
-    params.put("newSeverity", s);
-    return this;
-  }
-
-  /**
-   * Add a comment
-   */
-  public IssueChange comment(String s) {
-    params.put("comment", s);
-    return this;
-  }
-
-  public IssueChange cost(Double d) {
-    params.put("newCost", d);
-    return this;
-  }
-
-  public IssueChange line(Integer i) {
-    params.put("newLine", i);
-    return this;
-  }
-
-  public IssueChange transition(String s) {
-    params.put("transition", s);
-    return this;
-  }
-
-  public IssueChange assignee(String login) {
-    params.put("newAssignee", login);
-    return this;
-  }
-
-  public IssueChange description(String s) {
-    params.put("newDesc", s);
-    return this;
-  }
-
-  public IssueChange attribute(String key, @Nullable String value) {
-    if (value == null) {
-      params.put("newAttr[" + key + "]", "");
-    } else {
-      params.put("newAttr[" + key + "]", value);
-    }
-    return this;
-  }
-}
index 85c0b86cf3c73a19a133bf50d1a81faf83853833..5cfe1b2728c2a516a9be915381980a3d33692eac 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.wsclient.issue;
 
+import javax.annotation.Nullable;
 import java.util.List;
 
 /**
@@ -28,7 +29,9 @@ public interface IssueClient {
 
   Issues find(IssueQuery query);
 
-  void change(String issueKey, IssueChange change);
+  void assign(String issueKey, @Nullable String assignee);
+
+  void setSeverity(String issueKey, String severity);
 
   void create(NewIssue issue);
 
@@ -36,8 +39,4 @@ public interface IssueClient {
 
   void doTransition(String issueKey, String transition);
 
-  /**
-   * Shortcut for {@code #change(issueKey, IssueChange.create().comment(comment)}
-   */
-  void comment(String issueKey, String comment);
 }
index bc839f41869f46417426466336fec6673d9b8b81..1fa30efa8360b2e16b39d43717d123c66f5f4910 100644 (file)
@@ -24,6 +24,7 @@ import org.junit.Test;
 import java.util.Date;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
 
 public class EncodingUtilsTest {
   @Test
@@ -39,4 +40,12 @@ public class EncodingUtilsTest {
     assertThat(EncodingUtils.toQueryParam(new String[]{"foo"})).isEqualTo("foo");
     assertThat(EncodingUtils.toQueryParam(new String[]{""})).isEqualTo("");
   }
+
+  @Test
+  public void test_toMap() {
+    assertThat(EncodingUtils.toMap()).isEmpty();
+    assertThat(EncodingUtils.toMap("foo", "bar")).hasSize(1).includes(entry("foo", "bar"));
+    assertThat(EncodingUtils.toMap("1", "one", "2", "two")).hasSize(2).includes(entry("1", "one"), entry("2", "two"));
+    assertThat(EncodingUtils.toMap("foo", null)).isEmpty();
+  }
 }
index aa5fbe62a2fc5ae28315ea999a39894871232254..2c0fdaea03fbb0f4f6cc809372b99c62f65ccbab 100644 (file)
@@ -62,23 +62,33 @@ public class DefaultIssueClientTest {
   }
 
   @Test
-  public void should_apply_change() {
+  public void should_set_severity() {
     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
 
     IssueClient client = new DefaultIssueClient(requestFactory);
-    client.change("ABCDE", IssueChange.create().severity("BLOCKER").comment("because!"));
+    client.setSeverity("ABCDE", "BLOCKER");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change?newSeverity=BLOCKER&comment=because!&issue=ABCDE");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/set_severity?issue=ABCDE&severity=BLOCKER");
   }
 
   @Test
-  public void should_not_apply_empty_change() {
+  public void should_assign() {
     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
 
     IssueClient client = new DefaultIssueClient(requestFactory);
-    client.change("ABCDE", IssueChange.create());
+    client.assign("ABCDE", "emmerik");
 
-    assertThat(httpServer.requestedPath()).isNull();
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE&assignee=emmerik");
+  }
+
+  @Test
+  public void should_unassign() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+
+    IssueClient client = new DefaultIssueClient(requestFactory);
+    client.assign("ABCDE", null);
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE");
   }
 
   @Test
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueChangeTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueChangeTest.java
deleted file mode 100644 (file)
index a373a91..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 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.issue;
-
-import org.junit.Test;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.MapAssert.entry;
-
-public class IssueChangeTest {
-  @Test
-  public void should_create_empty_change() {
-    IssueChange change = IssueChange.create();
-    assertThat(change.urlParams()).isEmpty();
-  }
-
-  @Test
-  public void should_create_change() {
-    IssueChange change = IssueChange.create()
-      .comment("this is a comment")
-      .assignee("lancelot")
-      .attribute("JIRA", "FOO-1234")
-      .attribute("LINK", null)
-      .cost(4.2)
-      .transition("resolve")
-      .severity("BLOCKER");
-    assertThat(change.urlParams()).hasSize(7).includes(
-      entry("comment", "this is a comment"),
-      entry("newAssignee", "lancelot"),
-      entry("newAttr[JIRA]", "FOO-1234"),
-      entry("newAttr[LINK]", ""),
-      entry("newCost", 4.2),
-      entry("transition", "resolve"),
-      entry("newSeverity", "BLOCKER")
-    );
-  }
-}