Search in sources :

Example 31 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project cats by Endava.

the class ServiceCaller method initHttpClient.

@PostConstruct
public void initHttpClient() {
    try {
        final TrustManager[] trustAllCerts = this.buildTrustAllManager();
        final SSLSocketFactory sslSocketFactory = this.buildSslSocketFactory(trustAllCerts);
        okHttpClient = new OkHttpClient.Builder().proxy(authArguments.getProxy()).connectTimeout(apiArguments.getConnectionTimeout(), TimeUnit.SECONDS).readTimeout(apiArguments.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(apiArguments.getWriteTimeout(), TimeUnit.SECONDS).connectionPool(new ConnectionPool(10, 15, TimeUnit.MINUTES)).sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]).hostnameVerifier((hostname, session) -> true).build();
    } catch (GeneralSecurityException | IOException e) {
        LOGGER.warning("Failed to configure HTTP CLIENT", e);
    }
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) PostConstruct(javax.annotation.PostConstruct)

Example 32 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project xray-maven-plugin by Xray-App.

the class XrayFeaturesExporter method submitStandardServerDC.

public String submitStandardServerDC(String outputPath) throws Exception {
    OkHttpClient client = CommonUtils.getHttpClient(this.useInternalTestProxy, this.ignoreSslErrors, this.timeout);
    String credentials;
    if (jiraPersonalAccessToken != null) {
        credentials = "Bearer " + jiraPersonalAccessToken;
    } else {
        credentials = Credentials.basic(jiraUsername, jiraPassword);
    }
    String endpointUrl = jiraBaseUrl + "/rest/raven/2.0/export/test";
    Request request;
    Response response = null;
    HttpUrl url = HttpUrl.get(endpointUrl);
    HttpUrl.Builder builder = url.newBuilder();
    builder.addQueryParameter("fz", "true");
    if (issueKeys != null) {
        builder.addQueryParameter("keys", this.issueKeys);
    }
    if (filterId != null) {
        builder.addQueryParameter("filter", this.filterId);
    }
    request = new Request.Builder().url(builder.build()).get().addHeader("Authorization", credentials).build();
    try {
        response = client.newCall(request).execute();
        // String responseBody = response.body().string();
        if (response.isSuccessful()) {
            unzipContentsToFolder(response.body().byteStream(), outputPath);
            return ("ok");
        } else {
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw (e);
    }
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Example 33 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project xray-maven-plugin by Xray-App.

the class XrayResultsImporter method submitStandardCloud.

public String submitStandardCloud(String format, String reportFile) throws Exception {
    OkHttpClient client = CommonUtils.getHttpClient(useInternalTestProxy, ignoreSslErrors, this.timeout);
    String authenticationPayload = "{ \"client_id\": \"" + clientId + "\", \"client_secret\": \"" + clientSecret + "\" }";
    RequestBody body = RequestBody.create(authenticationPayload, MEDIA_TYPE_JSON);
    Request request = new Request.Builder().url(xrayCloudAuthenticateUrl).post(body).build();
    Response response = null;
    String authToken = null;
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            authToken = responseBody.replace("\"", "");
        } else {
            throw new IOException("failed to authenticate " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
    String credentials = "Bearer " + authToken;
    String[] supportedFormats = new String[] { XRAY_FORMAT, JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT, CUCUMBER_FORMAT };
    if (!Arrays.asList(supportedFormats).contains(format)) {
        throw new Exception("unsupported report format: " + format);
    }
    MediaType mediaType;
    String[] xmlBasedFormats = new String[] { JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT };
    if (Arrays.asList(xmlBasedFormats).contains(format)) {
        mediaType = MEDIA_TYPE_XML;
    } else {
        mediaType = MEDIA_TYPE_JSON;
    }
    String endpointUrl;
    if (XRAY_FORMAT.equals(format)) {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution";
    } else {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution/" + format;
    }
    RequestBody requestBody = null;
    try {
        String reportContent = new String(Files.readAllBytes(Paths.get(reportFile)));
        requestBody = RequestBody.create(reportContent, mediaType);
    } catch (Exception e1) {
        e1.printStackTrace();
        throw e1;
    }
    HttpUrl url = HttpUrl.get(endpointUrl);
    HttpUrl.Builder builder = url.newBuilder();
    if (projectKey != null) {
        builder.addQueryParameter("projectKey", this.projectKey);
    }
    if (fixVersion != null) {
        builder.addQueryParameter("fixVersion", this.fixVersion);
    }
    if (revision != null) {
        builder.addQueryParameter("revision", this.revision);
    }
    if (testPlanKey != null) {
        builder.addQueryParameter("testPlanKey", this.testPlanKey);
    }
    if (testExecKey != null) {
        builder.addQueryParameter("testExecKey", this.testExecKey);
    }
    if (testEnvironment != null) {
        builder.addQueryParameter("testEnvironment", this.testEnvironment);
    }
    request = new Request.Builder().url(builder.build()).post(requestBody).addHeader("Authorization", credentials).build();
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            JSONObject responseObj = new JSONObject(responseBody);
            // System.out.println("Test Execution: "+((JSONObject)(responseObj.get("testExecIssue"))).get("key"));
            return (responseBody);
        } else {
            // System.err.println(responseBody);
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody)

Example 34 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project xray-maven-plugin by Xray-App.

the class XrayResultsImporter method submitMultipartCloud.

public String submitMultipartCloud(String format, String reportFile, JSONObject testExecInfo, JSONObject testInfo) throws Exception {
    OkHttpClient client = CommonUtils.getHttpClient(this.useInternalTestProxy, this.ignoreSslErrors, this.timeout);
    String authenticationPayload = "{ \"client_id\": \"" + clientId + "\", \"client_secret\": \"" + clientSecret + "\" }";
    RequestBody body = RequestBody.create(authenticationPayload, MEDIA_TYPE_JSON);
    Request request = new Request.Builder().url(xrayCloudAuthenticateUrl).post(body).build();
    Response response = null;
    String authToken = null;
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            authToken = responseBody.replace("\"", "");
        } else {
            throw new IOException("failed to authenticate " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
    String credentials = "Bearer " + authToken;
    String[] supportedFormats = new String[] { XRAY_FORMAT, JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT, CUCUMBER_FORMAT };
    if (!Arrays.asList(supportedFormats).contains(format)) {
        throw new Exception("unsupported report format: " + format);
    }
    MediaType mediaType;
    String[] xmlBasedFormats = new String[] { JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT };
    if (Arrays.asList(xmlBasedFormats).contains(format)) {
        mediaType = MEDIA_TYPE_XML;
    } else {
        mediaType = MEDIA_TYPE_JSON;
    }
    String endpointUrl;
    if ("xray".equals(format)) {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution/multipart";
    } else {
        endpointUrl = xrayCloudApiBaseUrl + "/import/execution/" + format + "/multipart";
    }
    HttpUrl url = HttpUrl.get(endpointUrl);
    HttpUrl.Builder builder = url.newBuilder();
    MultipartBody requestBody = null;
    try {
        requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("results", reportFile, RequestBody.create(new File(reportFile), mediaType)).addFormDataPart("info", "info.json", RequestBody.create(testExecInfo.toString(), MEDIA_TYPE_JSON)).build();
    } catch (Exception e1) {
        e1.printStackTrace();
        throw e1;
    }
    request = new Request.Builder().url(builder.build()).post(requestBody).addHeader("Authorization", credentials).build();
    response = null;
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            JSONObject responseObj = new JSONObject(responseBody);
            // System.out.println("Test Execution: "+responseObj.get("key"));
            return responseBody;
        } else {
            // System.err.println(responseBody);
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 35 with OkHttpClient

use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project xray-maven-plugin by Xray-App.

the class XrayResultsImporter method submitStandardServerDC.

public String submitStandardServerDC(String format, String reportFile) throws Exception {
    OkHttpClient client = CommonUtils.getHttpClient(this.useInternalTestProxy, this.ignoreSslErrors, this.timeout);
    String credentials;
    if (jiraPersonalAccessToken != null) {
        credentials = "Bearer " + jiraPersonalAccessToken;
    } else {
        credentials = Credentials.basic(jiraUsername, jiraPassword);
    }
    String[] supportedFormats = new String[] { XRAY_FORMAT, JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT, CUCUMBER_FORMAT, BEHAVE_FORMAT };
    if (!Arrays.asList(supportedFormats).contains(format)) {
        throw new Exception("unsupported report format: " + format);
    }
    MediaType mediaType;
    String[] xmlBasedFormats = new String[] { JUNIT_FORMAT, TESTNG_FORMAT, ROBOT_FORMAT, NUNIT_FORMAT, XUNIT_FORMAT };
    if (Arrays.asList(xmlBasedFormats).contains(format)) {
        mediaType = MEDIA_TYPE_XML;
    } else {
        mediaType = MEDIA_TYPE_JSON;
    }
    String endpointUrl;
    if (XRAY_FORMAT.equals(format)) {
        endpointUrl = jiraBaseUrl + "/rest/raven/2.0/import/execution";
    } else {
        endpointUrl = jiraBaseUrl + "/rest/raven/2.0/import/execution/" + format;
    }
    Request request;
    HttpUrl url = HttpUrl.get(endpointUrl);
    HttpUrl.Builder builder = url.newBuilder();
    // for cucumber and behave reports send the report directly on the body
    try {
        if (XRAY_FORMAT.equals(format) || CUCUMBER_FORMAT.equals(format) || BEHAVE_FORMAT.equals(format)) {
            String reportContent = new String(Files.readAllBytes(Paths.get(reportFile)));
            RequestBody requestBody = RequestBody.create(reportContent, mediaType);
            request = new Request.Builder().url(builder.build()).post(requestBody).addHeader("Authorization", credentials).build();
        } else {
            MultipartBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", reportFile, RequestBody.create(new File(reportFile), mediaType)).build();
            // for cucumber and behave formats, these URL parameters are not yet available
            if (projectKey != null) {
                builder.addQueryParameter("projectKey", this.projectKey);
            }
            if (fixVersion != null) {
                builder.addQueryParameter("fixVersion", this.fixVersion);
            }
            if (revision != null) {
                builder.addQueryParameter("revision", this.revision);
            }
            if (testPlanKey != null) {
                builder.addQueryParameter("testPlanKey", this.testPlanKey);
            }
            if (testExecKey != null) {
                builder.addQueryParameter("testExecKey", this.testExecKey);
            }
            if (testEnvironment != null) {
                builder.addQueryParameter("testEnvironment", this.testEnvironment);
            }
            request = new Request.Builder().url(builder.build()).post(requestBody).addHeader("Authorization", credentials).build();
        }
    } catch (Exception e1) {
        e1.printStackTrace();
        throw e1;
    }
    Response response = null;
    try {
        response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            JSONObject responseObj = new JSONObject(responseBody);
            // System.out.println("Test Execution: "+((JSONObject)(responseObj.get("testExecIssue"))).get("key"));
            return (responseBody);
        } else {
            // System.err.println(responseBody);
            throw new IOException("Unexpected HTTP code " + response);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) File(java.io.File) RequestBody(okhttp3.RequestBody)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85