diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-06 17:15:18 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-06-06 17:15:18 +0200 |
commit | 48df13d33938b36a1af7feed65e9bb464af597e2 (patch) | |
tree | 053bd8ecc35e3d2bb013d83f688b8a5845f201ad /sonar-ws-client/src | |
parent | f1c7b9f5601d77f9ffa35c4f61129c898292299b (diff) | |
download | sonarqube-48df13d33938b36a1af7feed65e9bb464af597e2.tar.gz sonarqube-48df13d33938b36a1af7feed65e9bb464af597e2.zip |
SONAR-3755 Fix bug in Issue WS Client when encoding dates
Diffstat (limited to 'sonar-ws-client/src')
5 files changed, 64 insertions, 8 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java index 4d10be6f043..d022f5960b7 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java @@ -22,6 +22,10 @@ package org.sonar.wsclient.internal; import com.github.kevinsawicki.http.HttpRequest; import javax.annotation.Nullable; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -116,15 +120,36 @@ public class HttpRequestFactory { } public HttpRequest get(String wsUrl, Map<String, Object> queryParams) { - HttpRequest request = HttpRequest.get(baseUrl + wsUrl, queryParams, true); + HttpRequest request = HttpRequest.get(baseUrl + wsUrl, encode(queryParams), false); return prepare(request); } public HttpRequest post(String wsUrl, Map<String, Object> queryParams) { - HttpRequest request = HttpRequest.post(baseUrl + wsUrl, queryParams, true); + HttpRequest request = HttpRequest.post(baseUrl + wsUrl, encode(queryParams), false); return prepare(request); } + private static Map<String, Object> encode(Map<String, Object> queryParams){ + Map<String, Object> newQueryParams = new LinkedHashMap<String, Object>(); + for (Map.Entry<String, Object> entry : queryParams.entrySet()) { + newQueryParams.put(entry.getKey(), encode(entry.getValue())); + } + return newQueryParams; + } + + private static String encode(Object value){ + try { + if (value != null) { + return URLEncoder.encode(value.toString(), "UTF-8"); + } + else { + return ""; + } + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("Fail to encore parameter", e); + } + } + private HttpRequest prepare(HttpRequest request) { if (proxyHost != null) { request.useProxy(proxyHost, proxyPort); 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 43575e83b5f..0c3151017a4 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 @@ -23,8 +23,14 @@ import com.github.kevinsawicki.http.HttpRequest; import org.junit.Rule; import org.junit.Test; import org.sonar.wsclient.MockHttpServerInterceptor; +import org.sonar.wsclient.issue.DefaultIssueClient; +import org.sonar.wsclient.issue.IssueClient; +import org.sonar.wsclient.issue.IssueQuery; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Collections; +import java.util.Date; import static org.fest.assertions.Assertions.assertThat; @@ -85,4 +91,29 @@ public class HttpRequestFactoryTest { HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap()); // it's not possible to check that the proxy is correctly configured } + + @Test + public void should_encore_characters() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); + + IssueClient client = new DefaultIssueClient(requestFactory); + client.find(IssueQuery.create().issues("ABC DE")); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC+DE"); + + client.find(IssueQuery.create().issues("ABC+BDE")); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC%2BBDE"); + + client.find(IssueQuery.create().createdAfter(toDate("2013-01-01"))); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?createdAfter=2013-01-01T00%3A00%3A00%2B0100"); + } + + protected static Date toDate(String sDate) { + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.parse(sDate); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java index 4498541a1db..1bddcc0bc1a 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java @@ -55,7 +55,7 @@ public class DefaultActionPlanClientTest { ActionPlanClient client = new DefaultActionPlanClient(requestFactory); List<ActionPlan> actionPlans = client.find("com.sonarsource.it.samples:simple-sample"); - assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/search?project=com.sonarsource.it.samples:simple-sample"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/search?project=com.sonarsource.it.samples%3Asimple-sample"); assertThat(actionPlans).hasSize(1); ActionPlan actionPlan = actionPlans.get(0); assertThat(actionPlan.key()).isEqualTo("382f6f2e-ad9d-424a-b973-9b065e04348a"); @@ -79,7 +79,7 @@ public class DefaultActionPlanClientTest { ActionPlan result = client.create( NewActionPlan.create().name("Short term").project("org.sonar.Sample").description("Short term issues").deadLine(stringToDate("2014-01-01"))); - assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/create?project=org.sonar.Sample&description=Short%20term%20issues&name=Short%20term&deadLine=2014-01-01"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/create?project=org.sonar.Sample&description=Short+term+issues&name=Short+term&deadLine=2014-01-01"); assertThat(result).isNotNull(); } @@ -92,7 +92,7 @@ public class DefaultActionPlanClientTest { ActionPlan result = client.update( UpdateActionPlan.create().key("382f6f2e-ad9d-424a-b973-9b065e04348a").name("Short term").description("Short term issues").deadLine(stringToDate("2014-01-01"))); - assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/update?description=Short%20term%20issues&name=Short%20term&deadLine=2014-01-01&key=382f6f2e-ad9d-424a-b973-9b065e04348a"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/update?description=Short+term+issues&name=Short+term&deadLine=2014-01-01&key=382f6f2e-ad9d-424a-b973-9b065e04348a"); assertThat(result).isNotNull(); } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java index 7ad6f6df3c8..90d4229680e 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java @@ -130,7 +130,7 @@ public class DefaultIssueClientTest { IssueClient client = new DefaultIssueClient(requestFactory); Issue result = client.create(NewIssue.create().component("Action.java").rule("squid:AvoidCycle")); - assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/create?component=Action.java&rule=squid:AvoidCycle"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/create?component=Action.java&rule=squid%3AAvoidCycle"); assertThat(result).isNotNull(); } @@ -172,7 +172,7 @@ public class DefaultIssueClientTest { IssueClient client = new DefaultIssueClient(requestFactory); IssueComment comment = client.addComment("ISSUE-1", "this is my comment"); - assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/add_comment?issue=ISSUE-1&text=this%20is%20my%20comment"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/add_comment?issue=ISSUE-1&text=this+is+my+comment"); assertThat(comment).isNotNull(); assertThat(comment.key()).isEqualTo("COMMENT-123"); assertThat(comment.htmlText()).isEqualTo("this is my comment"); 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..ca327f370ca 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 @@ -41,7 +41,7 @@ public class DefaultUserClientTest { UserQuery query = UserQuery.create().logins("simon", "loic"); List<User> users = client.find(query); - assertThat(httpServer.requestedPath()).isEqualTo("/api/users/search?logins=simon,loic"); + assertThat(httpServer.requestedPath()).isEqualTo("/api/users/search?logins=simon%2Cloic"); assertThat(users).hasSize(1); User simon = users.get(0); assertThat(simon.login()).isEqualTo("simon"); |