// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static final String API_TOKEN = "secret";
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static void addGitLabApiToken() throws IOException {
//     for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
//         if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
//             List<Domain> domains = credentialsStore.getDomains();
//             credentialsStore.addCredentials(domains.get(0),
//                 new StringCredentialsImpl(CredentialsScope.SYSTEM, API_TOKEN_ID, "GitLab API Token", Secret.fromString(API_TOKEN)));
//         }
//     }
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static void assertApiImpl(GitLabClient client, Class<? extends GitLabApiProxy> apiImplClass) throws Exception {
//     Field apiField = ((ResteasyGitLabClient) client).getClass().getDeclaredField("api");
//     apiField.setAccessible(true);
//     assertThat(apiField.get(client), instanceOf(apiImplClass));
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpResponse responseNotFound() {
//     return responseWithStatus(Response.Status.NOT_FOUND);
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpResponse responseOk() {
//     return responseWithStatus(Response.Status.OK);
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpRequest versionRequest(String id) {
//     return request().withMethod(HttpMethod.GET).withPath("/gitlab/api/" + id + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN);
// }
// 
// Path: src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClientBuilder.java
// @Restricted(NoExternalUse.class)
// public abstract class GitLabClientBuilder implements Comparable<GitLabClientBuilder>, ExtensionPoint, Serializable {
//     public static GitLabClientBuilder getGitLabClientBuilderById(String id) {
//         for (GitLabClientBuilder provider : getAllGitLabClientBuilders()) {
//             if (provider.id().equals(id)) {
//                 return provider;
//             }
//         }
// 
//         throw new NoSuchElementException("unknown client-builder-id: " + id);
//     }
// 
//     public static List<GitLabClientBuilder> getAllGitLabClientBuilders() {
//         List<GitLabClientBuilder> builders = new ArrayList<>(Jenkins.getInstance().getExtensionList(GitLabClientBuilder.class));
//         sort(builders);
//         return builders;
//     }
// 
//     private final String id;
//     private final int ordinal;
// 
//     protected GitLabClientBuilder(String id, int ordinal) {
//         this.id = id;
//         this.ordinal = ordinal;
//     }
// 
//     @NonNull
//     public final String id() {
//         return id;
//     }
// 
//     @NonNull
//     public abstract GitLabClient buildClient(String url, String token, boolean ignoreCertificateErrors, int connectionTimeout, int readTimeout);
// 
//     @Override
//     public final int compareTo(@NonNull GitLabClientBuilder other) {
//         int o = ordinal - other.ordinal;
//         return o != 0 ? o : id().compareTo(other.id());
//     }
// }


import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.API_TOKEN;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.addGitLabApiToken;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.assertApiImpl;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.responseNotFound;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.responseOk;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.versionRequest;
import static org.junit.Assert.fail;
import static org.mockserver.matchers.Times.exactly;
import static org.mockserver.matchers.Times.once;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClientBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.HttpRequest;
    @Before
    public void setup() throws IOException {
        gitLabUrl = "http://localhost:" + mockServer.getPort() + "/gitlab";
        addGitLabApiToken();

        List<GitLabClientBuilder> builders = Arrays.<GitLabClientBuilder>asList(new V3GitLabClientBuilder(), new V4GitLabClientBuilder());
        api = new AutodetectingGitLabClient(builders, gitLabUrl, API_TOKEN, true, 10, 10);

        v3Request = versionRequest(V3GitLabApiProxy.ID);
        v4Request = versionRequest(V4GitLabApiProxy.ID);
    }

    @Test
    public void buildClient_success_v3() throws Exception {
        mockServerClient.when(v3Request).respond(responseOk());
        api.getCurrentUser();
        assertApiImpl(api, V3GitLabApiProxy.class);
        mockServerClient.verify(v3Request, v3Request);
    }

    @Test
    public void buildClient_success_v4() throws Exception {
        mockServerClient.when(v3Request).respond(responseNotFound());
        mockServerClient.when(v4Request).respond(responseOk());
        api.getCurrentUser();
        assertApiImpl(api, V4GitLabApiProxy.class);
        mockServerClient.verify(v3Request, v4Request, v4Request);
    }

    @Test
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static final String API_TOKEN = "secret";
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static void addGitLabApiToken() throws IOException {
//     for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
//         if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
//             List<Domain> domains = credentialsStore.getDomains();
//             credentialsStore.addCredentials(domains.get(0),
//                 new StringCredentialsImpl(CredentialsScope.SYSTEM, API_TOKEN_ID, "GitLab API Token", Secret.fromString(API_TOKEN)));
//         }
//     }
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static void assertApiImpl(GitLabClient client, Class<? extends GitLabApiProxy> apiImplClass) throws Exception {
//     Field apiField = ((ResteasyGitLabClient) client).getClass().getDeclaredField("api");
//     apiField.setAccessible(true);
//     assertThat(apiField.get(client), instanceOf(apiImplClass));
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpResponse responseNotFound() {
//     return responseWithStatus(Response.Status.NOT_FOUND);
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpResponse responseOk() {
//     return responseWithStatus(Response.Status.OK);
// }
// 
// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/TestUtility.java
// static HttpRequest versionRequest(String id) {
//     return request().withMethod(HttpMethod.GET).withPath("/gitlab/api/" + id + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN);
// }
// 
// Path: src/main/java/com/dabsquared/gitlabjenkins/gitlab/api/GitLabClientBuilder.java
// @Restricted(NoExternalUse.class)
// public abstract class GitLabClientBuilder implements Comparable<GitLabClientBuilder>, ExtensionPoint, Serializable {
//     public static GitLabClientBuilder getGitLabClientBuilderById(String id) {
//         for (GitLabClientBuilder provider : getAllGitLabClientBuilders()) {
//             if (provider.id().equals(id)) {
//                 return provider;
//             }
//         }
// 
//         throw new NoSuchElementException("unknown client-builder-id: " + id);
//     }
// 
//     public static List<GitLabClientBuilder> getAllGitLabClientBuilders() {
//         List<GitLabClientBuilder> builders = new ArrayList<>(Jenkins.getInstance().getExtensionList(GitLabClientBuilder.class));
//         sort(builders);
//         return builders;
//     }
// 
//     private final String id;
//     private final int ordinal;
// 
//     protected GitLabClientBuilder(String id, int ordinal) {
//         this.id = id;
//         this.ordinal = ordinal;
//     }
// 
//     @NonNull
//     public final String id() {
//         return id;
//     }
// 
//     @NonNull
//     public abstract GitLabClient buildClient(String url, String token, boolean ignoreCertificateErrors, int connectionTimeout, int readTimeout);
// 
//     @Override
//     public final int compareTo(@NonNull GitLabClientBuilder other) {
//         int o = ordinal - other.ordinal;
//         return o != 0 ? o : id().compareTo(other.id());
//     }
// }


// Path: src/test/java/com/dabsquared/gitlabjenkins/gitlab/api/impl/AutodetectingGitLabClientTest.java
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.API_TOKEN;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.addGitLabApiToken;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.assertApiImpl;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.responseNotFound;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.responseOk;
import static com.dabsquared.gitlabjenkins.gitlab.api.impl.TestUtility.versionRequest;
import static org.junit.Assert.fail;
import static org.mockserver.matchers.Times.exactly;
import static org.mockserver.matchers.Times.once;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClientBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockserver.client.MockServerClient;
import org.mockserver.junit.MockServerRule;
import org.mockserver.model.HttpRequest;

    @Before
    public void setup() throws IOException {
        gitLabUrl = "http://localhost:" + mockServer.getPort() + "/gitlab";
        addGitLabApiToken();

        List<GitLabClientBuilder> builders = Arrays.<GitLabClientBuilder>asList(new V3GitLabClientBuilder(), new V4GitLabClientBuilder());
        api = new AutodetectingGitLabClient(builders, gitLabUrl, API_TOKEN, true, 10, 10);

        v3Request = versionRequest(V3GitLabApiProxy.ID);
        v4Request = versionRequest(V4GitLabApiProxy.ID);
    }

    @Test
    public void buildClient_success_v3() throws Exception {
        mockServerClient.when(v3Request).respond(responseOk());
        api.getCurrentUser();
        assertApiImpl(api, V3GitLabApiProxy.class);
        mockServerClient.verify(v3Request, v3Request);
    }

    @Test
    public void buildClient_success_v4() throws Exception {
        mockServerClient.when(v3Request).respond(responseNotFound());
        mockServerClient.when(v4Request).respond(responseOk());
        api.getCurrentUser();
        assertApiImpl(api, V4GitLabApiProxy.class);
        mockServerClient.verify(v3Request, v4Request, v4Request);
    }

    @Test
    public void buildClient_success_switching_apis() throws Exception {