All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codacy.scoobydoo.RestAssuredWrapper Maven / Gradle / Ivy

There is a newer version: 3.30.0
Show newest version
package com.codacy.scoobydoo;

import com.codacy.scoobydoo.configuration.Configuration;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;

public class RestAssuredWrapper {

    protected enum AuthType { ACCESS_TOKEN, API_TOKEN, PREEMPTIVE_BASIC }

    private static boolean shouldLogAll;

    protected Configuration config;

    public RestAssuredWrapper(Configuration configuration) {
        config = configuration;
        shouldLogAll = config.getData("apiLogAll").equals(Constant.Status.ENABLED);
    }

    public ValidatableResponse delete(RestAssuredWrapperSettings.Builder settingsBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.DELETE, settingsBuilder));
    }

    public ValidatableResponse get(RestAssuredWrapperSettings.Builder settingsBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.GET, settingsBuilder));
    }

    public ValidatableResponse head(RestAssuredWrapperSettings.Builder settingsBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.HEAD, settingsBuilder));
    }

    public ValidatableResponse patch(RestAssuredWrapperSettings.Builder settingsBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.PATCH, settingsBuilder));
    }

    public ValidatableResponse post(RestAssuredWrapperSettings.Builder settingsBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.POST, settingsBuilder));
    }

    public ValidatableResponse put(RestAssuredWrapperSettings.Builder settingBuilder) {
        return runMethod(new RestAssuredWrapperSettings(Method.PUT, settingBuilder));
    }



    private static ValidatableResponse runMethod(RestAssuredWrapperSettings settings) {

        final String requestName = settings.getRequestName();
        if(requestName != null && !requestName.isBlank()) {
            LoggingHelper.info(settings.getRequestName());
        }

        RequestSpecification requestSpecification = given();

        if(shouldLogAll || settings.shouldLogAll()) {
            requestSpecification.filter(new RestAssuredLogFilter());
            requestSpecification.log().all();
        }

        requestSpecification.when();

        List
headersList = settings.getHeaders(); if(settings.getAuthType() != null) { switch (settings.getAuthType()) { case ACCESS_TOKEN -> requestSpecification.auth().oauth2(settings.getAccessToken()); case API_TOKEN -> headersList.add(new Header("api-token", settings.getApiToken())); case PREEMPTIVE_BASIC -> requestSpecification.auth().preemptive().basic(settings.getUserCredentials().get("username"), settings.getUserCredentials().get("password")); default -> throw new IllegalArgumentException("No logic implemented for AuthType [" + settings.getAuthType() + "]."); } } if(!headersList.isEmpty()) { requestSpecification.headers(new Headers(headersList)); } if (settings.getBody() != null) { requestSpecification.body(settings.getBody()); } if (settings.getQueryParameters() != null) { requestSpecification.queryParams(settings.getQueryParameters()); } final String endpoint = settings.getEndpoint(); Response response; switch (settings.getMethod()) { case DELETE -> response = requestSpecification.delete(endpoint); case GET -> response = requestSpecification.get(endpoint); case HEAD -> response = requestSpecification.head(endpoint); case PATCH -> response = requestSpecification.patch(endpoint); case POST -> response = requestSpecification.post(endpoint); case PUT -> response = requestSpecification.put(endpoint); default -> throw new IllegalArgumentException("No implementation defined for HTTP method [" + settings.getMethod().name() + "]."); } ValidatableResponse validatableResponse = response.then(); if(shouldLogAll || settings.shouldLogAll()) { validatableResponse.log().all(); } return validatableResponse; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy