From 4291a1377f2d53719c6384ad127238f6fe763668 Mon Sep 17 00:00:00 2001 From: Karsten Kraus Date: Sun, 3 Dec 2017 22:43:44 +0100 Subject: [PATCH] switch connection-testing and api-autodetection to get-request as head request might cause NPE (see RESTEASY-1347) --- .../connection/GitLabConnectionConfig.java | 2 +- .../gitlabjenkins/gitlab/api/GitLabClient.java | 2 -- .../gitlab/api/impl/AutodetectingGitLabClient.java | 14 +------------- .../gitlab/api/impl/ResteasyGitLabClient.java | 5 ----- .../api/impl/AutodetectingGitLabClientTest.java | 10 +++++----- .../gitlabjenkins/gitlab/api/impl/TestUtility.java | 4 ++-- .../gitlabjenkins/service/GitLabClientStub.java | 5 ----- .../gitlabjenkins/util/GitLabClientStub.java | 5 ----- 8 files changed, 9 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java b/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java index 2e99765..e1efd6d 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java @@ -138,7 +138,7 @@ public class GitLabConnectionConfig extends GlobalConfiguration { @QueryParameter int connectionTimeout, @QueryParameter int readTimeout) { try { - new GitLabConnection("", url, apiTokenId, clientBuilderId, ignoreCertificateErrors, connectionTimeout, readTimeout).getClient().headCurrentUser(); + new GitLabConnection("", url, apiTokenId, clientBuilderId, ignoreCertificateErrors, connectionTimeout, readTimeout).getClient().getCurrentUser(); return FormValidation.ok(Messages.connection_success()); } catch (WebApplicationException e) { return FormValidation.error(Messages.connection_error(e.getMessage())); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.java index 85f3c05..b65a160 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClient.java @@ -36,8 +36,6 @@ public interface GitLabClient { Branch getBranch(String projectId, String branch); - void headCurrentUser(); - User getCurrentUser(); User addUser(String email, String username, String name, String password); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.java index 680acb6..6510f0a 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClient.java @@ -198,18 +198,6 @@ final class AutodetectingGitLabClient implements GitLabClient { }); } - @Override - public void headCurrentUser() { - execute( - new GitLabOperation() { - @Override - Void execute(GitLabClient client) { - client.headCurrentUser(); - return null; - } - }); - } - @Override public User getCurrentUser() { return execute( @@ -287,7 +275,7 @@ final class AutodetectingGitLabClient implements GitLabClient { for (GitLabClientBuilder candidate : builders) { GitLabClient client = candidate.buildClient(url, token, ignoreCertificateErrors, connectionTimeout, readTimeout); try { - client.headCurrentUser(); + client.getCurrentUser(); return client; } catch (NotFoundException ignored) { // api-endpoint not found (== api-level not supported by this client) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.java index d806ef7..3ab968e 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/ResteasyGitLabClient.java @@ -95,11 +95,6 @@ final class ResteasyGitLabClient implements GitLabClient { return api.getBranch(projectId, branch); } - @Override - public void headCurrentUser() { - api.headCurrentUser(); - } - @Override public User getCurrentUser() { return api.getCurrentUser(); diff --git a/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClientTest.java b/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClientTest.java index 085fbe1..4e4f346 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClientTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClientTest.java @@ -46,7 +46,7 @@ public class AutodetectingGitLabClientTest { @Test public void buildClient_success_v3() throws Exception { mockServerClient.when(v3Request).respond(responseOk()); - api.headCurrentUser(); + api.getCurrentUser(); assertApiImpl(api, V3GitLabApiProxy.class); mockServerClient.verify(v3Request, v3Request); } @@ -55,7 +55,7 @@ public class AutodetectingGitLabClientTest { public void buildClient_success_v4() throws Exception { mockServerClient.when(v3Request).respond(responseNotFound()); mockServerClient.when(v4Request).respond(responseOk()); - api.headCurrentUser(); + api.getCurrentUser(); assertApiImpl(api, V4GitLabApiProxy.class); mockServerClient.verify(v3Request, v4Request, v4Request); } @@ -64,12 +64,12 @@ public class AutodetectingGitLabClientTest { public void buildClient_success_switching_apis() throws Exception { mockServerClient.when(v3Request, once()).respond(responseNotFound()); mockServerClient.when(v4Request, exactly(2)).respond(responseOk()); - api.headCurrentUser(); + api.getCurrentUser(); assertApiImpl(api, V4GitLabApiProxy.class); mockServerClient.when(v4Request, once()).respond(responseNotFound()); mockServerClient.when(v3Request, exactly(2)).respond(responseOk()); - api.headCurrentUser(); + api.getCurrentUser(); assertApiImpl(api, V3GitLabApiProxy.class); mockServerClient.verify(v3Request, v4Request, v4Request, v3Request, v3Request); @@ -80,7 +80,7 @@ public class AutodetectingGitLabClientTest { mockServerClient.when(v3Request).respond(responseNotFound()); mockServerClient.when(v4Request).respond(responseNotFound()); try { - api.headCurrentUser(); + api.getCurrentUser(); fail("endpoint should throw exception when no matching delegate is found"); } catch (NoSuchElementException e) { mockServerClient.verify(v3Request, v4Request); diff --git a/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java b/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java index 8bd500d..3c0eb60 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java @@ -19,7 +19,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.List; -import static javax.ws.rs.HttpMethod.HEAD; +import static javax.ws.rs.HttpMethod.GET; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.OK; import static org.hamcrest.MatcherAssert.assertThat; @@ -46,7 +46,7 @@ class TestUtility { } static HttpRequest versionRequest(String id) { - return request().withMethod(HEAD).withPath("/gitlab/api/" + id + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN); + return request().withMethod(GET).withPath("/gitlab/api/" + id + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN); } static HttpResponse responseOk() { diff --git a/src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.java b/src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.java index 14a1508..a8ef86f 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/service/GitLabClientStub.java @@ -139,11 +139,6 @@ class GitLabClientStub implements GitLabClient { return null; } - @Override - public void headCurrentUser() { - - } - @Override public User getCurrentUser() { return null; diff --git a/src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.java b/src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.java index 1f17205..9db5f7c 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/util/GitLabClientStub.java @@ -88,11 +88,6 @@ class GitLabClientStub implements GitLabClient { return null; } - @Override - public void headCurrentUser() { - - } - @Override public User getCurrentUser() { return null;