aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-17 12:05:00 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-17 13:41:34 +0100
commit08d3297f4a786d1763f7f6475561307339fe6985 (patch)
tree099b5e0527191b5dbd5eba3ebba745524bea1fdd /sonar-ws
parent1da240b65309492730876b70e91dc081e22a7cb2 (diff)
downloadsonarqube-08d3297f4a786d1763f7f6475561307339fe6985.tar.gz
sonarqube-08d3297f4a786d1763f7f6475561307339fe6985.zip
SONAR-6947 Add tests and bug fixes
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java3
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java20
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsParameters.java4
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java34
4 files changed, 21 insertions, 40 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpConnector.java
index 2b3bcaf8978..66ac3cc89cd 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
@@ -80,7 +80,8 @@ public class HttpConnector implements WsConnector {
private String proxyPassword;
private int proxyPort = 0;
- private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
+ private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS;
+ private int readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
private Builder() {
}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java
index 97b26436b0c..cda4c549b74 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpRequestFactory.java
@@ -24,6 +24,7 @@ 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;
@@ -136,9 +137,9 @@ public class HttpRequestFactory {
public <T extends Message> T execute(WsRequest wsRequest, Parser<T> protobufParser) {
HttpRequest httpRequest = wsRequestToHttpRequest(wsRequest);
- String response = execute(httpRequest);
+ InputStream response = executeWithStream(httpRequest);
try {
- return protobufParser.parseFrom(response.getBytes());
+ return protobufParser.parseFrom(response);
} catch (InvalidProtocolBufferException e) {
Throwables.propagate(e);
}
@@ -147,9 +148,9 @@ public class HttpRequestFactory {
}
private HttpRequest wsRequestToHttpRequest(WsRequest wsRequest) {
- HttpRequest httpRequest = wsRequest.getMethod().equals(WsRequest.Method.GET)
- ? HttpRequest.post(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true)
- : HttpRequest.get(buildUrl(wsRequest.getEndpoint()), wsRequest.getParams(), true);
+ 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:
@@ -188,6 +189,15 @@ public class HttpRequestFactory {
}
}
+ 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) {
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 c2821a26970..1557e97b632 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
@@ -36,4 +36,8 @@ public class PermissionsWsParameters {
public static final String PARAM_DESCRIPTION = "description";
public static final String PARAM_PROJECT_KEY_PATTERN = "projectKeyPattern";
public static final String PARAM_QUALIFIER = "qualifier";
+
+ private PermissionsWsParameters() {
+ // static utils only
+ }
}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java
index a3bdf4967f6..13ff1fa14f3 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java
@@ -23,21 +23,17 @@ package org.sonarqube.ws.client;
import com.google.common.net.HttpHeaders;
import org.junit.After;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsComponents;
-import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
-import org.sonarqube.ws.client.permission.GroupsWsRequest;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonarqube.ws.client.HttpConnector.newDefaultHttpConnector;
import static org.sonarqube.ws.client.HttpConnector.newHttpConnector;
import static org.sonarqube.ws.client.WsRequest.newGetRequest;
-import static org.sonarqube.ws.client.WsRequest.newPostRequest;
public class WsClientTest {
@Rule
@@ -152,34 +148,4 @@ public class WsClientTest {
assertThat(requestFactory.getProxyLogin()).isEqualTo("proxyLogin");
assertThat(requestFactory.getProxyPassword()).isEqualTo("proxyPass");
}
-
- @Ignore
- @Test
- public void contact_localhost() {
- /**
- * This is a temporary test to have a simple end-to-end test
- * To make it work launch a default sonar server locally
- */
- WsClient ws = new WsClient(newHttpConnector()
- .url("http://localhost:9000")
- .login("admin")
- .password("admin")
- .build());
-
- // test with json response
- String stringResponse = ws.execute(newGetRequest("api/webservices/list"));
- assertThat(stringResponse).contains("webservices", "permissions");
-
- // test with protobuf response
- WsGroupsResponse protobufResponse = ws.execute(newPostRequest("api/permissions/groups")
- .setMediaType(WsRequest.MediaType.PROTOBUF)
- .setParam("permission", "admin"),
- WsGroupsResponse.parser());
- assertThat(protobufResponse.getGroups(0).getName()).contains("sonar-administrator");
-
- // test with specific client
- WsGroupsResponse groupsResponse = ws.permissionsClient().groups(new GroupsWsRequest()
- .setPermission("admin"));
- assertThat(groupsResponse.getGroups(0).getName()).contains("sonar-administrator");
- }
}