aboutsummaryrefslogtreecommitdiffstats
path: root/it
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2016-12-05 10:04:56 +0100
committerStas Vilchik <vilchiks@gmail.com>2016-12-07 14:36:18 +0100
commit5fcfc0fe44bfe4c011a4783ae8e499fb3dfb0ecb (patch)
tree29d859a7fae4af5e70c5c26bfaab03c9e59199fa /it
parent65bf2d0dea8e5b2ca27a53ad668d77b4e2ded44b (diff)
downloadsonarqube-5fcfc0fe44bfe4c011a4783ae8e499fb3dfb0ecb.tar.gz
sonarqube-5fcfc0fe44bfe4c011a4783ae8e499fb3dfb0ecb.zip
Refactor every okhttp3 usage into ItUtils.call()
Diffstat (limited to 'it')
-rw-r--r--it/it-tests/src/test/java/it/analysis/ReportDumpTest.java20
-rw-r--r--it/it-tests/src/test/java/it/serverSystem/HttpHeadersTest.java20
-rw-r--r--it/it-tests/src/test/java/it/serverSystem/ServerSystemTest.java53
-rw-r--r--it/it-tests/src/test/java/it/user/SsoAuthenticationTest.java49
-rw-r--r--it/it-tests/src/test/java/util/ItUtils.java26
5 files changed, 59 insertions, 109 deletions
diff --git a/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java b/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java
index e8343ba971b..a440a0a5e29 100644
--- a/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java
+++ b/it/it-tests/src/test/java/it/analysis/ReportDumpTest.java
@@ -27,10 +27,6 @@ import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
-import java.util.concurrent.TimeUnit;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.junit.ClassRule;
@@ -67,19 +63,9 @@ public class ReportDumpTest {
}
private void verifyUrl(String url) throws IOException {
- HttpUrl httpUrl = HttpUrl.parse(url);
- Request request = new Request.Builder()
- .url(httpUrl)
- .get()
- .build();
- Response response = new OkHttpClient.Builder()
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .build()
- .newCall(request)
- .execute();
- assertThat(response.isSuccessful()).as(httpUrl.toString()).isTrue();
- assertThat(response.body().string()).as(httpUrl.toString()).isNotEmpty();
+ Response response = ItUtils.call(url);
+ assertThat(response.isSuccessful()).as(url).isTrue();
+ assertThat(response.body().string()).as(url).isNotEmpty();
}
}
diff --git a/it/it-tests/src/test/java/it/serverSystem/HttpHeadersTest.java b/it/it-tests/src/test/java/it/serverSystem/HttpHeadersTest.java
index 359c53c9c3d..4910729c859 100644
--- a/it/it-tests/src/test/java/it/serverSystem/HttpHeadersTest.java
+++ b/it/it-tests/src/test/java/it/serverSystem/HttpHeadersTest.java
@@ -19,7 +19,6 @@
*/
package it.serverSystem;
-import com.google.common.base.Throwables;
import com.sonar.orchestrator.Orchestrator;
import it.Category4Suite;
import java.io.File;
@@ -27,16 +26,14 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
-import java.util.concurrent.TimeUnit;
import okhttp3.CacheControl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
import okhttp3.Response;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.call;
public class HttpHeadersTest {
@@ -157,21 +154,6 @@ public class HttpHeadersTest {
assertThat(httpResponse.headers().get("Content-Type")).isEqualTo(expectedContentType);
}
- private static Response call(String url) {
- Request request = new Request.Builder().get().url(url).build();
- try {
- // SonarQube ws-client cannot be used as it does not
- // expose HTTP headers
- return new OkHttpClient.Builder()
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .build()
- .newCall(request).execute();
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
- }
-
/**
* Every JS and CSS files contains a hash between the file name and the extension.
*/
diff --git a/it/it-tests/src/test/java/it/serverSystem/ServerSystemTest.java b/it/it-tests/src/test/java/it/serverSystem/ServerSystemTest.java
index 71695dabe36..1cd6ca181a4 100644
--- a/it/it-tests/src/test/java/it/serverSystem/ServerSystemTest.java
+++ b/it/it-tests/src/test/java/it/serverSystem/ServerSystemTest.java
@@ -25,14 +25,10 @@ import it.Category4Suite;
import java.io.File;
import java.io.IOException;
import java.util.Map;
+import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
import org.json.simple.JSONValue;
import org.junit.Before;
import org.junit.ClassRule;
@@ -50,6 +46,7 @@ import util.ItUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static util.ItUtils.call;
import static util.ItUtils.newAdminWsClient;
import static util.selenium.Selenese.runSelenese;
@@ -160,24 +157,14 @@ public class ServerSystemTest {
@Test
public void http_response_should_be_gzipped() throws IOException {
- HttpClient httpclient = new DefaultHttpClient();
- try {
- HttpGet get = new HttpGet(orchestrator.getServer().getUrl());
- HttpResponse response = httpclient.execute(get);
-
- assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
- assertThat(response.getLastHeader("Content-Encoding")).isNull();
- EntityUtils.consume(response.getEntity());
-
- get = new HttpGet(orchestrator.getServer().getUrl());
- get.addHeader("Accept-Encoding", "gzip, deflate");
- response = httpclient.execute(get);
- assertThat(response.getLastHeader("Content-Encoding").getValue()).isEqualTo("gzip");
- EntityUtils.consume(response.getEntity());
-
- } finally {
- httpclient.getConnectionManager().shutdown();
- }
+ String url = orchestrator.getServer().getUrl() + "/api/rules/search";
+ Response metricsResponse = call(url);
+ assertThat(metricsResponse.isSuccessful()).as("Response code is %s", metricsResponse.code()).isTrue();
+ assertThat(metricsResponse.header("Content-Encoding")).isNull();
+
+ Response homeResponse = call(url, "Accept-Encoding", "gzip, deflate");
+ assertThat(homeResponse.isSuccessful()).as("Response code is %s", metricsResponse.code()).isTrue();
+ assertThat(homeResponse.header("Content-Encoding")).isEqualToIgnoringCase("gzip");
}
/**
@@ -198,20 +185,11 @@ public class ServerSystemTest {
// TODO should be moved elsewhere
@Test
public void api_ws_shortcut() throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
- try {
- HttpGet get = new HttpGet(orchestrator.getServer().getUrl() + "/api");
- HttpResponse response = httpclient.execute(get);
-
- assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
- String json = IOUtils.toString(response.getEntity().getContent());
- Map jsonAsMap = (Map) JSONValue.parse(json);
- assertThat(jsonAsMap.get("webServices")).isNotNull();
- EntityUtils.consume(response.getEntity());
-
- } finally {
- httpclient.getConnectionManager().shutdown();
- }
+ Response response = call(orchestrator.getServer().getUrl() + "/api");
+ assertThat(response.isSuccessful()).as("Response code is %s", response.code()).isTrue();
+ String json = IOUtils.toString(response.body().byteStream());
+ Map jsonAsMap = (Map) JSONValue.parse(json);
+ assertThat(jsonAsMap.get("webServices")).isNotNull();
}
private String getValidIpAddress() throws IOException {
@@ -221,4 +199,5 @@ public class ServerSystemTest {
assertThat(response.getValidIpAddressesCount()).isGreaterThan(0);
return response.getValidIpAddresses(0);
}
+
}
diff --git a/it/it-tests/src/test/java/it/user/SsoAuthenticationTest.java b/it/it-tests/src/test/java/it/user/SsoAuthenticationTest.java
index 7c723037336..bf147c5fd6b 100644
--- a/it/it-tests/src/test/java/it/user/SsoAuthenticationTest.java
+++ b/it/it-tests/src/test/java/it/user/SsoAuthenticationTest.java
@@ -19,14 +19,9 @@
*/
package it.user;
-import com.google.common.base.Throwables;
import com.sonar.orchestrator.Orchestrator;
-import java.io.IOException;
import java.util.List;
-import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
@@ -36,6 +31,7 @@ import org.junit.Test;
import util.user.UserRule;
import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.call;
/**
* Test SSO authentication (using HTTP headers).
@@ -76,31 +72,31 @@ public class SsoAuthenticationTest {
@Test
public void authenticate() {
- call(USER_LOGIN, USER_NAME, USER_EMAIL, null);
+ doCall(USER_LOGIN, USER_NAME, USER_EMAIL, null);
USER_RULE.verifyUserExists(USER_LOGIN, USER_NAME, USER_EMAIL);
}
@Test
public void authenticate_with_only_login() throws Exception {
- call(USER_LOGIN, null, null, null);
+ doCall(USER_LOGIN, null, null, null);
USER_RULE.verifyUserExists(USER_LOGIN, USER_LOGIN, null);
}
@Test
public void update_user_when_headers_are_updated() {
- call(USER_LOGIN, USER_NAME, USER_EMAIL, null);
+ doCall(USER_LOGIN, USER_NAME, USER_EMAIL, null);
USER_RULE.verifyUserExists(USER_LOGIN, USER_NAME, USER_EMAIL);
// As we don't keep the JWT token is the test, the user is updated
- call(USER_LOGIN, "new name", "new email", null);
+ doCall(USER_LOGIN, "new name", "new email", null);
USER_RULE.verifyUserExists(USER_LOGIN, "new name", "new email");
}
@Test
public void authenticate_with_groups() {
- call(USER_LOGIN, null, null, GROUP_1);
+ doCall(USER_LOGIN, null, null, GROUP_1);
USER_RULE.verifyUserGroupMembership(USER_LOGIN, GROUP_1);
}
@@ -113,7 +109,7 @@ public class SsoAuthenticationTest {
USER_RULE.createUser(USER_LOGIN, "password");
USER_RULE.associateGroupsToUser(USER_LOGIN, GROUP_1, GROUP_2);
- call(USER_LOGIN, null, null, GROUP_2 + "," + GROUP_3);
+ doCall(USER_LOGIN, null, null, GROUP_2 + "," + GROUP_3);
USER_RULE.verifyUserGroupMembership(USER_LOGIN, GROUP_2, GROUP_3);
}
@@ -141,7 +137,7 @@ public class SsoAuthenticationTest {
public void fail_when_email_already_exists() throws Exception {
USER_RULE.createUser("another", "Another", USER_EMAIL, "another");
- Response response = call(USER_LOGIN, USER_NAME, USER_EMAIL, null);
+ Response response = doCall(USER_LOGIN, USER_NAME, USER_EMAIL, null);
String expectedError = "You can't sign up because email 'tester@email.com' is already used by an existing user. This means that you probably already registered with another account";
assertThat(response.code()).isEqualTo(200);
@@ -149,31 +145,12 @@ public class SsoAuthenticationTest {
assertThat(FileUtils.readLines(orchestrator.getServer().getWebLogs(), Charsets.UTF_8)).doesNotContain(expectedError);
}
- private static Response call(String login, @Nullable String name, @Nullable String email, @Nullable String groups) {
- return doCall(login, name, email, groups);
- }
-
private static Response doCall(String login, @Nullable String name, @Nullable String email, @Nullable String groups) {
- Request.Builder requestBuilder = new Request.Builder().get().url(orchestrator.getServer().getUrl())
- .addHeader(LOGIN_HEADER, login);
- if (name != null) {
- requestBuilder.addHeader(NAME_HEADER, name);
- }
- if (email != null) {
- requestBuilder.addHeader(EMAIL_HEADER, email);
- }
- if (groups != null) {
- requestBuilder.addHeader(GROUPS_HEADER, groups);
- }
- try {
- return new OkHttpClient.Builder()
- .connectTimeout(30, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .build()
- .newCall(requestBuilder.build()).execute();
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
+ return call(orchestrator.getServer().getUrl(),
+ LOGIN_HEADER, login,
+ NAME_HEADER, name,
+ EMAIL_HEADER, email,
+ GROUPS_HEADER, groups);
}
private boolean checkLocalAuthentication(String login, String password) {
diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java
index e445b242f15..483ecc2a781 100644
--- a/it/it-tests/src/test/java/util/ItUtils.java
+++ b/it/it-tests/src/test/java/util/ItUtils.java
@@ -20,6 +20,7 @@
package util;
import com.google.common.base.Splitter;
+import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@@ -37,12 +38,16 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -295,4 +300,25 @@ public class ItUtils {
}.getType();
return gson.fromJson(json, type);
}
+
+ public static Response call(String url, String... headers) {
+ Request.Builder requestBuilder = new Request.Builder().get().url(url);
+ for (int i = 0; i < headers.length; i += 2) {
+ String headerName = headers[i];
+ String headerValue = headers[i + 1];
+ if (headerValue != null) {
+ requestBuilder.addHeader(headerName, headerValue);
+ }
+ }
+ try {
+ return new OkHttpClient.Builder()
+ .connectTimeout(30, TimeUnit.SECONDS)
+ .readTimeout(30, TimeUnit.SECONDS)
+ .build()
+ .newCall(requestBuilder.build())
+ .execute();
+ } catch (IOException e) {
+ throw Throwables.propagate(e);
+ }
+ }
}