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 lombok.Getter;

import java.util.List;

import static io.restassured.RestAssured.given;

public class RestAssuredWrapper {

    protected enum AuthType { ACCESS_TOKEN, API_TOKEN, PREEMPTIVE_BASIC }

    public enum LogLevel {

        NONE("none", 0), REQUEST_ONLY("requestOnly", 1), ALL("all", 2);

        private final String propertyValue;
        @Getter private final int level;

        LogLevel(String propertyValue, int level) {
            this.propertyValue = propertyValue;
            this.level = level;
        }

        public static LogLevel getLogLevel(String propertyValue) {
            for(LogLevel logLevel : LogLevel.values()) {
                if(logLevel.propertyValue.equals(propertyValue)) {
                    return logLevel;
                }
            }
            throw new IllegalArgumentException("No LogLevel found for propertyValue [" + propertyValue + "].");
        }

        public boolean isEqualOrAbove(LogLevel targetLevel) {
            return this.getLevel() >= targetLevel.getLevel();
        }

        public static LogLevel min(LogLevel logLevelA, LogLevel logLevelB) {
            return logLevelA.getLevel() < logLevelB.getLevel() ? logLevelA : logLevelB;
        }

        public String toString() {
            return propertyValue;
        }
    }

    private static LogLevel logLevelFromProperties;

    protected Configuration config;

    public RestAssuredWrapper(Configuration configuration) {
        config = configuration;
        logLevelFromProperties = LogLevel.getLogLevel(config.getData("apiLogLevel"));
    }

    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();

        LogLevel minLogLevel = LogLevel.min(logLevelFromProperties, settings.getLogLevel());
        requestSpecification.filter(new RestAssuredLogFilter(minLogLevel));

        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) { if (settings.getBodyMapperType() != null) { requestSpecification.body(settings.getBody(), settings.getBodyMapperType()); } else { requestSpecification.body(settings.getBody()); } } if (!settings.getQueryParameters().isEmpty()) { requestSpecification.queryParams(settings.getQueryParameters()); } if(!settings.getRepeatedQueryParameters().isEmpty()) { settings.getRepeatedQueryParameters().forEach(requestSpecification::queryParam); } 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() + "]."); } return response.then(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy