<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
- <version>4.1</version>
+ <version>5.1</version>
</dependency>
</dependencies>
</dependencyManagement>
import org.sonar.wsclient.user.DefaultUserClient;
import org.sonar.wsclient.user.UserClient;
+import javax.annotation.Nullable;
+
/**
* @since 3.6
*/
public class SonarClient {
- private final HttpRequestFactory requestFactory;
+ public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
+ public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
+
+ /**
+ * Visibility relaxed for unit tests
+ */
+ final HttpRequestFactory requestFactory;
private SonarClient(Builder builder) {
- requestFactory = new HttpRequestFactory(builder.url, builder.login, builder.password);
+ 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);
}
public IssueClient issueClient() {
return new Builder();
}
+ /**
+ * Create a client with default configuration. Use {@link #builder()} to define a custom configuration.
+ */
+ public static SonarClient create(String serverUrl) {
+ return builder().url(serverUrl).build();
+ }
+
public static class Builder {
- private String login, password, url;
+ private String login, password, url, proxyHost, proxyLogin, proxyPassword;
+ private int proxyPort = 0;
+ private int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MILLISECONDS, readTimeoutMs = DEFAULT_READ_TIMEOUT_MILLISECONDS;
private Builder() {
}
- public Builder login(String login) {
+ public Builder url(String url) {
+ this.url = url;
+ return this;
+ }
+
+ public Builder login(@Nullable String login) {
this.login = login;
return this;
}
- public Builder password(String password) {
+ public Builder password(@Nullable String password) {
this.password = password;
return this;
}
- public Builder url(String url) {
- this.url = url;
+ public Builder proxy(String proxyHost, int proxyPort) {
+ this.proxyHost = proxyHost;
+ this.proxyPort = proxyPort;
+ return this;
+ }
+
+ public Builder proxyLogin(@Nullable String proxyLogin) {
+ this.proxyLogin = proxyLogin;
+ return this;
+ }
+
+ public Builder proxyPassword(@Nullable String proxyPassword) {
+ this.proxyPassword = proxyPassword;
+ return this;
+ }
+
+ /**
+ * Sets a specified timeout value, in milliseconds, to be used when opening HTTP connection.
+ * A timeout of zero is interpreted as an infinite timeout. Default value is {@link SonarClient#DEFAULT_CONNECT_TIMEOUT_MILLISECONDS}
+ */
+ public Builder connectTimeoutMilliseconds(int i) {
+ this.connectTimeoutMs = i;
+ return this;
+ }
+
+ /**
+ * Sets the read timeout to a specified timeout, in milliseconds.
+ * A timeout of zero is interpreted as an infinite timeout. Default value is {@link SonarClient#DEFAULT_READ_TIMEOUT_MILLISECONDS}
+ */
+ public Builder readTimeoutMilliseconds(int i) {
+ this.readTimeoutMs = i;
return this;
}
import com.github.kevinsawicki.http.HttpRequest;
+import javax.annotation.Nullable;
import java.util.Map;
/**
*/
public class HttpRequestFactory {
- static final int CONNECT_TIMEOUT_MILLISECONDS = 30000;
- static final int READ_TIMEOUT_MILLISECONDS = 60000;
+ private final String baseUrl;
+ private String login, password, proxyHost, proxyLogin, proxyPassword;
+ private int proxyPort;
+ private int connectTimeoutInMilliseconds;
+ private int readTimeoutInMilliseconds;
- private final String baseURl;
- private final String login, password;
+ public HttpRequestFactory(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
- public HttpRequestFactory(String baseURl, String login, String password) {
- 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 HttpRequest get(String wsUrl, Map<String, Object> queryParams) {
- HttpRequest request = HttpRequest.get(baseURl + wsUrl, queryParams, true);
+ HttpRequest request = HttpRequest.get(baseUrl + wsUrl, queryParams, true);
return prepare(request);
}
public HttpRequest post(String wsUrl, Map<String, Object> queryParams) {
- HttpRequest request = HttpRequest.post(baseURl + wsUrl, queryParams, true);
+ HttpRequest request = HttpRequest.post(baseUrl + wsUrl, queryParams, true);
return prepare(request);
}
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(CONNECT_TIMEOUT_MILLISECONDS)
- .readTimeout(READ_TIMEOUT_MILLISECONDS)
+ .connectTimeout(connectTimeoutInMilliseconds)
+ .readTimeout(readTimeoutInMilliseconds)
.trustAllCerts()
.trustAllCerts();
if (login != null) {
public class SonarClientTest {
@Test
public void should_build_clients() {
- SonarClient client = SonarClient.builder().url("http://localhost:9000").build();
+ SonarClient client = SonarClient.create("http://localhost:9000");
assertThat(client.issueClient()).isNotNull().isInstanceOf(DefaultIssueClient.class);
assertThat(client.actionPlanClient()).isNotNull().isInstanceOf(DefaultActionPlanClient.class);
assertThat(client.userClient()).isNotNull().isInstanceOf(DefaultUserClient.class);
@Test
public void url_should_not_be_empty() {
try {
- SonarClient.builder().url("").build();
+ SonarClient.create("");
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Server URL must be set");
}
}
+
+ @Test
+ public void test_default_configuration() throws Exception {
+ SonarClient client = SonarClient.create("http://localhost:9000");
+ assertThat(client.requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000");
+ assertThat(client.requestFactory.getLogin()).isNull();
+ assertThat(client.requestFactory.getPassword()).isNull();
+ assertThat(client.requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(SonarClient.DEFAULT_CONNECT_TIMEOUT_MILLISECONDS);
+ assertThat(client.requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(SonarClient.DEFAULT_READ_TIMEOUT_MILLISECONDS);
+ assertThat(client.requestFactory.getProxyHost()).isNull();
+ assertThat(client.requestFactory.getProxyPort()).isEqualTo(0);
+ assertThat(client.requestFactory.getProxyLogin()).isNull();
+ assertThat(client.requestFactory.getProxyPassword()).isNull();
+
+ }
+
+ @Test
+ public void test_custom_configuration() throws Exception {
+ SonarClient client = SonarClient.builder().url("http://localhost:9000")
+ .login("eric")
+ .password("pass")
+ .connectTimeoutMilliseconds(12345)
+ .readTimeoutMilliseconds(6789)
+ .proxy("localhost", 2052)
+ .proxyLogin("proxyLogin")
+ .proxyPassword("proxyPass")
+ .build();
+ assertThat(client.requestFactory.getBaseUrl()).isEqualTo("http://localhost:9000");
+ assertThat(client.requestFactory.getLogin()).isEqualTo("eric");
+ assertThat(client.requestFactory.getPassword()).isEqualTo("pass");
+ assertThat(client.requestFactory.getConnectTimeoutInMilliseconds()).isEqualTo(12345);
+ assertThat(client.requestFactory.getReadTimeoutInMilliseconds()).isEqualTo(6789);
+ assertThat(client.requestFactory.getProxyHost()).isEqualTo("localhost");
+ assertThat(client.requestFactory.getProxyPort()).isEqualTo(2052);
+ assertThat(client.requestFactory.getProxyLogin()).isEqualTo("proxyLogin");
+ assertThat(client.requestFactory.getProxyPassword()).isEqualTo("proxyPass");
+ }
}
public void test_get() {
httpServer.doReturnStatus(200).doReturnBody("list of issues");
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap());
assertThat(request.method()).isEqualTo("GET");
public void test_post() {
httpServer.doReturnStatus(200);
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
HttpRequest request = factory.post("/api/issues/change", Collections.<String, Object>emptyMap());
assertThat(request.method()).isEqualTo("POST");
public void test_authentication() {
httpServer.doReturnStatus(200).doReturnBody("list of issues");
- HttpRequestFactory factory = new HttpRequestFactory(httpServer.url(), "karadoc", "legrascestlavie");
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie");
HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap());
assertThat(request.body()).isEqualTo("list of issues");
assertThat(httpServer.requestedPath()).isEqualTo("/api/issues");
assertThat(httpServer.requestHeaders().get("Authorization")).isEqualTo("Basic a2FyYWRvYzpsZWdyYXNjZXN0bGF2aWU=");
}
+
+ @Test
+ public void test_proxy() throws Exception {
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setProxyHost("localhost").setProxyPort(5020);
+ HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap());
+ // it's not possible to check that the proxy is correctly configured
+ }
+
+ @Test
+ public void test_proxy_credentials() throws Exception {
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url())
+ .setProxyHost("localhost").setProxyPort(5020)
+ .setProxyLogin("john").setProxyPassword("smith");
+ HttpRequest request = factory.get("/api/issues", Collections.<String, Object>emptyMap());
+ // it's not possible to check that the proxy is correctly configured
+ }
}
@Test
public void should_find_action_plans() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" +
"\"name\": \"Long term\",\n" +
"\"status\": \"CLOSED\",\n" +
@Test
public void should_create_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
@Test
public void should_update_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
@Test
public void should_delete_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
client.delete("382f6f2e-ad9d-424a-b973-9b065e04348a");
@Test
public void should_fail_to_delete_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnStatus(500);
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
@Test
public void should_open_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
@Test
public void should_close_action_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"actionPlan\": {\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\"}}");
ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
@Test
public void should_find_issues() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_fail_to_find_issues() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnStatus(500);
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_set_severity() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_assign() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_unassign() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_plan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_unplan() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_create_issue() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_get_transitions() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\n" +
" \"transitions\": [\n" +
" \"resolve\",\n" +
@Test
public void should_apply_transition() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"issue\": {\"key\": \"ABCDE\"}}");
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_add_comment() throws Exception {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody(IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/DefaultIssueClientTest/add_comment_result.json")));
IssueClient client = new DefaultIssueClient(requestFactory);
@Test
public void should_find_issues() {
- HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null);
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
httpServer.doReturnBody("{\"users\": [{\"login\": \"simon\", \"name\": \"Simon\", \"active\": true}]}");
UserClient client = new DefaultUserClient(requestFactory);