com.codacy.scoobydoo.RestAssuredWrapperSettings Maven / Gradle / Ivy
package com.codacy.scoobydoo;
import com.codacy.scoobydoo.RestAssuredWrapper.AuthType;
import com.codacy.scoobydoo.RestAssuredWrapper.LogLevel;
import io.restassured.http.ContentType;
import io.restassured.http.Header;
import io.restassured.http.Method;
import io.restassured.mapper.ObjectMapperType;
import lombok.Getter;
import org.openqa.selenium.Cookie;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RestAssuredWrapperSettings {
@Getter private final String accessToken;
@Getter private final String apiToken;
@Getter private final AuthType authType;
@Getter private final Object body;
@Getter private final ObjectMapperType bodyMapperType;
@Getter private final ContentType contentType;
@Getter private final String endpoint;
@Getter private final List headers;
@Getter private final LogLevel logLevel;
@Getter private Method method;
@Getter private final Map queryParameters;
@Getter private final Map> repeatedQueryParameters;
@Getter private final String requestName;
@Getter private final Map userCredentials;
public RestAssuredWrapperSettings(Builder builder) {
this.accessToken = builder.accessToken;
this.apiToken = builder.apiToken;
this.authType = builder.authType;
this.body = builder.body;
this.bodyMapperType = builder.bodyMapperType;
this.contentType = builder.contentType;
this.endpoint = builder.endpoint;
this.headers = builder.headers;
this.logLevel = builder.logLevel;
this.method = builder.method;
this.queryParameters = builder.queryParameters;
this.repeatedQueryParameters = builder.repeatedQueryParameters;
this.requestName = builder.requestName;
this.userCredentials = builder.userCredentials;
}
public RestAssuredWrapperSettings(Method method, Builder builder) {
this(builder);
this.method = method;
}
public static class Builder {
private String accessToken;
private String apiToken;
private AuthType authType;
private Object body;
private ObjectMapperType bodyMapperType;
private ContentType contentType;
private String endpoint;
final private List headers;
private LogLevel logLevel = LogLevel.ALL;
private Method method;
private final Map queryParameters;
private final Map> repeatedQueryParameters;
private String requestName;
private Map userCredentials;
public Builder() {
headers = new ArrayList<>();
queryParameters = new HashMap<>();
repeatedQueryParameters = new HashMap<>();
}
public Builder withApiToken(String apiToken) {
if(apiToken != null) {
this.authType = AuthType.API_TOKEN;
this.apiToken = apiToken;
}
return this;
}
public Builder withAuthenticationBasic(Map userCredentials) {
if(userCredentials != null) {
this.authType = AuthType.PREEMPTIVE_BASIC;
this.userCredentials = userCredentials;
}
return this;
}
public Builder withAuthenticationToken(String accessToken) {
if(accessToken != null) {
this.authType = AuthType.ACCESS_TOKEN;
this.accessToken = accessToken;
}
return this;
}
public Builder withBody(Object body) {
this.body = body;
return this;
}
public Builder withBody(ContentType contentType, String body) {
this.body = body;
this.contentType = contentType;
return this;
}
public Builder withBody(Object body, ObjectMapperType bodyMapperType) {
this.body = body;
this.bodyMapperType = bodyMapperType;
return this;
}
public Builder toEndpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
public Builder addHeader(Header header) {
this.headers.add(header);
return this;
}
public Builder addHeaders(Header... headers) {
this.headers.addAll(Arrays.asList(headers));
return this;
}
public Builder addCookie(Cookie cookie) {
addCookie(cookie.getName(), cookie.getValue());
return this;
}
public Builder addCookie(String name, String value) {
addCookie(name + "=" + value);
return this;
}
public Builder addCookie(String cookie) {
boolean foundCookieHeader = false;
for (int i = 0; !foundCookieHeader && i < this.headers.size(); i++) {
Header header = this.headers.get(i);
String headerName = header.getName();
if (headerName.equalsIgnoreCase("cookie")) {
String existingCookieValue = header.getValue();
this.headers.set(i, new Header(headerName, existingCookieValue + ";" + cookie));
foundCookieHeader = true;
}
}
if (!foundCookieHeader) {
addHeader(new Header("Cookie", cookie));
}
return this;
}
public Builder forAction(Method method) {
this.method = method;
return this;
}
public Builder querying(Map queryParameters) {
if (queryParameters != null && !queryParameters.isEmpty()) {
this.queryParameters.putAll(queryParameters);
}
return this;
}
public Builder querying(String key, String value) {
this.queryParameters.put(key, value);
return this;
}
public Builder queryingRepeatedParameter(String parameterName, Collection values) {
this.repeatedQueryParameters.put(parameterName, values);
return this;
}
public Builder withLogLevel(LogLevel logLevel) {
this.logLevel = logLevel;
return this;
}
public Builder withName(String requestName) {
this.requestName = requestName;
return this;
}
public RestAssuredWrapperSettings build() {
return new RestAssuredWrapperSettings(this);
}
}
}