aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-11-25 10:54:56 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-11-30 19:01:45 +0100
commitbba16edb777a69137b3122dfbc5b898049f274c5 (patch)
tree17be034a5b93b109d2dd1148398225f77875e56b /sonar-ws
parent08dbeeee650d4792b6aaabb109ccd9504d282804 (diff)
downloadsonarqube-bba16edb777a69137b3122dfbc5b898049f274c5.tar.gz
sonarqube-bba16edb777a69137b3122dfbc5b898049f274c5.zip
SONAR-7054 use ws-client in batch
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/pom.xml44
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java3
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java75
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/BaseService.java63
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/GetRequest.java34
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java247
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java17
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java229
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpResponse.java90
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpWsClient.java89
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/MockWsResponse.java108
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/PostRequest.java (renamed from sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServerInterceptor.java)58
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java74
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java22
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/WsRequest.java73
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/WsResponse.java41
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ComputeEngineService.java44
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/ce/SubmitWsRequest.java71
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/ce/package-info.java25
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsService.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java)31
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssueFilterParameters.java2
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesWsClient.java)20
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java)123
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java2
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java)20
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensService.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsClient.java)42
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsParameters.java2
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java84
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java93
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java297
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java2
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java86
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServer.java128
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/PostRequestTest.java60
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java151
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/WsRequestTest.java80
-rw-r--r--sonar-ws/src/test/protobuf/ws-testing.proto29
37 files changed, 1580 insertions, 1079 deletions
diff --git a/sonar-ws/pom.xml b/sonar-ws/pom.xml
index 3409a153d02..457bfa77dbb 100644
--- a/sonar-ws/pom.xml
+++ b/sonar-ws/pom.xml
@@ -13,21 +13,31 @@
<name>SonarQube :: Web Service</name>
<description>Protocol Buffers specification of Web Services</description>
+ <properties>
+ <sonar.exclusions>target/generated-sources/**/*</sonar.exclusions>
+ <sonar.test.exclusions>target/generated-test-sources/**/*</sonar.test.exclusions>
+ </properties>
+
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.squareup.okhttp</groupId>
+ <artifactId>okhttp</artifactId>
+ <version>${okhttp.version}</version>
+ </dependency>
+ <dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
- <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
@@ -36,32 +46,14 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-testing-harness</artifactId>
+ <version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
+ <groupId>com.squareup.okhttp</groupId>
+ <artifactId>mockwebserver</artifactId>
+ <version>${okhttp.version}</version>
<scope>test</scope>
</dependency>
-
- <!-- Jetty dependencies -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-server</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>test-jetty-servlet</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.googlecode.json-simple</groupId>
- <artifactId>json-simple</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.kevinsawicki</groupId>
- <artifactId>http-request</artifactId>
- </dependency>
</dependencies>
</project>
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java b/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java
index ab3beddeca9..6947b77b59e 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java
@@ -27,7 +27,7 @@ import java.util.Locale;
import java.util.Map;
/**
- * @since 3.1
+ * @since 5.3
*/
public final class MediaTypes {
@@ -35,6 +35,7 @@ public final class MediaTypes {
public static final String XML = "application/xml";
public static final String TXT = "text/plain";
public static final String PROTOBUF = "application/x-protobuf";
+ public static final String ZIP = "application/zip";
public static final String DEFAULT = "application/octet-stream";
private static final Map<String, String> MAP = new ImmutableMap.Builder<String, String>()
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
new file mode 100644
index 00000000000..41c3339c92b
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
@@ -0,0 +1,75 @@
+/*
+ * 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 java.util.LinkedHashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.sonarqube.ws.MediaTypes;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static java.util.Objects.requireNonNull;
+
+abstract class BaseRequest<SELF extends BaseRequest> implements WsRequest {
+
+ private final String path;
+
+ private String mediaType = MediaTypes.JSON;
+
+ // keep the same order -> do not use HashMap
+ private final Map<String, String> params = new LinkedHashMap<>();
+
+ BaseRequest(String path) {
+ this.path = path;
+ }
+
+ @Override
+ public String getPath() {
+ return path;
+ }
+
+ @Override
+ public String getMediaType() {
+ return mediaType;
+ }
+
+ /**
+ * Expected media type of response. Default is {@link MediaTypes#JSON}.
+ */
+ public SELF setMediaType(String s) {
+ requireNonNull(s, "media type of response cannot be null");
+ this.mediaType = s;
+ return (SELF) this;
+ }
+
+ public SELF setParam(String key, @Nullable Object value) {
+ checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
+ if (value != null) {
+ this.params.put(key, value.toString());
+ }
+ return (SELF) this;
+ }
+
+ @Override
+ public Map<String, String> getParams() {
+ return params;
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseService.java
new file mode 100644
index 00000000000..11d4d006419
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseService.java
@@ -0,0 +1,63 @@
+/*
+ * 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 java.io.InputStream;
+import org.sonarqube.ws.MediaTypes;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Strings.isNullOrEmpty;
+
+public abstract class BaseService {
+
+ private final WsConnector wsConnector;
+ private final String controller;
+
+ public BaseService(WsConnector wsConnector, String controllerPath) {
+ checkArgument(!isNullOrEmpty(controllerPath));
+ this.wsConnector = wsConnector;
+ this.controller = controllerPath;
+ }
+
+ protected <T extends Message> T call(BaseRequest request, Parser<T> parser) {
+ request.setMediaType(MediaTypes.PROTOBUF);
+ WsResponse response = wsConnector.call(request);
+ return convert(response, parser);
+ }
+
+ protected WsResponse call(WsRequest request) {
+ return wsConnector.call(request);
+ }
+
+ public <T extends Message> T convert(WsResponse response, Parser<T> parser) {
+ try (InputStream byteStream = response.getContentStream()) {
+ // HTTP header "Content-Type" is not verified. It may be different than protobuf.
+ return parser.parseFrom(byteStream);
+ } catch (Exception e) {
+ throw new IllegalStateException("Fail to parse protobuf response of " + response.getRequestUrl(), e);
+ }
+ }
+
+ protected String path(String action) {
+ return String.format("%s/%s", controller, action);
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/GetRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/GetRequest.java
new file mode 100644
index 00000000000..b59903fd563
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/GetRequest.java
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * @since 5.3
+ */
+public class GetRequest extends BaseRequest<GetRequest> {
+ public GetRequest(String path) {
+ super(path);
+ }
+
+ @Override
+ public Method getMethod() {
+ return Method.GET;
+ }
+}
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
index 66ac3cc89cd..7f46d08cacb 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
@@ -17,73 +17,192 @@
* 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 com.squareup.okhttp.Call;
+import com.squareup.okhttp.Credentials;
+import com.squareup.okhttp.Headers;
+import com.squareup.okhttp.HttpUrl;
+import com.squareup.okhttp.Interceptor;
+import com.squareup.okhttp.MediaType;
+import com.squareup.okhttp.MultipartBuilder;
+import com.squareup.okhttp.OkHttpClient;
+import com.squareup.okhttp.Request;
+import com.squareup.okhttp.RequestBody;
+import com.squareup.okhttp.Response;
+import java.io.IOException;
+import java.net.Proxy;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static com.google.common.base.Strings.nullToEmpty;
+import static java.lang.String.format;
+
public class HttpConnector implements WsConnector {
- public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
- public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
+ public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30_000;
+ public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60_000;
/**
- * Visibility relaxed for unit tests
+ * Base URL with trailing slash, for instance "https://localhost/sonarqube/".
+ * It is required for further usage of {@link HttpUrl#resolve(String)}.
*/
- final HttpRequestFactory requestFactory;
+ private final HttpUrl baseUrl;
+ private final String userAgent;
+ private final String credentials;
+ private final String proxyCredentials;
+ private final OkHttpClient okHttpClient = new OkHttpClient();
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);
+ this.baseUrl = HttpUrl.parse(builder.url.endsWith("/") ? builder.url : format("%s/", builder.url));
+ this.userAgent = builder.userAgent;
+
+ if (isNullOrEmpty(builder.login)) {
+ // no login nor access token
+ this.credentials = null;
+ } else {
+ // password is null when login represents an access token. In this case
+ // the Basic credentials consider an empty password.
+ this.credentials = Credentials.basic(builder.login, nullToEmpty(builder.password));
+ }
+
+ if (builder.proxy != null) {
+ this.okHttpClient.setProxy(builder.proxy);
+ }
+ // proxy credentials can be used on system-wide proxies, so even if builder.proxy is null
+ if (isNullOrEmpty(builder.proxyLogin)) {
+ this.proxyCredentials = null;
+ } else {
+ this.proxyCredentials = Credentials.basic(builder.proxyLogin, nullToEmpty(builder.proxyPassword));
+ }
+
+ this.okHttpClient.setConnectTimeout(builder.connectTimeoutMs, TimeUnit.MILLISECONDS);
+ this.okHttpClient.setReadTimeout(builder.readTimeoutMs, TimeUnit.MILLISECONDS);
+ this.okHttpClient.interceptors().addAll(builder.interceptors);
}
@Override
- public String execute(WsRequest wsRequest) {
- return requestFactory.execute(wsRequest);
+ public String baseUrl() {
+ return baseUrl.url().toExternalForm();
+ }
+
+ public OkHttpClient okHttpClient() {
+ return okHttpClient;
+ }
+
+ @CheckForNull
+ public String userAgent() {
+ return userAgent;
+ }
+
+ @CheckForNull
+ public String credentials() {
+ return credentials;
}
@Override
- public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) {
- return requestFactory.execute(wsRequest, protobufParser);
+ public WsResponse call(WsRequest httpRequest) {
+ if (httpRequest instanceof GetRequest) {
+ return get((GetRequest) httpRequest);
+ }
+ if (httpRequest instanceof PostRequest) {
+ return post((PostRequest) httpRequest);
+ }
+ throw new IllegalArgumentException(format("Unsupported implementation: %s", httpRequest.getClass()));
}
- /**
- * Create a builder of {@link WsClient}s.
- */
- public static Builder newHttpConnector() {
- return new Builder();
+ private WsResponse get(GetRequest getRequest) {
+ HttpUrl.Builder urlBuilder = prepareUrlBuilder(getRequest);
+ Request.Builder okRequestBuilder = prepareOkRequestBuilder(getRequest, urlBuilder).get();
+ return doCall(okRequestBuilder.build());
}
- /**
- * 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();
+ private WsResponse post(PostRequest postRequest) {
+ HttpUrl.Builder urlBuilder = prepareUrlBuilder(postRequest);
+ Request.Builder okRequestBuilder = prepareOkRequestBuilder(postRequest, urlBuilder);
+
+ Map<String, PostRequest.Part> parts = postRequest.getParts();
+ if (parts.isEmpty()) {
+ okRequestBuilder.post(RequestBody.create(null, ""));
+ } else {
+ MultipartBuilder body = new MultipartBuilder().type(MultipartBuilder.FORM);
+ for (Map.Entry<String, PostRequest.Part> param : parts.entrySet()) {
+ PostRequest.Part part = param.getValue();
+ body.addPart(
+ Headers.of("Content-Disposition", format("form-data; name=\"%s\"", param.getKey())),
+ RequestBody.create(MediaType.parse(part.getMediaType()), part.getFile()));
+ }
+ okRequestBuilder.post(body.build());
+ }
+
+ return doCall(okRequestBuilder.build());
+ }
+
+ private HttpUrl.Builder prepareUrlBuilder(WsRequest wsRequest) {
+ String path = wsRequest.getPath();
+ HttpUrl.Builder urlBuilder = baseUrl
+ .resolve(path.startsWith("/") ? path.replaceAll("^(/)+", "") : path)
+ .newBuilder();
+ for (Map.Entry<String, String> param : wsRequest.getParams().entrySet()) {
+ urlBuilder.addQueryParameter(param.getKey(), param.getValue());
+ }
+ return urlBuilder;
+ }
+
+ private Request.Builder prepareOkRequestBuilder(WsRequest getRequest, HttpUrl.Builder urlBuilder) {
+ Request.Builder okHttpRequestBuilder = new Request.Builder()
+ .url(urlBuilder.build())
+ .addHeader("Accept", getRequest.getMediaType())
+ .addHeader("Accept-Charset", "UTF-8");
+ if (credentials != null) {
+ okHttpRequestBuilder.header("Authorization", credentials);
+ }
+ if (proxyCredentials != null) {
+ okHttpRequestBuilder.header("Proxy-Authorization", proxyCredentials);
+ }
+ if (userAgent != null) {
+ okHttpRequestBuilder.addHeader("User-Agent", userAgent);
+ }
+ return okHttpRequestBuilder;
+ }
+
+ private HttpResponse doCall(Request okRequest) {
+ Call call = okHttpClient.newCall(okRequest);
+ try {
+ Response okResponse = call.execute();
+ if (!okResponse.isSuccessful()) {
+ throw new HttpException(okRequest.urlString(), okResponse.code(), okResponse.message());
+ }
+ return new HttpResponse(okResponse);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to request " + okRequest.urlString(), e);
+ }
}
public static class Builder {
+ private String url;
+ private String userAgent;
private String login;
private String password;
- private String url;
- private String proxyHost;
+ private Proxy proxy;
private String proxyLogin;
private String proxyPassword;
- private int proxyPort = 0;
-
private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
private int readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
+ private final List<Interceptor> interceptors = new ArrayList<>();
- private Builder() {
+ /**
+ * Optional User Agent
+ */
+ public Builder userAgent(@Nullable String userAgent) {
+ this.userAgent = userAgent;
+ return this;
}
/**
@@ -95,43 +214,26 @@ public class HttpConnector implements WsConnector {
}
/**
- * Optional login, for example "admin"
+ * Optional login/password, for example "admin"
*/
- public Builder login(@Nullable String login) {
+ public Builder credentials(@Nullable String login, @Nullable String password) {
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
+ * Optional access token, for example {@code "ABCDE"}. Alternative to {@link #credentials(String, String)}
*/
- 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;
+ public Builder token(@Nullable String token) {
+ this.login = token;
+ this.password = null;
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}
+ * A timeout of zero is interpreted as an infinite timeout. Default value is {@link #DEFAULT_CONNECT_TIMEOUT_MILLISECONDS}
*/
public Builder connectTimeoutMilliseconds(int i) {
this.connectTimeoutMs = i;
@@ -140,20 +242,35 @@ public class HttpConnector implements WsConnector {
/**
* 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}
+ * A timeout of zero is interpreted as an infinite timeout. Default value is {@link #DEFAULT_READ_TIMEOUT_MILLISECONDS}
*/
public Builder readTimeoutMilliseconds(int i) {
this.readTimeoutMs = i;
return this;
}
+ public Builder proxy(@Nullable Proxy proxy) {
+ this.proxy = proxy;
+ return this;
+ }
+
+ public Builder proxyCredentials(@Nullable String proxyLogin, @Nullable String proxyPassword) {
+ this.proxyLogin = proxyLogin;
+ this.proxyPassword = proxyPassword;
+ return this;
+ }
+
/**
- * Build a new client
+ * Adds a OkHttp interceptor, for example to log request URLs or response errors.
+ * See https://github.com/square/okhttp/wiki/Interceptors
*/
+ public Builder interceptor(Interceptor interceptor) {
+ this.interceptors.add(interceptor);
+ return this;
+ }
+
public HttpConnector build() {
- if (url == null || "".equals(url)) {
- throw new IllegalStateException("Server URL must be set");
- }
+ checkArgument(!isNullOrEmpty(url), "Server URL is not defined");
return new HttpConnector(this);
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java
index 09aa68e5995..393c6232f89 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java
@@ -20,24 +20,27 @@
package org.sonarqube.ws.client;
/**
- * @since 3.6
+ * @since 5.3
*/
public class HttpException extends RuntimeException {
private final String url;
- private final int status;
+ private final int code;
- public HttpException(String url, int status, String message) {
- super(String.format("Error %d on %s : %s", status, url, message));
+ public HttpException(String url, int code, String message) {
+ super(String.format("Error %d on %s : %s", code, url, message));
this.url = url;
- this.status = status;
+ this.code = code;
}
public String url() {
return url;
}
- public int status() {
- return status;
+ /**
+ * @see java.net.HttpURLConnection constants
+ */
+ public int code() {
+ return code;
}
}
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
deleted file mode 100644
index cda4c549b74..00000000000
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * 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.github.kevinsawicki.http.HttpRequest;
-import com.google.common.base.Throwables;
-import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.Message;
-import com.google.protobuf.Parser;
-import java.io.InputStream;
-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;
-import static java.net.HttpURLConnection.HTTP_OK;
-
-/**
- * Not an API. Please do not use this class, except maybe for unit tests.
- */
-public class HttpRequestFactory {
-
- private static final int[] RESPONSE_SUCCESS = {HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT};
-
- private final String baseUrl;
- private String login;
- private String password;
- private String proxyHost;
- private String proxyLogin;
- private String proxyPassword;
- private int proxyPort;
- private int connectTimeoutInMilliseconds;
- private int readTimeoutInMilliseconds;
-
- public HttpRequestFactory(String baseUrl) {
- this.baseUrl = baseUrl;
- }
-
- public HttpRequestFactory setLogin(@Nullable String login) {
- this.login = login;
- return this;
- }
-
- public HttpRequestFactory setPassword(@Nullable String password) {
- this.password = password;
- return this;
- }
-
- public HttpRequestFactory setProxyHost(@Nullable String proxyHost) {
- this.proxyHost = proxyHost;
- return this;
- }
-
- public HttpRequestFactory setProxyLogin(@Nullable String proxyLogin) {
- this.proxyLogin = proxyLogin;
- return this;
- }
-
- public HttpRequestFactory setProxyPassword(@Nullable String proxyPassword) {
- this.proxyPassword = proxyPassword;
- return this;
- }
-
- public HttpRequestFactory setProxyPort(int proxyPort) {
- this.proxyPort = proxyPort;
- return this;
- }
-
- public HttpRequestFactory setConnectTimeoutInMilliseconds(int connectTimeoutInMilliseconds) {
- this.connectTimeoutInMilliseconds = connectTimeoutInMilliseconds;
- return this;
- }
-
- public HttpRequestFactory setReadTimeoutInMilliseconds(int readTimeoutInMilliseconds) {
- this.readTimeoutInMilliseconds = readTimeoutInMilliseconds;
- return this;
- }
-
- public String getBaseUrl() {
- return baseUrl;
- }
-
- public String getLogin() {
- return login;
- }
-
- public String getPassword() {
- return password;
- }
-
- public String getProxyHost() {
- return proxyHost;
- }
-
- public String getProxyLogin() {
- return proxyLogin;
- }
-
- public String getProxyPassword() {
- return proxyPassword;
- }
-
- public int getProxyPort() {
- return proxyPort;
- }
-
- public int getConnectTimeoutInMilliseconds() {
- return connectTimeoutInMilliseconds;
- }
-
- public int getReadTimeoutInMilliseconds() {
- return readTimeoutInMilliseconds;
- }
-
- public String execute(WsRequest wsRequest) {
- HttpRequest httpRequest = wsRequestToHttpRequest(wsRequest);
- return execute(httpRequest);
- }
-
- public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) {
- HttpRequest httpRequest = wsRequestToHttpRequest(wsRequest);
- InputStream response = executeWithStream(httpRequest);
- try {
- return protobufParser.parseFrom(response);
- } catch (InvalidProtocolBufferException e) {
- Throwables.propagate(e);
- }
-
- throw new IllegalStateException("Uncatched exception when parsing protobuf response");
- }
-
- private HttpRequest wsRequestToHttpRequest(WsRequest wsRequest) {
- HttpRequest httpRequest = WsRequest.Method.GET.equals(wsRequest.getMethod())
- ? HttpRequest.get(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true)
- : HttpRequest.post(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true);
- httpRequest = prepare(httpRequest);
- switch (wsRequest.getMediaType()) {
- case PROTOBUF:
- httpRequest.accept(MediaTypes.PROTOBUF);
- break;
- case JSON:
- httpRequest.accept(MediaTypes.JSON);
- break;
- case TEXT:
- httpRequest.accept(MediaTypes.TXT);
- break;
- default:
- httpRequest.accept(MediaTypes.DEFAULT);
- break;
- }
-
- return httpRequest;
- }
-
- private String buildUrl(String part) {
- StringBuilder url = new StringBuilder();
- url.append(baseUrl);
- if (!part.startsWith("/")) {
- url.append('/');
- }
- url.append(part);
- return url.toString();
- }
-
- private static String execute(HttpRequest request) {
- try {
- checkSuccess(request);
- return request.body(HttpRequest.CHARSET_UTF8);
- } catch (HttpRequest.HttpRequestException e) {
- throw new IllegalStateException("Fail to request " + request.url(), e);
- }
- }
-
- private static InputStream executeWithStream(HttpRequest request) {
- try {
- checkSuccess(request);
- return request.stream();
- } catch (HttpRequest.HttpRequestException e) {
- throw new IllegalStateException("Fail to request " + request.url(), e);
- }
- }
-
- private static void checkSuccess(HttpRequest request) {
- boolean isSuccess = Arrays.binarySearch(RESPONSE_SUCCESS, request.code()) >= 0;
- if (!isSuccess) {
- throw new HttpException(request.url().toString(), request.code(), request.body());
- }
- }
-
- private HttpRequest prepare(HttpRequest request) {
- if (proxyHost != null) {
- request.useProxy(proxyHost, proxyPort);
- if (proxyLogin != null) {
- request.proxyBasic(proxyLogin, proxyPassword);
- }
- }
- request
- .acceptGzipEncoding()
- .uncompress(true)
- .acceptJson()
- .acceptCharset(HttpRequest.CHARSET_UTF8)
- .connectTimeout(connectTimeoutInMilliseconds)
- .readTimeout(readTimeoutInMilliseconds)
- .trustAllCerts()
- .trustAllHosts();
- if (login != null) {
- request.basic(login, password);
- }
- return request;
- }
-}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpResponse.java
new file mode 100644
index 00000000000..b2e3f2766e4
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpResponse.java
@@ -0,0 +1,90 @@
+/*
+ * 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.squareup.okhttp.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
+
+class HttpResponse implements WsResponse {
+
+ private final Response okResponse;
+
+ HttpResponse(Response okResponse) {
+ this.okResponse = okResponse;
+ }
+
+ @Override
+ public String getRequestUrl() {
+ return okResponse.request().urlString();
+ }
+
+ @Override
+ public boolean hasContent() {
+ return okResponse.code() != HTTP_NO_CONTENT;
+ }
+
+ @Override
+ public String getContentType() {
+ return okResponse.header("Content-Type");
+ }
+
+ /**
+ * Get stream of bytes
+ */
+ @Override
+ public InputStream getContentStream() {
+ try {
+ return okResponse.body().byteStream();
+ } catch (IOException e) {
+ throw fail(e);
+ }
+ }
+
+ /**
+ * Get stream of characters, decoded with the charset
+ * of the Content-Type header. If that header is either absent or lacks a
+ * charset, this will attempt to decode the response body as UTF-8.
+ */
+ @Override
+ public Reader getContentReader() {
+ try {
+ return okResponse.body().charStream();
+ } catch (IOException e) {
+ throw fail(e);
+ }
+ }
+
+ @Override
+ public String getContent() {
+ try {
+ return okResponse.body().string();
+ } catch (IOException e) {
+ throw fail(e);
+ }
+ }
+
+ private RuntimeException fail(Exception e) {
+ throw new IllegalStateException("Fail to read response of " + getRequestUrl(), e);
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpWsClient.java
new file mode 100644
index 00000000000..e9e69a7e8b7
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpWsClient.java
@@ -0,0 +1,89 @@
+/*
+ * 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 org.sonarqube.ws.client.ce.ComputeEngineService;
+import org.sonarqube.ws.client.component.ComponentsService;
+import org.sonarqube.ws.client.issue.IssuesService;
+import org.sonarqube.ws.client.permission.PermissionsService;
+import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
+import org.sonarqube.ws.client.usertoken.UserTokensService;
+
+/**
+ * Entry point of the Java Client for SonarQube Web Services
+ *
+ * @since 5.3
+ */
+public class HttpWsClient implements WsClient {
+
+ private final ComputeEngineService ceWsClient;
+ private final PermissionsService permissionsService;
+ private final ComponentsService componentsService;
+ private final QualityProfilesService qualityProfilesService;
+ private final IssuesService issuesService;
+ private final UserTokensService userTokensService;
+ private final WsConnector wsConnector;
+
+ public HttpWsClient(WsConnector wsConnector) {
+ this.wsConnector = wsConnector;
+ this.ceWsClient = new ComputeEngineService(wsConnector);
+ this.permissionsService = new PermissionsService(wsConnector);
+ this.componentsService = new ComponentsService(wsConnector);
+ this.qualityProfilesService = new QualityProfilesService(wsConnector);
+ this.issuesService = new IssuesService(wsConnector);
+ this.userTokensService = new UserTokensService(wsConnector);
+ }
+
+ @Override
+ public WsConnector wsConnector() {
+ return wsConnector;
+ }
+
+ @Override
+ public PermissionsService permissions() {
+ return this.permissionsService;
+ }
+
+ @Override
+ public ComputeEngineService computeEngine() {
+ return ceWsClient;
+ }
+
+ @Override
+ public ComponentsService components() {
+ return componentsService;
+ }
+
+ @Override
+ public QualityProfilesService qualityProfiles() {
+ return qualityProfilesService;
+ }
+
+ @Override
+ public IssuesService issues() {
+ return issuesService;
+ }
+
+ @Override
+ public UserTokensService userTokens() {
+ return userTokensService;
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/MockWsResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/MockWsResponse.java
new file mode 100644
index 00000000000..00411fb8601
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/MockWsResponse.java
@@ -0,0 +1,108 @@
+/*
+ * 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.common.base.Throwables;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.IOUtils;
+import org.sonarqube.ws.MediaTypes;
+
+import static java.util.Objects.requireNonNull;
+
+public class MockWsResponse implements WsResponse {
+
+ private String requestUrl;
+ private byte[] content;
+ private String contentType;
+
+ @Override
+ public String getContentType() {
+ requireNonNull(contentType);
+ return contentType;
+ }
+
+ public MockWsResponse setContentType(String contentType) {
+ this.contentType = contentType;
+ return this;
+ }
+
+ public MockWsResponse setRequestUrl(String requestUrl) {
+ this.requestUrl = requestUrl;
+ return this;
+ }
+
+ public MockWsResponse setContent(InputStream is) {
+ try {
+ return setContent(IOUtils.toByteArray(is));
+ } catch (IOException e) {
+ throw Throwables.propagate(e);
+ }
+ }
+
+ public MockWsResponse setContent(byte[] b) {
+ this.content = b;
+ return this;
+ }
+
+ public MockWsResponse setContent(String s) {
+ this.content = s.getBytes(StandardCharsets.UTF_8);
+ return this;
+ }
+
+ @Override
+ public boolean hasContent() {
+ return content != null;
+ }
+
+ @Override
+ public String getRequestUrl() {
+ requireNonNull(requestUrl);
+ return requestUrl;
+ }
+
+ @Override
+ public InputStream getContentStream() {
+ requireNonNull(content);
+ return new ByteArrayInputStream(content);
+ }
+
+ @Override
+ public Reader getContentReader() {
+ requireNonNull(content);
+ return new StringReader(new String(content, StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public String getContent() {
+ requireNonNull(content);
+ return new String(content, StandardCharsets.UTF_8);
+ }
+
+ public static MockWsResponse createJson(String json) {
+ return new MockWsResponse()
+ .setContentType(MediaTypes.JSON)
+ .setContentType(json);
+ }
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServerInterceptor.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/PostRequest.java
index cd361262709..0c003894da2 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServerInterceptor.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/PostRequest.java
@@ -17,55 +17,53 @@
* 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 org.junit.rules.ExternalResource;
-
+import java.io.File;
+import java.util.LinkedHashMap;
import java.util.Map;
-public final class MockHttpServerInterceptor extends ExternalResource {
+/**
+ * @since 5.3
+ */
+public class PostRequest extends BaseRequest<PostRequest> {
- private MockHttpServer server;
+ private final Map<String, Part> parts = new LinkedHashMap<>();
- @Override
- protected final void before() throws Throwable {
- server = new MockHttpServer();
- server.start();
+ public PostRequest(String path) {
+ super(path);
}
@Override
- protected void after() {
- server.stop();
+ public Method getMethod() {
+ return Method.POST;
}
- public MockHttpServerInterceptor stubResponseBody(String body) {
- server.doReturnBody(body);
+ public PostRequest setPart(String name, Part part) {
+ this.parts.put(name, part);
return this;
}
- public MockHttpServerInterceptor stubStatusCode(int status) {
- server.doReturnStatus(status);
- return this;
+ public Map<String, Part> getParts() {
+ return parts;
}
- public String requestedPath() {
- return server.requestPath();
- }
+ public static class Part {
+ private final String mediaType;
+ private final File file;
- public Map requestHeaders() {
- return server.requestHeaders();
- }
+ public Part(String mediaType, File file) {
+ this.mediaType = mediaType;
+ this.file = file;
+ }
- public Map requestParams() {
- return server.requestParams();
- }
+ public String getMediaType() {
+ return mediaType;
+ }
- public int port() {
- return server.getPort();
+ public File getFile() {
+ return file;
+ }
}
- public String url() {
- return "http://localhost:" + port();
- }
}
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 39886b57a61..4b125627b5b 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
@@ -17,74 +17,30 @@
* 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.common.annotations.VisibleForTesting;
-import com.google.protobuf.Message;
-import com.google.protobuf.Parser;
-import org.sonarqube.ws.client.component.ComponentsWsClient;
-import org.sonarqube.ws.client.issue.IssuesWsClient;
-import org.sonarqube.ws.client.permission.PermissionsWsClient;
-import org.sonarqube.ws.client.qualityprofile.QualityProfilesWsClient;
-import org.sonarqube.ws.client.usertoken.UserTokensWsClient;
-
-import static org.sonarqube.ws.client.WsRequest.MediaType.PROTOBUF;
+import org.sonarqube.ws.client.ce.ComputeEngineService;
+import org.sonarqube.ws.client.component.ComponentsService;
+import org.sonarqube.ws.client.issue.IssuesService;
+import org.sonarqube.ws.client.permission.PermissionsService;
+import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
+import org.sonarqube.ws.client.usertoken.UserTokensService;
/**
- * Entry point of the Java Client for SonarQube Web Services.
- * <p/>
- * Example:
- * <pre>
- * WsClient client = new WsClient(Connector);
- * </pre>
- *
- * @since 5.2
+ * @since 5.3
*/
-public class WsClient {
-
- @VisibleForTesting
- final WsConnector wsConnector;
- private final PermissionsWsClient permissionsWsClient;
- private final ComponentsWsClient componentsWsClient;
- private final QualityProfilesWsClient qualityProfilesWsClient;
- private final IssuesWsClient issuesWsClient;
- private final UserTokensWsClient userTokensWsClient;
-
- public WsClient(WsConnector wsConnector) {
- this.wsConnector = wsConnector;
- this.permissionsWsClient = new PermissionsWsClient(this);
- this.componentsWsClient = new ComponentsWsClient(this);
- this.qualityProfilesWsClient = new QualityProfilesWsClient(this);
- this.issuesWsClient = new IssuesWsClient(this);
- userTokensWsClient = new UserTokensWsClient(this);
- }
-
- public String execute(WsRequest wsRequest) {
- return wsConnector.execute(wsRequest);
- }
+public interface WsClient {
+ ComponentsService components();
- public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) {
- return wsConnector.execute(wsRequest.setMediaType(PROTOBUF), protobufParser);
- }
+ ComputeEngineService computeEngine();
- public PermissionsWsClient permissionsClient() {
- return this.permissionsWsClient;
- }
+ IssuesService issues();
- public ComponentsWsClient componentsWsClient() {
- return componentsWsClient;
- }
+ PermissionsService permissions();
- public QualityProfilesWsClient qualityProfilesWsClient() {
- return qualityProfilesWsClient;
- }
+ QualityProfilesService qualityProfiles();
- public IssuesWsClient issuesWsClient() {
- return issuesWsClient;
- }
+ UserTokensService userTokens();
- public UserTokensWsClient userTokensWsClient() {
- return userTokensWsClient;
- }
+ WsConnector wsConnector();
}
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
index c4f282744ed..d7754d598fc 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsConnector.java
@@ -20,11 +20,23 @@
package org.sonarqube.ws.client;
-import com.google.protobuf.Message;
-import com.google.protobuf.Parser;
-
+/**
+ * @since 5.3
+ */
public interface WsConnector {
- String execute(WsRequest wsRequest);
- <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser);
+ /**
+ * @throws IllegalStateException if the request could not be executed due to
+ * a connectivity problem or timeout. Because networks can
+ * fail during an exchange, it is possible that the remote server
+ * accepted the request before the failure
+ * @throws HttpException if the response code is not in range [200..300)
+ */
+ WsResponse call(WsRequest wsRequest);
+
+ /**
+ * Server base URL, always with trailing slash, for instance "http://localhost:9000/"
+ */
+ String baseUrl();
+
}
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 9e4a91f13c4..dc635e963c7 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
@@ -20,77 +20,22 @@
package org.sonarqube.ws.client;
-import java.util.HashMap;
import java.util.Map;
-import javax.annotation.Nullable;
-import static java.util.Objects.requireNonNull;
-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 endpoint;
-
- 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;
- }
-
- private WsRequest setMethod(Method method) {
- this.method = method;
- return this;
- }
-
- public MediaType getMediaType() {
- return mimeType;
- }
-
- public WsRequest setMediaType(MediaType type) {
- requireNonNull(type);
- this.mimeType = type;
- return this;
- }
+/**
+ * @since 5.3
+ */
+public interface WsRequest {
- public WsRequest setParam(String key, @Nullable Object value) {
- requireNonNull(key, "a WS parameter key cannot be null");
- if (value != null) {
- this.params.put(key, value);
- } else {
- this.params.remove(key);
- }
+ Method getMethod();
- return this;
- }
+ String getPath();
- public String getEndpoint() {
- return endpoint;
- }
+ String getMediaType();
- public Map<String, Object> getParams() {
- return params;
- }
+ Map<String, String> getParams();
- public enum Method {
+ enum Method {
GET, POST
}
-
- public enum MediaType {
- PROTOBUF, JSON, TEXT
- }
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsResponse.java
new file mode 100644
index 00000000000..c6550ffb44a
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsResponse.java
@@ -0,0 +1,41 @@
+/*
+ * 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 java.io.InputStream;
+import java.io.Reader;
+
+/**
+ * @since 5.3
+ */
+public interface WsResponse {
+
+ boolean hasContent();
+
+ String getContentType();
+
+ String getRequestUrl();
+
+ InputStream getContentStream();
+
+ Reader getContentReader();
+
+ String getContent();
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ComputeEngineService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ComputeEngineService.java
new file mode 100644
index 00000000000..465b96dc7b9
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ComputeEngineService.java
@@ -0,0 +1,44 @@
+/*
+ * 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.ce;
+
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsCe;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.WsConnector;
+
+public class ComputeEngineService extends BaseService {
+
+ public ComputeEngineService(WsConnector wsConnector) {
+ super(wsConnector, "api/ce");
+ }
+
+ public WsCe.SubmitResponse submit(SubmitWsRequest request) {
+ PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, request.getReport());
+ PostRequest post = new PostRequest(path("submit"))
+ .setParam("projectKey", request.getProjectKey())
+ .setParam("projectName", request.getProjectName())
+ .setParam("projectBranch", request.getProjectBranch())
+ .setPart("report", filePart);
+ return call(post, WsCe.SubmitResponse.parser());
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/SubmitWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/SubmitWsRequest.java
new file mode 100644
index 00000000000..af414ee6fcc
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/SubmitWsRequest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.ce;
+
+import java.io.File;
+import javax.annotation.CheckForNull;
+
+public class SubmitWsRequest {
+
+ private String projectKey;
+ private String projectName;
+ private String projectBranch;
+ private File report;
+
+ public String getProjectKey() {
+ return projectKey;
+ }
+
+ public SubmitWsRequest setProjectKey(String projectKey) {
+ this.projectKey = projectKey;
+ return this;
+ }
+
+ @CheckForNull
+ public String getProjectName() {
+ return projectName;
+ }
+
+ public SubmitWsRequest setProjectName(String projectName) {
+ this.projectName = projectName;
+ return this;
+ }
+
+ @CheckForNull
+ public String getProjectBranch() {
+ return projectBranch;
+ }
+
+ public SubmitWsRequest setProjectBranch(String projectBranch) {
+ this.projectBranch = projectBranch;
+ return this;
+ }
+
+ @CheckForNull
+ public File getReport() {
+ return report;
+ }
+
+ public SubmitWsRequest setReport(File report) {
+ this.report = report;
+ return this;
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/package-info.java
new file mode 100644
index 00000000000..8b9789bcf15
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonarqube.ws.client.ce;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsService.java
index 1b081fe6ebe..fcee519bdb0 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsService.java
@@ -21,29 +21,22 @@
package org.sonarqube.ws.client.component;
import org.sonarqube.ws.WsComponents.SearchWsResponse;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
+public class ComponentsService extends BaseService {
-public class ComponentsWsClient {
- private static final String ENDPOINT = "api/components/";
- private final WsClient wsClient;
-
- public ComponentsWsClient(WsClient wsClient) {
- this.wsClient = wsClient;
+ public ComponentsService(WsConnector wsConnector) {
+ super(wsConnector, "api/components");
}
public SearchWsResponse search(SearchWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("search"))
- .setParam("qualifiers", request.getQualifiers())
- .setParam("p", request.getPage())
- .setParam("ps", request.getPageSize())
- .setParam("q", request.getQuery()),
- SearchWsResponse.parser());
- }
-
- private static String action(String action) {
- return ENDPOINT + action;
+ GetRequest get = new GetRequest(path("search"))
+ .setParam("qualifiers", request.getQualifiers())
+ .setParam("p", request.getPage())
+ .setParam("ps", request.getPageSize())
+ .setParam("q", request.getQuery());
+ return call(get, SearchWsResponse.parser());
}
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssueFilterParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssueFilterParameters.java
index 8fe92d8820f..f28bc23033d 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssueFilterParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssueFilterParameters.java
@@ -26,7 +26,7 @@ import com.google.common.collect.Iterables;
import java.util.List;
/**
- * @since 3.7
+ * @since 5.3
*/
public class IssueFilterParameters {
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java
index 70708974382..343481ddbe4 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesWsClient.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java
@@ -25,9 +25,10 @@ import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonarqube.ws.Issues.SearchWsResponse;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.ACTION_PLANS;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.ADDITIONAL_FIELDS;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.ASC;
@@ -62,17 +63,16 @@ import static org.sonarqube.ws.client.issue.IssueFilterParameters.SEVERITIES;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.STATUSES;
import static org.sonarqube.ws.client.issue.IssueFilterParameters.TAGS;
-public class IssuesWsClient {
+public class IssuesService extends BaseService {
private static final Joiner LIST_TO_PARAMS_STRING = Joiner.on(",").skipNulls();
- private final WsClient wsClient;
- public IssuesWsClient(WsClient wsClient) {
- this.wsClient = wsClient;
+ public IssuesService(WsConnector wsConnector) {
+ super(wsConnector, "api/issues");
}
public SearchWsResponse search(SearchWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("search"))
+ return call(
+ new GetRequest(path("search"))
.setParam(ACTION_PLANS, listToParamList(request.getActionPlans()))
.setParam(ADDITIONAL_FIELDS, listToParamList(request.getAdditionalFields()))
.setParam(ASC, request.getAsc())
@@ -113,10 +113,6 @@ public class IssuesWsClient {
SearchWsResponse.parser());
}
- private static String action(String action) {
- return "api/issues/" + action;
- }
-
@CheckForNull
private static String listToParamList(@Nullable List<String> strings) {
return strings == null
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/PermissionsService.java
index bb14ee02718..89f18f852ca 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsService.java
@@ -27,10 +27,11 @@ import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
import org.sonarqube.ws.WsPermissions.UpdateTemplateWsResponse;
import org.sonarqube.ws.WsPermissions.UsersWsResponse;
import org.sonarqube.ws.WsPermissions.WsSearchGlobalPermissionsResponse;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
-import static org.sonarqube.ws.client.WsRequest.newPostRequest;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
@@ -45,27 +46,26 @@ import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_T
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
-public class PermissionsWsClient {
- private final WsClient wsClient;
+public class PermissionsService extends BaseService {
- public PermissionsWsClient(WsClient wsClient) {
- this.wsClient = wsClient;
+ public PermissionsService(WsConnector wsConnector) {
+ super(wsConnector, PermissionsWsParameters.CONTROLLER);
}
public WsPermissions.WsGroupsResponse groups(GroupsWsRequest request) {
- return wsClient.execute(newGetRequest(action("groups"))
+ GetRequest get = new GetRequest(path("groups"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
.setParam("p", request.getPage())
.setParam("ps", request.getPageSize())
.setParam("selected", request.getSelected())
- .setParam("q", request.getQuery()),
- WsPermissions.WsGroupsResponse.parser());
+ .setParam("q", request.getQuery());
+ return call(get, WsPermissions.WsGroupsResponse.parser());
}
public void addGroup(AddGroupWsRequest request) {
- wsClient.execute(newPostRequest(action("add_group"))
+ call(new PostRequest(path("add_group"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
@@ -74,7 +74,7 @@ public class PermissionsWsClient {
}
public void addGroupToTemplate(AddGroupToTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("add_group_to_template"))
+ call(new PostRequest(path("add_group_to_template"))
.setParam(PARAM_GROUP_ID, request.getGroupId())
.setParam(PARAM_GROUP_NAME, request.getGroupName())
.setParam(PARAM_PERMISSION, request.getPermission())
@@ -83,7 +83,7 @@ public class PermissionsWsClient {
}
public void addUser(AddUserWsRequest request) {
- wsClient.execute(newPostRequest(action("add_user"))
+ call(new PostRequest(path("add_user"))
.setParam(PARAM_USER_LOGIN, request.getLogin())
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
@@ -91,7 +91,7 @@ public class PermissionsWsClient {
}
public void addUserToTemplate(AddUserToTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("add_user_to_template"))
+ call(new PostRequest(path("add_user_to_template"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_USER_LOGIN, request.getLogin())
.setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
@@ -99,7 +99,7 @@ public class PermissionsWsClient {
}
public void applyTemplate(ApplyTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("apply_template"))
+ call(new PostRequest(path("apply_template"))
.setParam(PARAM_PROJECT_ID, request.getProjectId())
.setParam(PARAM_PROJECT_KEY, request.getProjectKey())
.setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
@@ -107,22 +107,21 @@ public class PermissionsWsClient {
}
public CreateTemplateWsResponse createTemplate(CreateTemplateWsRequest request) {
- return wsClient.execute(newPostRequest(
- action("create_template"))
- .setParam(PARAM_NAME, request.getName())
- .setParam(PARAM_DESCRIPTION, request.getDescription())
- .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()),
- CreateTemplateWsResponse.parser());
+ PostRequest post = new PostRequest(path("create_template"))
+ .setParam(PARAM_NAME, request.getName())
+ .setParam(PARAM_DESCRIPTION, request.getDescription())
+ .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern());
+ return call(post, CreateTemplateWsResponse.parser());
}
public void deleteTemplate(DeleteTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("delete_template"))
+ call(new PostRequest(path("delete_template"))
.setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
.setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
}
public void removeGroup(RemoveGroupWsRequest request) {
- wsClient.execute(newPostRequest(action("remove_group"))
+ call(new PostRequest(path("remove_group"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_GROUP_ID, request.getGroupId())
.setParam(PARAM_GROUP_NAME, request.getGroupName())
@@ -131,7 +130,7 @@ public class PermissionsWsClient {
}
public void removeGroupFromTemplate(RemoveGroupFromTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("remove_group_from_template"))
+ call(new PostRequest(path("remove_group_from_template"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_GROUP_ID, request.getGroupId())
.setParam(PARAM_GROUP_NAME, request.getGroupName())
@@ -140,7 +139,7 @@ public class PermissionsWsClient {
}
public void removeUser(RemoveUserWsRequest request) {
- wsClient.execute(newPostRequest(action("remove_user"))
+ call(new PostRequest(path("remove_user"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_USER_LOGIN, request.getLogin())
.setParam(PARAM_PROJECT_ID, request.getProjectId())
@@ -148,7 +147,7 @@ public class PermissionsWsClient {
}
public void removeUserFromTemplate(RemoveUserFromTemplateWsRequest request) {
- wsClient.execute(newPostRequest(action("remove_user_from_template"))
+ call(new PostRequest(path("remove_user_from_template"))
.setParam(PARAM_PERMISSION, request.getPermission())
.setParam(PARAM_USER_LOGIN, request.getLogin())
.setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
@@ -156,62 +155,50 @@ public class PermissionsWsClient {
}
public WsSearchGlobalPermissionsResponse searchGlobalPermissions() {
- return wsClient.execute(
- newGetRequest(action("search_global_permissions")),
- WsSearchGlobalPermissionsResponse.parser());
+ GetRequest get = new GetRequest(path("search_global_permissions"));
+ return call(get, WsSearchGlobalPermissionsResponse.parser());
}
public SearchProjectPermissionsWsResponse searchProjectPermissions(SearchProjectPermissionsWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("search_project_permissions"))
- .setParam(PARAM_PROJECT_ID, request.getProjectId())
- .setParam(PARAM_PROJECT_KEY, request.getProjectKey())
- .setParam(PARAM_QUALIFIER, request.getQualifier())
- .setParam("p", request.getPage())
- .setParam("ps", request.getPageSize())
- .setParam("q", request.getQuery()),
- SearchProjectPermissionsWsResponse.parser());
+ GetRequest get = new GetRequest(path("search_project_permissions"))
+ .setParam(PARAM_PROJECT_ID, request.getProjectId())
+ .setParam(PARAM_PROJECT_KEY, request.getProjectKey())
+ .setParam(PARAM_QUALIFIER, request.getQualifier())
+ .setParam("p", request.getPage())
+ .setParam("ps", request.getPageSize())
+ .setParam("q", request.getQuery());
+ return call(get, SearchProjectPermissionsWsResponse.parser());
}
public SearchTemplatesWsResponse searchTemplates(SearchTemplatesWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("search_templates"))
- .setParam("q", request.getQuery()),
- SearchTemplatesWsResponse.parser());
+ GetRequest get = new GetRequest(path("search_templates"))
+ .setParam("q", request.getQuery());
+ return call(get, SearchTemplatesWsResponse.parser());
}
public void setDefaultTemplate(SetDefaultTemplateWsRequest request) {
- wsClient.execute(
- newPostRequest(action("set_default_template"))
- .setParam(PARAM_QUALIFIER, request.getQualifier())
- .setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
- .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
+ call(new PostRequest(path("set_default_template"))
+ .setParam(PARAM_QUALIFIER, request.getQualifier())
+ .setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
+ .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
}
public UpdateTemplateWsResponse updateTemplate(UpdateTemplateWsRequest request) {
- return wsClient.execute(
- newPostRequest(action("update_template"))
- .setParam(PARAM_DESCRIPTION, request.getDescription())
- .setParam(PARAM_ID, request.getId())
- .setParam(PARAM_NAME, request.getName())
- .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()),
- UpdateTemplateWsResponse.parser());
+ return call(new PostRequest(path("update_template"))
+ .setParam(PARAM_DESCRIPTION, request.getDescription())
+ .setParam(PARAM_ID, request.getId())
+ .setParam(PARAM_NAME, request.getName())
+ .setParam(PARAM_PROJECT_KEY_PATTERN, request.getProjectKeyPattern()), UpdateTemplateWsResponse.parser());
}
public UsersWsResponse users(UsersWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("users"))
- .setParam(PARAM_PERMISSION, request.getPermission())
- .setParam(PARAM_PROJECT_ID, request.getProjectId())
- .setParam(PARAM_PROJECT_KEY, request.getProjectKey())
- .setParam("selected", request.getSelected())
- .setParam("p", request.getPage())
- .setParam("ps", request.getPageSize())
- .setParam("q", request.getQuery()),
- UsersWsResponse.parser());
- }
-
- private static String action(String action) {
- return PermissionsWsParameters.ENDPOINT + "/" + action;
+ return call(new GetRequest(path("users"))
+ .setParam(PARAM_PERMISSION, request.getPermission())
+ .setParam(PARAM_PROJECT_ID, request.getProjectId())
+ .setParam(PARAM_PROJECT_KEY, request.getProjectKey())
+ .setParam("selected", request.getSelected())
+ .setParam("p", request.getPage())
+ .setParam("ps", request.getPageSize())
+ .setParam("q", request.getQuery()), UsersWsResponse.parser());
}
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java
index 1557e97b632..edd9f6b3a4a 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java
@@ -21,7 +21,7 @@
package org.sonarqube.ws.client.permission;
public class PermissionsWsParameters {
- public static final String ENDPOINT = "api/permissions";
+ public static final String CONTROLLER = "api/permissions";
public static final String PARAM_PERMISSION = "permission";
public static final String PARAM_GROUP_NAME = "groupName";
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
index dcbdec4951c..505ac3e8281 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
@@ -21,20 +21,19 @@
package org.sonarqube.ws.client.qualityprofile;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
+public class QualityProfilesService extends BaseService {
-public class QualityProfilesWsClient {
- private final WsClient wsClient;
-
- public QualityProfilesWsClient(WsClient wsClient) {
- this.wsClient = wsClient;
+ public QualityProfilesService(WsConnector wsConnector) {
+ super(wsConnector, "api/qualityprofiles");
}
public SearchWsResponse search(SearchWsRequest request) {
- return wsClient.execute(
- newGetRequest(action("search"))
+ return call(
+ new GetRequest(path("search"))
.setParam("defaults", request.getDefaults())
.setParam("language", request.getLanguage())
.setParam("profileName", request.getProfileName())
@@ -42,7 +41,4 @@ public class QualityProfilesWsClient {
SearchWsResponse.parser());
}
- private static String action(String action) {
- return "api/qualityprofiles/" + action;
- }
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensService.java
index fdd7eeca721..ce9a39d2211 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsClient.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensService.java
@@ -20,51 +20,27 @@
package org.sonarqube.ws.client.usertoken;
-import org.sonarqube.ws.WsComponents.SearchWsResponse;
import org.sonarqube.ws.WsUserTokens.GenerateWsResponse;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.WsConnector;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
-import static org.sonarqube.ws.client.WsRequest.newPostRequest;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_GENERATE;
-import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_REVOKE;
-import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.ACTION_SEARCH;
+import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_LOGIN;
import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.PARAM_NAME;
-import static org.sonarqube.ws.client.usertoken.UserTokensWsParameters.USER_TOKENS_ENDPOINT;
-public class UserTokensWsClient {
- private static final String SLASH = "/";
- private final WsClient wsClient;
+public class UserTokensService extends BaseService {
- public UserTokensWsClient(WsClient wsClient) {
- this.wsClient = wsClient;
+ public UserTokensService(WsConnector wsConnector) {
+ super(wsConnector, CONTROLLER);
}
public GenerateWsResponse generate(GenerateWsRequest request) {
- return wsClient.execute(
- newPostRequest(action(ACTION_GENERATE))
+ return call(
+ new PostRequest(path(ACTION_GENERATE))
.setParam(PARAM_LOGIN, request.getLogin())
.setParam(PARAM_NAME, request.getName()),
GenerateWsResponse.parser());
}
-
- public void revoke(RevokeWsRequest request) {
- wsClient.execute(
- newPostRequest(action(ACTION_REVOKE))
- .setParam(PARAM_LOGIN, request.getLogin())
- .setParam(PARAM_NAME, request.getName()));
- }
-
- public SearchWsResponse search(SearchWsRequest request) {
- return wsClient.execute(
- newGetRequest(action(ACTION_SEARCH))
- .setParam(PARAM_LOGIN, request.getLogin()),
- SearchWsResponse.parser()
- );
- }
-
- private static String action(String action) {
- return USER_TOKENS_ENDPOINT + SLASH + action;
- }
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsParameters.java
index 203ee5243a8..a82d6e0989c 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/usertoken/UserTokensWsParameters.java
@@ -21,7 +21,7 @@
package org.sonarqube.ws.client.usertoken;
public class UserTokensWsParameters {
- public static final String USER_TOKENS_ENDPOINT = "api/user_tokens";
+ public static final String CONTROLLER = "api/user_tokens";
public static final String ACTION_GENERATE = "generate";
public static final String ACTION_REVOKE = "revoke";
public static final String ACTION_SEARCH = "search";
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
new file mode 100644
index 00000000000..42fcf4705d1
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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 org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonarqube.ws.MediaTypes;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.data.MapEntry.entry;
+
+public class BaseRequestTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ FakeRequest underTest = new FakeRequest("api/foo");
+
+ @Test
+ public void test_defaults() {
+ assertThat(underTest.getMethod()).isEqualTo(WsRequest.Method.GET);
+ assertThat(underTest.getParams()).isEmpty();
+ assertThat(underTest.getMediaType()).isEqualTo(MediaTypes.JSON);
+ assertThat(underTest.getPath()).isEqualTo("api/foo");
+ }
+
+ @Test
+ public void setMediaType() {
+ underTest.setMediaType(MediaTypes.PROTOBUF);
+ assertThat(underTest.getMediaType()).isEqualTo(MediaTypes.PROTOBUF);
+ }
+
+ @Test
+ public void keep_order_of_params() {
+ assertThat(underTest.getParams()).isEmpty();
+
+ underTest.setParam("keyB", "b");
+ assertThat(underTest.getParams()).containsExactly(entry("keyB", "b"));
+
+ underTest.setParam("keyA", "a");
+ assertThat(underTest.getParams()).containsExactly(entry("keyB", "b"), entry("keyA", "a"));
+ }
+
+ @Test
+ public void null_param_value() {
+ underTest.setParam("key", null);
+ assertThat(underTest.getParams()).isEmpty();
+ }
+
+ @Test
+ public void fail_if_null_param_key() {
+ expectedException.expect(IllegalArgumentException.class);
+ underTest.setParam(null, "val");
+ }
+
+ private static class FakeRequest extends BaseRequest<FakeRequest> {
+ FakeRequest(String path) {
+ super(path);
+ }
+
+ @Override
+ public Method getMethod() {
+ return Method.GET;
+ }
+ }
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java
new file mode 100644
index 00000000000..2d92809cbd7
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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 java.io.IOException;
+import org.junit.Test;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.Testing;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class BaseServiceTest {
+
+ WsConnector wsConnector = mock(WsConnector.class);
+
+ @Test
+ public void test_call() throws Exception {
+ new BaseService(wsConnector, "api/issues") {
+
+ public void test() throws IOException {
+ GetRequest get = new GetRequest(path("issue")).setMediaType(MediaTypes.JSON);
+ when(wsConnector.call(get)).thenReturn(new MockWsResponse().setContent("ok"));
+
+ WsResponse response = call(get);
+
+ assertThat(response.getContent()).isEqualTo("ok");
+ }
+
+ }.test();
+ }
+
+ @Test
+ public void call_and_convert_protobuf() {
+ new BaseService(wsConnector, "api/issues") {
+
+ public void test() {
+ GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC");
+ when(wsConnector.call(get)).thenReturn(newProtobufFakeResponse());
+
+ Testing.Fake message = call(get, Testing.Fake.parser());
+
+ assertThat(message.getLabel()).isEqualTo("ok");
+ assertThat(get.getPath()).isEqualTo("api/issues/issue");
+ // media type automatically set to protobuf
+ assertThat(get.getMediaType()).isEqualTo(MediaTypes.PROTOBUF);
+ }
+
+ }.test();
+ }
+
+ @Test
+ public void fail_to_parse_protobuf_response() {
+ new BaseService(wsConnector, "api/issues") {
+
+ public void test() {
+ GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC");
+ when(wsConnector.call(get)).thenReturn(MockWsResponse.createJson("{}").setRequestUrl("http://local/api/issues/issue?key=ABC"));
+
+ try {
+ call(get, Testing.Fake.parser());
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Fail to parse protobuf response of http://local/api/issues/issue?key=ABC");
+ }
+ }
+ }.test();
+ }
+
+ private static WsResponse newProtobufFakeResponse() {
+ Testing.Fake message = Testing.Fake.newBuilder().setLabel("ok").build();
+ return new MockWsResponse().setContent(message.toByteArray());
+ }
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
new file mode 100644
index 00000000000..79362ea815f
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java
@@ -0,0 +1,297 @@
+/*
+ * 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.squareup.okhttp.Interceptor;
+import com.squareup.okhttp.Response;
+import com.squareup.okhttp.mockwebserver.MockResponse;
+import com.squareup.okhttp.mockwebserver.MockWebServer;
+import com.squareup.okhttp.mockwebserver.RecordedRequest;
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonarqube.ws.MediaTypes;
+
+import static com.squareup.okhttp.Credentials.basic;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+
+public class HttpConnectorTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ MockWebServer server;
+ String serverUrl;
+
+ @Before
+ public void setUp() throws Exception {
+ server = new MockWebServer();
+ server.start();
+ serverUrl = server.url("").url().toString();
+ }
+
+ @Test
+ public void test_default_settings() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl).build();
+ assertThat(underTest.baseUrl()).isEqualTo(serverUrl);
+ GetRequest request = new GetRequest("api/issues/search").setMediaType(MediaTypes.PROTOBUF);
+ WsResponse response = underTest.call(request);
+
+ // verify default timeouts on client
+ assertThat(underTest.okHttpClient().getConnectTimeout()).isEqualTo(HttpConnector.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS);
+ assertThat(underTest.okHttpClient().getReadTimeout()).isEqualTo(HttpConnector.DEFAULT_READ_TIMEOUT_MILLISECONDS);
+
+ // verify response
+ assertThat(response.hasContent()).isTrue();
+ assertThat(response.getContent()).isEqualTo("hello, world!");
+
+ // verify the request received by server
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getMethod()).isEqualTo("GET");
+ assertThat(recordedRequest.getPath()).isEqualTo("/api/issues/search");
+ assertThat(recordedRequest.getHeader("Accept")).isEqualTo(MediaTypes.PROTOBUF);
+ assertThat(recordedRequest.getHeader("Accept-Charset")).isEqualTo("UTF-8");
+ assertThat(recordedRequest.getHeader("User-Agent")).startsWith("okhttp/");
+ // compression is handled by OkHttp
+ assertThat(recordedRequest.getHeader("Accept-Encoding")).isEqualTo("gzip");
+ }
+
+ @Test
+ public void use_basic_authentication() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .credentials("theLogin", "thePassword")
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", "thePassword"));
+ }
+
+ @Test
+ public void use_basic_authentication_with_null_password() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .credentials("theLogin", null)
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", ""));
+ }
+
+ /**
+ * Access token replaces the couple {login,password} and is sent through
+ * the login field
+ */
+ @Test
+ public void use_access_token() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .token("theToken")
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theToken", ""));
+ }
+
+ @Test
+ public void use_proxy_authentication() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .proxyCredentials("theProxyLogin", "theProxyPassword")
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getHeader("Proxy-Authorization")).isEqualTo(basic("theProxyLogin", "theProxyPassword"));
+ }
+
+ @Test
+ public void override_timeouts() {
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .readTimeoutMilliseconds(42)
+ .connectTimeoutMilliseconds(74)
+ .build();
+
+ assertThat(underTest.okHttpClient().getReadTimeout()).isEqualTo(42);
+ assertThat(underTest.okHttpClient().getConnectTimeout()).isEqualTo(74);
+ }
+
+ @Test
+ public void send_user_agent() throws Exception {
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .userAgent("Maven Plugin/2.3")
+ .build();
+
+ underTest.call(new GetRequest("api/issues/search"));
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getHeader("User-Agent")).isEqualTo("Maven Plugin/2.3");
+ }
+
+ @Test
+ public void fail_if_unknown_implementation_of_request() {
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl).build();
+ try {
+ underTest.call(mock(WsRequest.class));
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessageContaining("Unsupported implementation: ");
+ }
+ }
+
+ @Test
+ public void send_post_request() throws Exception {
+ answerHelloWorld();
+ PostRequest request = new PostRequest("api/issues/search")
+ .setParam("severity", "MAJOR")
+ .setMediaType(MediaTypes.PROTOBUF);
+
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl).build();
+ WsResponse response = underTest.call(request);
+
+ // verify response
+ assertThat(response.hasContent()).isTrue();
+ assertThat(response.getContent()).isEqualTo("hello, world!");
+
+ // verify the request received by server
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getMethod()).isEqualTo("POST");
+ assertThat(recordedRequest.getPath()).isEqualTo("/api/issues/search?severity=MAJOR");
+ }
+
+ @Test
+ public void upload_file() throws Exception {
+ answerHelloWorld();
+ File reportFile = temp.newFile();
+ FileUtils.write(reportFile, "the report content");
+ PostRequest request = new PostRequest("api/report/upload")
+ .setParam("project", "theKey")
+ .setPart("report", new PostRequest.Part(MediaTypes.TXT, reportFile))
+ .setMediaType(MediaTypes.PROTOBUF);
+
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl).build();
+ WsResponse response = underTest.call(request);
+
+ assertThat(response.hasContent()).isTrue();
+ RecordedRequest recordedRequest = server.takeRequest();
+ assertThat(recordedRequest.getMethod()).isEqualTo("POST");
+ assertThat(recordedRequest.getPath()).isEqualTo("/api/report/upload?project=theKey");
+ String body = IOUtils.toString(recordedRequest.getBody().inputStream());
+ assertThat(body)
+ .contains("Content-Disposition: form-data; name=\"report\"")
+ .contains("Content-Type: text/plain")
+ .contains("the report content");
+ }
+
+ @Test
+ public void http_error() throws Exception {
+ server.enqueue(new MockResponse().setResponseCode(404));
+ PostRequest request = new PostRequest("api/issues/search");
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl).build();
+
+ try {
+ underTest.call(request);
+ fail();
+ } catch (HttpException e) {
+ assertThat(e.code()).isEqualTo(404);
+
+ }
+ }
+
+ @Test
+ public void intercept_request_and_response() {
+ final AtomicBoolean called = new AtomicBoolean(false);
+ Interceptor interceptor = new Interceptor() {
+ @Override
+ public Response intercept(Chain chain) throws IOException {
+ called.set(true);
+ return chain.proceed(chain.request());
+ }
+ };
+
+ answerHelloWorld();
+ HttpConnector underTest = new HttpConnector.Builder()
+ .url(serverUrl)
+ .interceptor(interceptor)
+ .build();
+ underTest.call(new GetRequest(""));
+
+ assertThat(called.get()).isTrue();
+ }
+
+ @Test
+ public void support_base_url_ending_with_slash() throws Exception {
+ assertThat(serverUrl).endsWith("/");
+ HttpConnector underTest = new HttpConnector.Builder().url(StringUtils.removeEnd(serverUrl, "/")).build();
+ GetRequest request = new GetRequest("api/issues/search");
+
+ answerHelloWorld();
+ WsResponse response = underTest.call(request);
+
+ assertThat(response.hasContent()).isTrue();
+ }
+
+ @Test
+ public void support_base_url_with_context() {
+ // just to be sure
+ assertThat(serverUrl).endsWith("/");
+ HttpConnector underTest = new HttpConnector.Builder().url(serverUrl + "sonar").build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ answerHelloWorld();
+ assertThat(underTest.call(request).getRequestUrl()).isEqualTo(serverUrl + "sonar/api/issues/search");
+
+ request = new GetRequest("/api/issues/search");
+ answerHelloWorld();
+ assertThat(underTest.call(request).getRequestUrl()).isEqualTo(serverUrl + "sonar/api/issues/search");
+ }
+
+ private void answerHelloWorld() {
+ server.enqueue(new MockResponse().setBody("hello, world!"));
+ }
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java
index 3eab476fec9..2db49ea358b 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java
@@ -27,7 +27,7 @@ public class HttpExceptionTest {
@Test
public void test_exception() throws Exception {
HttpException exception = new HttpException("http://localhost:9000/api/search", 500, "Not found");
- assertThat(exception.status()).isEqualTo(500);
+ assertThat(exception.code()).isEqualTo(500);
assertThat(exception.url()).isEqualTo("http://localhost:9000/api/search");
assertThat(exception.getMessage()).isEqualTo("Error 500 on http://localhost:9000/api/search : Not found");
}
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
deleted file mode 100644
index a256004a73c..00000000000
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpRequestFactoryTest.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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 org.junit.Rule;
-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
- public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Test
- public void test_get() {
- httpServer.stubStatusCode(200).stubResponseBody("{'issues': []}");
-
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
- String json = factory.execute(newGetRequest("/api/issues"));
-
- assertThat(json).isEqualTo("{'issues': []}");
- assertThat(httpServer.requestedPath()).isEqualTo("/api/issues");
- }
-
- @Test
- public void should_throw_illegal_state_exc_if_connect_exception() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Fail to request http://localhost:1/api/issues");
-
- HttpRequestFactory factory = new HttpRequestFactory("http://localhost:1");
- factory.execute(newGetRequest("/api/issues"));
- }
-
- @Test
- public void test_authentication() {
- httpServer.stubStatusCode(200).stubResponseBody("{}");
-
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie");
- String json = factory.execute(newGetRequest("/api/issues"));
-
- assertThat(json).isEqualTo("{}");
- assertThat(httpServer.requestedPath()).isEqualTo("/api/issues");
- assertThat(httpServer.requestHeaders().get("Authorization")).isEqualTo("Basic a2FyYWRvYzpsZWdyYXNjZXN0bGF2aWU=");
- }
-
- @Test
- public void test_proxy() throws Exception {
- expectedException.expect(IllegalStateException.class);
-
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url())
- .setProxyHost("localhost").setProxyPort(1)
- .setProxyLogin("john").setProxyPassword("smith");
- factory.execute(newGetRequest("/api/issues"));
- }
-
- @Test
- public void beginning_slash_is_optional() throws Exception {
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
- factory.execute(newGetRequest("api/foo"));
- assertThat(httpServer.requestedPath()).isEqualTo("/api/foo");
-
- factory.execute(newGetRequest("/api/bar"));
- assertThat(httpServer.requestedPath()).isEqualTo("/api/bar");
- }
-}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServer.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServer.java
deleted file mode 100644
index c24be3ef64c..00000000000
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/MockHttpServer.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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 java.io.IOException;
-import java.net.HttpURLConnection;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.eclipse.jetty.server.Handler;
-import org.eclipse.jetty.server.Request;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.handler.AbstractHandler;
-
-import static org.apache.commons.io.IOUtils.write;
-
-public class MockHttpServer {
- private Server server;
- private String responseBody;
- private byte[] binaryResponseBody;
- private int responseStatus = HttpURLConnection.HTTP_OK;
- private String requestPath;
- private Map requestHeaders = new HashMap(), requestParams = new HashMap();
- private String contentType;
-
- public void start() throws Exception {
- // 0 is random available port
- server = new Server(0);
- server.setHandler(getMockHandler());
- server.start();
- }
-
- public Handler getMockHandler() {
- Handler handler = new AbstractHandler() {
- @Override
- public void handle(String target, Request baseRequest, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
- requestPath = baseRequest.getUri().toString();
- requestHeaders.clear();
- Enumeration names = baseRequest.getHeaderNames();
- while (names.hasMoreElements()) {
- String headerName = (String) names.nextElement();
- requestHeaders.put(headerName, baseRequest.getHeader(headerName));
- }
- requestParams.clear();
- names = baseRequest.getParameterNames();
- while (names.hasMoreElements()) {
- String headerName = (String) names.nextElement();
- requestParams.put(headerName, baseRequest.getParameter(headerName));
- }
- response.setStatus(responseStatus);
- response.setContentType("application/json;charset=utf-8");
- if (responseBody != null) {
- write(responseBody, response.getOutputStream());
- } else {
- write(binaryResponseBody, response.getOutputStream());
- }
- baseRequest.setHandled(true);
- }
- };
- return handler;
- }
-
- public void stop() {
- try {
- if (server != null) {
- server.stop();
- }
- } catch (Exception e) {
- throw new IllegalStateException("Fail to stop HTTP server", e);
- }
- }
-
- public MockHttpServer doReturnBody(String responseBody) {
- this.responseBody = responseBody;
- return this;
- }
-
- public MockHttpServer doReturnBody(byte[] responseBody) {
- this.binaryResponseBody = responseBody;
- return this;
- }
-
- public MockHttpServer doReturnStatus(int status) {
- this.responseStatus = status;
- return this;
- }
-
- public MockHttpServer doReturnContentType(String contentType) {
- this.contentType = contentType;
- return this;
- }
-
- public String requestPath() {
- return requestPath;
- }
-
- public Map requestHeaders() {
- return requestHeaders;
- }
-
- public Map requestParams() {
- return requestParams;
- }
-
- public int getPort() {
- return server.getConnectors()[0].getLocalPort();
- }
-}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/PostRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/PostRequestTest.java
new file mode 100644
index 00000000000..3693cdff9a8
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/PostRequestTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonarqube.ws.MediaTypes;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PostRequestTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void post_is_post() {
+ PostRequest request = new PostRequest("api/issues/search");
+ assertThat(request.getMethod()).isEqualTo(WsRequest.Method.POST);
+ }
+
+ @Test
+ public void empty_parts_and_params_by_default() {
+ PostRequest request = new PostRequest("api/issues/search");
+ assertThat(request.getParts()).isEmpty();
+ assertThat(request.getParams()).isEmpty();
+ }
+
+ @Test
+ public void add_part() throws IOException {
+ PostRequest request = new PostRequest("api/issues/search");
+ File reportFile = temp.newFile();
+ request.setPart("report", new PostRequest.Part(MediaTypes.JSON, reportFile));
+
+ assertThat(request.getParts()).hasSize(1);
+ PostRequest.Part part = request.getParts().get("report");
+ assertThat(part.getMediaType()).isEqualTo(MediaTypes.JSON);
+ assertThat(part.getFile()).isSameAs(reportFile);
+ }
+}
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
deleted file mode 100644
index 13ff1fa14f3..00000000000
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java
+++ /dev/null
@@ -1,151 +0,0 @@
-/*
- * 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.common.net.HttpHeaders;
-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 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;
-
-public class WsClientTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- MockHttpServer server;
-
- WsClient underTest;
-
- @Before
- public void setUp() throws Exception {
- server = new MockHttpServer();
- server.start();
-
- underTest = new WsClient(newDefaultHttpConnector("http://localhost:" + server.getPort()));
- }
-
- @After
- public void stopServer() {
- if (server != null) {
- server.stop();
- }
- }
-
- @Test
- public void return_protobuf_response() throws Exception {
- server.doReturnBody(
- WsComponents.SearchWsResponse
- .newBuilder()
- .addComponents(WsComponents.SearchWsResponse.Component.getDefaultInstance())
- .build()
- .toByteArray());
- server.doReturnStatus(HTTP_OK);
- server.doReturnContentType(MediaTypes.PROTOBUF);
-
- WsComponents.SearchWsResponse response = underTest.execute(
- newGetRequest("api/components/search")
- .setMediaType(WsRequest.MediaType.PROTOBUF),
- WsComponents.SearchWsResponse.parser());
-
- assertThat(response.getComponentsCount()).isEqualTo(1);
- assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT))
- .isEqualTo(MediaTypes.PROTOBUF);
- }
-
- @Test
- public void return_json_response() throws Exception {
- String expectedResponse = "{\"key\":value}";
- server.doReturnBody(expectedResponse);
- server.doReturnStatus(HTTP_OK);
- server.doReturnContentType(MediaTypes.JSON);
-
- String response = underTest.execute(newGetRequest("api/components/search"));
-
- assertThat(response).isEqualTo(expectedResponse);
- assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.JSON);
- }
-
- @Test
- public void url_should_not_be_null() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Server URL must be set");
-
- new WsClient(newHttpConnector().build());
- }
-
- @Test
- public void url_should_not_be_empty() {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Server URL must be set");
-
- new WsClient(newDefaultHttpConnector(""));
- }
-
- @Test
- public void test_default_configuration() throws Exception {
- 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 = new WsClient(newHttpConnector()
- .url("http://localhost:9000")
- .login("eric")
- .password("pass")
- .connectTimeoutMilliseconds(12345)
- .readTimeoutMilliseconds(6789)
- .proxy("localhost", 2052)
- .proxyLogin("proxyLogin")
- .proxyPassword("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");
- }
-}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsRequestTest.java
deleted file mode 100644
index 28b5c04aede..00000000000
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsRequestTest.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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 org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonarqube.ws.client.WsRequest.Method;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonarqube.ws.client.WsRequest.newGetRequest;
-import static org.sonarqube.ws.client.WsRequest.newPostRequest;
-
-public class WsRequestTest {
-
- static final String ENDPOINT = "api/issues/search";
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- WsRequest underTest;
-
- @Test
- public void get_request() {
- underTest = newGetRequest("api/issues/search");
-
- assertThat(underTest.getMethod()).isEqualTo(Method.GET);
- }
-
- @Test
- public void post_request() {
- underTest = newPostRequest("api/issues/search");
-
- assertThat(underTest.getMethod()).isEqualTo(Method.POST);
- }
-
- @Test
- public void set_non_null_param() {
- underTest = newGetRequest("api/issues/search")
- .setParam("key", "value");
-
- assertThat(underTest.getParams().get("key")).isEqualTo("value");
- }
-
- @Test
- public void set_null_param_remove_existing_param() {
- underTest = newGetRequest(ENDPOINT)
- .setParam("key", "value")
- .setParam("key", null);
-
- assertThat(underTest.getParams().get("key")).isNull();
- }
-
- @Test
- public void fail_if_key_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("a WS parameter key cannot be null");
-
- underTest = newGetRequest(ENDPOINT)
- .setParam(null, "value");
- }
-}
diff --git a/sonar-ws/src/test/protobuf/ws-testing.proto b/sonar-ws/src/test/protobuf/ws-testing.proto
new file mode 100644
index 00000000000..9e802d9e1ca
--- /dev/null
+++ b/sonar-ws/src/test/protobuf/ws-testing.proto
@@ -0,0 +1,29 @@
+// SonarQube, open source software quality management tool.
+// Copyright (C) 2008-2015 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.
+
+syntax = "proto2";
+
+package sonarqube.ws.testing;
+
+option java_package = "org.sonarqube.ws";
+option java_outer_classname = "Testing";
+option optimize_for = SPEED;
+
+message Fake {
+ optional string label = 1;
+}