diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-06-12 23:28:00 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-06-13 15:46:48 +0200 |
commit | 9678ebc554ee2c2581268c956040e3ebc7adcb12 (patch) | |
tree | 379f934e4c31d598bc08ea91310e52a4a39ca5c0 /sonar-ws-client/src/test/java/org/sonar | |
parent | 92b4d736ed08613824523d2acd033cc39b56b412 (diff) | |
download | sonarqube-9678ebc554ee2c2581268c956040e3ebc7adcb12.tar.gz sonarqube-9678ebc554ee2c2581268c956040e3ebc7adcb12.zip |
Improve error handling of ws-client
Diffstat (limited to 'sonar-ws-client/src/test/java/org/sonar')
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java | 4 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/base/HttpExceptionTest.java | 34 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java | 57 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java | 12 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java (renamed from sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java) | 13 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java (renamed from sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultIssueClientTest.java) | 13 | ||||
-rw-r--r-- | sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java (renamed from sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueJsonParserTest.java) | 24 |
7 files changed, 112 insertions, 45 deletions
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java index 8216d88767e..74b601c3f3d 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java @@ -20,8 +20,8 @@ package org.sonar.wsclient; import org.junit.Test; -import org.sonar.wsclient.issue.DefaultActionPlanClient; -import org.sonar.wsclient.issue.DefaultIssueClient; +import org.sonar.wsclient.issue.internal.DefaultActionPlanClient; +import org.sonar.wsclient.issue.internal.DefaultIssueClient; import org.sonar.wsclient.user.DefaultUserClient; import static org.fest.assertions.Assertions.assertThat; diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/base/HttpExceptionTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/base/HttpExceptionTest.java new file mode 100644 index 00000000000..d0a41a02cf2 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/base/HttpExceptionTest.java @@ -0,0 +1,34 @@ +/* + * 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.base; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class HttpExceptionTest { + @Test + public void test_exception() throws Exception { + HttpException exception = new HttpException("http://localhost:9000/api/search", 500); + assertThat(exception.status()).isEqualTo(500); + assertThat(exception.url()).isEqualTo("http://localhost:9000/api/search"); + assertThat(exception.getMessage()).isEqualTo("Error 500 on http://localhost:9000/api/search"); + } +} 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 0006ad7295b..9969090a1ee 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 @@ -19,20 +19,21 @@ */ package org.sonar.wsclient.internal; -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 org.sonar.wsclient.issue.internal.DefaultIssueClient; +import java.net.ConnectException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; public class HttpRequestFactoryTest { @Rule @@ -40,56 +41,62 @@ public class HttpRequestFactoryTest { @Test public void test_get() { - httpServer.doReturnStatus(200).doReturnBody("list of issues"); + httpServer.doReturnStatus(200).doReturnBody("{'issues': []}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap()); + String json = factory.get("/api/issues", Collections.<String, Object>emptyMap()); - assertThat(request.method()).isEqualTo("GET"); - assertThat(request.body()).isEqualTo("list of issues"); - assertThat(request.code()).isEqualTo(200); + assertThat(json).isEqualTo("{'issues': []}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); } @Test + public void should_throw_illegal_state_exc_if_connect_exception() { + HttpRequestFactory factory = new HttpRequestFactory("http://localhost:1"); + try { + factory.get("/api/issues", Collections.<String, Object>emptyMap()); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IllegalStateException.class); + assertThat(e).hasMessage("java.net.ConnectException: Connection refused"); + } + } + + @Test public void test_post() { - httpServer.doReturnStatus(200); + httpServer.doReturnStatus(200).doReturnBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - HttpRequest request = factory.post("/api/issues/change", Collections.<String, Object>emptyMap()); + String json = factory.post("/api/issues/change", Collections.<String, Object>emptyMap()); - assertThat(request.method()).isEqualTo("POST"); - assertThat(request.code()).isEqualTo(200); + assertThat(json).isEqualTo("{}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change"); } @Test public void test_authentication() { - httpServer.doReturnStatus(200).doReturnBody("list of issues"); + httpServer.doReturnStatus(200).doReturnBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie"); - HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap()); + String json = factory.get("/api/issues", Collections.<String, Object>emptyMap()); - assertThat(request.body()).isEqualTo("list of issues"); - assertThat(request.code()).isEqualTo(200); + assertThat(json).isEqualTo("{}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); assertThat(httpServer.requestHeaders().get("Authorization")).isEqualTo("Basic a2FyYWRvYzpsZWdyYXNjZXN0bGF2aWU="); } @Test public void test_proxy() throws Exception { - HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setProxyHost("localhost").setProxyPort(5020); - 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 test_proxy_credentials() throws Exception { HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()) - .setProxyHost("localhost").setProxyPort(5020) + .setProxyHost("localhost").setProxyPort(1) .setProxyLogin("john").setProxyPassword("smith"); - HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap()); - // it's not possible to check that the proxy is correctly configured + try { + factory.get("/api/issues", Collections.<String, Object>emptyMap()); + fail(); + } catch (IllegalStateException e) { + // it's not possible to check that the proxy is correctly configured + assertThat(e.getCause()).isInstanceOf(ConnectException.class); + } } @Test diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java index bd1ddc18e01..941894aee78 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueQueryTest.java @@ -21,6 +21,9 @@ package org.sonar.wsclient.issue; import org.junit.Test; +import java.text.ParseException; +import java.text.SimpleDateFormat; + import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.MapAssert.entry; @@ -32,7 +35,8 @@ public class IssueQueryTest { } @Test - public void get_all_issues_by_parameter() { + public void get_all_issues_by_parameter() throws ParseException { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"); IssueQuery query = IssueQuery.create() .issues("ABCDE", "FGHIJ") .assignees("arthur", "perceval") @@ -47,12 +51,14 @@ public class IssueQueryTest { .statuses("OPEN", "CLOSED") .severities("BLOCKER", "INFO") .reporters("login1", "login2") + .createdBefore(df.parse("2015-12-13T05:59")) + .createdAfter(df.parse("2012-01-23T13:40")) .sort("ASSIGNEE") .asc(false) .pageSize(5) .pageIndex(4); - assertThat(query.urlParams()).hasSize(17); + assertThat(query.urlParams()).hasSize(19); assertThat(query.urlParams()).includes(entry("issues", "ABCDE,FGHIJ")); assertThat(query.urlParams()).includes(entry("assignees", "arthur,perceval")); assertThat(query.urlParams()).includes(entry("assigned", true)); @@ -66,6 +72,8 @@ public class IssueQueryTest { assertThat(query.urlParams()).includes(entry("statuses", "OPEN,CLOSED")); assertThat(query.urlParams()).includes(entry("severities", "BLOCKER,INFO")); assertThat(query.urlParams()).includes(entry("reporters", "login1,login2")); + assertThat((String)query.urlParams().get("createdBefore")).startsWith("2015-12-13T05:59"); + assertThat((String)query.urlParams().get("createdAfter")).startsWith("2012-01-23T13:40:00"); assertThat(query.urlParams()).includes(entry("sort", "ASSIGNEE")); assertThat(query.urlParams()).includes(entry("asc", false)); assertThat(query.urlParams()).includes(entry("pageSize", 5)); 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/internal/DefaultActionPlanClientTest.java index e56572fc2b1..2e74730df14 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/internal/DefaultActionPlanClientTest.java @@ -18,12 +18,17 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.wsclient.issue; +package org.sonar.wsclient.issue.internal; import org.junit.Rule; import org.junit.Test; import org.sonar.wsclient.MockHttpServerInterceptor; +import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.internal.HttpRequestFactory; +import org.sonar.wsclient.issue.ActionPlan; +import org.sonar.wsclient.issue.ActionPlanClient; +import org.sonar.wsclient.issue.NewActionPlan; +import org.sonar.wsclient.issue.UpdateActionPlan; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -115,8 +120,10 @@ public class DefaultActionPlanClientTest { try { client.delete("382f6f2e-ad9d-424a-b973-9b065e04348a"); fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("Fail to delete action plan. Bad HTTP response status: 500"); + } catch (HttpException e) { + assertThat(e.status()).isEqualTo(500); + assertThat(e.url()).startsWith("http://localhost"); + assertThat(e.url()).endsWith("/api/action_plans/delete?key=382f6f2e-ad9d-424a-b973-9b065e04348a"); } } 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/internal/DefaultIssueClientTest.java index 7ad6f6df3c8..d262073e26e 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/internal/DefaultIssueClientTest.java @@ -17,13 +17,16 @@ * 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; +package org.sonar.wsclient.issue.internal; import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.Test; 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; @@ -57,8 +60,10 @@ public class DefaultIssueClientTest { try { client.find(IssueQuery.create()); fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("Fail to search for issues. Bad HTTP response status: 500"); + } catch (HttpException e) { + assertThat(e.status()).isEqualTo(500); + assertThat(e.url()).startsWith("http://localhost"); + assertThat(e.url()).endsWith("/api/issues/search"); } } @@ -167,7 +172,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/DefaultIssueClientTest/add_comment_result.json"))); + httpServer.doReturnBody(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"); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueJsonParserTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java index 09cb3941742..c84bfe8c001 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueJsonParserTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/IssueJsonParserTest.java @@ -17,11 +17,17 @@ * 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; +package org.sonar.wsclient.issue.internal; import org.apache.commons.io.IOUtils; import org.junit.Test; +import org.sonar.wsclient.base.Paging; import org.sonar.wsclient.component.Component; +import org.sonar.wsclient.issue.ActionPlan; +import org.sonar.wsclient.issue.Issue; +import org.sonar.wsclient.issue.IssueComment; +import org.sonar.wsclient.issue.Issues; +import org.sonar.wsclient.issue.internal.IssueJsonParser; import org.sonar.wsclient.user.User; import java.util.List; @@ -31,7 +37,7 @@ import static org.fest.assertions.Assertions.assertThat; public class IssueJsonParserTest { @Test public void test_GET_search() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/search.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/search.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues).isNotNull(); List<Issue> list = issues.list(); @@ -86,7 +92,7 @@ public class IssueJsonParserTest { @Test public void test_GET_empty_search() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/empty.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/empty.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues).isNotNull(); assertThat(issues.list()).isEmpty(); @@ -96,7 +102,7 @@ public class IssueJsonParserTest { @Test public void test_GET_transitions() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/getTransitions.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/getTransitions.json")); List<String> transitions = new IssueJsonParser().parseTransitions(json); assertThat(transitions).isNotNull(); @@ -106,7 +112,7 @@ public class IssueJsonParserTest { @Test public void should_parse_comments() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/issue-with-comments.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.size()).isEqualTo(1); @@ -128,7 +134,7 @@ public class IssueJsonParserTest { @Test public void should_parse_users() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-users.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/issue-with-users.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.users()).hasSize(2); @@ -148,7 +154,7 @@ public class IssueJsonParserTest { @Test public void should_parse_components() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-components.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/issue-with-components.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.components()).hasSize(1); @@ -162,7 +168,7 @@ public class IssueJsonParserTest { @Test public void should_parse_projects() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-projects.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/issue-with-projects.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.projects()).hasSize(1); @@ -176,7 +182,7 @@ public class IssueJsonParserTest { @Test public void should_parse_action_plans() throws Exception { - String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-action-plans.json")); + String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/internal/IssueJsonParserTest/issue-with-action-plans.json")); Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.actionPlans()).hasSize(1); |