diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-09 15:05:14 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-17 13:40:18 +0100 |
commit | a3aa1505a13938c766cb40fb74e8c7532e5dfb34 (patch) | |
tree | c1d18ff655a5b161500e4c7fbf70533722721057 /sonar-ws | |
parent | e754b602349f2c69373d24036d6f9a70aa8353ea (diff) | |
download | sonarqube-a3aa1505a13938c766cb40fb74e8c7532e5dfb34.tar.gz sonarqube-a3aa1505a13938c766cb40fb74e8c7532e5dfb34.zip |
SONAR-6947 New WsClient
Diffstat (limited to 'sonar-ws')
9 files changed, 381 insertions, 182 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java new file mode 100644 index 00000000000..2b3bcaf8978 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java @@ -0,0 +1,160 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.sonarqube.ws.client; + +import com.google.protobuf.Message; +import com.google.protobuf.Parser; +import javax.annotation.Nullable; + +public class HttpConnector implements WsConnector { + + public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000; + public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000; + + /** + * Visibility relaxed for unit tests + */ + final HttpRequestFactory requestFactory; + + private HttpConnector(Builder builder) { + this.requestFactory = new HttpRequestFactory(builder.url) + .setLogin(builder.login) + .setPassword(builder.password) + .setProxyHost(builder.proxyHost) + .setProxyPort(builder.proxyPort) + .setProxyLogin(builder.proxyLogin) + .setProxyPassword(builder.proxyPassword) + .setConnectTimeoutInMilliseconds(builder.connectTimeoutMs) + .setReadTimeoutInMilliseconds(builder.readTimeoutMs); + } + + @Override + public String execute(WsRequest wsRequest) { + return requestFactory.execute(wsRequest); + } + + @Override + public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) { + return requestFactory.execute(wsRequest, protobufParser); + } + + /** + * Create a builder of {@link WsClient}s. + */ + public static Builder newHttpConnector() { + return new Builder(); + } + + /** + * Create a client with default configuration. Use {@link #newHttpConnector()} to define + * a custom configuration (credentials, HTTP proxy, HTTP timeouts). + */ + public static HttpConnector newDefaultHttpConnector(String serverUrl) { + return newHttpConnector().url(serverUrl).build(); + } + + public static class Builder { + private String login; + private String password; + private String url; + private String proxyHost; + private String proxyLogin; + private String proxyPassword; + private int proxyPort = 0; + + private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS; + + private Builder() { + } + + /** + * Mandatory HTTP server URL, eg "http://localhost:9000" + */ + public Builder url(String url) { + this.url = url; + return this; + } + + /** + * Optional login, for example "admin" + */ + public Builder login(@Nullable String login) { + this.login = login; + return this; + } + + /** + * Optional password related to {@link #login(String)}, for example "admin" + */ + public Builder password(@Nullable String password) { + this.password = password; + return this; + } + + /** + * Host and port of the optional HTTP proxy + */ + public Builder proxy(@Nullable String proxyHost, int proxyPort) { + this.proxyHost = proxyHost; + this.proxyPort = proxyPort; + return this; + } + + public Builder proxyLogin(@Nullable String proxyLogin) { + this.proxyLogin = proxyLogin; + return this; + } + + public Builder proxyPassword(@Nullable String proxyPassword) { + this.proxyPassword = proxyPassword; + return this; + } + + /** + * Sets a specified timeout value, in milliseconds, to be used when opening HTTP connection. + * A timeout of zero is interpreted as an infinite timeout. Default value is {@link HttpConnector#DEFAULT_CONNECT_TIMEOUT_MILLISECONDS} + */ + public Builder connectTimeoutMilliseconds(int i) { + this.connectTimeoutMs = i; + return this; + } + + /** + * Sets the read timeout to a specified timeout, in milliseconds. + * A timeout of zero is interpreted as an infinite timeout. Default value is {@link HttpConnector#DEFAULT_READ_TIMEOUT_MILLISECONDS} + */ + public Builder readTimeoutMilliseconds(int i) { + this.readTimeoutMs = i; + return this; + } + + /** + * Build a new client + */ + public HttpConnector build() { + if (url == null || "".equals(url)) { + throw new IllegalStateException("Server URL must be set"); + } + return new HttpConnector(this); + } + + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java index 3b10c9d0a48..97b26436b0c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java @@ -21,12 +21,12 @@ package org.sonarqube.ws.client; import com.github.kevinsawicki.http.HttpRequest; import com.google.common.base.Throwables; -import com.google.common.net.MediaType; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Message; import com.google.protobuf.Parser; import java.util.Arrays; import javax.annotation.Nullable; +import org.sonarqube.ws.MediaTypes; import static java.net.HttpURLConnection.HTTP_CREATED; import static java.net.HttpURLConnection.HTTP_NO_CONTENT; @@ -136,8 +136,9 @@ public class HttpRequestFactory { public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) { HttpRequest httpRequest = wsRequestToHttpRequest(wsRequest); + String response = execute(httpRequest); try { - return protobufParser.parseFrom(httpRequest.bytes()); + return protobufParser.parseFrom(response.getBytes()); } catch (InvalidProtocolBufferException e) { Throwables.propagate(e); } @@ -147,19 +148,21 @@ public class HttpRequestFactory { private HttpRequest wsRequestToHttpRequest(WsRequest wsRequest) { HttpRequest httpRequest = wsRequest.getMethod().equals(WsRequest.Method.GET) - ? HttpRequest.post(buildUrl(wsRequest.getUrl()), wsRequest.getParams(), true) - : HttpRequest.get(buildUrl(wsRequest.getUrl()), wsRequest.getParams(), true); + ? HttpRequest.post(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true) + : HttpRequest.get(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true); httpRequest = prepare(httpRequest); switch (wsRequest.getMediaType()) { case PROTOBUF: - httpRequest.accept(MediaType.PROTOBUF.toString()); + httpRequest.accept(MediaTypes.PROTOBUF); break; case JSON: - httpRequest.accept(MediaType.JSON_UTF_8.toString()); + httpRequest.accept(MediaTypes.JSON); break; case TEXT: + httpRequest.accept(MediaTypes.TXT); + break; default: - httpRequest.accept(MediaType.PLAIN_TEXT_UTF_8.toString()); + httpRequest.accept(MediaTypes.DEFAULT); break; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java index 641ac51d8a1..2981de40769 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java @@ -20,154 +20,43 @@ package org.sonarqube.ws.client; +import com.google.common.annotations.VisibleForTesting; import com.google.protobuf.Message; import com.google.protobuf.Parser; -import javax.annotation.Nullable; +import org.sonarqube.ws.client.permission.PermissionsWsClient; + +import static org.sonarqube.ws.client.WsRequest.MediaType.PROTOBUF; /** * Entry point of the Java Client for SonarQube Web Services. * <p/> * Example: * <pre> - * WsClient client = WsClient.create("http://localhost:9000"); + * WsClient client = new WsClient(Connector); * </pre> * * @since 5.2 */ public class WsClient { - public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000; - public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000; - - /** - * Visibility relaxed for unit tests - */ - final HttpRequestFactory requestFactory; + @VisibleForTesting + final WsConnector wsConnector; + private final PermissionsWsClient permissionsWsClient; - private WsClient(Builder builder) { - this(new HttpRequestFactory(builder.url) - .setLogin(builder.login) - .setPassword(builder.password) - .setProxyHost(builder.proxyHost) - .setProxyPort(builder.proxyPort) - .setProxyLogin(builder.proxyLogin) - .setProxyPassword(builder.proxyPassword) - .setConnectTimeoutInMilliseconds(builder.connectTimeoutMs) - .setReadTimeoutInMilliseconds(builder.readTimeoutMs)); - } - - /** - * Visible for testing - */ - WsClient(HttpRequestFactory requestFactory) { - this.requestFactory = requestFactory; - } - - /** - * Create a builder of {@link WsClient}s. - */ - public static Builder builder() { - return new Builder(); - } - - /** - * Create a client with default configuration. Use {@link #builder()} to define - * a custom configuration (credentials, HTTP proxy, HTTP timeouts). - */ - public static WsClient create(String serverUrl) { - return builder().url(serverUrl).build(); + public WsClient(WsConnector wsConnector) { + this.wsConnector = wsConnector; + this.permissionsWsClient = new PermissionsWsClient(this); } public String execute(WsRequest wsRequest) { - return requestFactory.execute(wsRequest); + return wsConnector.execute(wsRequest); } public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) { - return requestFactory.execute(wsRequest, protobufParser); + return wsConnector.execute(wsRequest.setMediaType(PROTOBUF), protobufParser); } - public static class Builder { - private String login; - private String password; - private String url; - private String proxyHost; - private String proxyLogin; - private String proxyPassword; - private int proxyPort = 0; - private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS; - - private Builder() { - } - - /** - * Mandatory HTTP server URL, eg "http://localhost:9000" - */ - public Builder url(String url) { - this.url = url; - return this; - } - - /** - * Optional login, for example "admin" - */ - public Builder login(@Nullable String login) { - this.login = login; - return this; - } - - /** - * Optional password related to {@link #login(String)}, for example "admin" - */ - public Builder password(@Nullable String password) { - this.password = password; - return this; - } - - /** - * Host and port of the optional HTTP proxy - */ - public Builder proxy(@Nullable String proxyHost, int proxyPort) { - this.proxyHost = proxyHost; - this.proxyPort = proxyPort; - return this; - } - - public Builder proxyLogin(@Nullable String proxyLogin) { - this.proxyLogin = proxyLogin; - return this; - } - - public Builder proxyPassword(@Nullable String proxyPassword) { - this.proxyPassword = proxyPassword; - return this; - } - - /** - * Sets a specified timeout value, in milliseconds, to be used when opening HTTP connection. - * A timeout of zero is interpreted as an infinite timeout. Default value is {@link WsClient#DEFAULT_CONNECT_TIMEOUT_MILLISECONDS} - */ - public Builder connectTimeoutMilliseconds(int i) { - this.connectTimeoutMs = i; - return this; - } - - /** - * Sets the read timeout to a specified timeout, in milliseconds. - * A timeout of zero is interpreted as an infinite timeout. Default value is {@link WsClient#DEFAULT_READ_TIMEOUT_MILLISECONDS} - */ - public Builder readTimeoutMilliseconds(int i) { - this.readTimeoutMs = i; - return this; - } - - /** - * Build a new client - */ - public WsClient build() { - if (url == null || "".equals(url)) { - throw new IllegalStateException("Server URL must be set"); - } - return new WsClient(this); - } + public PermissionsWsClient permissionsClient() { + return this.permissionsWsClient; } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java new file mode 100644 index 00000000000..0f55b0dcc2a --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java @@ -0,0 +1,33 @@ +/* + * + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.sonarqube.ws.client; + +import com.google.protobuf.Message; +import com.google.protobuf.Parser; +import org.sonarqube.ws.client.WsRequest; + +public interface WsConnector { + String execute(WsRequest wsRequest); + + <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser); +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java index 9641e0de785..8fe8f9941fa 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java @@ -22,25 +22,37 @@ package org.sonarqube.ws.client; import java.util.HashMap; import java.util.Map; +import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkNotNull; +import static org.sonarqube.ws.client.WsRequest.Method.GET; +import static org.sonarqube.ws.client.WsRequest.Method.POST; public class WsRequest { private final Map<String, Object> params = new HashMap<>(); private Method method = Method.GET; private MediaType mimeType = MediaType.JSON; - private String url; + private String endpoint; - public WsRequest(String url) { - this.url = url; + private WsRequest(String endpoint) { + this.endpoint = endpoint; + } + + public static WsRequest newPostRequest(String endpoint) { + return new WsRequest(endpoint) + .setMethod(POST); + } + + public static WsRequest newGetRequest(String endpoint) { + return new WsRequest(endpoint) + .setMethod(GET); } public Method getMethod() { return method; } - public WsRequest setMethod(Method method) { - checkNotNull(method); + private WsRequest setMethod(Method method) { this.method = method; return this; } @@ -55,15 +67,19 @@ public class WsRequest { return this; } - public WsRequest setParam(String key, Object value) { - checkNotNull(key); - checkNotNull(value); - this.params.put(key, value); + public WsRequest setParam(String key, @Nullable Object value) { + checkNotNull(key, "a WS parameter key cannot be null"); + if (value != null) { + this.params.put(key, value); + } else { + this.params.remove(key); + } + return this; } - public String getUrl() { - return url; + public String getEndpoint() { + return endpoint; } public Map<String, Object> getParams() { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java new file mode 100644 index 00000000000..c84ebab181f --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java @@ -0,0 +1,22 @@ +package org.sonarqube.ws.client.permission; + +import org.sonarqube.ws.WsPermissions; +import org.sonarqube.ws.client.WsClient; + +import static org.sonarqube.ws.client.WsRequest.newGetRequest; + +public class PermissionsWsClient { + private final WsClient wsClient; + + public PermissionsWsClient(WsClient wsClient) { + this.wsClient = wsClient; + } + + public WsPermissions.WsGroupsResponse groups(WsGroupsRequest request) { + return wsClient.execute(newGetRequest("api/permissions/groups") + .setParam("permission", request.getPermission()) + .setParam("projectId", request.getProjectId()) + .setParam("projectKey", request.getProjectKey()), + WsPermissions.WsGroupsResponse.parser()); + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/WsGroupsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/WsGroupsRequest.java new file mode 100644 index 00000000000..e8d6da0a92f --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/WsGroupsRequest.java @@ -0,0 +1,34 @@ +package org.sonarqube.ws.client.permission; + +public class WsGroupsRequest { + private String permission; + private String projectId; + private String projectKey; + + public String getPermission() { + return permission; + } + + public WsGroupsRequest setPermission(String permission) { + this.permission = permission; + return this; + } + + public String getProjectId() { + return projectId; + } + + public WsGroupsRequest setProjectId(String projectId) { + this.projectId = projectId; + return this; + } + + public String getProjectKey() { + return projectKey; + } + + public WsGroupsRequest setProjectKey(String projectKey) { + this.projectKey = projectKey; + return this; + } +} diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java index 5007e4d827a..a256004a73c 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonarqube.ws.client.WsRequest.newGetRequest; public class HttpRequestFactoryTest { @Rule @@ -36,7 +37,7 @@ public class HttpRequestFactoryTest { httpServer.stubStatusCode(200).stubResponseBody("{'issues': []}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - String json = factory.execute(new WsRequest("/api/issues")); + String json = factory.execute(newGetRequest("/api/issues")); assertThat(json).isEqualTo("{'issues': []}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); @@ -48,7 +49,7 @@ public class HttpRequestFactoryTest { expectedException.expectMessage("Fail to request http://localhost:1/api/issues"); HttpRequestFactory factory = new HttpRequestFactory("http://localhost:1"); - factory.execute(new WsRequest("/api/issues")); + factory.execute(newGetRequest("/api/issues")); } @Test @@ -56,7 +57,7 @@ public class HttpRequestFactoryTest { httpServer.stubStatusCode(200).stubResponseBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie"); - String json = factory.execute(new WsRequest("/api/issues")); + String json = factory.execute(newGetRequest("/api/issues")); assertThat(json).isEqualTo("{}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); @@ -70,16 +71,16 @@ public class HttpRequestFactoryTest { HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()) .setProxyHost("localhost").setProxyPort(1) .setProxyLogin("john").setProxyPassword("smith"); - factory.execute(new WsRequest("/api/issues")); + factory.execute(newGetRequest("/api/issues")); } @Test public void beginning_slash_is_optional() throws Exception { HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - factory.execute(new WsRequest("api/foo")); + factory.execute(newGetRequest("api/foo")); assertThat(httpServer.requestedPath()).isEqualTo("/api/foo"); - factory.execute(new WsRequest("/api/bar")); + factory.execute(newGetRequest("/api/bar")); assertThat(httpServer.requestedPath()).isEqualTo("/api/bar"); } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java index 715d8a4cc76..1e0c65ba55d 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java @@ -21,16 +21,22 @@ package org.sonarqube.ws.client; import com.google.common.net.HttpHeaders; -import com.google.common.net.MediaType; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.WsComponents; +import org.sonarqube.ws.WsPermissions.WsGroupsResponse; +import org.sonarqube.ws.client.permission.WsGroupsRequest; import static java.net.HttpURLConnection.HTTP_OK; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonarqube.ws.client.HttpConnector.newDefaultHttpConnector; +import static org.sonarqube.ws.client.HttpConnector.newHttpConnector; +import static org.sonarqube.ws.client.WsRequest.newGetRequest; +import static org.sonarqube.ws.client.WsRequest.newPostRequest; public class WsClientTest { @Rule @@ -45,7 +51,7 @@ public class WsClientTest { server = new MockHttpServer(); server.start(); - underTest = WsClient.create("http://localhost:" + server.getPort()); + underTest = new WsClient(newDefaultHttpConnector("http://localhost:" + server.getPort())); } @After @@ -64,14 +70,16 @@ public class WsClientTest { .build() .toByteArray()); server.doReturnStatus(HTTP_OK); - server.doReturnContentType(MediaType.PROTOBUF.toString()); + server.doReturnContentType(MediaTypes.PROTOBUF); WsComponents.WsSearchResponse response = underTest.execute( - new WsRequest("api/components/search").setMediaType(WsRequest.MediaType.PROTOBUF), - WsComponents.WsSearchResponse.PARSER); + newGetRequest("api/components/search") + .setMediaType(WsRequest.MediaType.PROTOBUF), + WsComponents.WsSearchResponse.parser()); assertThat(response.getComponentsCount()).isEqualTo(1); - assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)).isEqualTo(MediaType.PROTOBUF.toString()); + assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)) + .isEqualTo(MediaTypes.PROTOBUF); } @Test @@ -79,12 +87,12 @@ public class WsClientTest { String expectedResponse = "{\"key\":value}"; server.doReturnBody(expectedResponse); server.doReturnStatus(HTTP_OK); - server.doReturnContentType(MediaType.JSON_UTF_8.toString()); + server.doReturnContentType(MediaTypes.JSON); - String response = underTest.execute(new WsRequest("api/components/search")); + String response = underTest.execute(newGetRequest("api/components/search")); assertThat(response).isEqualTo(expectedResponse); - assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)).isEqualTo(MediaType.JSON_UTF_8.toString()); + assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.JSON); } @Test @@ -92,7 +100,7 @@ public class WsClientTest { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Server URL must be set"); - WsClient.builder().build(); + new WsClient(newHttpConnector().build()); } @Test @@ -100,26 +108,28 @@ public class WsClientTest { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Server URL must be set"); - WsClient.create(""); + new WsClient(newDefaultHttpConnector("")); } @Test public void test_default_configuration() throws Exception { - underTest = WsClient.create("http://localhost:9000"); - assertThat(underTest.requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000"); - assertThat(underTest.requestFactory.getLogin()).isNull(); - assertThat(underTest.requestFactory.getPassword()).isNull(); - assertThat(underTest.requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(WsClient.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS); - assertThat(underTest.requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(WsClient.DEFAULT_READ_TIMEOUT_MILLISECONDS); - assertThat(underTest.requestFactory.getProxyHost()).isNull(); - assertThat(underTest.requestFactory.getProxyPort()).isEqualTo(0); - assertThat(underTest.requestFactory.getProxyLogin()).isNull(); - assertThat(underTest.requestFactory.getProxyPassword()).isNull(); + underTest = new WsClient(newDefaultHttpConnector("http://localhost:9000")); + + HttpRequestFactory requestFactory = ((HttpConnector) underTest.wsConnector).requestFactory; + assertThat(requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000"); + assertThat(requestFactory.getLogin()).isNull(); + assertThat(requestFactory.getPassword()).isNull(); + assertThat(requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(HttpConnector.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS); + assertThat(requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(HttpConnector.DEFAULT_READ_TIMEOUT_MILLISECONDS); + assertThat(requestFactory.getProxyHost()).isNull(); + assertThat(requestFactory.getProxyPort()).isEqualTo(0); + assertThat(requestFactory.getProxyLogin()).isNull(); + assertThat(requestFactory.getProxyPassword()).isNull(); } @Test public void test_custom_configuration() throws Exception { - underTest = WsClient.builder() + underTest = new WsClient(newHttpConnector() .url("http://localhost:9000") .login("eric") .password("pass") @@ -128,15 +138,46 @@ public class WsClientTest { .proxy("localhost", 2052) .proxyLogin("proxyLogin") .proxyPassword("proxyPass") - .build(); - assertThat(underTest.requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000"); - assertThat(underTest.requestFactory.getLogin()).isEqualTo("eric"); - assertThat(underTest.requestFactory.getPassword()).isEqualTo("pass"); - assertThat(underTest.requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(12345); - assertThat(underTest.requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(6789); - assertThat(underTest.requestFactory.getProxyHost()).isEqualTo("localhost"); - assertThat(underTest.requestFactory.getProxyPort()).isEqualTo(2052); - assertThat(underTest.requestFactory.getProxyLogin()).isEqualTo("proxyLogin"); - assertThat(underTest.requestFactory.getProxyPassword()).isEqualTo("proxyPass"); + .build()); + + HttpRequestFactory requestFactory = ((HttpConnector) underTest.wsConnector).requestFactory; + assertThat(requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000"); + assertThat(requestFactory.getLogin()).isEqualTo("eric"); + assertThat(requestFactory.getPassword()).isEqualTo("pass"); + assertThat(requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(12345); + assertThat(requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(6789); + assertThat(requestFactory.getProxyHost()).isEqualTo("localhost"); + assertThat(requestFactory.getProxyPort()).isEqualTo(2052); + assertThat(requestFactory.getProxyLogin()).isEqualTo("proxyLogin"); + assertThat(requestFactory.getProxyPassword()).isEqualTo("proxyPass"); + } + + @Test + public void contact_localhost() { + /** + * This is a temporary test to have a simple end-to-end test + * To make it work launch a default sonar server locally + */ + WsClient ws = new WsClient(newHttpConnector() + .url("http://localhost:9000") + .login("admin") + .password("admin") + .build()); + + // test with json response + String stringResponse = ws.execute(newGetRequest("api/webservices/list")); + assertThat(stringResponse).contains("webservices", "permissions"); + + // test with protobuf response + WsGroupsResponse protobufResponse = ws.execute(newPostRequest("api/permissions/groups") + .setMediaType(WsRequest.MediaType.PROTOBUF) + .setParam("permission", "admin"), + WsGroupsResponse.parser()); + assertThat(protobufResponse.getGroups(0).getName()).contains("sonar-administrator"); + + // test with specific client + WsGroupsResponse groupsResponse = ws.permissionsClient().groups(new WsGroupsRequest() + .setPermission("admin")); + assertThat(groupsResponse.getGroups(0).getName()).contains("sonar-administrator"); } } |